123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- /**
- * Tests for utility functions used by the OpenID library.
- *
- * PHP versions 4 and 5
- *
- * LICENSE: See the COPYING file included in this distribution.
- *
- * @package OpenID
- * @author JanRain, Inc. <openid@janrain.com>
- * @copyright 2005-2008 Janrain, Inc.
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
- */
- require_once 'Auth/OpenID.php';
- class Tests_Auth_OpenID_Util extends PHPUnit_Framework_TestCase {
- function test_base64()
- {
- // This is not good for international use, but PHP doesn't
- // appear to provide access to the local alphabet.
- $letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $digits = "0123456789";
- $extra = "+/=";
- $allowed_s = $letters . $digits . $extra;
- $allowed_d = array();
- for ($i = 0; $i < strlen($allowed_s); $i++) {
- $c = $allowed_s[$i];
- $allowed_d[$c] = null;
- }
- function checkEncoded($obj, $str, $allowed_array)
- {
- for ($i = 0; $i < strlen($str); $i++) {
- $obj->assertTrue(array_key_exists($str[$i],
- $allowed_array));
- }
- }
- $cases = array(
- "",
- "x",
- "\x00",
- "\x01",
- str_repeat("\x00", 100),
- implode("", array_map('chr', range(0, 255)))
- );
- foreach ($cases as $s) {
- $b64 = base64_encode($s);
- checkEncoded($this, $b64, $allowed_d);
- $s_prime = base64_decode($b64);
- $this->assertEquals($s_prime, $s);
- }
- function random_ordinal($unused)
- {
- return rand(0, 255);
- }
- // Randomized test
- foreach (range(0, 49) as $i) {
- $n = rand(0, 2048);
- $s = implode("", array_map('chr',
- array_map('random_ordinal',
- range(0, $n))));
- $b64 = base64_encode($s);
- checkEncoded($this, $b64, $allowed_d);
- $s_prime = base64_decode($b64);
- $this->assertEquals($s_prime, $s);
- }
- }
- function test_urldefrag()
- {
- $cases = array(
- array('http://foo.com', 'http://foo.com'),
- array('http://foo.com/', 'http://foo.com/'),
- array('http://foo.com/path', 'http://foo.com/path'),
- array('http://foo.com/path?query', 'http://foo.com/path?query'),
- array('http://foo.com/path?query=v', 'http://foo.com/path?query=v'),
- array('http://foo.com/?query=v', 'http://foo.com/?query=v'),
- );
- foreach ($cases as $pair) {
- list($orig, $after) = $pair;
- list($base, $frag) = Auth_OpenID::urldefrag($orig);
- $this->assertEquals($after, $base);
- $this->assertEquals($frag, '');
- list($base, $frag) = Auth_OpenID::urldefrag($orig . "#fragment");
- $this->assertEquals($after, $base);
- $this->assertEquals('fragment', $frag);
- }
- }
- function test_normalizeUrl()
- {
- $this->assertEquals("http://foo.com/",
- Auth_OpenID::normalizeUrl("foo.com"));
- $this->assertEquals("http://foo.com/",
- Auth_OpenID::normalizeUrl("http://foo.com"));
- $this->assertEquals("https://foo.com/",
- Auth_OpenID::normalizeUrl("https://foo.com"));
- $this->assertEquals("http://foo.com/bar",
- Auth_OpenID::normalizeUrl("foo.com/bar"));
- $this->assertEquals("http://foo.com/bar",
- Auth_OpenID::normalizeUrl("http://foo.com/bar"));
- $this->assertEquals("http://foo.com/",
- Auth_OpenID::normalizeUrl("http://foo.com/"));
- $this->assertEquals("https://foo.com/",
- Auth_OpenID::normalizeUrl("https://foo.com/"));
- $this->assertEquals("https://foo.com/bar" ,
- Auth_OpenID::normalizeUrl("https://foo.com/bar"));
- $this->assertEquals("http://foo.com/bar" ,
- Auth_OpenID::normalizeUrl("HTtp://foo.com/bar"));
- $this->assertEquals("http://foo.com/bar" ,
- Auth_OpenID::normalizeUrl("HTtp://foo.com/bar#fraggle"));
- $this->assertEquals("http://foo.com/bAr/" ,
- Auth_OpenID::normalizeUrl("HTtp://fOo.com/bAr/.#fraggle"));
- if (0) {
- $this->assertEquals("http://foo.com/%E8%8D%89",
- Auth_OpenID::normalizeUrl("foo.com/\u8349"));
- $this->assertEquals("http://foo.com/%E8%8D%89",
- Auth_OpenID::normalizeUrl("http://foo.com/\u8349"));
- }
- $non_ascii_domain_cases = array(
- array("http://xn--vl1a.com/",
- "\u8349.com"),
- array("http://xn--vl1a.com/",
- "http://\u8349.com"),
- array("http://xn--vl1a.com/",
- "\u8349.com/"),
- array("http://xn--vl1a.com/",
- "http://\u8349.com/"),
- array("http://xn--vl1a.com/%E8%8D%89",
- "\u8349.com/\u8349"),
- array("http://xn--vl1a.com/%E8%8D%89",
- "http://\u8349.com/\u8349"),
- );
- // XXX
- /*
- codecs.getencoder('idna')
- except LookupError:
- # If there is no idna codec, these cases with
- # non-ascii-representable domain names should fail.
- should_raise = True
- else:
- should_raise = False
- for expected, case in non_ascii_domain_cases:
- try:
- actual = Auth_OpenID::normalizeUrl(case)
- except UnicodeError:
- assert should_raise
- else:
- assert not should_raise and actual == expected, case
- */
- $this->assertNull(Auth_OpenID::normalizeUrl(null));
- $this->assertNull(Auth_OpenID::normalizeUrl(''));
- $this->assertNull(Auth_OpenID::normalizeUrl('http://'));
- }
- function test_appendArgs()
- {
- $simple = 'http://www.example.com/';
- $cases = array(
- array('empty list',
- array($simple, array()),
- $simple),
- array('empty dict',
- array($simple, array()),
- $simple),
- array('one list',
- array($simple, array(array('a', 'b'))),
- $simple . '?a=b'),
- array('one dict',
- array($simple, array('a' => 'b')),
- $simple . '?a=b'),
- array('two list (same)',
- array($simple, array(array('a', 'b'),
- array('a', 'c'))),
- $simple . '?a=b&a=c'),
- array('two list',
- array($simple, array(array('a', 'b'),
- array('b', 'c'))),
- $simple . '?a=b&b=c'),
- array('two list (order)',
- array($simple, array(array('b', 'c'),
- array('a', 'b'))),
- $simple . '?b=c&a=b'),
- array('two dict (order)',
- array($simple, array('b' => 'c',
- 'a' => 'b')),
- $simple . '?a=b&b=c'),
- array('escape',
- array($simple, array(array('=', '='))),
- $simple . '?%3D=%3D'),
- array('escape (URL)',
- array($simple, array(array('this_url',
- $simple))),
- $simple .
- '?this_url=http%3A%2F%2Fwww.example.com%2F'),
- array('use dots',
- array($simple, array(array('openid.stuff',
- 'bother'))),
- $simple . '?openid.stuff=bother'),
- array('args exist (empty)',
- array($simple . '?stuff=bother', array()),
- $simple . '?stuff=bother'),
- array('args exist',
- array($simple . '?stuff=bother',
- array(array('ack', 'ack'))),
- $simple . '?stuff=bother&ack=ack'),
- array('args exist',
- array($simple . '?stuff=bother',
- array(array('ack', 'ack'))),
- $simple . '?stuff=bother&ack=ack'),
- array('args exist (dict)',
- array($simple . '?stuff=bother',
- array('ack' => 'ack')),
- $simple . '?stuff=bother&ack=ack'),
- array('args exist (dict 2)',
- array($simple . '?stuff=bother',
- array('ack' => 'ack', 'zebra' => 'lion')),
- $simple . '?stuff=bother&ack=ack&zebra=lion'),
- array('three args (dict)',
- array($simple, array('stuff' => 'bother',
- 'ack' => 'ack',
- 'zebra' => 'lion')),
- $simple . '?ack=ack&stuff=bother&zebra=lion'),
- array('three args (list)',
- array($simple, array(
- array('stuff', 'bother'),
- array('ack', 'ack'),
- array('zebra', 'lion'))),
- $simple . '?stuff=bother&ack=ack&zebra=lion'),
- );
- // Tests.
- foreach ($cases as $case) {
- list($desc, $data, $expected) = $case;
- list($url, $query) = $data;
- $this->assertEquals($expected,
- Auth_OpenID::appendArgs($url, $query));
- }
- }
- function test_getQuery()
- {
- $queries = array(
- '' => array(),
- 'single' => array(),
- 'no&pairs' => array(),
- 'x%3Dy' => array(),
- 'single&real=value' => array('real' => 'value'),
- 'x=y&m=x%3Dn' => array('x' => 'y', 'm' => 'x=n'),
- '&m=x%20y' => array('m' => 'x y'),
- 'single&&m=x%20y&bogus' => array('m' => 'x y'),
- // Even with invalid encoding. But don't do that.
- 'too=many=equals&' => array('too' => 'many=equals')
- );
- foreach ($queries as $s => $data) {
- $query = Auth_OpenID::getQuery($s);
- foreach ($data as $key => $value) {
- $this->assertTrue($query[$key] === $value);
- }
- foreach ($query as $key => $value) {
- $this->assertTrue($data[$key] === $value);
- }
- }
- }
- }
|