2 // $Id: SpamTest.php,v 1.7 2007/05/04 14:42:28 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_array_count_leaves()
29 // Empty array = 0, if option is not set
31 $this->assertEquals(0, array_count_leaves($array, FALSE));
32 $this->assertEquals(1, array_count_leaves($array, TRUE));
38 $this->assertEquals(0, array_count_leaves($array, FALSE));
39 $this->assertEquals(1, array_count_leaves($array, TRUE));
42 foreach(array(NULL, TRUE, FALSE, -1, 0, 1, '', 'foobar') as $value) {
43 $this->assertEquals(1, array_count_leaves($value, FALSE));
44 $this->assertEquals(1, array_count_leaves($value, TRUE));
51 array(), // Empty array
57 'k3' => array(), // Empty array
62 'k6' => array(), // Empty array
67 'k9' => array(), // Empty array
72 'k12' => array(), // Empty array
75 $this->assertEquals(14, array_count_leaves($array, FALSE));
76 $this->assertEquals(19, array_count_leaves($array, TRUE));
79 function testFunc_array_merge_leaves()
83 $result = array(2, 1);
84 $this->assertEquals($result, array_merge_leaves($array1, $array2));
86 // All NUMERIC keys are always renumbered from 0
87 $array1 = array('10' => 'f3');
88 $array2 = array('10' => 'f4');
89 $result = array('f3', 'f4');
90 $this->assertEquals($result, array_merge_leaves($array1, $array2));
93 $array1 = array('20' => 'f5');
95 $result = array('f5');
96 $this->assertEquals($result, array_merge_leaves($array1, $array2));
98 // Non-numeric keys and values will be marged as you think
99 $array1 = array('a' => 'f1');
100 $array2 = array('a' => 'f2');
101 $result = array('a' => array('f1', 'f2'));
102 $this->assertEquals($result, array_merge_leaves($array1, $array2));
104 // Non-numeric keys: An array and a value will be marged
105 $array1 = array('b' => array('k1'));
106 $array2 = array('b' => 'k2');
107 $result = array('b' => array('k1', 'k2'));
108 $this->assertEquals($result, array_merge_leaves($array1, $array2));
139 $this->assertEquals($result, array_merge_leaves($array1, $array2));
142 function testFunc_uri_pickup()
144 // 1st argument: Null
145 foreach($this->setup_string_null() as $key => $value){
146 $this->assertEquals(0, count(uri_pickup($value)), $key);
149 // 1st argument: Some
150 $test_string = <<<EOF
151 TTP://wwW.Example.Org#TTP_and_www
152 https://nasty.example.org:443/foo/xxx#port443/slash
153 sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
154 ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
155 http://192.168.1.4:443#IPv4
157 $results = uri_pickup_normalize(uri_pickup($test_string));
158 $this->assertEquals(5, count($results));
160 // ttp://wwW.Example.Org:80#TTP_and_www
161 $this->assertEquals('http', $results[0]['scheme']);
162 $this->assertEquals('', $results[0]['userinfo']);
163 $this->assertEquals('example.org', $results[0]['host']);
164 $this->assertEquals('', $results[0]['port']);
165 $this->assertEquals('/', $results[0]['path']);
166 $this->assertEquals('', $results[0]['file']);
167 $this->assertEquals('', $results[0]['query']);
168 $this->assertEquals('ttp_and_www', $results[0]['fragment']);
170 // https://nasty.example.org:443/foo/xxx#port443/slash
171 $this->assertEquals('https', $results[1]['scheme']);
172 $this->assertEquals('', $results[1]['userinfo']);
173 $this->assertEquals('nasty.example.org', $results[1]['host']);
174 $this->assertEquals('', $results[1]['port']);
175 $this->assertEquals('/foo/', $results[1]['path']);
176 $this->assertEquals('xxx', $results[1]['file']);
177 $this->assertEquals('', $results[1]['query']);
178 $this->assertEquals('port443', $results[1]['fragment']);
180 // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
181 $this->assertEquals('sftp', $results[2]['scheme']);
182 $this->assertEquals('', $results[2]['userinfo']);
183 $this->assertEquals('foobar.example.org', $results[2]['host']);
184 $this->assertEquals('80', $results[2]['port']);
185 $this->assertEquals('/', $results[2]['path']);
186 $this->assertEquals('dfsdfs', $results[2]['file']);
187 $this->assertEquals('', $results[2]['query']);
188 $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
190 // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
191 $this->assertEquals('ftp', $results[3]['scheme']);
192 $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
193 $this->assertEquals('10.0.0.1', $results[3]['host']);
194 $this->assertEquals('', $results[3]['port']);
195 $this->assertEquals('/', $results[3]['path']);
196 $this->assertEquals('top_story.htm', $results[3]['file']);
197 $this->assertEquals('', $results[3]['query']);
198 $this->assertEquals('', $results[3]['fragment']);
201 function testFunc_scheme_normalize()
204 foreach($this->setup_string_null() as $key => $value){
205 $this->assertEquals('', scheme_normalize($value), $key);
209 $this->assertEquals('http', scheme_normalize('HTTP'));
212 $this->assertEquals('pop3', scheme_normalize('pop'));
213 $this->assertEquals('nntp', scheme_normalize('news'));
214 $this->assertEquals('imap', scheme_normalize('imap4'));
215 $this->assertEquals('nntps', scheme_normalize('snntp'));
216 $this->assertEquals('nntps', scheme_normalize('snews'));
217 $this->assertEquals('pop3s', scheme_normalize('spop3'));
218 $this->assertEquals('pop3s', scheme_normalize('pops'));
221 $this->assertEquals('http', scheme_normalize('ttp'));
222 $this->assertEquals('https', scheme_normalize('ttps'));
224 // Abbrevs considererd harmless
225 $this->assertEquals('', scheme_normalize('ttp', FALSE));
226 $this->assertEquals('', scheme_normalize('ttps', FALSE));
229 function testFunc_host_normalize()
232 foreach($this->setup_string_null() as $key => $value){
233 $this->assertEquals('', host_normalize($value), $key);
236 // Hostname is case-insensitive
237 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
239 // Cut 'www' (destructive)
240 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
243 function testFunc_port_normalize()
245 $scheme = 'dont_care';
247 // 1st argument: Null
248 $this->assertEquals('', port_normalize(NULL, $scheme));
249 $this->assertEquals('', port_normalize(TRUE, $scheme));
250 $this->assertEquals('', port_normalize(FALSE, $scheme));
251 $this->assertEquals('', port_normalize(array('foobar'), $scheme));
252 $this->assertEquals('', port_normalize('', $scheme));
254 // 1st argument: Known port
255 $this->assertEquals('', port_normalize(-1, $scheme));
256 $this->assertEquals(0, port_normalize(0, $scheme));
257 $this->assertEquals(1, port_normalize(1, $scheme));
258 $this->assertEquals('', port_normalize( 21, 'ftp'));
259 $this->assertEquals('', port_normalize( 22, 'ssh'));
260 $this->assertEquals('', port_normalize( 23, 'telnet'));
261 $this->assertEquals('', port_normalize( 25, 'smtp'));
262 $this->assertEquals('', port_normalize( 69, 'tftp'));
263 $this->assertEquals('', port_normalize( 70, 'gopher'));
264 $this->assertEquals('', port_normalize( 79, 'finger'));
265 $this->assertEquals('', port_normalize( 80, 'http'));
266 $this->assertEquals('', port_normalize( 110, 'pop3'));
267 $this->assertEquals('', port_normalize( 115, 'sftp'));
268 $this->assertEquals('', port_normalize( 119, 'nntp'));
269 $this->assertEquals('', port_normalize( 143, 'imap'));
270 $this->assertEquals('', port_normalize( 194, 'irc'));
271 $this->assertEquals('', port_normalize( 210, 'wais'));
272 $this->assertEquals('', port_normalize( 443, 'https'));
273 $this->assertEquals('', port_normalize( 563, 'nntps'));
274 $this->assertEquals('', port_normalize( 873, 'rsync'));
275 $this->assertEquals('', port_normalize( 990, 'ftps'));
276 $this->assertEquals('', port_normalize( 992, 'telnets'));
277 $this->assertEquals('', port_normalize( 993, 'imaps'));
278 $this->assertEquals('', port_normalize( 994, 'ircs'));
279 $this->assertEquals('', port_normalize( 995, 'pop3s'));
280 $this->assertEquals('', port_normalize(3306, 'mysql'));
281 $this->assertEquals(8080, port_normalize( 8080, $scheme));
282 $this->assertEquals(65535, port_normalize(65535, $scheme));
283 $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
285 // 1st argument: Invalid type
286 $this->assertEquals('1x', port_normalize('001', $scheme) . 'x');
287 $this->assertEquals('', port_normalize('+0', $scheme));
288 $this->assertEquals('', port_normalize('0-1', $scheme)); // intval() says '0'
289 $this->assertEquals('', port_normalize('str', $scheme));
291 // 2nd and 3rd argument: Null
292 $this->assertEquals(80, port_normalize(80, NULL, TRUE));
293 $this->assertEquals(80, port_normalize(80, TRUE, TRUE));
294 $this->assertEquals(80, port_normalize(80, FALSE, TRUE));
295 $this->assertEquals(80, port_normalize(80, array('foobar'), TRUE));
296 $this->assertEquals(80, port_normalize(80, '', TRUE));
298 // 2nd and 3rd argument: Do $scheme_normalize
299 $this->assertEquals('', port_normalize(80, 'TTP', TRUE));
300 $this->assertEquals('', port_normalize(110, 'POP', TRUE));
301 $this->assertEquals(80, port_normalize(80, 'HTTP', FALSE));
304 function testFunc_path_normalize()
306 // 1st argument: Null
307 foreach($this->setup_string_null() as $key => $value){
308 $this->assertEquals('/', path_normalize($value), $key);
311 // 1st argument: CASE sensitive
312 $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
313 $this->assertEquals('/#hoge', path_normalize('#hoge'));
314 $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
315 $this->assertEquals('/b/', path_normalize('/a/../../../b/'));
318 $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
319 $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
320 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
321 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
322 $this->assertEquals('', path_normalize(array('a'), array('b')));
325 function testFunc_file_normalize()
327 // 1st argument: Null
328 foreach($this->setup_string_null() as $key => $value){
329 $this->assertEquals('', file_normalize($value), $key);
332 // 1st argument: Cut DirectoryIndexes (Destructive)
352 // Apache 2.0.59 default 'index.html' variants
354 'index.html.cz.iso8859-2',
363 'index.html.he.iso8859-8',
364 'index.html.hr.iso8859-2',
366 'index.html.ja.iso2022-jp',
367 'index.html.ko.euc-kr',
368 'index.html.lb.utf8',
372 'index.html.po.iso8859-2',
375 'index.html.ru.cp866',
376 'index.html.ru.cp-1251',
377 'index.html.ru.iso-ru',
378 'index.html.ru.koi8-r',
379 'index.html.ru.utf8',
381 'index.html.var', // default
382 'index.html.zh-cn.gb2312',
383 'index.html.zh-tw.big5',
385 'index.html.po.iso8859-2',
386 'index.html.zh-tw.big5',
388 'index.ja.en.de.html',
392 'index.html.en.ja.ca.z',
394 $this->assertEquals('', file_normalize($arg));
397 //$this->assertEquals('foo/', file_normalize('foo/index.html'));
399 //$this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
400 //$this->assertEquals('exe.exe', file_normalize('exe.exe'));
401 //$this->assertEquals('sample.html', file_normalize('sample.html.en'));
402 //$this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
403 //$this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
404 //$this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
407 function testFunc_query_normalize()
409 // 1st argument: Null
410 foreach($this->setup_string_null() as $key => $value){
411 $this->assertEquals('', query_normalize($value), $key);
414 $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
415 $this->assertEquals('eg=foobar', query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
418 function testFunc_generate_glob_regex()
420 // 1st argument: Null
421 foreach($this->setup_string_null() as $key => $value){
422 $this->assertEquals('', generate_glob_regex($value), $key);
425 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
426 $this->assertEquals('A.A', generate_glob_regex('A?A'));
429 function testFunc_generate_host_regex()
431 // 1st argument: Null
432 foreach($this->setup_string_null() as $key => $value){
433 $this->assertEquals('', generate_host_regex($value), $key);
436 $this->assertEquals('localhost', generate_host_regex('localhost'));
437 $this->assertEquals('example\.org', generate_host_regex('example.org'));
438 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
439 $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org'));
440 $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org'));
441 $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40'));
443 // Should match with 192.168.0.0/16
444 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
447 function testFunc_get_blocklist()
449 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
450 $this->fail('SPAM_INI_FILE not defined or not found');
454 // get_blocklist_add()
457 get_blocklist_add($array, 'foo', 'bar');
458 $this->assertEquals(1, count($array));
459 $this->assertEquals('bar', $array['foo']);
461 get_blocklist_add($array, 'hoge', 'fuga');
462 $this->assertEquals(2, count($array));
463 $this->assertEquals('bar', $array['foo']);
464 $this->assertEquals('fuga', $array['hoge']);
466 get_blocklist_add($array, -1, '*.txt');
467 $this->assertEquals(3, count($array));
468 $this->assertEquals('bar', $array['foo']);
469 $this->assertEquals('fuga', $array['hoge']);
470 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
474 $array = get_blocklist();
475 $this->assertTrue(isset($array['C']));
476 $this->assertTrue(isset($array['goodhost']));
478 $array = get_blocklist('B-1');
479 $this->assertTrue(isset($array['*.blogspot.com']));
481 $array = get_blocklist('goodhost');
482 $this->assertTrue(isset($array['IANA-examples']));
485 function testFunc_is_badhost()
488 $this->assertEquals(FALSE, is_badhost(array(), FALSE, TRUE));
489 $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
492 $this->assertEquals(TRUE, is_badhost('=.blogspot.com', FALSE, TRUE));
496 '*.blogspot.com' => array(
501 is_badhost('=.blogspot.com', FALSE, FALSE)