From 4c4b91d3e365f2db75433fd8059a94b7bfc966d6 Mon Sep 17 00:00:00 2001 From: henoheno Date: Sun, 24 Jun 2007 00:21:32 +0900 Subject: [PATCH] whois_responsibility() related, and uri_pickup() --- spam/SpamTest.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- spam/spam.php | 16 +++++++++---- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/spam/SpamTest.php b/spam/SpamTest.php index 7acb360..20171a4 100644 --- a/spam/SpamTest.php +++ b/spam/SpamTest.php @@ -1,5 +1,5 @@ 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')); + } } ?> diff --git a/spam/spam.php b/spam/spam.php index 3383326..ea35012 100644 --- a/spam/spam.php +++ b/spam/spam.php @@ -1,5 +1,5 @@ "\'\[\]/\#]+)*/+)?' . // 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]; -- 2.11.0