XRDS.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * XRDS-parsing tests for the Yadis library.
  4. */
  5. require_once 'Auth/Yadis/XRDS.php';
  6. require_once 'Auth/Yadis/XRIRes.php';
  7. require_once 'Auth/Yadis/XRI.php';
  8. require_once 'Tests/Auth/Yadis/TestUtil.php';
  9. class Tests_Auth_Yadis_XRDS extends PHPUnit_Framework_TestCase {
  10. function test_good()
  11. {
  12. $files = array(
  13. 'brian.xrds' => 1,
  14. 'pip.xrds' => 2
  15. );
  16. foreach ($files as $filename => $service_count) {
  17. $xml = Tests_Auth_Yadis_readdata($filename);
  18. $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
  19. $this->assertTrue($xrds !== null);
  20. if ($xrds) {
  21. $this->assertEquals(count($xrds->services()), $service_count);
  22. } else {
  23. $this->fail("Could not test XRDS service list because the ".
  24. "XRDS object is null");
  25. }
  26. }
  27. }
  28. function test_good_multi()
  29. {
  30. $xml = Tests_Auth_Yadis_readdata("brian.multi.xrds");
  31. $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
  32. $this->assertTrue($xrds !== null);
  33. $this->assertEquals(count($xrds->services()), 1);
  34. $s = $xrds->services();
  35. $s = $s[0];
  36. $types = $s->getTypes();
  37. $this->assertTrue(count($types) == 1);
  38. $this->assertEquals('http://openid.net/signon/1.0',
  39. $types[0]);
  40. }
  41. function test_good_uri_multi()
  42. {
  43. $xml = Tests_Auth_Yadis_readdata("brian.multi_uri.xrds");
  44. $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
  45. $this->assertTrue($xrds !== null);
  46. $this->assertEquals(1, count($xrds->services()));
  47. }
  48. function test_uri_sorting()
  49. {
  50. $xml = Tests_Auth_Yadis_readdata("uri_priority.xrds");
  51. $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
  52. $services = $xrds->services();
  53. $uris = $services[0]->getURIs();
  54. $expected_uris = array(
  55. "http://zero.priority/",
  56. "http://one.priority/",
  57. "http://no.priority/"
  58. );
  59. $this->assertEquals($uris, $expected_uris);
  60. }
  61. function test_bad()
  62. {
  63. $this->assertTrue(Auth_Yadis_XRDS::parseXRDS(null) === null);
  64. $this->assertTrue(Auth_Yadis_XRDS::parseXRDS(5) === null);
  65. $this->assertTrue(Auth_Yadis_XRDS::parseXRDS('') === null);
  66. $this->assertTrue(Auth_Yadis_XRDS::parseXRDS('<html></html>') ===
  67. null);
  68. $this->assertTrue(Auth_Yadis_XRDS::parseXRDS("\x00") === null);
  69. }
  70. function test_getCanonicalID()
  71. {
  72. $canonicalIDtests = array(
  73. array("@ootao*test1", "delegated-20060809.xrds",
  74. "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
  75. array("@ootao*test1", "delegated-20060809-r1.xrds",
  76. "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
  77. array("@ootao*test1", "delegated-20060809-r2.xrds",
  78. "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
  79. array("@ootao*test1", "sometimesprefix.xrds",
  80. "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
  81. array("@ootao*test1", "prefixsometimes.xrds",
  82. "@!5BAD.2AA.3C72.AF46!0000.0000.3B9A.CA01"),
  83. array("=keturn*isDrummond", "spoof1.xrds", null),
  84. array("=keturn*isDrummond", "spoof2.xrds", null),
  85. array("@keturn*is*drummond", "spoof3.xrds", null),
  86. // Don't let IRI authorities be canonical for the GCS.
  87. array("phreak.example.com", "delegated-20060809-r2.xrds", null)
  88. // TODO: Refs
  89. // ("@ootao*test.ref", "ref.xrds", "@!BAE.A650.823B.2475")
  90. );
  91. foreach ($canonicalIDtests as $tupl) {
  92. list($iname, $filename, $expectedID) = $tupl;
  93. $xml = Tests_Auth_Yadis_readdata($filename);
  94. $xrds = Auth_Yadis_XRDS::parseXRDS($xml);
  95. $this->_getCanonicalID($iname, $xrds, $expectedID);
  96. }
  97. }
  98. function _getCanonicalID($iname, $xrds, $expectedID)
  99. {
  100. if ($expectedID === null) {
  101. $result = Auth_Yadis_getCanonicalID($iname, $xrds);
  102. if ($result !== false) {
  103. $this->fail($iname.' (got '.$result.')');
  104. }
  105. } else {
  106. $cid = Auth_Yadis_getCanonicalID($iname, $xrds);
  107. $this->assertEquals(Auth_Yadis_XRI($expectedID), $cid);
  108. }
  109. }
  110. function test_services_filters()
  111. {
  112. // First, just be sure that service objects do the right
  113. // thing.
  114. $xml = Tests_Auth_Yadis_readdata("brian_priority.xrds");
  115. $xrds = Auth_Yadis_XRDS::parseXRDS($xml,
  116. array('openid' =>
  117. 'http://openid.net/xmlns/1.0'));
  118. $this->assertTrue($xrds !== null);
  119. // Get list of service objects.
  120. $services = $xrds->services();
  121. $this->assertEquals(count($services), 2, "first service count");
  122. // Query the two service objecs.
  123. $s1 = $services[0];
  124. $this->assertEquals($s1->getPriority(), 1, "first priority check");
  125. $types = $s1->getTypes();
  126. $this->assertEquals(count($types), 1, "first type check");
  127. $s2 = $services[1];
  128. $this->assertEquals($s2->getPriority(), 2, "second priority check");
  129. $types = $s2->getTypes();
  130. $this->assertEquals(count($types), 1, "second type check");
  131. function _DelegateFilter($service)
  132. {
  133. if ($service->getElements('openid:Delegate')) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. // Make sure that a filter which matches both DOES match both.
  139. $this->assertEquals(count(
  140. $xrds->services(array("_DelegateFilter"))), 2,
  141. "_DelegateFilter check");
  142. // This filter should match all services in the document.
  143. function _HasTypeAndURI($service)
  144. {
  145. if ($service->getTypes() &&
  146. $service->getURIs()) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. // This filter should only match one.
  152. function _URIMatchesSchtuff($service)
  153. {
  154. $uris = $service->getURIs();
  155. foreach ($uris as $uri) {
  156. if (preg_match("|schtuff|", $uri)) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. // This filter should only match one.
  163. function _URIMatchesMyOpenID($service)
  164. {
  165. $uris = $service->getURIs();
  166. foreach ($uris as $uri) {
  167. if (preg_match("|myopenid|", $uri)) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. // Make sure a pair of filters in ALL mode only match one service.
  174. $this->assertEquals(count(
  175. $xrds->services(array("_HasTypeAndURI",
  176. "_URIMatchesSchtuff"),
  177. SERVICES_YADIS_MATCH_ALL)), 1,
  178. "_HasTypeAndURI / _URIMatchesSchtuff check");
  179. // Make sure a pair of filters in ALL mode only match one service.
  180. $this->assertEquals(count(
  181. $xrds->services(array("_HasTypeAndURI",
  182. "_URIMatchesMyOpenID"),
  183. SERVICES_YADIS_MATCH_ALL)), 1,
  184. "_HasTypeAndURI / _URIMatchesMyOpenID check");
  185. // Make sure a pair of filters in ANY mode matches both services.
  186. $this->assertEquals(count(
  187. $xrds->services(array("_URIMatchesMyOpenID",
  188. "_URIMatchesSchtuff"))), 2,
  189. "_URIMatchesMyOpenID / _URIMatchesSchtuff check");
  190. // Make sure the order of the services returned (when using
  191. // filters) is correct.
  192. $s = $xrds->services(array("_URIMatchesMyOpenID",
  193. "_URIMatchesSchtuff"));
  194. $this->assertTrue($s[0]->getPriority() === 1, "s[0] priority check");
  195. $this->assertTrue($s[1]->getPriority() === 2, "s[1] priority check");
  196. // Make sure a bad filter mode gets us a null service list.
  197. $this->assertTrue($xrds->services(array("_URIMatchesMyOpenID",
  198. "_URIMatchesSchtuff"),
  199. "bogus") === null,
  200. "bogus filter check");
  201. }
  202. function test_multisegment_xri()
  203. {
  204. $xml = Tests_Auth_Yadis_readdata('subsegments.xrds');
  205. $xmldoc = Auth_Yadis_XRDS::parseXRDS($xml);
  206. $result = Auth_Yadis_getCanonicalId('xri://=nishitani*masaki', $xmldoc);
  207. $this->assertEquals($result, "xri://=!E117.EF2F.454B.C707!0000.0000.3B9A.CA01");
  208. }
  209. }