2 // $Id: SpamTest.php,v 1.5 2007/04/30 07:41:09 henoheno Exp $
3 // Copyright (C) 2007 heno
5 // Design test case for spam.php (called from runner.php)
7 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
9 require_once('spam.php');
10 require_once('PHPUnit/PHPUnit.php');
12 class SpamTest extends PHPUnit_TestCase
14 function setup_string_null()
20 '[array(foobar)]' => array('foobar'),
27 function testFunc_uri_pickup()
30 foreach($this->setup_string_null() as $key => $value){
31 $this->assertEquals(0, count(uri_pickup($value)), $key);
36 TTP://wwW.Example.Org#TTP_and_www
37 https://nasty.example.org:443/foo/xxx#port443/slash
38 sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
39 ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
40 http://192.168.1.4:443#IPv4
42 $results = uri_pickup_normalize(uri_pickup($test_string));
43 $this->assertEquals(5, count($results));
45 // ttp://wwW.Example.Org:80#TTP_and_www
46 $this->assertEquals('http', $results[0]['scheme']);
47 $this->assertEquals('', $results[0]['userinfo']);
48 $this->assertEquals('example.org', $results[0]['host']);
49 $this->assertEquals('', $results[0]['port']);
50 $this->assertEquals('/', $results[0]['path']);
51 $this->assertEquals('', $results[0]['file']);
52 $this->assertEquals('', $results[0]['query']);
53 $this->assertEquals('ttp_and_www', $results[0]['fragment']);
55 // https://nasty.example.org:443/foo/xxx#port443/slash
56 $this->assertEquals('https', $results[1]['scheme']);
57 $this->assertEquals('', $results[1]['userinfo']);
58 $this->assertEquals('nasty.example.org', $results[1]['host']);
59 $this->assertEquals('', $results[1]['port']);
60 $this->assertEquals('/foo/', $results[1]['path']);
61 $this->assertEquals('xxx', $results[1]['file']);
62 $this->assertEquals('', $results[1]['query']);
63 $this->assertEquals('port443', $results[1]['fragment']);
65 // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
66 $this->assertEquals('sftp', $results[2]['scheme']);
67 $this->assertEquals('', $results[2]['userinfo']);
68 $this->assertEquals('foobar.example.org', $results[2]['host']);
69 $this->assertEquals('80', $results[2]['port']);
70 $this->assertEquals('/', $results[2]['path']);
71 $this->assertEquals('dfsdfs', $results[2]['file']);
72 $this->assertEquals('', $results[2]['query']);
73 $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
75 // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
76 $this->assertEquals('ftp', $results[3]['scheme']);
77 $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
78 $this->assertEquals('10.0.0.1', $results[3]['host']);
79 $this->assertEquals('', $results[3]['port']);
80 $this->assertEquals('/', $results[3]['path']);
81 $this->assertEquals('top_story.htm', $results[3]['file']);
82 $this->assertEquals('', $results[3]['query']);
83 $this->assertEquals('', $results[3]['fragment']);
86 function testFunc_scheme_normalize()
89 foreach($this->setup_string_null() as $key => $value){
90 $this->assertEquals('', scheme_normalize($value), $key);
94 $this->assertEquals('http', scheme_normalize('HTTP'));
97 $this->assertEquals('pop3', scheme_normalize('pop'));
98 $this->assertEquals('nntp', scheme_normalize('news'));
99 $this->assertEquals('imap', scheme_normalize('imap4'));
100 $this->assertEquals('nntps', scheme_normalize('snntp'));
101 $this->assertEquals('nntps', scheme_normalize('snews'));
102 $this->assertEquals('pop3s', scheme_normalize('spop3'));
103 $this->assertEquals('pop3s', scheme_normalize('pops'));
106 $this->assertEquals('http', scheme_normalize('ttp'));
107 $this->assertEquals('https', scheme_normalize('ttps'));
109 // Abbrevs considererd harmless
110 $this->assertEquals('', scheme_normalize('ttp', FALSE));
111 $this->assertEquals('', scheme_normalize('ttps', FALSE));
114 function testFunc_host_normalize()
117 foreach($this->setup_string_null() as $key => $value){
118 $this->assertEquals('', host_normalize($value), $key);
121 // Hostname is case-insensitive
122 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
124 // Cut 'www' (destructive)
125 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
128 function testFunc_port_normalize()
130 $scheme = 'dont_care';
132 // 1st argument: Null
133 $this->assertEquals('', port_normalize(NULL, $scheme));
134 $this->assertEquals('', port_normalize(TRUE, $scheme));
135 $this->assertEquals('', port_normalize(FALSE, $scheme));
136 $this->assertEquals('', port_normalize(array('foobar'), $scheme));
137 $this->assertEquals('', port_normalize('', $scheme));
139 // 1st argument: Known port
140 $this->assertEquals('', port_normalize(-1, $scheme));
141 $this->assertEquals(0, port_normalize(0, $scheme));
142 $this->assertEquals(1, port_normalize(1, $scheme));
143 $this->assertEquals('', port_normalize( 21, 'ftp'));
144 $this->assertEquals('', port_normalize( 22, 'ssh'));
145 $this->assertEquals('', port_normalize( 23, 'telnet'));
146 $this->assertEquals('', port_normalize( 25, 'smtp'));
147 $this->assertEquals('', port_normalize( 69, 'tftp'));
148 $this->assertEquals('', port_normalize( 70, 'gopher'));
149 $this->assertEquals('', port_normalize( 79, 'finger'));
150 $this->assertEquals('', port_normalize( 80, 'http'));
151 $this->assertEquals('', port_normalize( 110, 'pop3'));
152 $this->assertEquals('', port_normalize( 115, 'sftp'));
153 $this->assertEquals('', port_normalize( 119, 'nntp'));
154 $this->assertEquals('', port_normalize( 143, 'imap'));
155 $this->assertEquals('', port_normalize( 194, 'irc'));
156 $this->assertEquals('', port_normalize( 210, 'wais'));
157 $this->assertEquals('', port_normalize( 443, 'https'));
158 $this->assertEquals('', port_normalize( 563, 'nntps'));
159 $this->assertEquals('', port_normalize( 873, 'rsync'));
160 $this->assertEquals('', port_normalize( 990, 'ftps'));
161 $this->assertEquals('', port_normalize( 992, 'telnets'));
162 $this->assertEquals('', port_normalize( 993, 'imaps'));
163 $this->assertEquals('', port_normalize( 994, 'ircs'));
164 $this->assertEquals('', port_normalize( 995, 'pop3s'));
165 $this->assertEquals('', port_normalize(3306, 'mysql'));
166 $this->assertEquals(8080, port_normalize( 8080, $scheme));
167 $this->assertEquals(65535, port_normalize(65535, $scheme));
168 $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
170 // 1st argument: Invalid type
171 $this->assertEquals('1x', port_normalize('001', $scheme) . 'x');
172 $this->assertEquals('', port_normalize('+0', $scheme));
173 $this->assertEquals('', port_normalize('0-1', $scheme)); // intval() says '0'
174 $this->assertEquals('', port_normalize('str', $scheme));
176 // 2nd and 3rd argument: Null
177 $this->assertEquals(80, port_normalize(80, NULL, TRUE));
178 $this->assertEquals(80, port_normalize(80, TRUE, TRUE));
179 $this->assertEquals(80, port_normalize(80, FALSE, TRUE));
180 $this->assertEquals(80, port_normalize(80, array('foobar'), TRUE));
181 $this->assertEquals(80, port_normalize(80, '', TRUE));
183 // 2nd and 3rd argument: Do $scheme_normalize
184 $this->assertEquals('', port_normalize(80, 'TTP', TRUE));
185 $this->assertEquals('', port_normalize(110, 'POP', TRUE));
186 $this->assertEquals(80, port_normalize(80, 'HTTP', FALSE));
189 function testFunc_path_normalize()
191 // 1st argument: Null
192 foreach($this->setup_string_null() as $key => $value){
193 $this->assertEquals('/', path_normalize($value), $key);
196 // 1st argument: CASE sensitive
197 $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
198 $this->assertEquals('/#hoge', path_normalize('#hoge'));
199 $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
200 $this->assertEquals('/b/', path_normalize('/a/../../../b/'));
203 $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
204 $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
205 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
206 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
207 $this->assertEquals('', path_normalize(array('a'), array('b')));
210 function testFunc_file_normalize()
212 // 1st argument: Null
213 foreach($this->setup_string_null() as $key => $value){
214 $this->assertEquals('', file_normalize($value), $key);
217 // 1st argument: Cut DirectoryIndexes (Destructive)
237 // Apache 2.0.59 default 'index.html' variants
239 'index.html.cz.iso8859-2',
248 'index.html.he.iso8859-8',
249 'index.html.hr.iso8859-2',
251 'index.html.ja.iso2022-jp',
252 'index.html.ko.euc-kr',
253 'index.html.lb.utf8',
257 'index.html.po.iso8859-2',
260 'index.html.ru.cp866',
261 'index.html.ru.cp-1251',
262 'index.html.ru.iso-ru',
263 'index.html.ru.koi8-r',
264 'index.html.ru.utf8',
266 'index.html.var', // default
267 'index.html.zh-cn.gb2312',
268 'index.html.zh-tw.big5',
270 'index.html.po.iso8859-2',
271 'index.html.zh-tw.big5',
273 'index.ja.en.de.html',
277 'index.html.en.ja.ca.z',
279 $this->assertEquals('', file_normalize($arg));
282 //$this->assertEquals('foo/', file_normalize('foo/index.html'));
284 //$this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
285 //$this->assertEquals('exe.exe', file_normalize('exe.exe'));
286 //$this->assertEquals('sample.html', file_normalize('sample.html.en'));
287 //$this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
288 //$this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
289 //$this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
292 function testFunc_query_normalize()
294 // 1st argument: Null
295 foreach($this->setup_string_null() as $key => $value){
296 $this->assertEquals('', query_normalize($value), $key);
299 $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
300 $this->assertEquals('eg=foobar', query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
303 function testFunc_generate_glob_regex()
305 // 1st argument: Null
306 foreach($this->setup_string_null() as $key => $value){
307 $this->assertEquals('', generate_glob_regex($value), $key);
310 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
311 $this->assertEquals('A.A', generate_glob_regex('A?A'));
314 function testFunc_generate_host_regex()
316 // 1st argument: Null
317 foreach($this->setup_string_null() as $key => $value){
318 $this->assertEquals('', generate_host_regex($value), $key);
321 $this->assertEquals('localhost', generate_host_regex('localhost'));
322 $this->assertEquals('example\.org', generate_host_regex('example.org'));
323 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
324 $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org'));
325 $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org'));
326 $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40'));
328 // Should match with 192.168.0.0/16
329 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
332 function testFunc_get_blocklist()
334 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
335 $this->fail('SPAM_INI_FILE not defined or not found');
339 // get_blocklist_add()
342 get_blocklist_add($array, 'foo', 'bar');
343 $this->assertEquals(1, count($array));
344 $this->assertEquals('bar', $array['foo']);
346 get_blocklist_add($array, 'hoge', 'fuga');
347 $this->assertEquals(2, count($array));
348 $this->assertEquals('bar', $array['foo']);
349 $this->assertEquals('fuga', $array['hoge']);
351 get_blocklist_add($array, -1, '*.txt');
352 $this->assertEquals(3, count($array));
353 $this->assertEquals('bar', $array['foo']);
354 $this->assertEquals('fuga', $array['hoge']);
355 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
359 $array = get_blocklist();
360 $this->assertTrue(isset($array['C']));
361 $this->assertTrue(isset($array['goodhost']));
363 $array = get_blocklist('B-1');
364 $this->assertTrue(isset($array['*.blogspot.com']));
366 $array = get_blocklist('goodhost');
367 $this->assertTrue(isset($array['IANA-examples']));
370 function testFunc_is_badhost()
375 $this->assertEquals(array(), is_badhost(array(), TRUE, $remains));
381 '*.blogspot.com' => array(
386 is_badhost('=.blogspot.com', TRUE, $remains)
390 is_badhost('nothing', TRUE, $remains);
391 $this->assertEquals(array(0 => 'nothing'), $remains);