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
|
<?php
/*
* Unit tests for verification of return_to URLs for a realm.
*/
require_once 'Auth/OpenID/Discover.php';
require_once 'Auth/OpenID/TrustRoot.php';
require_once 'Auth/Yadis/Yadis.php';
require_once 'PHPUnit.php';
// Because "null" cannot be passed by reference.
$NULL_FETCHER = null;
/*
* Tests for building the discovery URL from a realm and a return_to
* URL
*/
class Tests_Auth_OpenID_BuildDiscoveryURL extends PHPUnit_TestCase {
/*
* Build a discovery URL out of the realm and a return_to and make
* sure that it matches the expected discovery URL
*/
function failUnlessDiscoURL($realm, $expected_discovery_url)
{
$actual_discovery_url = Auth_OpenID_TrustRoot::buildDiscoveryURL($realm);
$this->assertEquals($expected_discovery_url, $actual_discovery_url);
}
/*
* There is no wildcard and the realm is the same as the return_to
* URL
*/
function test_trivial()
{
$this->failUnlessDiscoURL('http://example.com/foo',
'http://example.com/foo');
}
/*
* There is a wildcard
*/
function test_wildcard()
{
$this->failUnlessDiscoURL('http://*.example.com/foo',
'http://www.example.com/foo');
}
}
class _MockDiscover {
function _MockDiscover(&$data) {
$this->data =& $data;
}
function mockDiscover($uri, $fetcher, $discover_function=null)
{
$result = new Auth_Yadis_DiscoveryResult($uri);
$result->response_text = $this->data;
$result->normalized_uri = $uri;
return $result;
}
}
class Tests_Auth_OpenID_ExtractReturnToURLs extends PHPUnit_TestCase {
var $disco_url = 'http://example.com/';
function failUnlessXRDSHasReturnURLs($data, $expected_return_urls)
{
$discover_object = new _MockDiscover($data);
$actual_return_urls = Auth_OpenID_getAllowedReturnURLs($this->disco_url, $NULL_FETCHER, array(&$discover_object, 'mockDiscover'));
$this->assertEquals($expected_return_urls, $actual_return_urls);
}
function failUnlessDiscoveryFailure($text)
{
$discover_object = new _MockDiscover($text);
$this->assertFalse(Auth_OpenID_getAllowedReturnURLs($this->disco_url, $NULL_FETCHER, array(&$discover_object, 'mockDiscover')));
}
function test_empty()
{
$this->failUnlessDiscoveryFailure('');
}
function test_badXML()
{
$this->failUnlessDiscoveryFailure('>');
}
function test_noEntries()
{
$this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
<XRD>
</XRD>
</xrds:XRDS>
', array());
}
function test_noReturnToEntries()
{
$this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
<XRD>
<Service priority="10">
<Type>http://specs.openid.net/auth/2.0/server</Type>
<URI>http://www.myopenid.com/server</URI>
</Service>
</XRD>
</xrds:XRDS>
', array());
}
function test_oneEntry()
{
$this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
<XRD>
<Service>
<Type>http://specs.openid.net/auth/2.0/return_to</Type>
<URI>http://rp.example.com/return</URI>
</Service>
</XRD>
</xrds:XRDS>
', array('http://rp.example.com/return'));
}
function test_twoEntries()
{
$this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
<XRD>
<Service priority="0">
<Type>http://specs.openid.net/auth/2.0/return_to</Type>
<URI>http://rp.example.com/return</URI>
</Service>
<Service priority="1">
<Type>http://specs.openid.net/auth/2.0/return_to</Type>
<URI>http://other.rp.example.com/return</URI>
</Service>
</XRD>
</xrds:XRDS>
', array('http://rp.example.com/return',
'http://other.rp.example.com/return'));
}
function test_twoEntries_withOther()
{
$this->failUnlessXRDSHasReturnURLs('<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns:xrds="xri://$xrds"
xmlns="xri://$xrd*($v*2.0)"
>
<XRD>
<Service priority="0">
<Type>http://specs.openid.net/auth/2.0/return_to</Type>
<URI>http://rp.example.com/return</URI>
</Service>
<Service priority="1">
<Type>http://specs.openid.net/auth/2.0/return_to</Type>
<URI>http://other.rp.example.com/return</URI>
</Service>
<Service priority="0">
<Type>http://example.com/LOLCATS</Type>
<URI>http://example.com/invisible+uri</URI>
</Service>
</XRD>
</xrds:XRDS>
', array('http://rp.example.com/return',
'http://other.rp.example.com/return'));
}
}
class Tests_Auth_OpenID_ReturnToMatches extends PHPUnit_TestCase {
function test_noEntries()
{
$this->assertFalse(Auth_OpenID_returnToMatches(array(), 'anything'));
}
function test_exactMatch()
{
$r = 'http://example.com/return.to';
$this->assertTrue(Auth_OpenID_returnToMatches(array($r), $r));
}
function test_garbageMatch()
{
$r = 'http://example.com/return.to';
$this->assertTrue(Auth_OpenID_returnToMatches(
array('This is not a URL at all. In fact, it has characters, ' .
'like "<" that are not allowed in URLs', $r), $r));
}
function test_descendant()
{
$r = 'http://example.com/return.to';
$this->assertTrue(Auth_OpenID_returnToMatches(array($r),
'http://example.com/return.to/user:joe'));
}
function test_wildcard()
{
$this->assertFalse(Auth_OpenID_returnToMatches(
array('http://*.example.com/return.to'),
'http://example.com/return.to'));
}
function test_noMatch()
{
$r = 'http://example.com/return.to';
$this->assertFalse(Auth_OpenID_returnToMatches(array($r),
'http://example.com/xss_exploit'));
}
}
class Verifier {
function Verifier(&$test_case, $return_to)
{
$this->tc =& $test_case;
$this->return_to = $return_to;
}
function verify($disco_url)
{
$this->tc->assertEquals('http://www.example.com/', $disco_url);
if ($this->return_to === false) {
return false;
} else {
return array($this->return_to);
}
}
}
class Tests_Auth_OpenID_VerifyReturnTo extends PHPUnit_TestCase {
function test_bogusRealm()
{
$this->assertFalse(Auth_OpenID_verifyReturnTo('', 'http://example.com/', $NULL_FETCHER));
}
function test_verifyWithDiscoveryCalled()
{
$realm = 'http://*.example.com/';
$return_to = 'http://www.example.com/foo';
$v = new Verifier($this, $return_to);
$this->assertTrue(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify')));
}
function test_verifyFailWithDiscoveryCalled()
{
$realm = 'http://*.example.com/';
$return_to = 'http://www.example.com/foo';
$v = new Verifier($this, 'http://something-else.invalid/');
$this->assertFalse(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify')));
}
function test_verifyFailIfDiscoveryRedirects()
{
$realm = 'http://*.example.com/';
$return_to = 'http://www.example.com/foo';
$v = new Verifier($this, false);
$this->assertFalse(Auth_OpenID_verifyReturnTo($realm, $return_to, $NULL_FETCHER, array(&$v, 'verify')));
}
}
class Tests_Auth_OpenID_RPVerify extends PHPUnit_TestSuite {
function getName()
{
return "Tests_Auth_OpenID_RPVerify";
}
function Tests_Auth_OpenID_RPVerify()
{
$this->addTestSuite('Tests_Auth_OpenID_VerifyReturnTo');
$this->addTestSuite('Tests_Auth_OpenID_ReturnToMatches');
$this->addTestSuite('Tests_Auth_OpenID_ExtractReturnToURLs');
$this->addTestSuite('Tests_Auth_OpenID_BuildDiscoveryURL');
}
}
?>
|