XRI.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * XRI resolution / handling tests.
  4. *
  5. * @package OpenID
  6. * @author JanRain, Inc. <openid@janrain.com>
  7. * @copyright 2005-2008 Janrain, Inc.
  8. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  9. */
  10. require_once "Auth/Yadis/XRIRes.php";
  11. require_once "Auth/Yadis/XRI.php";
  12. require_once "Auth/Yadis/Yadis.php";
  13. class Tests_Auth_Yadis_XriDiscoveryTestCase extends PHPUnit_Framework_TestCase {
  14. function runTest()
  15. {
  16. $this->assertEquals(
  17. Auth_Yadis_identifierScheme('=john.smith'), 'XRI');
  18. $this->assertEquals(
  19. Auth_Yadis_identifierScheme(''), 'URI');
  20. $this->assertEquals(
  21. Auth_Yadis_identifierScheme('@smiths/john'), 'XRI');
  22. $this->assertEquals(
  23. Auth_Yadis_identifierScheme('smoker.myopenid.com'), 'URI');
  24. $this->assertEquals(
  25. Auth_Yadis_identifierScheme('xri://=john'), 'XRI');
  26. }
  27. }
  28. class Tests_Auth_Yadis_XriEscapingTestCase extends PHPUnit_Framework_TestCase {
  29. function test_escaping_percents()
  30. {
  31. $this->assertEquals(Auth_Yadis_escapeForIRI('@example/abc%2Fd/ef'),
  32. '@example/abc%252Fd/ef');
  33. }
  34. function runTest()
  35. {
  36. // no escapes
  37. $this->assertEquals('@example/foo/(@bar)',
  38. Auth_Yadis_escapeForIRI('@example/foo/(@bar)'));
  39. // escape slashes
  40. $this->assertEquals('@example/foo/(@bar%2Fbaz)',
  41. Auth_Yadis_escapeForIRI('@example/foo/(@bar/baz)'));
  42. $this->assertEquals('@example/foo/(@bar%2Fbaz)/(+a%2Fb)',
  43. Auth_Yadis_escapeForIRI('@example/foo/(@bar/baz)/(+a/b)'));
  44. // escape query ? and fragment
  45. $this->assertEquals('@example/foo/(@baz%3Fp=q%23r)?i=j#k',
  46. Auth_Yadis_escapeForIRI('@example/foo/(@baz?p=q#r)?i=j#k'));
  47. }
  48. }
  49. class Tests_Auth_Yadis_ProxyQueryTestCase extends PHPUnit_Framework_TestCase {
  50. function setUp()
  51. {
  52. $this->proxy_url = 'http://xri.example.com/';
  53. $this->fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  54. $this->proxy = new Auth_Yadis_ProxyResolver($this->fetcher,
  55. $this->proxy_url);
  56. $this->servicetype = 'xri://+i-service*(+forwarding)*($v*1.0)';
  57. $this->servicetype_enc = 'xri%3A%2F%2F%2Bi-service%2A%28%2Bforwarding%29%2A%28%24v%2A1.0%29';
  58. }
  59. function runTest()
  60. {
  61. $st = $this->servicetype;
  62. $ste = $this->servicetype_enc;
  63. $args_esc = "_xrd_r=application%2Fxrds%2Bxml&_xrd_t=" . $ste;
  64. $h = $this->proxy_url;
  65. $this->assertEquals($h . '=foo?' . $args_esc,
  66. $this->proxy->queryURL('=foo', $st));
  67. $this->assertEquals($h . '=foo/bar?baz&' . $args_esc,
  68. $this->proxy->queryURL('=foo/bar?baz', $st));
  69. $this->assertEquals($h . '=foo/bar?baz=quux&' . $args_esc,
  70. $this->proxy->queryURL('=foo/bar?baz=quux', $st));
  71. $this->assertEquals($h . '=foo/bar?mi=fa&so=la&' . $args_esc,
  72. $this->proxy->queryURL('=foo/bar?mi=fa&so=la', $st));
  73. $args_esc = "_xrd_r=application%2Fxrds%2Bxml&_xrd_t=" . $ste;
  74. $h = $this->proxy_url;
  75. $this->assertEquals($h . '=foo/bar??' . $args_esc,
  76. $this->proxy->queryURL('=foo/bar?', $st));
  77. $this->assertEquals($h . '=foo/bar????' . $args_esc,
  78. $this->proxy->queryURL('=foo/bar???', $st));
  79. }
  80. }
  81. class Tests_Auth_Yadis_TestGetRootAuthority extends PHPUnit_Framework_TestCase {
  82. function runTest()
  83. {
  84. $xris = array(
  85. array("@foo", "@"),
  86. array("@foo*bar", "@"),
  87. array("@*foo*bar", "@"),
  88. array("@foo/bar", "@"),
  89. array("!!990!991", "!"),
  90. array("!1001!02", "!"),
  91. array("=foo*bar", "="),
  92. array("(example.com)/foo", "(example.com)"),
  93. array("(example.com)*bar/foo", "(example.com)"),
  94. array("baz.example.com/foo", "baz.example.com"),
  95. array("baz.example.com:8080/foo", "baz.example.com:8080")
  96. // Looking at the ABNF in XRI Syntax 2.0, I don't think you can
  97. // have example.com*bar. You can do (example.com)*bar, but that
  98. // would mean something else.
  99. // ("example.com*bar/(=baz)", "example.com*bar"),
  100. // ("baz.example.com!01/foo", "baz.example.com!01"),
  101. );
  102. foreach ($xris as $tupl) {
  103. list($thexri, $expected_root) = $tupl;
  104. $this->assertEquals(Auth_Yadis_XRI($expected_root),
  105. Auth_Yadis_rootAuthority($thexri),
  106. 'rootAuthority test ('.$thexri.')');
  107. }
  108. }
  109. }
  110. class Tests_Auth_Yadis_XRI extends PHPUnit_Framework_TestSuite {
  111. function getName()
  112. {
  113. return "Tests_Auth_Yadis_XRI";
  114. }
  115. function Tests_Auth_Yadis_XRI()
  116. {
  117. $this->addTest(new Tests_Auth_Yadis_ProxyQueryTestCase());
  118. $this->addTest(new Tests_Auth_Yadis_XriEscapingTestCase());
  119. $this->addTest(new Tests_Auth_Yadis_XriDiscoveryTestCase());
  120. $this->addTest(new Tests_Auth_Yadis_TestGetRootAuthority());
  121. }
  122. }