OSDN Git Service

Added
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
index df98e95..20171a4 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.8 2007/05/04 14:54:21 henoheno Exp $
+// $Id: SpamTest.php,v 1.17 2007/06/23 15:21:32 henoheno Exp $
 // Copyright (C) 2007 heno
 //
 // Design test case for spam.php (called from runner.php)
@@ -24,6 +24,44 @@ class SpamTest extends PHPUnit_TestCase
                );
        }
 
+       function testFunc_strings()
+       {
+               // 1st argument: Null
+               $this->assertEquals('',  strings(NULL,  0));
+               $this->assertEquals('',  strings(TRUE,  0));
+               $this->assertEquals('',  strings(FALSE, 0));
+               $this->assertEquals('',  strings('',    0));
+               $this->assertEquals('0', strings(0,     0));
+               $this->assertEquals('1', strings(1,     0));
+
+               // Setup
+               $t1 = '1'    . "\n";
+               $t2 = '12'   . "\n";
+               $t3 = '123'  . "\n";
+               $t4 = '1234' . "\n";
+               $t5 = '12345';
+               $test = $t1 . $t2 . $t3 . $t4 . $t5;
+
+               // Minimum length
+               $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, -1));
+               $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test,  0));
+               $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test,  1));
+               $this->assertEquals(      $t2 . $t3 . $t4 . $t5, strings($test,  2));
+               $this->assertEquals(            $t3 . $t4 . $t5, strings($test,  3));
+               $this->assertEquals(                  $t4 . $t5, strings($test,  4));
+               $this->assertEquals(                  $t4 . $t5, strings($test)); // Default
+               $this->assertEquals(                        $t5, strings($test,  5));
+
+               // Preserve the last newline
+               $this->assertEquals($t4 . $t5,        strings($test       , 4));
+               $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4));
+
+               // Ignore sequential spaces, and spaces at the beginning/end of lines
+               $test = '   A' . '      ' . '   ' . 'B  ';
+               $this->assertEquals($test, strings($test, 0, FALSE));
+               $this->assertEquals('A B', strings($test, 0, TRUE ));
+       }
+
        function testFunc_array_count_leaves()
        {
                // Empty array = 0, if option is not set
@@ -76,36 +114,69 @@ class SpamTest extends PHPUnit_TestCase
                $this->assertEquals(19, array_count_leaves($array, TRUE));
        }
 
-       function testFunc_array_merge_leaves()
+       function testPhpFunc_array_unique()
+       {
+               $this->assertEquals(array(1), array_unique(array(1, 1)));
+
+               // Keys are preserved, array()s inside are preserved
+               $this->assertEquals(
+                       array(0, 2 => array(1, 1), 3 => 2),
+                       array_unique(
+                               array(0, 0, array(1, 1), 2, 2)
+                       )
+               );
+
+               // Keys are preserved
+               $this->assertEquals(
+                       array(0, 2 => array(1, 1), 3 => 2),
+                       array_unique(array(0, 0, array(1, 1), 2, 2))
+               );
+
+               // ONLY the first array() is preserved
+               $this->assertEquals(
+                       array(0 => array(1, 1)),
+                       array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
+               );
+       }
+
+       // And array_unique_recursive()
+       function testPhPFunc_array_merge_recursive()
        {
+               $array1 = array(1);
+               $array2 = array(1);
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(1, 1), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array(1),    $result);
+
                $array1 = array(2);
                $array2 = array(1);
                $result = array(2, 1);
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // All NUMERIC keys are always renumbered from 0
                $array1 = array('10' => 'f3');
                $array2 = array('10' => 'f4');
                $result = array('f3', 'f4');
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // One more thing ...
                $array1 = array('20' => 'f5');
                $array2 = array();
                $result = array('f5');
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Non-numeric keys and values will be marged as you think
                $array1 = array('a' => 'f1');
                $array2 = array('a' => 'f2');
                $result = array('a' => array('f1', 'f2'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Non-numeric keys: An array and a value will be marged
                $array1 = array('b' => array('k1'));
                $array2 = array('b' => 'k2');
                $result = array('b' => array('k1', 'k2'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
                // Combination
                $array1 = array(
@@ -136,21 +207,23 @@ class SpamTest extends PHPUnit_TestCase
                        1,
                        'f4',
                );
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
-
-               // ----
+               $this->assertEquals($result, array_merge_recursive($array1, $array2));
 
-               // Values will not be unique now
+               // Values will not be unique
                $array1 = array(5, 4);
                $array2 = array(4, 5);
-               $result = array(5, 4, 4, 5);
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array(5, 4, 4, 5), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array(5, 4),       $result);
 
                // One more thing ...
                $array1 = array('b' => array('k3'));
                $array2 = array('b' => 'k3');
-               $result = array('b' => array('k3', 'k3'));
-               $this->assertEquals($result, array_merge_leaves($array1, $array2));
+               $result = array_merge_recursive($array1, $array2);
+               $this->assertEquals(array('b' => array('k3', 'k3')), $result);
+               $result = array_unique_recursive($result);
+               $this->assertEquals(array('b' => array('k3')),       $result);
        }
 
        function testFunc_uri_pickup()
@@ -210,6 +283,54 @@ EOF;
                $this->assertEquals('top_story.htm',  $results[3]['file']);
                $this->assertEquals('',               $results[3]['query']);
                $this->assertEquals('',               $results[3]['fragment']);
+
+
+               // Specific tests ----
+
+               // Divider: Back-slash
+               $test_string = ' http:\\backslash.org\fobar.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('backslash.org',  $results[0]['host']);
+
+               // Host: Underscore
+               $test_string = ' http://under_score.org/fobar.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('under_score.org',$results[0]['host']);     // Not 'under'
+
+               // Host: IPv4
+               $test_string = ' http://192.168.0.1/fobar.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('192.168.0.1',    $results[0]['host']);
+
+               // Host: Starts
+               $test_string = ' http://_sss/foo.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('_sss',           $results[0]['host']);
+               $this->assertEquals('foo.html',       $results[0]['file']);
+
+               // Host: Ends
+               $test_string = ' http://sss_/foo.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('sss_',           $results[0]['host']);
+               $this->assertEquals('foo.html',       $results[0]['file']);
+
+
+               // Specific tests ---- Fails
+
+               // Divider: Colon only (Too sensitive to capture)
+               $test_string = ' http:colon.org ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals(0, count($results));
+
+               // Host: Too short
+               $test_string = ' http://s/foo.html http://ss/foo.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals(0, count($results));
+
+               $test_string = ' http://sss/foo.html ';
+               $results = uri_pickup_normalize(uri_pickup($test_string));
+               $this->assertEquals('sss',            $results[0]['host']);
+               $this->assertEquals('foo.html',       $results[0]['file']);
        }
 
        function testFunc_scheme_normalize()
@@ -266,32 +387,32 @@ EOF;
                $this->assertEquals('', port_normalize('',   $scheme));
 
                // 1st argument: Known port
-               $this->assertEquals('',    port_normalize(-1,   $scheme));
-               $this->assertEquals(0,     port_normalize(0,    $scheme));
-               $this->assertEquals(1,     port_normalize(1,    $scheme));
-               $this->assertEquals('',    port_normalize(  21, 'ftp'));
-               $this->assertEquals('',    port_normalize(  22, 'ssh'));
-               $this->assertEquals('',    port_normalize(  23, 'telnet'));
-               $this->assertEquals('',    port_normalize(  25, 'smtp'));
-               $this->assertEquals('',    port_normalize(  69, 'tftp'));
-               $this->assertEquals('',    port_normalize(  70, 'gopher'));
-               $this->assertEquals('',    port_normalize(  79, 'finger'));
-               $this->assertEquals('',    port_normalize(  80, 'http'));
-               $this->assertEquals('',    port_normalize( 110, 'pop3'));
-               $this->assertEquals('',    port_normalize( 115, 'sftp'));
-               $this->assertEquals('',    port_normalize( 119, 'nntp'));
-               $this->assertEquals('',    port_normalize( 143, 'imap'));
-               $this->assertEquals('',    port_normalize( 194, 'irc'));
-               $this->assertEquals('',    port_normalize( 210, 'wais'));
-               $this->assertEquals('',    port_normalize( 443, 'https'));
-               $this->assertEquals('',    port_normalize( 563, 'nntps'));
-               $this->assertEquals('',    port_normalize( 873, 'rsync'));
-               $this->assertEquals('',    port_normalize( 990, 'ftps'));
-               $this->assertEquals('',    port_normalize( 992, 'telnets'));
-               $this->assertEquals('',    port_normalize( 993, 'imaps'));
-               $this->assertEquals('',    port_normalize( 994, 'ircs'));
-               $this->assertEquals('',    port_normalize( 995, 'pop3s'));
-               $this->assertEquals('',    port_normalize(3306, 'mysql'));
+               $this->assertEquals('',    port_normalize(   -1, $scheme));
+               $this->assertEquals(0,     port_normalize(    0, $scheme));
+               $this->assertEquals(1,     port_normalize(    1, $scheme));
+               $this->assertEquals('',    port_normalize(   21, 'ftp'));
+               $this->assertEquals('',    port_normalize(   22, 'ssh'));
+               $this->assertEquals('',    port_normalize(   23, 'telnet'));
+               $this->assertEquals('',    port_normalize(   25, 'smtp'));
+               $this->assertEquals('',    port_normalize(   69, 'tftp'));
+               $this->assertEquals('',    port_normalize(   70, 'gopher'));
+               $this->assertEquals('',    port_normalize(   79, 'finger'));
+               $this->assertEquals('',    port_normalize(   80, 'http'));
+               $this->assertEquals('',    port_normalize(  110, 'pop3'));
+               $this->assertEquals('',    port_normalize(  115, 'sftp'));
+               $this->assertEquals('',    port_normalize(  119, 'nntp'));
+               $this->assertEquals('',    port_normalize(  143, 'imap'));
+               $this->assertEquals('',    port_normalize(  194, 'irc'));
+               $this->assertEquals('',    port_normalize(  210, 'wais'));
+               $this->assertEquals('',    port_normalize(  443, 'https'));
+               $this->assertEquals('',    port_normalize(  563, 'nntps'));
+               $this->assertEquals('',    port_normalize(  873, 'rsync'));
+               $this->assertEquals('',    port_normalize(  990, 'ftps'));
+               $this->assertEquals('',    port_normalize(  992, 'telnets'));
+               $this->assertEquals('',    port_normalize(  993, 'imaps'));
+               $this->assertEquals('',    port_normalize(  994, 'ircs'));
+               $this->assertEquals('',    port_normalize(  995, 'pop3s'));
+               $this->assertEquals('',    port_normalize( 3306, 'mysql'));
                $this->assertEquals(8080,  port_normalize( 8080, $scheme));
                $this->assertEquals(65535, port_normalize(65535, $scheme));
                $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
@@ -496,24 +617,40 @@ EOF;
                $this->assertTrue(isset($array['IANA-examples']));
        }
 
-       function testFunc_is_badhost()
+
+       function testFunc_whois_responsibility()
        {
-               // FALSE (Nothing)
-               $this->assertEquals(FALSE,   is_badhost(array(), FALSE, TRUE));
-               $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
+               // 1st argument: Null
+               foreach($this->setup_string_null() as $key => $value){
+                       $this->assertEquals('',        whois_responsibility($value), $key);
+               }
 
-               // TRUE
-               $this->assertEquals(TRUE,    is_badhost('=.blogspot.com', FALSE, TRUE));
-               $this->assertEquals(
-                       array(
-                               'B-1' => array(
-                                       '*.blogspot.com' => array(
-                                               '=.blogspot.com'
-                                       )
-                               )
-                       ),
-                       is_badhost('=.blogspot.com', FALSE, FALSE)
-               );
+               // 'act.edu.au' is known as 3rd level domain
+               $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
+               $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
+               $this->AssertEquals('act.edu.au',  whois_responsibility('act.edu.au'));
+               $this->AssertEquals('edu.au',      whois_responsibility('edu.au'));
+               $this->AssertEquals('au',          whois_responsibility('au'));
+
+               // 'co.uk' is known as 2nd level domain
+               $this->AssertEquals('bar.co.uk',   whois_responsibility('foo.bar.co.uk'));
+               $this->AssertEquals('bar.co.uk',   whois_responsibility('bar.co.uk'));
+               $this->AssertEquals('co.uk',       whois_responsibility('co.uk'));
+               $this->AssertEquals('uk',          whois_responsibility('uk'));
+
+               // 'bar.uk' is not 2nd level (implicit responsibility)
+               $this->AssertEquals('bar.uk',      whois_responsibility('foo.bar.uk'));
+               $this->AssertEquals('bar.uk',      whois_responsibility('bar.uk'));
+
+               // IPv4
+               $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
+
+               // Invalid Top-Level Domain (With implicit)
+               $this->AssertEquals('bar.local',  whois_responsibility('foo.bar.local'));       // Implicit responsibility
+               $this->AssertEquals('bar.local',  whois_responsibility('bar.local'));
+               $this->AssertEquals('local',      whois_responsibility('local'));
+               $this->AssertEquals('localhost',  whois_responsibility('localhost'));
+               $this->AssertEquals('s',          whois_responsibility('s'));
        }
 }