OpenID_Yadis.php 6.5 KB

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