OSDN Git Service

whois_responsibility() related, and uri_pickup()
authorhenoheno <henoheno>
Sat, 23 Jun 2007 15:21:32 +0000 (00:21 +0900)
committerhenoheno <henoheno>
Sat, 23 Jun 2007 15:21:32 +0000 (00:21 +0900)
spam/SpamTest.php
spam/spam.php

index 7acb360..20171a4 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: SpamTest.php,v 1.16 2007/06/23 14:11:10 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)
@@ -301,6 +301,36 @@ EOF;
                $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()
@@ -586,6 +616,42 @@ EOF;
                $array = get_blocklist('goodhost');
                $this->assertTrue(isset($array['IANA-examples']));
        }
+
+
+       function testFunc_whois_responsibility()
+       {
+               // 1st argument: Null
+               foreach($this->setup_string_null() as $key => $value){
+                       $this->assertEquals('',        whois_responsibility($value), $key);
+               }
+
+               // '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'));
+       }
 }
 
 ?>
index 3383326..ea35012 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.189 2007/06/23 14:11:10 henoheno Exp $
+// $Id: spam.php,v 1.190 2007/06/23 15:21:10 henoheno Exp $
 // Copyright (C) 2006-2007 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 //
@@ -189,7 +189,7 @@ function uri_pickup($string = '')
                        // 3: Host
                        '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
                        '(?:[0-9]{1,3}\.){3}[0-9]{1,3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
-                       '[a-z0-9][a-z0-9_.-]+[a-z0-9]' .                // hostname(FQDN) : foo.example.org
+                       '[a-z0-9_-][a-z0-9_.-]+[a-z0-9_-]' .            // hostname(FQDN) : foo.example.org
                ')' .
                '(?::([0-9]*))?' .                                      // 4: Port
                '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
@@ -1526,7 +1526,13 @@ function summarize_detail_newtral($progress = array())
        foreach($progress['hosts'] as $value) {
                // 'A.foo.bar.example.com'
                $resp = whois_responsibility($value);   // 'example.com'
-               $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
+               if (empty($resp)) {
+                       // One or more test, or do nothing here
+                       $resp = strval($value);
+                       $rest = '';
+               } else {
+                       $rest = rtrim(substr($value, 0, - strlen($resp)), '.'); // 'A.foo.bar'
+               }
                $trie = array_merge_recursive($trie, array($resp => array($rest => NULL)));
        }
 
@@ -2038,13 +2044,15 @@ function whois_responsibility($fqdn = 'foo.bar.example.com', $parent = FALSE, $i
                ),
        );
 
-       if (is_ip($fqdn) || ! is_string($fqdn)) return $fqdn;
+       if (! is_string($fqdn)) return '';
+       if (is_ip($fqdn))       return $fqdn;
 
        $result  = array();
        $dcursor = & $domain;
        $array   = array_reverse(explode('.', $fqdn));
        $i = 0;
        while(TRUE) {
+               if (! isset($array[$i])) break;
                $acursor = $array[$i];
                if (is_array($dcursor) && isset($dcursor[$acursor])) {
                        $result[] = & $array[$i];