OSDN Git Service

testFunc_uri_pickup(): Added two more tests
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamPickupTest.php
index f61f725..4612faa 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamPickupTest.php,v 1.2 2007/07/02 15:27:20 henoheno Exp $
+// $Id: SpamPickupTest.php,v 1.9 2009/01/02 10:15:48 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -54,7 +54,7 @@ class SpamPickupTest extends PHPUnit_TestCase
 
        function testFunc_host_normalize()
        {
-               // Null
+               // Invalid: Null
                foreach($this->setup_string_null() as $key => $value){
                        $this->assertEquals('', host_normalize($value), $key);
                }
@@ -62,8 +62,12 @@ class SpamPickupTest extends PHPUnit_TestCase
                // Hostname is case-insensitive
                $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
 
-               // Cut 'www' (destructive)
+               // Cut 'www' with traditional ASCII-based FQDN (destructive)
                $this->assertEquals('example.org', host_normalize('WWW.example.org'));
+
+               // Don't cut 'www' with Non-ASCII-based string such as IDN
+               $this->assertEquals("www.example.org\0foobar",
+                        host_normalize("WWW.example.org\0foobar"));
        }
 
        function testFunc_port_normalize()
@@ -255,9 +259,10 @@ class SpamPickupTest extends PHPUnit_TestCase
                        sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
                        ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
                        http://192.168.1.4:443#IPv4
+                       http://localhost/index.php?%2Fofficial&word=f
 EOF;
                $results = uri_pickup_normalize(uri_pickup($test_string));
-               $this->assertEquals(5, count($results));
+               $this->assertEquals(6, count($results));
 
                // ttp://wwW.Example.Org:80#TTP_and_www
                $this->assertEquals('http',           $results[0]['scheme']);
@@ -299,6 +304,26 @@ EOF;
                $this->assertEquals('',               $results[3]['query']);
                $this->assertEquals('',               $results[3]['fragment']);
 
+               // http://192.168.1.4:443#IPv4
+               $this->assertEquals('http',           $results[4]['scheme']);
+               $this->assertEquals('',               $results[4]['userinfo']);
+               $this->assertEquals('192.168.1.4',    $results[4]['host']);
+               $this->assertEquals('443',            $results[4]['port']);
+               $this->assertEquals('/',              $results[4]['path']);
+               $this->assertEquals('',               $results[4]['file']);
+               $this->assertEquals('',               $results[4]['query']);
+               $this->assertEquals('ipv4',           $results[4]['fragment']);
+
+               // http://localhost/index.php?%2Fofficial&word=f
+               $this->assertEquals('http',           $results[5]['scheme']);
+               $this->assertEquals('',               $results[5]['userinfo']);
+               $this->assertEquals('localhost',      $results[5]['host']);
+               $this->assertEquals('',               $results[5]['port']);
+               $this->assertEquals('/',              $results[5]['path']);
+               $this->assertEquals('',               $results[5]['file']);
+               $this->assertEquals('%2Fofficial&word=f', $results[5]['query']);
+               $this->assertEquals('',               $results[5]['fragment']);
+
 
                // Specific tests ----
 
@@ -307,6 +332,24 @@ EOF;
                $results = uri_pickup_normalize(uri_pickup($test_string));
                $this->assertEquals('backslash.org',  $results[0]['host']);
 
+               // Divider: percent-encoded
+               //$test_string = ' http%3A%2F%5Cpercent-encoded.org%5Cfobar.html ';
+               //$results = uri_pickup_normalize(uri_pickup($test_string));
+               //$this->assertEquals('percent-encoded.org',  $results[0]['host']);
+
+               // Host: Without path
+               $test_string = ' http://nopathstring.com ';
+               $results = uri_pickup($test_string);
+               $this->assertEquals('', $results[0]['path']);
+               $this->assertEquals('', $results[0]['file']);
+               $results[0]['path'] = '/';
+               $this->assertEquals('', $results[0]['file'], '[Seems referense trouble]');
+               //
+               $results = uri_pickup($test_string);
+               $results = uri_pickup_normalize($results);
+               $this->assertEquals('/',$results[0]['path']);
+               $this->assertEquals('', $results[0]['file']);
+
                // Host: Underscore
                $test_string = ' http://under_score.org/fobar.html ';
                $results = uri_pickup_normalize(uri_pickup($test_string));
@@ -346,8 +389,37 @@ EOF;
                $results = uri_pickup_normalize(uri_pickup($test_string));
                $this->assertEquals('sss',            $results[0]['host']);
                $this->assertEquals('foo.html',       $results[0]['file']);
+
+               // uri_pickup_normalize_pathfile()
+               $test_string = ' http://example.com/path/to/directory-accidentally-not-ended-with-slash ';
+               $results = uri_pickup_normalize_pathfile(uri_pickup($test_string));
+               $this->assertEquals('/path/to/directory-accidentally-not-ended-with-slash', $results[0]['path']);
+               $this->assertEquals(TRUE,  isset($results[0]['path']));
+               $this->assertEquals(FALSE, isset($results[0]['file']));
+               $this->assertEquals('http://example.com/path/to/directory-accidentally-not-ended-with-slash',
+                       uri_pickup_implode($results[0]));
        }
 
+       function testFunc_spam_uri_pickup()
+       {
+               // Divider: percent-encoded
+               $test_string = ' http://victim.example.org/http%3A%2F%5Cnasty.example.org ';
+               $results = spam_uri_pickup($test_string);
+               $this->assertEquals('victim.example.org', $results[0]['host']);
+               $this->assertEquals('nasty.example.org',  $results[1]['host']);
+
+               // Domain exposure (site:)
+               $test_string = ' http://search.example.org/?q=%20site:nasty.example.org ';
+               $results = spam_uri_pickup($test_string);
+               $this->assertEquals('nasty.example.org', $results[0]['host']);
+               $this->assertEquals('search.example.org',  $results[1]['host']);
+               
+               // Domain exposure (%20site:)
+               $test_string = ' http://search2.example.org/?q=%20site:nasty2.example.org ';
+               $results = spam_uri_pickup($test_string);
+               $this->assertEquals('nasty2.example.org', $results[0]['host']);
+               $this->assertEquals('search2.example.org',  $results[1]['host']);
+       }
 }
 
 ?>
\ No newline at end of file