OpenID_Yadis.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Tests for the combination of Yadis discovery and the OpenID
  4. * protocol.
  5. */
  6. require_once "PHPUnit.php";
  7. require_once "Auth/Yadis/XRDS.php";
  8. require_once "Auth/OpenID/Discover.php";
  9. global $__XRDS_BOILERPLATE;
  10. $__XRDS_BOILERPLATE = '<?xml version="1.0" encoding="UTF-8"?>
  11. <xrds:XRDS xmlns:xrds="xri://$xrds"
  12. xmlns="xri://$xrd*($v*2.0)"
  13. xmlns:openid="http://openid.net/xmlns/1.0">
  14. <XRD>
  15. %s
  16. </XRD>
  17. </xrds:XRDS>
  18. ';
  19. // Different sets of server URLs for use in the URI tag
  20. global $__server_url_options;
  21. $__server_url_options = array(
  22. array(), // This case should not generate an endpoint object
  23. array('http://server.url/'),
  24. array('https://server.url/'),
  25. array('https://server.url/', 'http://server.url/'),
  26. array('https://server.url/',
  27. 'http://server.url/',
  28. 'http://example.server.url/'),
  29. );
  30. // A couple of example extension type URIs. These are not at all
  31. // official, but are just here for testing.
  32. global $__ext_types;
  33. $__ext_types = array(
  34. 'http://janrain.com/extension/blah',
  35. 'http://openid.net/sreg/1.0');
  36. // All valid combinations of Type tags that should produce an OpenID
  37. // endpoint
  38. global $__openid_types;
  39. $__openid_types = array(
  40. Auth_OpenID_TYPE_1_0,
  41. Auth_OpenID_TYPE_1_1);
  42. $temp = array();
  43. foreach (__subsets($__ext_types) as $exts) {
  44. foreach (__subsets($__openid_types) as $ts) {
  45. if ($ts) {
  46. $temp[] = array_merge($exts, $ts);
  47. }
  48. }
  49. }
  50. global $__type_uri_options;
  51. $__type_uri_options = $temp;
  52. // Range of valid Delegate tag values for generating test data
  53. global $__delegate_options;
  54. $__delegate_options = array(
  55. null,
  56. 'http://vanity.domain/',
  57. 'https://somewhere/yadis/');
  58. $temp = array();
  59. foreach ($__delegate_options as $delegate) {
  60. foreach ($__type_uri_options as $type_uris) {
  61. foreach ($__server_url_options as $uris) {
  62. $temp[] = array($uris, $type_uris, $delegate);
  63. }
  64. }
  65. }
  66. // All combinations of valid URIs, Type URIs and Delegate tags
  67. global $__data;
  68. $__data = $temp;
  69. function _mkXRDS($services_str)
  70. {
  71. global $__XRDS_BOILERPLATE;
  72. return sprintf($__XRDS_BOILERPLATE, $services_str);
  73. }
  74. function _mkService($uris = null, $type_uris = null,
  75. $delegate = null, $dent = ' ')
  76. {
  77. $chunks = array($dent, "<Service>\n");
  78. $dent2 = $dent . ' ';
  79. if ($type_uris) {
  80. foreach ($type_uris as $type_uri) {
  81. $chunks = array_merge($chunks,
  82. array($dent2 . '<Type>',
  83. $type_uri, "</Type>\n"));
  84. }
  85. }
  86. if ($uris) {
  87. foreach ($uris as $uri) {
  88. if (is_array($uri)) {
  89. list($uri, $prio) = $uri;
  90. } else {
  91. $prio = null;
  92. }
  93. $chunks = array_merge($chunks, array($dent2, '<URI'));
  94. if ($prio !== null) {
  95. $chunks = array_merge($chunks, array(' priority="', strval($prio), '"'));
  96. }
  97. $chunks = array_merge($chunks, array('>', $uri, "</URI>\n"));
  98. }
  99. }
  100. if ($delegate) {
  101. $chunks = array_merge($chunks,
  102. array($dent2, '<openid:Delegate>',
  103. $delegate, "</openid:Delegate>\n"));
  104. }
  105. $chunks = array_merge($chunks, array($dent, "</Service>\n"));
  106. return implode("", $chunks);
  107. }
  108. // Used for generating test data
  109. function __subsets($list)
  110. {
  111. // Generate all non-empty sublists of a list
  112. $subsets_list = array(array());
  113. foreach ($list as $elem) {
  114. $temp = array();
  115. foreach ($subsets_list as $t) {
  116. $temp[] = array_merge(array($elem), $t);
  117. }
  118. $subsets_list = array_merge($subsets_list, $temp);
  119. }
  120. return $subsets_list;
  121. }
  122. class Tests_Auth_OpenID_Tester extends PHPUnit_TestCase {
  123. function Tests_Auth_OpenID_Tester($uris, $type_uris, $delegate)
  124. {
  125. $this->uris = $uris;
  126. $this->type_uris = $type_uris;
  127. $this->local_id = $delegate;
  128. parent::PHPUnit_TestCase();
  129. }
  130. function setUp()
  131. {
  132. $this->yadis_url = 'http://unit.test/';
  133. // Create an XRDS document to parse
  134. $services = _mkService($this->uris,
  135. $this->type_uris,
  136. $this->local_id);
  137. $this->xrds = _mkXRDS($services);
  138. }
  139. function runTest()
  140. {
  141. // Parse into endpoint objects that we will check
  142. $xrds_object = Auth_Yadis_XRDS::parseXRDS($this->xrds);
  143. $endpoints = array();
  144. if ($xrds_object) {
  145. $endpoints = $xrds_object->services(array('filter_MatchesAnyOpenIDType'));
  146. $endpoints = Auth_OpenID_makeOpenIDEndpoints($this->yadis_url, $endpoints);
  147. }
  148. // make sure there are the same number of endpoints as
  149. // URIs. This assumes that the type_uris contains at least one
  150. // OpenID type.
  151. $this->assertEquals(count($this->uris), count($endpoints),
  152. "URI <-> Endpoint count");
  153. // So that we can check equality on the endpoint types
  154. $type_uris = $this->type_uris;
  155. sort($type_uris);
  156. $seen_uris = array();
  157. foreach ($endpoints as $endpoint) {
  158. $seen_uris[] = $endpoint->server_url;
  159. // All endpoints will have same yadis_url
  160. $this->assertEquals($this->yadis_url, $endpoint->claimed_id);
  161. // and delegate
  162. $this->assertEquals($this->local_id, $endpoint->local_id);
  163. // and types
  164. $actual_types = $endpoint->type_uris;
  165. sort($actual_types);
  166. $this->assertEquals($actual_types, $type_uris);
  167. }
  168. // So that they will compare equal, because we don't care what
  169. // order they are in
  170. sort($seen_uris);
  171. $uris = $this->uris;
  172. sort($uris);
  173. // Make sure we saw all URIs, and saw each one once
  174. $this->assertEquals($uris, $seen_uris);
  175. }
  176. }
  177. class Tests_Auth_OpenID_OpenID_Yadis extends PHPUnit_TestSuite {
  178. function Tests_Auth_OpenID_OpenID_Yadis()
  179. {
  180. global $__data;
  181. foreach ($__data as $case) {
  182. $this->addTest(new Tests_Auth_OpenID_Tester($case[0], $case[1], $case[2]));
  183. }
  184. }
  185. function getName()
  186. {
  187. return 'Tests_Auth_OpenID_OpenID_Yadis';
  188. }
  189. }
  190. ?>