Message.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. <?php
  2. /**
  3. * Unit tests for the Auth_OpenID_Message implementation.
  4. */
  5. require_once "Auth/OpenID/Message.php";
  6. require_once "Auth/OpenID.php";
  7. class MessageTest extends PHPUnit_Framework_TestCase {
  8. function _argTest($ns, $key, $expected = null)
  9. {
  10. $a_default = 'a bogus default value';
  11. $this->assertEquals($this->msg->getArg($ns, $key), $expected);
  12. if ($expected === null) {
  13. $this->assertEquals(
  14. $this->msg->getArg($ns, $key, $a_default), $a_default);
  15. $result = $this->msg->getArg($ns, $key, Auth_OpenID_NO_DEFAULT);
  16. $this->assertTrue(Auth_OpenID::isFailure($result));
  17. } else {
  18. $this->assertEquals(
  19. $this->msg->getArg($ns, $key, $a_default), $expected);
  20. $this->assertEquals(
  21. $this->msg->getArg($ns, $key, Auth_OpenID_NO_DEFAULT),
  22. $expected);
  23. }
  24. }
  25. }
  26. class Tests_Auth_OpenID_EmptyMessage extends MessageTest {
  27. function setUp()
  28. {
  29. $this->msg = new Auth_OpenID_Message();
  30. }
  31. function test_toPostArgs()
  32. {
  33. $this->assertEquals($this->msg->toPostArgs(), array());
  34. }
  35. function test_toArgs()
  36. {
  37. $this->assertEquals($this->msg->toArgs(), array());
  38. }
  39. function test_toKVForm()
  40. {
  41. $this->assertEquals($this->msg->toKVForm(), '');
  42. }
  43. function test_toURLEncoded()
  44. {
  45. $this->assertEquals($this->msg->toURLEncoded(), '');
  46. }
  47. function test_toURL()
  48. {
  49. $base_url = 'http://base.url/';
  50. $this->assertEquals($this->msg->toURL($base_url), $base_url);
  51. }
  52. function test_getOpenID()
  53. {
  54. $this->assertEquals($this->msg->getOpenIDNamespace(), null);
  55. }
  56. function test_getKeyOpenID()
  57. {
  58. $key = $this->msg->getKey(Auth_OpenID_OPENID_NS, 'foo');
  59. $this->assertTrue(Auth_OpenID::isFailure($key));
  60. $this->msg->setOpenIDNamespace(Auth_OpenID_OPENID1_NS, false);
  61. $key = $this->msg->getKey(Auth_OpenID_OPENID_NS, 'foo');
  62. $this->assertEquals('openid.foo', $key);
  63. }
  64. function test_getKeyBARE()
  65. {
  66. $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'foo'), 'foo');
  67. }
  68. function test_getKeyNS1()
  69. {
  70. $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID1_NS, 'foo'), null);
  71. }
  72. function test_getKeyNS2()
  73. {
  74. $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID2_NS, 'foo'), null);
  75. }
  76. function test_getKeyNS3()
  77. {
  78. $this->assertEquals($this->msg->getKey('urn:nothing-significant', 'foo'),
  79. null);
  80. }
  81. function test_hasKey()
  82. {
  83. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'foo'), false);
  84. }
  85. function test_hasKeyBARE()
  86. {
  87. $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'foo'), false);
  88. }
  89. function test_hasKeyNS1()
  90. {
  91. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'foo'), false);
  92. }
  93. function test_hasKeyNS2()
  94. {
  95. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'foo'), false);
  96. }
  97. function test_hasKeyNS3()
  98. {
  99. $this->assertEquals($this->msg->hasKey('urn:nothing-significant', 'foo'),
  100. false);
  101. }
  102. function test_getArg()
  103. {
  104. $result = $this->msg->getArg(Auth_OpenID_OPENID_NS, 'foo');
  105. $this->assertTrue(Auth_OpenID::isFailure($result));
  106. }
  107. function test_getArgs()
  108. {
  109. $result = $this->msg->getArgs(Auth_OpenID_OPENID_NS);
  110. $this->assertTrue(Auth_OpenID::isFailure($result));
  111. }
  112. function test_getArgsBARE()
  113. {
  114. $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS), array());
  115. }
  116. function test_getArgsNS1()
  117. {
  118. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS), array());
  119. }
  120. function test_getArgsNS2()
  121. {
  122. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS), array());
  123. }
  124. function test_getArgsNS3()
  125. {
  126. $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
  127. }
  128. function test_updateArgs()
  129. {
  130. $result= $this->msg->updateArgs(Auth_OpenID_OPENID_NS,
  131. array('does not' => 'matter'));
  132. $this->assertTrue(Auth_OpenID::isFailure($result));
  133. }
  134. function _test_updateArgsNS($ns)
  135. {
  136. $update_args = array(
  137. 'Camper van Beethoven' => 'David Lowery',
  138. 'Magnolia Electric Co.' => 'Jason Molina');
  139. $this->assertEquals($this->msg->getArgs($ns), array());
  140. $this->msg->updateArgs($ns, $update_args);
  141. $this->assertEquals($this->msg->getArgs($ns), $update_args);
  142. }
  143. function test_updateArgsBARE()
  144. {
  145. $this->_test_updateArgsNS(Auth_OpenID_BARE_NS);
  146. }
  147. function test_updateArgsNS1()
  148. {
  149. $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS);
  150. }
  151. function test_updateArgsNS2()
  152. {
  153. $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS);
  154. }
  155. function test_updateArgsNS3()
  156. {
  157. $this->_test_updateArgsNS('urn:nothing-significant');
  158. }
  159. function test_setArg()
  160. {
  161. $result = $this->msg->setArg(Auth_OpenID_OPENID_NS,
  162. 'does not', 'matter');
  163. $this->assertTrue(Auth_OpenID::isFailure($result));
  164. }
  165. function _test_setArgNS($ns)
  166. {
  167. $key = 'Camper van Beethoven';
  168. $value = 'David Lowery';
  169. $this->assertEquals($this->msg->getArg($ns, $key), null);
  170. $this->msg->setArg($ns, $key, $value);
  171. $this->assertEquals($this->msg->getArg($ns, $key), $value);
  172. }
  173. function test_setArgBARE()
  174. {
  175. $this->_test_setArgNS(Auth_OpenID_BARE_NS);
  176. }
  177. function test_setArgNS1()
  178. {
  179. $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
  180. }
  181. function test_setArgNS2()
  182. {
  183. $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
  184. }
  185. function test_setArgNS3()
  186. {
  187. $this->_test_setArgNS('urn:nothing-significant');
  188. }
  189. function test_delArg()
  190. {
  191. $result = $this->msg->delArg(Auth_OpenID_OPENID_NS, 'does not');
  192. $this->assertTrue(Auth_OpenID::isFailure($result));
  193. }
  194. function _test_delArgNS($ns)
  195. {
  196. $key = 'Camper van Beethoven';
  197. $this->assertEquals($this->msg->delArg($ns, $key), false);
  198. }
  199. function test_delArgBARE()
  200. {
  201. $this->_test_delArgNS(Auth_OpenID_BARE_NS);
  202. }
  203. function test_delArgNS1()
  204. {
  205. $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
  206. }
  207. function test_delArgNS2()
  208. {
  209. $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
  210. }
  211. function test_delArgNS3()
  212. {
  213. $this->_test_delArgNS('urn:nothing-significant');
  214. }
  215. function test_isOpenID1()
  216. {
  217. $this->assertFalse($this->msg->isOpenID1());
  218. }
  219. function test_isOpenID2()
  220. {
  221. $this->assertFalse($this->msg->isOpenID2());
  222. }
  223. function test_args()
  224. {
  225. $this->_argTest(Auth_OpenID_BARE_NS, 'foo');
  226. $this->_argTest(Auth_OpenID_OPENID1_NS, 'foo');
  227. $this->_argTest(Auth_OpenID_OPENID2_NS, 'foo');
  228. $this->_argTest('urn:nothing-significant', 'foo');
  229. }
  230. }
  231. class Tests_Auth_OpenID_OpenID1Message extends MessageTest {
  232. function setUp()
  233. {
  234. $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
  235. 'openid.error' => 'unit test'));
  236. }
  237. function test_toPostArgs()
  238. {
  239. $this->assertEquals($this->msg->toPostArgs(),
  240. array('openid.mode' => 'error',
  241. 'openid.error' => 'unit test'));
  242. }
  243. function test_toArgs()
  244. {
  245. $this->assertEquals($this->msg->toArgs(),
  246. array('mode' => 'error',
  247. 'error' => 'unit test'));
  248. }
  249. function test_toKVForm()
  250. {
  251. $this->assertEquals($this->msg->toKVForm(),
  252. "error:unit test\nmode:error\n");
  253. }
  254. function test_toURLEncoded()
  255. {
  256. $this->assertEquals($this->msg->toURLEncoded(),
  257. 'openid.error=unit+test&openid.mode=error');
  258. }
  259. function test_toURL()
  260. {
  261. $base_url = 'http://base.url/';
  262. $actual = $this->msg->toURL($base_url);
  263. $actual_base = substr($actual, 0, strlen($base_url));
  264. $this->assertEquals($actual_base, $base_url);
  265. $this->assertEquals($actual[strlen($base_url)], '?');
  266. $query = substr($actual, strlen($base_url) + 1);
  267. $parsed = Auth_OpenID::parse_str($query);
  268. $this->assertEquals($parsed, array('openid.mode' => 'error',
  269. 'openid.error' => 'unit test'));
  270. }
  271. function test_getOpenID()
  272. {
  273. $this->assertEquals($this->msg->getOpenIDNamespace(),
  274. Auth_OpenID_OPENID1_NS);
  275. $this->assertTrue($this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
  276. }
  277. function test_getKeyOpenID()
  278. {
  279. $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID_NS, 'mode'),
  280. 'openid.mode');
  281. }
  282. function test_getKeyBARE()
  283. {
  284. $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'mode'), 'mode');
  285. }
  286. function test_getKeyNS1()
  287. {
  288. $this->assertEquals(
  289. $this->msg->getKey(Auth_OpenID_OPENID1_NS, 'mode'), 'openid.mode');
  290. }
  291. function test_getKeyNS2()
  292. {
  293. $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID2_NS, 'mode'), null);
  294. }
  295. function test_getKeyNS3()
  296. {
  297. $this->assertEquals(
  298. $this->msg->getKey('urn:nothing-significant', 'mode'), null);
  299. }
  300. function test_hasKey()
  301. {
  302. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'mode'), true);
  303. }
  304. function test_hasKeyBARE()
  305. {
  306. $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'mode'), false);
  307. }
  308. function test_hasKeyNS1()
  309. {
  310. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'mode'), true);
  311. }
  312. function test_hasKeyNS2()
  313. {
  314. $this->assertEquals(
  315. $this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'mode'), false);
  316. }
  317. function test_hasKeyNS3()
  318. {
  319. $this->assertEquals(
  320. $this->msg->hasKey('urn:nothing-significant', 'mode'), false);
  321. }
  322. function test_getArgs()
  323. {
  324. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID_NS),
  325. array('mode' => 'error',
  326. 'error' => 'unit test'));
  327. }
  328. function test_getArgsBARE()
  329. {
  330. $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS), array());
  331. }
  332. function test_getArgsNS1()
  333. {
  334. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS),
  335. array('mode' => 'error',
  336. 'error' => 'unit test'));
  337. }
  338. function test_getArgsNS2()
  339. {
  340. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS), array());
  341. }
  342. function test_getArgsNS3()
  343. {
  344. $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
  345. }
  346. function _test_updateArgsNS($ns, $before=null)
  347. {
  348. if ($before === null) {
  349. $before = array();
  350. }
  351. $update_args = array(
  352. 'Camper van Beethoven' => 'David Lowery',
  353. 'Magnolia Electric Co.' => 'Jason Molina');
  354. $this->assertEquals($this->msg->getArgs($ns), $before);
  355. $this->msg->updateArgs($ns, $update_args);
  356. $after = $before;
  357. $after = array_merge($after, $update_args);
  358. $this->assertEquals($this->msg->getArgs($ns), $after);
  359. }
  360. function test_updateArgs()
  361. {
  362. $this->_test_updateArgsNS(Auth_OpenID_OPENID_NS,
  363. array('mode' => 'error', 'error' => 'unit test'));
  364. }
  365. function test_updateArgsBARE()
  366. {
  367. $this->_test_updateArgsNS(Auth_OpenID_BARE_NS);
  368. }
  369. function test_updateArgsNS1()
  370. {
  371. $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS,
  372. array('mode' => 'error', 'error' => 'unit test'));
  373. }
  374. function test_updateArgsNS2()
  375. {
  376. $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS);
  377. }
  378. function test_updateArgsNS3()
  379. {
  380. $this->_test_updateArgsNS('urn:nothing-significant');
  381. }
  382. function _test_setArgNS($ns)
  383. {
  384. $key = 'Camper van Beethoven';
  385. $value = 'David Lowery';
  386. $this->assertEquals($this->msg->getArg($ns, $key), null);
  387. $this->msg->setArg($ns, $key, $value);
  388. $this->assertEquals($this->msg->getArg($ns, $key), $value);
  389. }
  390. function test_setArg()
  391. {
  392. $this->_test_setArgNS(Auth_OpenID_OPENID_NS);
  393. }
  394. function test_setArgBARE()
  395. {
  396. $this->_test_setArgNS(Auth_OpenID_BARE_NS);
  397. }
  398. function test_setArgNS1()
  399. {
  400. $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
  401. }
  402. function test_setArgNS2()
  403. {
  404. $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
  405. }
  406. function test_setArgNS3()
  407. {
  408. $this->_test_setArgNS('urn:nothing-significant');
  409. }
  410. function _test_delArgNS($ns)
  411. {
  412. $key = 'Camper van Beethoven';
  413. $value = 'David Lowery';
  414. $this->assertEquals($this->msg->delArg($ns, $key), false);
  415. $this->msg->setArg($ns, $key, $value);
  416. $this->assertEquals($this->msg->getArg($ns, $key), $value);
  417. $this->msg->delArg($ns, $key);
  418. $this->assertEquals($this->msg->getArg($ns, $key), null);
  419. }
  420. function test_delArg()
  421. {
  422. $this->_test_delArgNS(Auth_OpenID_OPENID_NS);
  423. }
  424. function test_delArgBARE()
  425. {
  426. $this->_test_delArgNS(Auth_OpenID_BARE_NS);
  427. }
  428. function test_delArgNS1()
  429. {
  430. $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
  431. }
  432. function test_delArgNS2()
  433. {
  434. $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
  435. }
  436. function test_delArgNS3()
  437. {
  438. $this->_test_delArgNS('urn:nothing-significant');
  439. }
  440. function test_isOpenID1()
  441. {
  442. $this->assertTrue($this->msg->isOpenID1());
  443. }
  444. function test_isOpenID2()
  445. {
  446. $this->assertFalse($this->msg->isOpenID2());
  447. }
  448. function test_args()
  449. {
  450. $this->_argTest(Auth_OpenID_BARE_NS, 'mode');
  451. $this->_argTest(Auth_OpenID_OPENID_NS, 'mode', 'error');
  452. $this->_argTest(Auth_OpenID_OPENID1_NS, 'mode', 'error');
  453. $this->_argTest(Auth_OpenID_OPENID2_NS, 'mode');
  454. $this->_argTest('urn:nothing-significant', 'mode');
  455. }
  456. }
  457. class Tests_Auth_OpenID_OpenID1ExplicitMessage extends PHPUnit_Framework_TestCase {
  458. function setUp()
  459. {
  460. $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
  461. 'openid.error' => 'unit test',
  462. 'openid.ns' => Auth_OpenID_OPENID1_NS));
  463. }
  464. function test_isOpenID1()
  465. {
  466. $this->assertTrue($this->msg->isOpenID1());
  467. $this->assertFalse(
  468. $this->msg->namespaces->isImplicit(Auth_OpenID_OPENID1_NS));
  469. }
  470. function test_isOpenID2()
  471. {
  472. $this->assertFalse($this->msg->isOpenID2());
  473. }
  474. function test_toPostArgs()
  475. {
  476. $this->assertEquals($this->msg->toPostArgs(),
  477. array('openid.mode' => 'error',
  478. 'openid.error' => 'unit test',
  479. 'openid.ns' => Auth_OpenID_OPENID1_NS));
  480. }
  481. function test_toArgs()
  482. {
  483. $this->assertEquals($this->msg->toArgs(),
  484. array('mode' => 'error',
  485. 'error' => 'unit test',
  486. 'ns' => Auth_OpenID_OPENID1_NS));
  487. }
  488. function test_toKVForm()
  489. {
  490. $this->assertEquals($this->msg->toKVForm(),
  491. "error:unit test\nmode:error\nns:".
  492. Auth_OpenID_OPENID1_NS."\n");
  493. }
  494. function test_toURLEncoded()
  495. {
  496. $this->assertEquals($this->msg->toURLEncoded(),
  497. 'openid.error=unit+test&openid.mode=error&openid.ns=http%3A%2F%2Fopenid.net%2Fsignon%2F1.0');
  498. }
  499. function test_toURL()
  500. {
  501. $base_url = 'http://base.url/';
  502. $actual = $this->msg->toURL($base_url);
  503. $actual_base = substr($actual, 0, strlen($base_url));
  504. $this->assertEquals($actual_base, $base_url);
  505. $this->assertEquals($actual[strlen($base_url)], '?');
  506. $query = substr($actual, strlen($base_url) + 1);
  507. $parsed = Auth_OpenID::parse_str($query);
  508. $this->assertEquals($parsed, array('openid.mode' => 'error',
  509. 'openid.error' => 'unit test',
  510. 'openid.ns' => Auth_OpenID_OPENID1_NS));
  511. }
  512. }
  513. class Tests_Auth_OpenID_OpenID2Message extends MessageTest {
  514. function setUp()
  515. {
  516. $this->msg = Auth_OpenID_Message::fromPostArgs(array('openid.mode' => 'error',
  517. 'openid.error' => 'unit test',
  518. 'openid.ns' => Auth_OpenID_OPENID2_NS));
  519. $this->msg->setArg(Auth_OpenID_BARE_NS, "xey", "value");
  520. }
  521. function test_toPostArgs()
  522. {
  523. $this->assertEquals($this->msg->toPostArgs(),
  524. array('openid.mode' => 'error',
  525. 'openid.error' => 'unit test',
  526. 'openid.ns' => Auth_OpenID_OPENID2_NS,
  527. 'xey' => 'value'));
  528. }
  529. function test_toArgs()
  530. {
  531. // This method can't tolerate BARE_NS.
  532. $this->msg->delArg(Auth_OpenID_BARE_NS, "xey");
  533. $this->assertEquals($this->msg->toArgs(),
  534. array('mode' => 'error',
  535. 'error' => 'unit test',
  536. 'ns' => Auth_OpenID_OPENID2_NS));
  537. }
  538. function test_toKVForm()
  539. {
  540. // Can't tolerate BARE_NS in kvform
  541. $this->msg->delArg(Auth_OpenID_BARE_NS, "xey");
  542. $this->assertEquals($this->msg->toKVForm(),
  543. sprintf("error:unit test\nmode:error\nns:%s\n",
  544. Auth_OpenID_OPENID2_NS));
  545. }
  546. function _test_urlencoded($s)
  547. {
  548. $expected = 'openid.error=unit+test&openid.mode=error&' .
  549. 'openid.ns=%s&xey=value';
  550. $expected = sprintf($expected, urlencode(Auth_OpenID_OPENID2_NS));
  551. $this->assertEquals($s, $expected);
  552. }
  553. function test_toURLEncoded()
  554. {
  555. $this->_test_urlencoded($this->msg->toURLEncoded());
  556. }
  557. function test_toURL()
  558. {
  559. $base_url = 'http://base.url/';
  560. $actual = $this->msg->toURL($base_url);
  561. $actual_base = substr($actual, 0, strlen($base_url));
  562. $this->assertEquals($actual_base, $base_url);
  563. $this->assertEquals($actual[strlen($base_url)], '?');
  564. $query = substr($actual, strlen($base_url) + 1);
  565. $this->_test_urlencoded($query);
  566. }
  567. function test_getOpenID()
  568. {
  569. $this->assertEquals($this->msg->getOpenIDNamespace(),
  570. Auth_OpenID_OPENID2_NS);
  571. }
  572. function test_getKeyOpenID()
  573. {
  574. $this->assertEquals($this->msg->getKey(Auth_OpenID_OPENID_NS, 'mode'),
  575. 'openid.mode');
  576. }
  577. function test_getKeyBARE()
  578. {
  579. $this->assertEquals($this->msg->getKey(Auth_OpenID_BARE_NS, 'mode'), 'mode');
  580. }
  581. function test_getKeyNS1()
  582. {
  583. $this->assertEquals(
  584. $this->msg->getKey(Auth_OpenID_OPENID1_NS, 'mode'), null);
  585. }
  586. function test_getKeyNS2()
  587. {
  588. $this->assertEquals(
  589. $this->msg->getKey(Auth_OpenID_OPENID2_NS, 'mode'), 'openid.mode');
  590. }
  591. function test_getKeyNS3()
  592. {
  593. $this->assertEquals(
  594. $this->msg->getKey('urn:nothing-significant', 'mode'), null);
  595. }
  596. function test_hasKeyOpenID()
  597. {
  598. $this->assertEquals($this->msg->hasKey(Auth_OpenID_OPENID_NS, 'mode'), true);
  599. }
  600. function test_hasKeyBARE()
  601. {
  602. $this->assertEquals($this->msg->hasKey(Auth_OpenID_BARE_NS, 'mode'), false);
  603. }
  604. function test_hasKeyNS1()
  605. {
  606. $this->assertEquals(
  607. $this->msg->hasKey(Auth_OpenID_OPENID1_NS, 'mode'), false);
  608. }
  609. function test_hasKeyNS2()
  610. {
  611. $this->assertEquals(
  612. $this->msg->hasKey(Auth_OpenID_OPENID2_NS, 'mode'), true);
  613. }
  614. function test_hasKeyNS3()
  615. {
  616. $this->assertEquals(
  617. $this->msg->hasKey('urn:nothing-significant', 'mode'), false);
  618. }
  619. function test_getArgsOpenID()
  620. {
  621. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID_NS),
  622. array('mode' => 'error',
  623. 'error' => 'unit test'));
  624. }
  625. function test_getArgsBARE()
  626. {
  627. $this->assertEquals($this->msg->getArgs(Auth_OpenID_BARE_NS),
  628. array('xey' => 'value'));
  629. }
  630. function test_getArgsNS1()
  631. {
  632. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID1_NS), array());
  633. }
  634. function test_getArgsNS2()
  635. {
  636. $this->assertEquals($this->msg->getArgs(Auth_OpenID_OPENID2_NS),
  637. array('mode' => 'error',
  638. 'error' => 'unit test'));
  639. }
  640. function test_getArgsNS3()
  641. {
  642. $this->assertEquals($this->msg->getArgs('urn:nothing-significant'), array());
  643. }
  644. function _test_updateArgsNS($ns, $before=null)
  645. {
  646. if ($before === null) {
  647. $before = array();
  648. }
  649. $update_args = array(
  650. 'Camper van Beethoven' => 'David Lowery',
  651. 'Magnolia Electric Co.' => 'Jason Molina');
  652. $this->assertEquals($this->msg->getArgs($ns), $before);
  653. $this->msg->updateArgs($ns, $update_args);
  654. $after = $before;
  655. $after = array_merge($after, $update_args);
  656. $this->assertEquals($this->msg->getArgs($ns), $after);
  657. }
  658. function test_updateArgsOpenID()
  659. {
  660. $this->_test_updateArgsNS(Auth_OpenID_OPENID_NS,
  661. array('mode' => 'error', 'error' => 'unit test'));
  662. }
  663. function test_updateArgsBARE()
  664. {
  665. $this->_test_updateArgsNS(Auth_OpenID_BARE_NS,
  666. array('xey' => 'value'));
  667. }
  668. function test_updateArgsNS1()
  669. {
  670. $this->_test_updateArgsNS(Auth_OpenID_OPENID1_NS);
  671. }
  672. function test_updateArgsNS2()
  673. {
  674. $this->_test_updateArgsNS(Auth_OpenID_OPENID2_NS,
  675. array('mode' => 'error', 'error' => 'unit test'));
  676. }
  677. function test_updateArgsNS3()
  678. {
  679. $this->_test_updateArgsNS('urn:nothing-significant');
  680. }
  681. function _test_setArgNS($ns)
  682. {
  683. $key = 'Camper van Beethoven';
  684. $value = 'David Lowery';
  685. $this->assertEquals($this->msg->getArg($ns, $key), null);
  686. $this->msg->setArg($ns, $key, $value);
  687. $this->assertEquals($this->msg->getArg($ns, $key), $value);
  688. }
  689. function test_setArgOpenID()
  690. {
  691. $this->_test_setArgNS(Auth_OpenID_OPENID_NS);
  692. }
  693. function test_setArgBARE()
  694. {
  695. $this->_test_setArgNS(Auth_OpenID_BARE_NS);
  696. }
  697. function test_setArgNS1()
  698. {
  699. $this->_test_setArgNS(Auth_OpenID_OPENID1_NS);
  700. }
  701. function test_setArgNS2()
  702. {
  703. $this->_test_setArgNS(Auth_OpenID_OPENID2_NS);
  704. }
  705. function test_setArgNS3()
  706. {
  707. $this->_test_setArgNS('urn:nothing-significant');
  708. }
  709. function test_badAlias()
  710. {
  711. // Make sure dotted aliases and OpenID protocol fields are not
  712. // allowed as namespace aliases.
  713. global $Auth_OpenID_OPENID_PROTOCOL_FIELDS;
  714. $all = array_merge($Auth_OpenID_OPENID_PROTOCOL_FIELDS, array('dotted.alias'));
  715. foreach ($all as $f) {
  716. $args = array(sprintf('openid.ns.%s', $f) => 'blah',
  717. sprintf('openid.%s.foo', $f) => 'test');
  718. // .fromPostArgs covers .fromPostArgs, .fromOpenIDArgs,
  719. // ._fromOpenIDArgs, and .fromOpenIDArgs (since it calls
  720. // .fromPostArgs). Python code raises AssertionError, but
  721. // we usually return null for bad things in PHP.
  722. $this->assertEquals($this->msg->fromPostArgs($args), null);
  723. }
  724. }
  725. function _test_delArgNS($ns)
  726. {
  727. $key = 'Camper van Beethoven';
  728. $value = 'David Lowery';
  729. $this->assertEquals($this->msg->delArg($ns, $key), false);
  730. $this->msg->setArg($ns, $key, $value);
  731. $this->assertEquals($this->msg->getArg($ns, $key), $value);
  732. $this->msg->delArg($ns, $key);
  733. $this->assertEquals($this->msg->getArg($ns, $key), null);
  734. }
  735. function test_delArgOpenID()
  736. {
  737. $this->_test_delArgNS(Auth_OpenID_OPENID_NS);
  738. }
  739. function test_delArgBARE()
  740. {
  741. $this->_test_delArgNS(Auth_OpenID_BARE_NS);
  742. }
  743. function test_delArgNS1()
  744. {
  745. $this->_test_delArgNS(Auth_OpenID_OPENID1_NS);
  746. }
  747. function test_delArgNS2()
  748. {
  749. $this->_test_delArgNS(Auth_OpenID_OPENID2_NS);
  750. }
  751. function test_delArgNS3()
  752. {
  753. $this->_test_delArgNS('urn:nothing-significant');
  754. }
  755. function test_overwriteExtensionArg()
  756. {
  757. $ns = 'urn:unittest_extension';
  758. $key = 'mykey';
  759. $value_1 = 'value_1';
  760. $value_2 = 'value_2';
  761. $this->msg->setArg($ns, $key, $value_1);
  762. $this->assertTrue($this->msg->getArg($ns, $key) == $value_1);
  763. $this->msg->setArg($ns, $key, $value_2);
  764. $this->assertTrue($this->msg->getArg($ns, $key) == $value_2);
  765. }
  766. function test_argList()
  767. {
  768. $this->assertEquals($this->msg->fromPostArgs(array('arg' => array(1, 2, 3))),
  769. null);
  770. }
  771. function test_isOpenID1()
  772. {
  773. $this->assertFalse($this->msg->isOpenID1());
  774. }
  775. function test_isOpenID2()
  776. {
  777. $this->assertTrue($this->msg->isOpenID2());
  778. }
  779. function test_args()
  780. {
  781. $this->_argTest(Auth_OpenID_BARE_NS, 'mode');
  782. $this->_argTest(Auth_OpenID_OPENID_NS, 'mode', 'error');
  783. $this->_argTest(Auth_OpenID_OPENID1_NS, 'mode');
  784. $this->_argTest(Auth_OpenID_OPENID2_NS, 'mode', 'error');
  785. $this->_argTest('urn:nothing-significant', 'mode');
  786. }
  787. }
  788. class Tests_Auth_OpenID_GeneralMessageTest extends PHPUnit_Framework_TestCase {
  789. function setUp()
  790. {
  791. $this->postargs = array(
  792. 'openid.ns' => Auth_OpenID_OPENID2_NS,
  793. 'openid.mode' => 'checkid_setup',
  794. 'openid.identity' => 'http://bogus.example.invalid:port/',
  795. 'openid.assoc_handle' => 'FLUB',
  796. 'openid.return_to' => 'Neverland');
  797. $this->action_url = 'scheme://host:port/path?query';
  798. $this->form_tag_attrs = array(
  799. 'company' => 'janrain',
  800. 'class' => 'fancyCSS');
  801. $this->submit_text = 'GO!';
  802. // Expected data regardless of input
  803. $this->required_form_attrs = array(
  804. 'accept-charset' => 'UTF-8',
  805. 'enctype' => 'application/x-www-form-urlencoded',
  806. 'method' => 'post');
  807. }
  808. function _checkForm($html, $message_, $action_url,
  809. $form_tag_attrs, $submit_text)
  810. {
  811. $parser = Auth_Yadis_getXMLParser();
  812. // Parse HTML source
  813. $this->assertTrue($parser->init($html, array()));
  814. // Get root element
  815. $form = $parser->evalXPath('/form[1]');
  816. $this->assertTrue(count($form) == 1);
  817. $form = $form[0];
  818. // Check required form attributes
  819. $form_attrs = $parser->attributes($form);
  820. foreach ($this->required_form_attrs as $k => $v) {
  821. $this->assertTrue($form_attrs[$k] == $v);
  822. }
  823. // Check extra form attributes
  824. foreach ($form_tag_attrs as $k => $v) {
  825. // Skip attributes that already passed the required
  826. // attribute check, since they should be ignored by the
  827. // form generation code.
  828. if (in_array($k, array_keys($this->required_form_attrs))) {
  829. continue;
  830. }
  831. $this->assertTrue($form_attrs[$k] == $v,
  832. "Form attr $k is ".$form_attrs[$k]." (expected $v)");
  833. }
  834. // Check hidden fields against post args
  835. $hiddens = array();
  836. $input_elements = $parser->evalXPath('input', $form);
  837. foreach ($input_elements as $e) {
  838. $attrs = $parser->attributes($e);
  839. if (strtoupper($attrs['type']) == 'HIDDEN') {
  840. $hiddens[] = $e;
  841. }
  842. }
  843. // For each post arg, make sure there is a hidden with that
  844. // value. Make sure there are no other hiddens.
  845. $postargs = $message_->toPostArgs();
  846. foreach ($postargs as $name => $value) {
  847. $found = false;
  848. foreach ($hiddens as $e) {
  849. $attrs = $parser->attributes($e);
  850. if ($attrs['name'] == $name) {
  851. $this->assertTrue($attrs['value'] == $value);
  852. $found = true;
  853. break;
  854. }
  855. }
  856. if (!$found) {
  857. $this->fail("Post arg $name not found in form");
  858. }
  859. }
  860. $keys = array_keys($postargs);
  861. foreach ($hiddens as $e) {
  862. $attrs = $parser->attributes($e);
  863. $this->assertTrue(in_array($attrs['name'], $keys));
  864. }
  865. // Check action URL
  866. $this->assertTrue($form_attrs['action'] == $action_url);
  867. // Check submit text
  868. $submits = array();
  869. foreach ($input_elements as $e) {
  870. $attrs = $parser->attributes($e);
  871. if (strtoupper($attrs['type']) == 'SUBMIT') {
  872. $submits[] = $e;
  873. }
  874. }
  875. $this->assertTrue(count($submits) == 1);
  876. $attrs = $parser->attributes($submits[0]);
  877. $this->assertTrue($attrs['value'] == $submit_text);
  878. }
  879. function test_toFormMarkup()
  880. {
  881. $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
  882. $html = $m->toFormMarkup($this->action_url, $this->form_tag_attrs,
  883. $this->submit_text);
  884. $this->_checkForm($html, $m, $this->action_url,
  885. $this->form_tag_attrs, $this->submit_text);
  886. }
  887. function test_overrideMethod()
  888. {
  889. // Be sure that caller cannot change form method to GET.
  890. $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
  891. $tag_attrs = $this->form_tag_attrs;
  892. $tag_attrs['method'] = 'GET';
  893. $html = $m->toFormMarkup($this->action_url, $this->form_tag_attrs,
  894. $this->submit_text);
  895. $this->_checkForm($html, $m, $this->action_url,
  896. $this->form_tag_attrs, $this->submit_text);
  897. }
  898. function test_overrideRequired()
  899. {
  900. // Be sure that caller CANNOT change the form charset for
  901. // encoding type.
  902. $m = Auth_OpenID_Message::fromPostArgs($this->postargs);
  903. $tag_attrs = $this->form_tag_attrs;
  904. $tag_attrs['accept-charset'] = 'UCS4';
  905. $tag_attrs['enctype'] = 'invalid/x-broken';
  906. $html = $m->toFormMarkup($this->action_url, $tag_attrs,
  907. $this->submit_text);
  908. $this->_checkForm($html, $m, $this->action_url,
  909. $tag_attrs, $this->submit_text);
  910. }
  911. function test_setOpenIDNamespace_invalid()
  912. {
  913. $m = new Auth_OpenID_Message();
  914. $invalid_things = array(
  915. // Empty string is not okay here.
  916. '',
  917. // Good guess! But wrong.
  918. 'http://openid.net/signon/2.0',
  919. // What?
  920. 'http://specs%\\\r2Eopenid.net/auth/2.0',
  921. // Too much escapings!
  922. 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0',
  923. // This is a Type URI, not a openid.ns value.
  924. 'http://specs.openid.net/auth/2.0/signon',
  925. );
  926. foreach ($invalid_things as $x) {
  927. $this->assertTrue($m->setOpenIDNamespace($x, true) === false);
  928. }
  929. }
  930. function test_isOpenID1()
  931. {
  932. $v1_namespaces = array(
  933. // Yes, there are two of them.
  934. 'http://openid.net/signon/1.1',
  935. 'http://openid.net/signon/1.0',
  936. );
  937. foreach ($v1_namespaces as $ns) {
  938. $m = new Auth_OpenID_Message($ns);
  939. $this->assertTrue($m->isOpenID1(),
  940. "$ns not recognized as OpenID 1");
  941. $this->assertEquals($ns, $m->getOpenIDNamespace());
  942. $this->assertTrue($m->namespaces->isImplicit($ns));
  943. }
  944. }
  945. function test_isOpenID2()
  946. {
  947. $ns = 'http://specs.openid.net/auth/2.0';
  948. $m = new Auth_OpenID_Message($ns);
  949. $this->assertTrue($m->isOpenID2());
  950. $this->assertFalse(
  951. $m->namespaces->isImplicit(Auth_OpenID_NULL_NAMESPACE));
  952. $this->assertEquals($ns, $m->getOpenIDNamespace());
  953. }
  954. function test_setOpenIDNamespace_explicit()
  955. {
  956. $m = new Auth_OpenID_Message();
  957. $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, false);
  958. $this->assertFalse($m->namespaces->isImplicit(
  959. Auth_OpenID_THE_OTHER_OPENID1_NS));
  960. }
  961. function test_setOpenIDNamespace_implicit()
  962. {
  963. $m = new Auth_OpenID_Message();
  964. $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, true);
  965. $this->assertTrue(
  966. $m->namespaces->isImplicit(Auth_OpenID_THE_OTHER_OPENID1_NS));
  967. }
  968. function test_explicitOpenID11NSSerialzation()
  969. {
  970. $m = new Auth_OpenID_Message();
  971. $m->setOpenIDNamespace(Auth_OpenID_THE_OTHER_OPENID1_NS, false);
  972. $post_args = $m->toPostArgs();
  973. $this->assertEquals($post_args,
  974. array('openid.ns' =>
  975. Auth_OpenID_THE_OTHER_OPENID1_NS));
  976. }
  977. function test_fromPostArgs_ns11()
  978. {
  979. // An example of the stuff that some Drupal installations send us,
  980. // which includes openid.ns but is 1.1.
  981. $query = array(
  982. 'openid.assoc_handle' => '',
  983. 'openid.claimed_id' => 'http://foobar.invalid/',
  984. 'openid.identity' => 'http://foobar.myopenid.com',
  985. 'openid.mode' => 'checkid_setup',
  986. 'openid.ns' => 'http://openid.net/signon/1.1',
  987. 'openid.ns.sreg' => 'http://openid.net/extensions/sreg/1.1',
  988. 'openid.return_to' => 'http://drupal.invalid/return_to',
  989. 'openid.sreg.required' => 'nickname,email',
  990. 'openid.trust_root' => 'http://drupal.invalid',
  991. );
  992. $m = Auth_OpenID_Message::fromPostArgs($query);
  993. $this->assertTrue($m->isOpenID1());
  994. }
  995. }
  996. class Tests_Auth_OpenID_NamespaceMap extends PHPUnit_Framework_TestCase {
  997. function test_onealias()
  998. {
  999. $nsm = new Auth_OpenID_NamespaceMap();
  1000. $uri = 'http://example.com/foo';
  1001. $alias = "foo";
  1002. $nsm->addAlias($uri, $alias);
  1003. $this->assertTrue($nsm->getNamespaceURI($alias) == $uri);
  1004. $this->assertTrue($nsm->getAlias($uri) == $alias);
  1005. }
  1006. function test_iteration()
  1007. {
  1008. $nsm = new Auth_OpenID_NamespaceMap();
  1009. $uripat = 'http://example.com/foo%d';
  1010. $nsm->add(sprintf($uripat, 0));
  1011. for ($n = 1; $n < 23; $n++) {
  1012. $this->assertTrue($nsm->contains(sprintf($uripat, $n - 1)));
  1013. $this->assertTrue($nsm->isDefined(sprintf($uripat, $n - 1)));
  1014. $nsm->add(sprintf($uripat, $n));
  1015. }
  1016. foreach ($nsm->iteritems() as $pair) {
  1017. list($uri, $alias) = $pair;
  1018. $this->assertTrue('ext'.substr($uri, 22) == $alias);
  1019. }
  1020. $it = $nsm->iterAliases();
  1021. $this->assertTrue(count($it) == 23);
  1022. $it = $nsm->iterNamespaceURIs();
  1023. $this->assertTrue(count($it) == 23);
  1024. }
  1025. }
  1026. class Tests_Auth_OpenID_Message extends PHPUnit_Framework_TestCase {
  1027. }
  1028. global $Tests_Auth_OpenID_Message_other;
  1029. $Tests_Auth_OpenID_Message_other = array(
  1030. new Tests_Auth_OpenID_EmptyMessage(),
  1031. new Tests_Auth_OpenID_OpenID1Message(),
  1032. new Tests_Auth_OpenID_OpenID2Message(),
  1033. new Tests_Auth_OpenID_NamespaceMap(),
  1034. new Tests_Auth_OpenID_OpenID1ExplicitMessage(),
  1035. new Tests_Auth_OpenID_GeneralMessageTest()
  1036. );