1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
<?php
require_once "Tests/Auth/OpenID/TestUtil.php";
require_once "Tests/Auth/OpenID/MemStore.php";
require_once "Auth/OpenID/Message.php";
require_once "Auth/OpenID/Consumer.php";
/**
* A consumer whose _requestAssocation will return predefined results
* instead of trying to actually perform association requests.
*/
class ErrorRaisingConsumer extends Auth_OpenID_GenericConsumer {
// The list of objects to be returned by successive calls to
// _requestAssocation. Each call will pop the first element from
// this list and return it to _negotiateAssociation. If the
// element is a Message object, it will be wrapped in a
// ServerErrorContainer exception. Otherwise it will be returned
// as-is.
var $return_messages = array();
function _requestAssociation($endpoint, $assoc_type, $session_type)
{
$m = array_pop($this->return_messages);
if (is_a($m, 'Auth_OpenID_Message')) {
return Auth_OpenID_ServerErrorContainer::fromMessage($m);
} else if (Auth_OpenID::isFailure($m)) {
return $m;
} else {
return $m;
}
}
}
/**
* Test the session type negotiation behavior of an OpenID 2 consumer.
*/
class TestOpenID2SessionNegotiation extends PHPUnit_Framework_TestCase {
function setUp()
{
$dumb = null;
$this->consumer = new ErrorRaisingConsumer($dumb);
$this->endpoint = new Auth_OpenID_ServiceEndpoint();
$this->endpoint->type_uris = array(Auth_OpenID_TYPE_2_0);
$this->endpoint->server_url = 'bogus';
}
/**
* Test the case where the response to an associate request is a
* server error or is otherwise undecipherable.
*/
function testBadResponse()
{
$this->consumer->return_messages = array(
new Auth_OpenID_Message($this->endpoint->preferredNamespace()));
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
/**
* Test the case where the response to an associate request is a
* a failure response object.
*/
function testBadResponseWithFailure()
{
$this->consumer->return_messages = array(
new Auth_OpenID_FailureResponse($this->endpoint));
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
/**
* Test the case where the association type (assoc_type) returned
* in an unsupported-type response is absent.
*/
function testEmptyAssocType()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', null);
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'new-session-type');
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Unsupported association type',
// 'Server responded with unsupported association ' +
// 'session but did not supply a fallback.')
}
/**
* Test the case where the session type (session_type) returned in
* an unsupported-type response is absent.
*/
function testEmptySessionType()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'new-assoc-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', null);
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Unsupported association type',
// 'Server responded with unsupported association ' +
// 'session but did not supply a fallback.')
}
/**
* Test the case where an unsupported-type response specifies a
* preferred (assoc_type, session_type) combination that is not
* allowed by the consumer's SessionNegotiator.
*/
function testNotAllowed()
{
$allowed_types = array();
$negotiator = new Auth_OpenID_SessionNegotiator($allowed_types);
$this->consumer->negotiator = $negotiator;
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'not-allowed');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'not-allowed');
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Unsupported association type',
// 'Server sent unsupported session/association type:')
}
/**
* Test the case where an unsupported-type response triggers a
* retry to get an association with the new preferred type.
*/
function testUnsupportedWithRetry()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'HMAC-SHA1');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'DH-SHA1');
$assoc = new Auth_OpenID_Association(
'handle', 'secret', 'issued', 10000, 'HMAC-SHA1');
$this->consumer->return_messages = array($msg, $assoc);
$this->assertTrue($this->consumer->_negotiateAssociation($this->endpoint) === $assoc);
// $this->failUnlessLogMatches('Unsupported association type');
}
/**
* Test the case where an unsupported-typ response triggers a
* retry, but the retry fails and None is returned instead.
*/
function testUnsupportedWithRetryAndFail()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'HMAC-SHA1');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'DH-SHA1');
$this->consumer->return_messages = array($msg,
new Auth_OpenID_Message($this->endpoint->preferredNamespace()));
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Unsupported association type',
// 'Server %s refused' % ($this->endpoint.server_url))
}
/**
* Test the valid case, wherein an association is returned on the
* first attempt to get one.
*/
function testValid()
{
$assoc = new Auth_OpenID_Association(
'handle', 'secret', 'issued', 10000, 'HMAC-SHA1');
$this->consumer->return_messages = array($assoc);
$this->assertTrue($this->consumer->_negotiateAssociation($this->endpoint) === $assoc);
// $this->failUnlessLogEmpty()
}
}
/**
* Tests for the OpenID 1 consumer association session behavior. See
* the docs for TestOpenID2SessionNegotiation. Notice that this class
* is not a subclass of the OpenID 2 tests. Instead, it uses many of
* the same inputs but inspects the log messages logged with
* oidutil.log. See the calls to $this->failUnlessLogMatches. Some
* of these tests pass openid2-style messages to the openid 1
* association processing logic to be sure it ignores the extra data.
*/
class TestOpenID1SessionNegotiation extends PHPUnit_Framework_TestCase {
function setUp()
{
$dumb = null;
$this->consumer = new ErrorRaisingConsumer($dumb);
$this->endpoint = new Auth_OpenID_ServiceEndpoint();
$this->endpoint->type_uris = array(Auth_OpenID_OPENID1_NS);
$this->endpoint->server_url = 'bogus';
}
function testBadResponse()
{
$this->consumer->return_messages =
array(new Auth_OpenID_Message($this->endpoint->preferredNamespace()));
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
function testEmptyAssocType()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', null);
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'new-session-type');
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
function testEmptySessionType()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'new-assoc-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', null);
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association');
}
function testNotAllowed()
{
$allowed_types = array();
$negotiator = new Auth_OpenID_SessionNegotiator($allowed_types);
$this->consumer->negotiator = $negotiator;
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'not-allowed');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'not-allowed');
$this->consumer->return_messages = array($msg);
$this->assertEquals($this->consumer->_negotiateAssociation($this->endpoint), null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
function testUnsupportedWithRetry()
{
$msg = new Auth_OpenID_Message($this->endpoint->preferredNamespace());
$msg->setArg(Auth_OpenID_OPENID_NS, 'error', 'Unsupported type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'error_code', 'unsupported-type');
$msg->setArg(Auth_OpenID_OPENID_NS, 'assoc_type', 'HMAC-SHA1');
$msg->setArg(Auth_OpenID_OPENID_NS, 'session_type', 'DH-SHA1');
$assoc = new Auth_OpenID_Association(
'handle', 'secretxx', 'issued', 10000, 'HMAC-SHA1');
$this->consumer->return_messages = array($assoc, $msg);
$result = $this->consumer->_negotiateAssociation($this->endpoint);
$this->assertTrue($result === null);
// $this->failUnlessLogMatches('Server error when requesting an association')
}
function testValid()
{
$assoc = new Auth_OpenID_Association(
'handle', 'secret', 'issued', 10000, 'HMAC-SHA1');
$this->consumer->return_messages = array($assoc);
$this->assertTrue($this->consumer->_negotiateAssociation($this->endpoint) === $assoc);
// $this->failUnlessLogEmpty()
}
}
class TestNegotiatorBehaviors extends PHPUnit_Framework_TestCase {
function setUp()
{
$this->allowed_types = array(
array('HMAC-SHA1', 'no-encryption'),
array('HMAC-SHA256', 'no-encryption')
);
$this->n = new Auth_OpenID_SessionNegotiator($this->allowed_types);
}
function testAddAllowedTypeNoSessionTypes()
{
$this->assertFalse($this->n->addAllowedType('invalid'));
}
function testAddAllowedTypeBadSessionType()
{
$this->assertFalse($this->n->addAllowedType('assoc1', 'invalid'));
}
function testAddAllowedTypeContents()
{
$assoc_type = 'HMAC-SHA1';
$this->assertTrue($this->n->addAllowedType($assoc_type));
foreach (Auth_OpenID_getSessionTypes($assoc_type) as $typ) {
$this->assertTrue(in_array(array($assoc_type, $typ),
$this->n->allowed_types));
}
}
}
class Tests_Auth_OpenID_Negotiation extends PHPUnit_Framework_TestSuite {
function getName()
{
return 'Tests_Auth_OpenID_Negotiation';
}
function Tests_Auth_OpenID_Negotiation()
{
$this->addTestSuite('TestNegotiatorBehaviors');
$this->addTestSuite('TestOpenID1SessionNegotiation');
$this->addTestSuite('TestOpenID2SessionNegotiation');
}
}
|