1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252 |
- <?php
- /**
- * Unit tests for the Auth_OpenID_Message implementation.
- */
- require_once "Auth/OpenID/Message.php";
- require_once "Auth/OpenID.php";
- class MessageTest extends PHPUnit_Framework_TestCase {
- function _argTest($ns, $key, $expected = null)
- {
- $a_default = 'a bogus default value';
- $this->assertEquals($this->msg->getArg($ns, $key), $expected);
- if ($expected === null) {
- $this->assertEquals(
- $this->msg->getArg($ns, $key, $a_default), $a_default);
- $result = $this->msg->getArg($ns, $key, Auth_OpenID_NO_DEFAULT);
- $this->assertTrue(Auth_OpenID::isFailure($result));
- } else {
- $this->assertEquals(
- $this->msg->getArg($ns, $key, $a_default), $expected);
- $this->assertEquals(
- $this->msg->getArg($ns, $key, Auth_OpenID_NO_DEFAULT),
- $expected);
- }
- }
- }
- class Tests_Auth_OpenID_EmptyMessage extends MessageTest {
- function setUp()
- {
- $this->msg = new Auth_OpenID_Message();
- }
- function test_toPostArgs()
- {
- $this->assertEquals($this->msg->toPostArgs(), array());
- }
- function test_toArgs()
- {
- $this->assertEquals($this->msg->toArgs(), array());
- }
- function test_toKVForm()
- {
- $this->assertEquals($this->msg->toKVForm(), '');
- }
- function test_toURLEncoded()
- {
- $this->assertEquals($this->msg->toURLEncoded(), '');
- }
- function test_toURL()
- {
- $base_url = 'http://base.url/';
- $this->assertEquals($this->msg->toURL($base_url), $base_url);
- }
- function test_getOpenID()
- {
- $this->assertEquals($this->msg->getOpenIDNamespace(), null);
- }
- function test_getKeyOpenID()
- {
- $key = $this->msg->getKey(Auth_OpenID_OPENID_NS, 'foo');
- $this->assertTrue(Auth_OpenID::isFailure($key));
- $this->msg->setOpenIDNamespace(Auth_OpenID_OPENID1_NS, false);
- $key = $this->msg->getKey(Auth_OpenID_OPENID_NS, 'foo');
- $this->assertEquals('openid.foo', $key);
- }
- function test_getKeyBARE()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'foo'), 'foo');
- }
- function test_getKeyNS1()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID1_NS, 'foo'), null);
- }
- function test_getKeyNS2()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID2_NS, 'foo'), null);
- }
- function test_getKeyNS3()
- {
- $this->assertEquals($this->msg->getKey('urn:nothing-significant', 'foo'),
- null);
- }
- function test_hasKey()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'foo'), false);
- }
- function test_hasKeyBARE()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'foo'), false);
- }
- function test_hasKeyNS1()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'foo'), false);
- }
- function test_hasKeyNS2()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'foo'), false);
- }
- function test_hasKeyNS3()
- {
- $this->assertEquals($this->msg->hasKey('urn:nothing-significant', 'foo'),
- false);
- }
- function test_getArg()
- {
- $result = $this->msg->getArg(Auth_OpenID_OPENID_NS, 'foo');
- $this->assertTrue(Auth_OpenID::isFailure($result));
- }
- function test_getArgs()
- {
- $result = $this->msg->getArgs(Auth_OpenID_OPENID_NS);
- $this->assertTrue(Auth_OpenID::isFailure($result));
- }
- function test_getArgsBARE()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS), array());
- }
- function test_getArgsNS1()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS), array());
- }
- function test_getArgsNS2()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS), array());
- }
- function test_getArgsNS3()
- {
- $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
- }
- function test_updateArgs()
- {
- $result= $this->msg->updateArgs(Auth_OpenID_OPENID_NS,
- array('does not' => 'matter'));
- $this->assertTrue(Auth_OpenID::isFailure($result));
- }
- function _test_updateArgsNS($ns)
- {
- $update_args = array(
- 'Camper van Beethoven' => 'David Lowery',
- 'Magnolia Electric Co.' => 'Jason Molina');
- $this->assertEquals($this->msg->getArgs($ns), array());
- $this->msg->updateArgs($ns, $update_args);
- $this->assertEquals($this->msg->getArgs($ns), $update_args);
- }
- function test_updateArgsBARE()
- {
- $this->_test_updateArgsNS(Auth_OpenID_BARE_NS);
- }
- function test_updateArgsNS1()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS);
- }
- function test_updateArgsNS2()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS);
- }
- function test_updateArgsNS3()
- {
- $this->_test_updateArgsNS('urn:nothing-significant');
- }
- function test_setArg()
- {
- $result = $this->msg->setArg(Auth_OpenID_OPENID_NS,
- 'does not', 'matter');
- $this->assertTrue(Auth_OpenID::isFailure($result));
- }
- function _test_setArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $value = 'David Lowery';
- $this->assertEquals($this->msg->getArg($ns, $key), null);
- $this->msg->setArg($ns, $key, $value);
- $this->assertEquals($this->msg->getArg($ns, $key), $value);
- }
- function test_setArgBARE()
- {
- $this->_test_setArgNS(Auth_OpenID_BARE_NS);
- }
- function test_setArgNS1()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_setArgNS2()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_setArgNS3()
- {
- $this->_test_setArgNS('urn:nothing-significant');
- }
- function test_delArg()
- {
- $result = $this->msg->delArg(Auth_OpenID_OPENID_NS, 'does not');
- $this->assertTrue(Auth_OpenID::isFailure($result));
- }
- function _test_delArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $this->assertEquals($this->msg->delArg($ns, $key), false);
- }
- function test_delArgBARE()
- {
- $this->_test_delArgNS(Auth_OpenID_BARE_NS);
- }
- function test_delArgNS1()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_delArgNS2()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_delArgNS3()
- {
- $this->_test_delArgNS('urn:nothing-significant');
- }
- function test_isOpenID1()
- {
- $this->assertFalse($this->msg->isOpenID1());
- }
- function test_isOpenID2()
- {
- $this->assertFalse($this->msg->isOpenID2());
- }
- function test_args()
- {
- $this->_argTest(Auth_OpenID_BARE_NS, 'foo');
- $this->_argTest(Auth_OpenID_OPENID1_NS, 'foo');
- $this->_argTest(Auth_OpenID_OPENID2_NS, 'foo');
- $this->_argTest('urn:nothing-significant', 'foo');
- }
- }
- class Tests_Auth_OpenID_OpenID1Message extends MessageTest {
- function setUp()
- {
- $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
- 'openid.error' => 'unit test'));
- }
- function test_toPostArgs()
- {
- $this->assertEquals($this->msg->toPostArgs(),
- array('openid.mode' => 'error',
- 'openid.error' => 'unit test'));
- }
- function test_toArgs()
- {
- $this->assertEquals($this->msg->toArgs(),
- array('mode' => 'error',
- 'error' => 'unit test'));
- }
- function test_toKVForm()
- {
- $this->assertEquals($this->msg->toKVForm(),
- "error:unit test\nmode:error\n");
- }
- function test_toURLEncoded()
- {
- $this->assertEquals($this->msg->toURLEncoded(),
- 'openid.error=unit+test&openid.mode=error');
- }
- function test_toURL()
- {
- $base_url = 'http://base.url/';
- $actual = $this->msg->toURL($base_url);
- $actual_base = substr($actual, 0, strlen($base_url));
- $this->assertEquals($actual_base, $base_url);
- $this->assertEquals($actual[strlen($base_url)], '?');
- $query = substr($actual, strlen($base_url) + 1);
- $parsed = Auth_OpenID::parse_str($query);
- $this->assertEquals($parsed, array('openid.mode' => 'error',
- 'openid.error' => 'unit test'));
- }
- function test_getOpenID()
- {
- $this->assertEquals($this->msg->getOpenIDNamespace(),
- Auth_OpenID_OPENID1_NS);
- $this->assertTrue($this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
- }
- function test_getKeyOpenID()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID_NS, 'mode'),
- 'openid.mode');
- }
- function test_getKeyBARE()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'mode'), 'mode');
- }
- function test_getKeyNS1()
- {
- $this->assertEquals(
- $this->msg->getKey(Auth_OpenID_OPENID1_NS, 'mode'), 'openid.mode');
- }
- function test_getKeyNS2()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID2_NS, 'mode'), null);
- }
- function test_getKeyNS3()
- {
- $this->assertEquals(
- $this->msg->getKey('urn:nothing-significant', 'mode'), null);
- }
- function test_hasKey()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'mode'), true);
- }
- function test_hasKeyBARE()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'mode'), false);
- }
- function test_hasKeyNS1()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'mode'), true);
- }
- function test_hasKeyNS2()
- {
- $this->assertEquals(
- $this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'mode'), false);
- }
- function test_hasKeyNS3()
- {
- $this->assertEquals(
- $this->msg->hasKey('urn:nothing-significant', 'mode'), false);
- }
- function test_getArgs()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID_NS),
- array('mode' => 'error',
- 'error' => 'unit test'));
- }
- function test_getArgsBARE()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS), array());
- }
- function test_getArgsNS1()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS),
- array('mode' => 'error',
- 'error' => 'unit test'));
- }
- function test_getArgsNS2()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS), array());
- }
- function test_getArgsNS3()
- {
- $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
- }
- function _test_updateArgsNS($ns, $before=null)
- {
- if ($before === null) {
- $before = array();
- }
- $update_args = array(
- 'Camper van Beethoven' => 'David Lowery',
- 'Magnolia Electric Co.' => 'Jason Molina');
- $this->assertEquals($this->msg->getArgs($ns), $before);
- $this->msg->updateArgs($ns, $update_args);
- $after = $before;
- $after = array_merge($after, $update_args);
- $this->assertEquals($this->msg->getArgs($ns), $after);
- }
- function test_updateArgs()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID_NS,
- array('mode' => 'error', 'error' => 'unit test'));
- }
- function test_updateArgsBARE()
- {
- $this->_test_updateArgsNS(Auth_OpenID_BARE_NS);
- }
- function test_updateArgsNS1()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS,
- array('mode' => 'error', 'error' => 'unit test'));
- }
- function test_updateArgsNS2()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS);
- }
- function test_updateArgsNS3()
- {
- $this->_test_updateArgsNS('urn:nothing-significant');
- }
- function _test_setArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $value = 'David Lowery';
- $this->assertEquals($this->msg->getArg($ns, $key), null);
- $this->msg->setArg($ns, $key, $value);
- $this->assertEquals($this->msg->getArg($ns, $key), $value);
- }
- function test_setArg()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID_NS);
- }
- function test_setArgBARE()
- {
- $this->_test_setArgNS(Auth_OpenID_BARE_NS);
- }
- function test_setArgNS1()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_setArgNS2()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_setArgNS3()
- {
- $this->_test_setArgNS('urn:nothing-significant');
- }
- function _test_delArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $value = 'David Lowery';
- $this->assertEquals($this->msg->delArg($ns, $key), false);
- $this->msg->setArg($ns, $key, $value);
- $this->assertEquals($this->msg->getArg($ns, $key), $value);
- $this->msg->delArg($ns, $key);
- $this->assertEquals($this->msg->getArg($ns, $key), null);
- }
- function test_delArg()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID_NS);
- }
- function test_delArgBARE()
- {
- $this->_test_delArgNS(Auth_OpenID_BARE_NS);
- }
- function test_delArgNS1()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_delArgNS2()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_delArgNS3()
- {
- $this->_test_delArgNS('urn:nothing-significant');
- }
- function test_isOpenID1()
- {
- $this->assertTrue($this->msg->isOpenID1());
- }
- function test_isOpenID2()
- {
- $this->assertFalse($this->msg->isOpenID2());
- }
- function test_args()
- {
- $this->_argTest(Auth_OpenID_BARE_NS, 'mode');
- $this->_argTest(Auth_OpenID_OPENID_NS, 'mode', 'error');
- $this->_argTest(Auth_OpenID_OPENID1_NS, 'mode', 'error');
- $this->_argTest(Auth_OpenID_OPENID2_NS, 'mode');
- $this->_argTest('urn:nothing-significant', 'mode');
- }
- }
- class Tests_Auth_OpenID_OpenID1ExplicitMessage extends PHPUnit_Framework_TestCase {
- function setUp()
- {
- $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
- 'openid.error' => 'unit test',
- 'openid.ns' => Auth_OpenID_OPENID1_NS));
- }
- function test_isOpenID1()
- {
- $this->assertTrue($this->msg->isOpenID1());
- $this->assertFalse(
- $this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
- }
- function test_isOpenID2()
- {
- $this->assertFalse($this->msg->isOpenID2());
- }
- function test_toPostArgs()
- {
- $this->assertEquals($this->msg->toPostArgs(),
- array('openid.mode' => 'error',
- 'openid.error' => 'unit test',
- 'openid.ns' => Auth_OpenID_OPENID1_NS));
- }
- function test_toArgs()
- {
- $this->assertEquals($this->msg->toArgs(),
- array('mode' => 'error',
- 'error' => 'unit test',
- 'ns' => Auth_OpenID_OPENID1_NS));
- }
- function test_toKVForm()
- {
- $this->assertEquals($this->msg->toKVForm(),
- "error:unit test\nmode:error\nns:".
- Auth_OpenID_OPENID1_NS."\n");
- }
- function test_toURLEncoded()
- {
- $this->assertEquals($this->msg->toURLEncoded(),
- 'openid.error=unit+test&openid.mode=error&openid.ns=http%3A%2F%2Fopenid.net%2Fsignon%2F1.0');
- }
- function test_toURL()
- {
- $base_url = 'http://base.url/';
- $actual = $this->msg->toURL($base_url);
- $actual_base = substr($actual, 0, strlen($base_url));
- $this->assertEquals($actual_base, $base_url);
- $this->assertEquals($actual[strlen($base_url)], '?');
- $query = substr($actual, strlen($base_url) + 1);
- $parsed = Auth_OpenID::parse_str($query);
- $this->assertEquals($parsed, array('openid.mode' => 'error',
- 'openid.error' => 'unit test',
- 'openid.ns' => Auth_OpenID_OPENID1_NS));
- }
- }
- class Tests_Auth_OpenID_OpenID2Message extends MessageTest {
- function setUp()
- {
- $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
- 'openid.error' => 'unit test',
- 'openid.ns' => Auth_OpenID_OPENID2_NS));
- $this->msg->setArg(Auth_OpenID_BARE_NS, "xey", "value");
- }
- function test_toPostArgs()
- {
- $this->assertEquals($this->msg->toPostArgs(),
- array('openid.mode' => 'error',
- 'openid.error' => 'unit test',
- 'openid.ns' => Auth_OpenID_OPENID2_NS,
- 'xey' => 'value'));
- }
- function test_toArgs()
- {
- // This method can't tolerate BARE_NS.
- $this->msg->delArg(Auth_OpenID_BARE_NS, "xey");
- $this->assertEquals($this->msg->toArgs(),
- array('mode' => 'error',
- 'error' => 'unit test',
- 'ns' => Auth_OpenID_OPENID2_NS));
- }
- function test_toKVForm()
- {
- // Can't tolerate BARE_NS in kvform
- $this->msg->delArg(Auth_OpenID_BARE_NS, "xey");
- $this->assertEquals($this->msg->toKVForm(),
- sprintf("error:unit test\nmode:error\nns:%s\n",
- Auth_OpenID_OPENID2_NS));
- }
- function _test_urlencoded($s)
- {
- $expected = 'openid.error=unit+test&openid.mode=error&' .
- 'openid.ns=%s&xey=value';
- $expected = sprintf($expected, urlencode(Auth_OpenID_OPENID2_NS));
- $this->assertEquals($s, $expected);
- }
- function test_toURLEncoded()
- {
- $this->_test_urlencoded($this->msg->toURLEncoded());
- }
- function test_toURL()
- {
- $base_url = 'http://base.url/';
- $actual = $this->msg->toURL($base_url);
- $actual_base = substr($actual, 0, strlen($base_url));
- $this->assertEquals($actual_base, $base_url);
- $this->assertEquals($actual[strlen($base_url)], '?');
- $query = substr($actual, strlen($base_url) + 1);
- $this->_test_urlencoded($query);
- }
- function test_getOpenID()
- {
- $this->assertEquals($this->msg->getOpenIDNamespace(),
- Auth_OpenID_OPENID2_NS);
- }
- function test_getKeyOpenID()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID_NS, 'mode'),
- 'openid.mode');
- }
- function test_getKeyBARE()
- {
- $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'mode'), 'mode');
- }
- function test_getKeyNS1()
- {
- $this->assertEquals(
- $this->msg->getKey(Auth_OpenID_OPENID1_NS, 'mode'), null);
- }
- function test_getKeyNS2()
- {
- $this->assertEquals(
- $this->msg->getKey(Auth_OpenID_OPENID2_NS, 'mode'), 'openid.mode');
- }
- function test_getKeyNS3()
- {
- $this->assertEquals(
- $this->msg->getKey('urn:nothing-significant', 'mode'), null);
- }
- function test_hasKeyOpenID()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'mode'), true);
- }
- function test_hasKeyBARE()
- {
- $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'mode'), false);
- }
- function test_hasKeyNS1()
- {
- $this->assertEquals(
- $this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'mode'), false);
- }
- function test_hasKeyNS2()
- {
- $this->assertEquals(
- $this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'mode'), true);
- }
- function test_hasKeyNS3()
- {
- $this->assertEquals(
- $this->msg->hasKey('urn:nothing-significant', 'mode'), false);
- }
- function test_getArgsOpenID()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID_NS),
- array('mode' => 'error',
- 'error' => 'unit test'));
- }
- function test_getArgsBARE()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS),
- array('xey' => 'value'));
- }
- function test_getArgsNS1()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS), array());
- }
- function test_getArgsNS2()
- {
- $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS),
- array('mode' => 'error',
- 'error' => 'unit test'));
- }
- function test_getArgsNS3()
- {
- $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
- }
- function _test_updateArgsNS($ns, $before=null)
- {
- if ($before === null) {
- $before = array();
- }
- $update_args = array(
- 'Camper van Beethoven' => 'David Lowery',
- 'Magnolia Electric Co.' => 'Jason Molina');
- $this->assertEquals($this->msg->getArgs($ns), $before);
- $this->msg->updateArgs($ns, $update_args);
- $after = $before;
- $after = array_merge($after, $update_args);
- $this->assertEquals($this->msg->getArgs($ns), $after);
- }
- function test_updateArgsOpenID()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID_NS,
- array('mode' => 'error', 'error' => 'unit test'));
- }
- function test_updateArgsBARE()
- {
- $this->_test_updateArgsNS(Auth_OpenID_BARE_NS,
- array('xey' => 'value'));
- }
- function test_updateArgsNS1()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS);
- }
- function test_updateArgsNS2()
- {
- $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS,
- array('mode' => 'error', 'error' => 'unit test'));
- }
- function test_updateArgsNS3()
- {
- $this->_test_updateArgsNS('urn:nothing-significant');
- }
- function _test_setArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $value = 'David Lowery';
- $this->assertEquals($this->msg->getArg($ns, $key), null);
- $this->msg->setArg($ns, $key, $value);
- $this->assertEquals($this->msg->getArg($ns, $key), $value);
- }
- function test_setArgOpenID()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID_NS);
- }
- function test_setArgBARE()
- {
- $this->_test_setArgNS(Auth_OpenID_BARE_NS);
- }
- function test_setArgNS1()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_setArgNS2()
- {
- $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_setArgNS3()
- {
- $this->_test_setArgNS('urn:nothing-significant');
- }
- function test_badAlias()
- {
- // Make sure dotted aliases and OpenID protocol fields are not
- // allowed as namespace aliases.
- global $Auth_OpenID_OPENID_PROTOCOL_FIELDS;
- $all = array_merge($Auth_OpenID_OPENID_PROTOCOL_FIELDS, array('dotted.alias'));
- foreach ($all as $f) {
- $args = array(sprintf('openid.ns.%s', $f) => 'blah',
- sprintf('openid.%s.foo', $f) => 'test');
- // .fromPostArgs covers .fromPostArgs, .fromOpenIDArgs,
- // ._fromOpenIDArgs, and .fromOpenIDArgs (since it calls
- // .fromPostArgs). Python code raises AssertionError, but
- // we usually return null for bad things in PHP.
- $this->assertEquals($this->msg->fromPostArgs($args), null);
- }
- }
- function _test_delArgNS($ns)
- {
- $key = 'Camper van Beethoven';
- $value = 'David Lowery';
- $this->assertEquals($this->msg->delArg($ns, $key), false);
- $this->msg->setArg($ns, $key, $value);
- $this->assertEquals($this->msg->getArg($ns, $key), $value);
- $this->msg->delArg($ns, $key);
- $this->assertEquals($this->msg->getArg($ns, $key), null);
- }
- function test_delArgOpenID()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID_NS);
- }
- function test_delArgBARE()
- {
- $this->_test_delArgNS(Auth_OpenID_BARE_NS);
- }
- function test_delArgNS1()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
- }
- function test_delArgNS2()
- {
- $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
- }
- function test_delArgNS3()
- {
- $this->_test_delArgNS('urn:nothing-significant');
- }
- function test_overwriteExtensionArg()
- {
- $ns = 'urn:unittest_extension';
- $key = 'mykey';
- $value_1 = 'value_1';
- $value_2 = 'value_2';
- $this->msg->setArg($ns, $key, $value_1);
- $this->assertTrue($this->msg->getArg($ns, $key) == $value_1);
- $this->msg->setArg($ns, $key, $value_2);
- $this->assertTrue($this->msg->getArg($ns, $key) == $value_2);
- }
- function test_argList()
- {
- $this->assertEquals($this->msg->fromPostArgs(array('arg' => array(1, 2, 3))),
- null);
- }
- function test_isOpenID1()
- {
- $this->assertFalse($this->msg->isOpenID1());
- }
- function test_isOpenID2()
- {
- $this->assertTrue($this->msg->isOpenID2());
- }
- function test_args()
- {
- $this->_argTest(Auth_OpenID_BARE_NS, 'mode');
- $this->_argTest(Auth_OpenID_OPENID_NS, 'mode', 'error');
- $this->_argTest(Auth_OpenID_OPENID1_NS, 'mode');
- $this->_argTest(Auth_OpenID_OPENID2_NS, 'mode', 'error');
- $this->_argTest('urn:nothing-significant', 'mode');
- }
- }
- class Tests_Auth_OpenID_GeneralMessageTest extends PHPUnit_Framework_TestCase {
- function setUp()
- {
- $this->postargs = array(
- 'openid.ns' => Auth_OpenID_OPENID2_NS,
- 'openid.mode' => 'checkid_setup',
- 'openid.identity' => 'http://bogus.example.invalid:port/',
- 'openid.assoc_handle' => 'FLUB',
- 'openid.return_to' => 'Neverland');
- $this->action_url = 'scheme://host:port/path?query';
- $this->form_tag_attrs = array(
- 'company' => 'janrain',
- 'class' => 'fancyCSS');
- $this->submit_text = 'GO!';
- // Expected data regardless of input
- $this->required_form_attrs = array(
- 'accept-charset' => 'UTF-8',
- 'enctype' => 'application/x-www-form-urlencoded',
- 'method' => 'post');
- }
- function _checkForm($html, $message_, $action_url,
- $form_tag_attrs, $submit_text)
- {
- $parser = Auth_Yadis_getXMLParser();
- // Parse HTML source
- $this->assertTrue($parser->init($html, array()));
- // Get root element
- $form = $parser->evalXPath('/form[1]');
- $this->assertTrue(count($form) == 1);
- $form = $form[0];
- // Check required form attributes
- $form_attrs = $parser->attributes($form);
- foreach ($this->required_form_attrs as $k => $v) {
- $this->assertTrue($form_attrs[$k] == $v);
- }
- // Check extra form attributes
- foreach ($form_tag_attrs as $k => $v) {
- // Skip attributes that already passed the required
- // attribute check, since they should be ignored by the
- // form generation code.
- if (in_array($k, array_keys($this->required_form_attrs))) {
- continue;
- }
- $this->assertTrue($form_attrs[$k] == $v,
- "Form attr $k is ".$form_attrs[$k]." (expected $v)");
- }
- // Check hidden fields against post args
- $hiddens = array();
- $input_elements = $parser->evalXPath('input', $form);
- foreach ($input_elements as $e) {
- $attrs = $parser->attributes($e);
- if (strtoupper($attrs['type']) == 'HIDDEN') {
- $hiddens[] = $e;
- }
- }
- // For each post arg, make sure there is a hidden with that
- // value. Make sure there are no other hiddens.
- $postargs = $message_->toPostArgs();
- foreach ($postargs as $name => $value) {
- $found = false;
- foreach ($hiddens as $e) {
- $attrs = $parser->attributes($e);
- if ($attrs['name'] == $name) {
- $this->assertTrue($attrs['value'] == $value);
- $found = true;
- break;
- }
- }
- if (!$found) {
- $this->fail("Post arg $name not found in form");
- }
- }
- $keys = array_keys($postargs);
- foreach ($hiddens as $e) {
- $attrs = $parser->attributes($e);
- $this->assertTrue(in_array($attrs['name'], $keys));
- }
- // Check action URL
- $this->assertTrue($form_attrs['action'] == $action_url);
- // Check submit text
- $submits = array();
- foreach ($input_elements as $e) {
- $attrs = $parser->attributes($e);
- if (strtoupper($attrs['type']) == 'SUBMIT') {
- $submits[] = $e;
- }
- }
- $this->assertTrue(count($submits) == 1);
- $attrs = $parser->attributes($submits[0]);
- $this->assertTrue($attrs['value'] == $submit_text);
- }
- function test_toFormMarkup()
- {
- $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
- $html = $m->toFormMarkup($this->action_url, $this->form_tag_attrs,
- $this->submit_text);
- $this->_checkForm($html, $m, $this->action_url,
- $this->form_tag_attrs, $this->submit_text);
- }
- function test_overrideMethod()
- {
- // Be sure that caller cannot change form method to GET.
- $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
- $tag_attrs = $this->form_tag_attrs;
- $tag_attrs['method'] = 'GET';
- $html = $m->toFormMarkup($this->action_url, $this->form_tag_attrs,
- $this->submit_text);
- $this->_checkForm($html, $m, $this->action_url,
- $this->form_tag_attrs, $this->submit_text);
- }
- function test_overrideRequired()
- {
- // Be sure that caller CANNOT change the form charset for
- // encoding type.
- $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
- $tag_attrs = $this->form_tag_attrs;
- $tag_attrs['accept-charset'] = 'UCS4';
- $tag_attrs['enctype'] = 'invalid/x-broken';
- $html = $m->toFormMarkup($this->action_url, $tag_attrs,
- $this->submit_text);
- $this->_checkForm($html, $m, $this->action_url,
- $tag_attrs, $this->submit_text);
- }
- function test_setOpenIDNamespace_invalid()
- {
- $m = new Auth_OpenID_Message();
- $invalid_things = array(
- // Empty string is not okay here.
- '',
- // Good guess! But wrong.
- 'http://openid.net/signon/2.0',
- // What?
- 'http://specs%\\\r2Eopenid.net/auth/2.0',
- // Too much escapings!
- 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0',
- // This is a Type URI, not a openid.ns value.
- 'http://specs.openid.net/auth/2.0/signon',
- );
- foreach ($invalid_things as $x) {
- $this->assertTrue($m->setOpenIDNamespace($x, true) === false);
- }
- }
- function test_isOpenID1()
- {
- $v1_namespaces = array(
- // Yes, there are two of them.
- 'http://openid.net/signon/1.1',
- 'http://openid.net/signon/1.0',
- );
- foreach ($v1_namespaces as $ns) {
- $m = new Auth_OpenID_Message($ns);
- $this->assertTrue($m->isOpenID1(),
- "$ns not recognized as OpenID 1");
- $this->assertEquals($ns, $m->getOpenIDNamespace());
- $this->assertTrue($m->namespaces->isImplicit($ns));
- }
- }
- function test_isOpenID2()
- {
- $ns = 'http://specs.openid.net/auth/2.0';
- $m = new Auth_OpenID_Message($ns);
- $this->assertTrue($m->isOpenID2());
- $this->assertFalse(
- $m->namespaces->isImplicit(Auth_OpenID_NULL_NAMESPACE));
- $this->assertEquals($ns, $m->getOpenIDNamespace());
- }
- function test_setOpenIDNamespace_explicit()
- {
- $m = new Auth_OpenID_Message();
- $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, false);
- $this->assertFalse($m->namespaces->isImplicit(
- Auth_OpenID_THE_OTHER_OPENID1_NS));
- }
- function test_setOpenIDNamespace_implicit()
- {
- $m = new Auth_OpenID_Message();
- $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, true);
- $this->assertTrue(
- $m->namespaces->isImplicit(Auth_OpenID_THE_OTHER_OPENID1_NS));
- }
- function test_explicitOpenID11NSSerialzation()
- {
- $m = new Auth_OpenID_Message();
- $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, false);
- $post_args = $m->toPostArgs();
- $this->assertEquals($post_args,
- array('openid.ns' =>
- Auth_OpenID_THE_OTHER_OPENID1_NS));
- }
- function test_fromPostArgs_ns11()
- {
- // An example of the stuff that some Drupal installations send us,
- // which includes openid.ns but is 1.1.
- $query = array(
- 'openid.assoc_handle' => '',
- 'openid.claimed_id' => 'http://foobar.invalid/',
- 'openid.identity' => 'http://foobar.myopenid.com',
- 'openid.mode' => 'checkid_setup',
- 'openid.ns' => 'http://openid.net/signon/1.1',
- 'openid.ns.sreg' => 'http://openid.net/extensions/sreg/1.1',
- 'openid.return_to' => 'http://drupal.invalid/return_to',
- 'openid.sreg.required' => 'nickname,email',
- 'openid.trust_root' => 'http://drupal.invalid',
- );
- $m = Auth_OpenID_Message::fromPostArgs($query);
- $this->assertTrue($m->isOpenID1());
- }
- }
- class Tests_Auth_OpenID_NamespaceMap extends PHPUnit_Framework_TestCase {
- function test_onealias()
- {
- $nsm = new Auth_OpenID_NamespaceMap();
- $uri = 'http://example.com/foo';
- $alias = "foo";
- $nsm->addAlias($uri, $alias);
- $this->assertTrue($nsm->getNamespaceURI($alias) == $uri);
- $this->assertTrue($nsm->getAlias($uri) == $alias);
- }
- function test_iteration()
- {
- $nsm = new Auth_OpenID_NamespaceMap();
- $uripat = 'http://example.com/foo%d';
- $nsm->add(sprintf($uripat, 0));
- for ($n = 1; $n < 23; $n++) {
- $this->assertTrue($nsm->contains(sprintf($uripat, $n - 1)));
- $this->assertTrue($nsm->isDefined(sprintf($uripat, $n - 1)));
- $nsm->add(sprintf($uripat, $n));
- }
- foreach ($nsm->iteritems() as $pair) {
- list($uri, $alias) = $pair;
- $this->assertTrue('ext'.substr($uri, 22) == $alias);
- }
- $it = $nsm->iterAliases();
- $this->assertTrue(count($it) == 23);
- $it = $nsm->iterNamespaceURIs();
- $this->assertTrue(count($it) == 23);
- }
- }
- class Tests_Auth_OpenID_Message extends PHPUnit_Framework_TestCase {
- }
- global $Tests_Auth_OpenID_Message_other;
- $Tests_Auth_OpenID_Message_other = array(
- new Tests_Auth_OpenID_EmptyMessage(),
- new Tests_Auth_OpenID_OpenID1Message(),
- new Tests_Auth_OpenID_OpenID2Message(),
- new Tests_Auth_OpenID_NamespaceMap(),
- new Tests_Auth_OpenID_OpenID1ExplicitMessage(),
- new Tests_Auth_OpenID_GeneralMessageTest()
- );
|