Util.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * Tests for utility functions used by the OpenID library.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. require_once 'PHPUnit.php';
  15. require_once 'Auth/OpenID.php';
  16. class Tests_Auth_OpenID_Util extends PHPUnit_TestCase {
  17. function test_base64()
  18. {
  19. // This is not good for international use, but PHP doesn't
  20. // appear to provide access to the local alphabet.
  21. $letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22. $digits = "0123456789";
  23. $extra = "+/=";
  24. $allowed_s = $letters . $digits . $extra;
  25. $allowed_d = array();
  26. for ($i = 0; $i < strlen($allowed_s); $i++) {
  27. $c = $allowed_s[$i];
  28. $allowed_d[$c] = null;
  29. }
  30. function checkEncoded($obj, $str, $allowed_array)
  31. {
  32. for ($i = 0; $i < strlen($str); $i++) {
  33. $obj->assertTrue(array_key_exists($str[$i],
  34. $allowed_array));
  35. }
  36. }
  37. $cases = array(
  38. "",
  39. "x",
  40. "\x00",
  41. "\x01",
  42. str_repeat("\x00", 100),
  43. implode("", array_map('chr', range(0, 255)))
  44. );
  45. foreach ($cases as $s) {
  46. $b64 = base64_encode($s);
  47. checkEncoded($this, $b64, $allowed_d);
  48. $s_prime = base64_decode($b64);
  49. $this->assertEquals($s_prime, $s);
  50. }
  51. function random_ordinal($unused)
  52. {
  53. return rand(0, 255);
  54. }
  55. // Randomized test
  56. foreach (range(0, 49) as $i) {
  57. $n = rand(0, 2048);
  58. $s = implode("", array_map('chr',
  59. array_map('random_ordinal',
  60. range(0, $n))));
  61. $b64 = base64_encode($s);
  62. checkEncoded($this, $b64, $allowed_d);
  63. $s_prime = base64_decode($b64);
  64. $this->assertEquals($s_prime, $s);
  65. }
  66. }
  67. function test_urldefrag()
  68. {
  69. $cases = array(
  70. array('http://foo.com', 'http://foo.com'),
  71. array('http://foo.com/', 'http://foo.com/'),
  72. array('http://foo.com/path', 'http://foo.com/path'),
  73. array('http://foo.com/path?query', 'http://foo.com/path?query'),
  74. array('http://foo.com/path?query=v', 'http://foo.com/path?query=v'),
  75. array('http://foo.com/?query=v', 'http://foo.com/?query=v'),
  76. );
  77. foreach ($cases as $pair) {
  78. list($orig, $after) = $pair;
  79. list($base, $frag) = Auth_OpenID::urldefrag($orig);
  80. $this->assertEquals($after, $base);
  81. $this->assertEquals($frag, '');
  82. list($base, $frag) = Auth_OpenID::urldefrag($orig . "#fragment");
  83. $this->assertEquals($after, $base);
  84. $this->assertEquals('fragment', $frag);
  85. }
  86. }
  87. function test_normalizeUrl()
  88. {
  89. $this->assertEquals("http://foo.com/",
  90. Auth_OpenID::normalizeUrl("foo.com"));
  91. $this->assertEquals("http://foo.com/",
  92. Auth_OpenID::normalizeUrl("http://foo.com"));
  93. $this->assertEquals("https://foo.com/",
  94. Auth_OpenID::normalizeUrl("https://foo.com"));
  95. $this->assertEquals("http://foo.com/bar",
  96. Auth_OpenID::normalizeUrl("foo.com/bar"));
  97. $this->assertEquals("http://foo.com/bar",
  98. Auth_OpenID::normalizeUrl("http://foo.com/bar"));
  99. $this->assertEquals("http://foo.com/",
  100. Auth_OpenID::normalizeUrl("http://foo.com/"));
  101. $this->assertEquals("https://foo.com/",
  102. Auth_OpenID::normalizeUrl("https://foo.com/"));
  103. $this->assertEquals("https://foo.com/bar" ,
  104. Auth_OpenID::normalizeUrl("https://foo.com/bar"));
  105. $this->assertEquals("http://foo.com/bar" ,
  106. Auth_OpenID::normalizeUrl("HTtp://foo.com/bar"));
  107. $this->assertEquals("http://foo.com/bar" ,
  108. Auth_OpenID::normalizeUrl("HTtp://foo.com/bar#fraggle"));
  109. $this->assertEquals("http://foo.com/bAr/" ,
  110. Auth_OpenID::normalizeUrl("HTtp://fOo.com/bAr/.#fraggle"));
  111. if (0) {
  112. $this->assertEquals("http://foo.com/%E8%8D%89",
  113. Auth_OpenID::normalizeUrl("foo.com/\u8349"));
  114. $this->assertEquals("http://foo.com/%E8%8D%89",
  115. Auth_OpenID::normalizeUrl("http://foo.com/\u8349"));
  116. }
  117. $non_ascii_domain_cases = array(
  118. array("http://xn--vl1a.com/",
  119. "\u8349.com"),
  120. array("http://xn--vl1a.com/",
  121. "http://\u8349.com"),
  122. array("http://xn--vl1a.com/",
  123. "\u8349.com/"),
  124. array("http://xn--vl1a.com/",
  125. "http://\u8349.com/"),
  126. array("http://xn--vl1a.com/%E8%8D%89",
  127. "\u8349.com/\u8349"),
  128. array("http://xn--vl1a.com/%E8%8D%89",
  129. "http://\u8349.com/\u8349"),
  130. );
  131. // XXX
  132. /*
  133. codecs.getencoder('idna')
  134. except LookupError:
  135. # If there is no idna codec, these cases with
  136. # non-ascii-representable domain names should fail.
  137. should_raise = True
  138. else:
  139. should_raise = False
  140. for expected, case in non_ascii_domain_cases:
  141. try:
  142. actual = Auth_OpenID::normalizeUrl(case)
  143. except UnicodeError:
  144. assert should_raise
  145. else:
  146. assert not should_raise and actual == expected, case
  147. */
  148. $this->assertNull(Auth_OpenID::normalizeUrl(null));
  149. $this->assertNull(Auth_OpenID::normalizeUrl(''));
  150. $this->assertNull(Auth_OpenID::normalizeUrl('http://'));
  151. }
  152. function test_appendArgs()
  153. {
  154. $simple = 'http://www.example.com/';
  155. $cases = array(
  156. array('empty list',
  157. array($simple, array()),
  158. $simple),
  159. array('empty dict',
  160. array($simple, array()),
  161. $simple),
  162. array('one list',
  163. array($simple, array(array('a', 'b'))),
  164. $simple . '?a=b'),
  165. array('one dict',
  166. array($simple, array('a' => 'b')),
  167. $simple . '?a=b'),
  168. array('two list (same)',
  169. array($simple, array(array('a', 'b'),
  170. array('a', 'c'))),
  171. $simple . '?a=b&a=c'),
  172. array('two list',
  173. array($simple, array(array('a', 'b'),
  174. array('b', 'c'))),
  175. $simple . '?a=b&b=c'),
  176. array('two list (order)',
  177. array($simple, array(array('b', 'c'),
  178. array('a', 'b'))),
  179. $simple . '?b=c&a=b'),
  180. array('two dict (order)',
  181. array($simple, array('b' => 'c',
  182. 'a' => 'b')),
  183. $simple . '?a=b&b=c'),
  184. array('escape',
  185. array($simple, array(array('=', '='))),
  186. $simple . '?%3D=%3D'),
  187. array('escape (URL)',
  188. array($simple, array(array('this_url',
  189. $simple))),
  190. $simple .
  191. '?this_url=http%3A%2F%2Fwww.example.com%2F'),
  192. array('use dots',
  193. array($simple, array(array('openid.stuff',
  194. 'bother'))),
  195. $simple . '?openid.stuff=bother'),
  196. array('args exist (empty)',
  197. array($simple . '?stuff=bother', array()),
  198. $simple . '?stuff=bother'),
  199. array('args exist',
  200. array($simple . '?stuff=bother',
  201. array(array('ack', 'ack'))),
  202. $simple . '?stuff=bother&ack=ack'),
  203. array('args exist',
  204. array($simple . '?stuff=bother',
  205. array(array('ack', 'ack'))),
  206. $simple . '?stuff=bother&ack=ack'),
  207. array('args exist (dict)',
  208. array($simple . '?stuff=bother',
  209. array('ack' => 'ack')),
  210. $simple . '?stuff=bother&ack=ack'),
  211. array('args exist (dict 2)',
  212. array($simple . '?stuff=bother',
  213. array('ack' => 'ack', 'zebra' => 'lion')),
  214. $simple . '?stuff=bother&ack=ack&zebra=lion'),
  215. array('three args (dict)',
  216. array($simple, array('stuff' => 'bother',
  217. 'ack' => 'ack',
  218. 'zebra' => 'lion')),
  219. $simple . '?ack=ack&stuff=bother&zebra=lion'),
  220. array('three args (list)',
  221. array($simple, array(
  222. array('stuff', 'bother'),
  223. array('ack', 'ack'),
  224. array('zebra', 'lion'))),
  225. $simple . '?stuff=bother&ack=ack&zebra=lion'),
  226. );
  227. // Tests.
  228. foreach ($cases as $case) {
  229. list($desc, $data, $expected) = $case;
  230. list($url, $query) = $data;
  231. $this->assertEquals($expected,
  232. Auth_OpenID::appendArgs($url, $query));
  233. }
  234. }
  235. function test_getQuery()
  236. {
  237. $queries = array(
  238. '' => array(),
  239. 'single' => array(),
  240. 'no&pairs' => array(),
  241. 'x%3Dy' => array(),
  242. 'single&real=value' => array('real' => 'value'),
  243. 'x=y&m=x%3Dn' => array('x' => 'y', 'm' => 'x=n'),
  244. '&m=x%20y' => array('m' => 'x y'),
  245. 'single&&m=x%20y&bogus' => array('m' => 'x y'),
  246. // Even with invalid encoding. But don't do that.
  247. 'too=many=equals&' => array('too' => 'many=equals')
  248. );
  249. foreach ($queries as $s => $data) {
  250. $query = Auth_OpenID::getQuery($s);
  251. foreach ($data as $key => $value) {
  252. $this->assertTrue($query[$key] === $value);
  253. }
  254. foreach ($query as $key => $value) {
  255. $this->assertTrue($data[$key] === $value);
  256. }
  257. }
  258. }
  259. }
  260. ?>