# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require "uri_util.php"; header("Content-type: text/plain"); echo "Testing remove_dot_segments() using RFC 3986 test vectors..."; $tests = array( # desired_uri input_uri array('/a/g', '/a/b/c/./../../g'), array('mid/6', 'mid/content=5/../6'), ); foreach($tests as $i => $a) { $desired = $a[0]; $args = array_splice($a, 1); $result = call_user_func_array('remove_dot_segments', $args); if ($result !== $desired) { die("FAILED on test $i '".$args[0]."' ('$result' should be '$desired')"); } } echo "OK\n"; echo "Testing normalize_uri()..."; $tests = array( # desired_uri input_uri array('/a/g', '/a/b/c/./../../g'), array('mid/6', 'mid/content=5/../6'), array('/a/g', '/a/b/c/.//../..///g'), array('file:///bin/sh', 'file:/bin/sh'), array('file:///bin/sh', 'file://bin/sh'), array('file:///bin/sh', 'file:///bin/sh'), array('file:///bin/sh', 'file://bin/////sh'), ); foreach($tests as $i => $a) { $desired = $a[0]; $args = array_splice($a, 1); $result = call_user_func_array('normalize_uri', $args); if ($result !== $desired) { die("FAILED on test $i '".$args[0]."' ('$result' should be '$desired')"); } } echo "OK\n"; echo "Testing absolute_uri() using RFC 3986 URIs..."; $base_uri = "http://a/b/c/d;p?q"; $tests = " g:h = g:h g = http://a/b/c/g ./g = http://a/b/c/g g/ = http://a/b/c/g/ /g = http://a/g //g = http://g ?y = http://a/b/c/d;p?y g?y = http://a/b/c/g?y #s = http://a/b/c/d;p?q#s g#s = http://a/b/c/g#s g?y#s = http://a/b/c/g?y#s ;x = http://a/b/c/;x g;x = http://a/b/c/g;x g;x?y#s = http://a/b/c/g;x?y#s = http://a/b/c/d;p?q . = http://a/b/c/ ./ = http://a/b/c/ .. = http://a/b/ ../ = http://a/b/ ../g = http://a/b/g ../.. = http://a/ ../../ = http://a/ ../../g = http://a/g ../../../g = http://a/g ../../../../g = http://a/g /./g = http://a/g /../g = http://a/g g. = http://a/b/c/g. .g = http://a/b/c/.g g.. = http://a/b/c/g.. ..g = http://a/b/c/..g ./../g = http://a/b/g ./g/. = http://a/b/c/g/ g/./h = http://a/b/c/g/h g/../h = http://a/b/c/h g;x=1/./y = http://a/b/c/g;x=1/y g;x=1/../y = http://a/b/c/y g?y/./x = http://a/b/c/g?y/./x g?y/../x = http://a/b/c/g?y/../x g#s/./x = http://a/b/c/g#s/./x g#s/../x = http://a/b/c/g#s/../x http:g = http:g "; $testlines = explode("\n", str_replace("\r\n", "\n", str_replace("\r", "\n", trim($tests)))); foreach ($testlines as $testline) { $tmp = trim($testline); if (empty($tmp)) { continue; } $tmp = explode(' = ', $testline, 2); $relative = trim($tmp[0]); $absolute = trim($tmp[1]); $result = absolute_uri($relative, $base_uri); if ($result !== $absolute) { die("FAILED on \"$relative\" (\"$result\" should be \"$absolute\"\n"); } } echo "OK\n"; echo "* Not testing parse_uri\n"; echo "* Not testing unparse_uri\n"; echo "* Not testing get_current_url\n"; echo "Testing relative_uri..."; $tests = array( # desired_result dest_uri base_uri array('foo/bar', 'http://www.example.com/foo/bar', 'http://www.example.com/foo'), array('bar', 'http://www.example.com/foo/bar', 'http://www.example.com/foo/'), array('bar', 'http://www.example.com/foo/bar', 'http://www.example.com/foo/bar'), array('../bar', 'http://www.example.com/foo/bar', 'http://www.example.com/foo/bar/'), array('foo/xxx', 'http://www.example.com/foo/xxx', 'http://www.example.com/foo'), array('xxx', 'http://www.example.com/foo/xxx', 'http://www.example.com/foo/'), array('xxx', 'http://www.example.com/foo/xxx', 'http://www.example.com/foo/bar'), array('../xxx', 'http://www.example.com/foo/xxx', 'http://www.example.com/foo/bar/'), array('foo/bar/', 'http://www.example.com/foo/bar/', 'http://www.example.com/foo'), array('bar/', 'http://www.example.com/foo/bar/', 'http://www.example.com/foo/'), array('bar/', 'http://www.example.com/foo/bar/', 'http://www.example.com/foo/bar'), array('./', 'http://www.example.com/foo/bar/', 'http://www.example.com/foo/bar/'), array('foo/xxx/', 'http://www.example.com/foo/xxx/', 'http://www.example.com/foo'), array('xxx/', 'http://www.example.com/foo/xxx/', 'http://www.example.com/foo/'), array('xxx/', 'http://www.example.com/foo/xxx/', 'http://www.example.com/foo/bar'), array('../xxx/', 'http://www.example.com/foo/xxx/', 'http://www.example.com/foo/bar/'), array('./', 'http://www.example.com/foo/', 'http://www.example.com/foo/bar'), array('./', 'http://www.example.com/', 'http://www.example.com/foo'), array('http://www2.example.com/ff', 'http://www2.example.com/ff', 'http://www.example.com/foo/'), array('http://www2.example.com/ff', 'http://www2.example.com/ff', 'http://www.example.com/foo/', null, null), array('http://www2.example.com/ff', 'http://www2.example.com/ff', 'http://www.example.com/foo/', null, true), array('//www2.example.com/ff', 'http://www2.example.com/ff', 'http://www.example.com/foo/', null, false), array('foo', 'http://www.example.com/foo', 'http://www.example.com/foo'), array('foo', 'http://www.example.com/foo', 'http://www.example.com/foo', null), array('foo', 'http://www.example.com/foo', 'http://www.example.com/foo', true), array('', 'http://www.example.com/foo', 'http://www.example.com/foo', false), array('../foo', 'http://www.example.com/foo', 'http://www.example.com/foo/'), array('../foo', 'http://www.example.com/foo', 'http://www.example.com/foo/bar'), array('../../foo', 'http://www.example.com/foo', 'http://www.example.com/foo/bar/'), ); foreach($tests as $i => $a) { $desired = $a[0]; $args = array_splice($a, 1); $result = call_user_func_array('relative_uri', $args); if ($result !== $desired) { die("FAILED on test $i '".$args[1]."' -> '".$args[0]."' ('$result' should be '$desired')"); } } echo "OK\n"; echo "Testing completed successfully.\n"; # vim:set ts=8 sw=4 sts=4 expandtab: