Util.php 12 KB

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