<?php
-// $Id: SpamTest.php,v 1.9 2007/05/05 04:59:31 henoheno Exp $
+// $Id: SpamTest.php,v 1.21 2007/08/18 09:10:35 henoheno Exp $
// Copyright (C) 2007 heno
//
// Design test case for spam.php (called from runner.php)
);
}
+ 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
function testFunc_array_merge_leaves()
{
+ // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
$array1 = array(1);
$array2 = array(1);
- $result = array(1);
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array(1, 1), $result);
+ $result = array_unique_recursive($result);
+ $this->assertEquals(array(1), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array(1), $result);
$array1 = array(2);
$array2 = array(1);
- $result = array(2, 1);
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array(2, 1), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array(1), $result);
- // All NUMERIC keys are always renumbered from 0
+ // 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));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array(10 => 'f4'), $result);
// One more thing ...
$array1 = array('20' => 'f5');
$array2 = array();
- $result = array('f5');
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array(0 => 'f5'), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array(20 => 'f5'), $result);
- // Non-numeric keys and values will be marged as you think
+ // 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));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array('a' => array('f1', 'f2')), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('a' => 'f2'), $result);
- // Non-numeric keys: An array and a value will be marged
+ // 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));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('b' => array(0 => 'k1')), $result);
- // Combination
+ // Combination?
$array1 = array(
2,
- 'a' => 'f1',
+ 'a' => 'f1',
'10' => 'f3',
'20' => 'f5',
- 'b' => array('k1'),
+ 'b' => array('k1'),
);
$array2 = array(
1,
- 'a' => 'f2',
+ 'a' => 'f2',
'10' => 'f4',
- 'b' => 'k2',
+ 'b' => 'k2',
);
$result = array (
2,
1,
'f4',
);
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
+ $result2 = array (
+ 0 => 1,
+ 10 => 'f4',
+ 20 => 'f5',
+ 'a' => 'f2',
+ 'b' => array ('k1'),
+ );
+ $this->assertEquals($result, array_merge_recursive($array1, $array2));
+ $this->assertEquals($result2, array_merge_leaves($array1, $array2));
// Values will not be unique?
$array1 = array(5, 4);
$array2 = array(4, 5);
- $result = array(5, 4);
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array(5, 4, 4, 5), $result);
+ $this->assertEquals(array(5, 4), array_unique_recursive($result));
+ $this->assertEquals(array(0=>4, 1=>5), array_merge_leaves($array1, $array2));
- // One more thing ...
+ // One more thing ...?
$array1 = array('b' => array('k3'));
$array2 = array('b' => 'k3');
- $result = array('b' => array('k3'));
- $this->assertEquals($result, array_merge_leaves($array1, $array2));
- }
-
- function testFunc_uri_pickup()
- {
- // 1st argument: Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals(0, count(uri_pickup($value)), $key);
- }
-
- // 1st argument: Some
- $test_string = <<<EOF
- TTP://wwW.Example.Org#TTP_and_www
- https://nasty.example.org:443/foo/xxx#port443/slash
- 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
-EOF;
- $results = uri_pickup_normalize(uri_pickup($test_string));
- $this->assertEquals(5, count($results));
-
- // ttp://wwW.Example.Org:80#TTP_and_www
- $this->assertEquals('http', $results[0]['scheme']);
- $this->assertEquals('', $results[0]['userinfo']);
- $this->assertEquals('example.org', $results[0]['host']);
- $this->assertEquals('', $results[0]['port']);
- $this->assertEquals('/', $results[0]['path']);
- $this->assertEquals('', $results[0]['file']);
- $this->assertEquals('', $results[0]['query']);
- $this->assertEquals('ttp_and_www', $results[0]['fragment']);
-
- // https://nasty.example.org:443/foo/xxx#port443/slash
- $this->assertEquals('https', $results[1]['scheme']);
- $this->assertEquals('', $results[1]['userinfo']);
- $this->assertEquals('nasty.example.org', $results[1]['host']);
- $this->assertEquals('', $results[1]['port']);
- $this->assertEquals('/foo/', $results[1]['path']);
- $this->assertEquals('xxx', $results[1]['file']);
- $this->assertEquals('', $results[1]['query']);
- $this->assertEquals('port443', $results[1]['fragment']);
-
- // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
- $this->assertEquals('sftp', $results[2]['scheme']);
- $this->assertEquals('', $results[2]['userinfo']);
- $this->assertEquals('foobar.example.org', $results[2]['host']);
- $this->assertEquals('80', $results[2]['port']);
- $this->assertEquals('/', $results[2]['path']);
- $this->assertEquals('dfsdfs', $results[2]['file']);
- $this->assertEquals('', $results[2]['query']);
- $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
-
- // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
- $this->assertEquals('ftp', $results[3]['scheme']);
- $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
- $this->assertEquals('10.0.0.1', $results[3]['host']);
- $this->assertEquals('', $results[3]['port']);
- $this->assertEquals('/', $results[3]['path']);
- $this->assertEquals('top_story.htm', $results[3]['file']);
- $this->assertEquals('', $results[3]['query']);
- $this->assertEquals('', $results[3]['fragment']);
- }
-
- function testFunc_scheme_normalize()
- {
- // Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals('', scheme_normalize($value), $key);
- }
-
- // CASE
- $this->assertEquals('http', scheme_normalize('HTTP'));
-
- // Aliases
- $this->assertEquals('pop3', scheme_normalize('pop'));
- $this->assertEquals('nntp', scheme_normalize('news'));
- $this->assertEquals('imap', scheme_normalize('imap4'));
- $this->assertEquals('nntps', scheme_normalize('snntp'));
- $this->assertEquals('nntps', scheme_normalize('snews'));
- $this->assertEquals('pop3s', scheme_normalize('spop3'));
- $this->assertEquals('pop3s', scheme_normalize('pops'));
-
- // Abbrevs
- $this->assertEquals('http', scheme_normalize('ttp'));
- $this->assertEquals('https', scheme_normalize('ttps'));
-
- // Abbrevs considererd harmless
- $this->assertEquals('', scheme_normalize('ttp', FALSE));
- $this->assertEquals('', scheme_normalize('ttps', FALSE));
- }
-
- function testFunc_host_normalize()
- {
- // Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals('', host_normalize($value), $key);
- }
-
- // Hostname is case-insensitive
- $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
-
- // Cut 'www' (destructive)
- $this->assertEquals('example.org', host_normalize('WWW.example.org'));
- }
-
- function testFunc_port_normalize()
- {
- $scheme = 'dont_care';
-
- // 1st argument: Null
- $this->assertEquals('', port_normalize(NULL, $scheme));
- $this->assertEquals('', port_normalize(TRUE, $scheme));
- $this->assertEquals('', port_normalize(FALSE, $scheme));
- $this->assertEquals('', port_normalize(array('foobar'), $scheme));
- $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(8080, port_normalize( 8080, $scheme));
- $this->assertEquals(65535, port_normalize(65535, $scheme));
- $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
-
- // 1st argument: Invalid type
- $this->assertEquals('1x', port_normalize('001', $scheme) . 'x');
- $this->assertEquals('', port_normalize('+0', $scheme));
- $this->assertEquals('', port_normalize('0-1', $scheme)); // intval() says '0'
- $this->assertEquals('', port_normalize('str', $scheme));
-
- // 2nd and 3rd argument: Null
- $this->assertEquals(80, port_normalize(80, NULL, TRUE));
- $this->assertEquals(80, port_normalize(80, TRUE, TRUE));
- $this->assertEquals(80, port_normalize(80, FALSE, TRUE));
- $this->assertEquals(80, port_normalize(80, array('foobar'), TRUE));
- $this->assertEquals(80, port_normalize(80, '', TRUE));
-
- // 2nd and 3rd argument: Do $scheme_normalize
- $this->assertEquals('', port_normalize(80, 'TTP', TRUE));
- $this->assertEquals('', port_normalize(110, 'POP', TRUE));
- $this->assertEquals(80, port_normalize(80, 'HTTP', FALSE));
- }
-
- function testFunc_path_normalize()
- {
- // 1st argument: Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals('/', path_normalize($value), $key);
- }
-
- // 1st argument: CASE sensitive
- $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
- $this->assertEquals('/#hoge', path_normalize('#hoge'));
- $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
- $this->assertEquals('/b/', path_normalize('/a/../../../b/'));
-
- // 2nd argument
- $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
- $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
- $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
- $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
- $this->assertEquals('', path_normalize(array('a'), array('b')));
- }
-
- function testFunc_file_normalize()
- {
- // 1st argument: Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals('', file_normalize($value), $key);
- }
-
- // 1st argument: Cut DirectoryIndexes (Destructive)
- foreach(array(
- 'default.htm',
- 'default.html',
- 'default.asp',
- 'default.aspx',
-\r 'index',
- 'index.htm',
- 'index.html',
- 'index.shtml',
- 'index.jsp',
- 'index.php',
- 'index.php',
- 'index.php3',
- 'index.php4',
- 'index.pl',
- 'index.py',
- 'index.rb',
- 'index.cgi',
-
- // Apache 2.0.59 default 'index.html' variants
- 'index.html.ca',
- 'index.html.cz.iso8859-2',
- 'index.html.de',
- 'index.html.dk',
- 'index.html.ee',
- 'index.html.el',
- 'index.html.en',
- 'index.html.es',
- 'index.html.et',
- 'index.html.fr',
- 'index.html.he.iso8859-8',
- 'index.html.hr.iso8859-2',
- 'index.html.it',
- 'index.html.ja.iso2022-jp',
- 'index.html.ko.euc-kr',
- 'index.html.lb.utf8',
- 'index.html.nl',
- 'index.html.nn',
- 'index.html.no',
- 'index.html.po.iso8859-2',
- 'index.html.pt',
- 'index.html.pt-br',
- 'index.html.ru.cp866',
- 'index.html.ru.cp-1251',
- 'index.html.ru.iso-ru',
- 'index.html.ru.koi8-r',
- 'index.html.ru.utf8',
- 'index.html.sv',
- 'index.html.var', // default
- 'index.html.zh-cn.gb2312',
- 'index.html.zh-tw.big5',
-
- 'index.html.po.iso8859-2',
- 'index.html.zh-tw.big5',
-
- 'index.ja.en.de.html',
-
- // .gz
- 'index.html.ca.gz',
- 'index.html.en.ja.ca.z',
- ) as $arg){
- $this->assertEquals('', file_normalize($arg));
- }
-
- //$this->assertEquals('foo/', file_normalize('foo/index.html'));
-
- //$this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
- //$this->assertEquals('exe.exe', file_normalize('exe.exe'));
- //$this->assertEquals('sample.html', file_normalize('sample.html.en'));
- //$this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
- //$this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
- //$this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
- }
-
- function testFunc_query_normalize()
- {
- // 1st argument: Null
- foreach($this->setup_string_null() as $key => $value){
- $this->assertEquals('', query_normalize($value), $key);
- }
-
- $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
- $this->assertEquals('eg=foobar', query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
+ $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);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('b' => array('k3')), $result);
+
+ // Preserve numeric keys?
+ $array1 = array('a' => array('' => NULL));
+ $array2 = array('a' => array(5 => NULL));
+ $array3 = array('a' => array(8 => NULL));
+ //
+ // BAD: PHP array_merge_recursive() don't preserve numeric keys
+ $result = array_merge_recursive($array1, $array2);
+ $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result); // 0?
+ $result = array_merge_recursive($array2, $array3);
+ $this->assertEquals(array('a' => array(5 => NULL, 6 => NULL)), $result); // 6?
+ //
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result); // 0?
+ $result = array_merge_leaves($array2, $array3);
+ $this->assertEquals(array('a' => array(5 => NULL, 8 => NULL)), $result); // 6?
+
+ // Merging array leaves
+ $array1 = array('a' => TRUE);
+ $array2 = array('b' => FALSE);
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result);
+
+ $array1 = array('a' => TRUE);
+ $array2 = array('a' => array('aa' => TRUE));
+ $this->assertEquals($array2, array_merge_leaves($array1, $array2));
+ $this->assertEquals($array2, array_merge_leaves($array2, $array1));
+
+ $array1 = array('a' => array('a1' => TRUE));
+ $array2 = array('a' => array('a2' => FALSE));
+ $result = array_merge_leaves($array1, $array2);
+ $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result);
}
function testFunc_generate_glob_regex()
$this->assertTrue(isset($array['goodhost']));
// badhost
$array = get_blocklist('B-1');
- $this->assertTrue(isset($array['*.blogspot.com']));
+ $this->assertTrue(isset($array['Google.com']));
// goodhost
$array = get_blocklist('goodhost');
$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'));
}
}