2 // $Id: SpamTest.php,v 1.9 2007/05/05 04:59:31 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 testPhpFunc_array_unique()
81 $this->assertEquals(array(1), array_unique(array(1, 1)));
83 // Keys are preserved, array()s inside are preserved
85 array(0, 2 => array(1, 1), 3 => 2),
87 array(0, 0, array(1, 1), 2, 2)
93 array(0, 2 => array(1, 1), 3 => 2),
94 array_unique(array(0, 0, array(1, 1), 2, 2))
97 // ONLY the first array() is preserved
99 array(0 => array(1, 1)),
100 array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
104 function testFunc_array_merge_leaves()
109 $this->assertEquals($result, array_merge_leaves($array1, $array2));
113 $result = array(2, 1);
114 $this->assertEquals($result, array_merge_leaves($array1, $array2));
116 // All NUMERIC keys are always renumbered from 0
117 $array1 = array('10' => 'f3');
118 $array2 = array('10' => 'f4');
119 $result = array('f3', 'f4');
120 $this->assertEquals($result, array_merge_leaves($array1, $array2));
122 // One more thing ...
123 $array1 = array('20' => 'f5');
125 $result = array('f5');
126 $this->assertEquals($result, array_merge_leaves($array1, $array2));
128 // Non-numeric keys and values will be marged as you think
129 $array1 = array('a' => 'f1');
130 $array2 = array('a' => 'f2');
131 $result = array('a' => array('f1', 'f2'));
132 $this->assertEquals($result, array_merge_leaves($array1, $array2));
134 // Non-numeric keys: An array and a value will be marged
135 $array1 = array('b' => array('k1'));
136 $array2 = array('b' => 'k2');
137 $result = array('b' => array('k1', 'k2'));
138 $this->assertEquals($result, array_merge_leaves($array1, $array2));
169 $this->assertEquals($result, array_merge_leaves($array1, $array2));
171 // Values will not be unique?
172 $array1 = array(5, 4);
173 $array2 = array(4, 5);
174 $result = array(5, 4);
175 $this->assertEquals($result, array_merge_leaves($array1, $array2));
177 // One more thing ...
178 $array1 = array('b' => array('k3'));
179 $array2 = array('b' => 'k3');
180 $result = array('b' => array('k3'));
181 $this->assertEquals($result, array_merge_leaves($array1, $array2));
184 function testFunc_uri_pickup()
186 // 1st argument: Null
187 foreach($this->setup_string_null() as $key => $value){
188 $this->assertEquals(0, count(uri_pickup($value)), $key);
191 // 1st argument: Some
192 $test_string = <<<EOF
193 TTP://wwW.Example.Org#TTP_and_www
194 https://nasty.example.org:443/foo/xxx#port443/slash
195 sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
196 ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
197 http://192.168.1.4:443#IPv4
199 $results = uri_pickup_normalize(uri_pickup($test_string));
200 $this->assertEquals(5, count($results));
202 // ttp://wwW.Example.Org:80#TTP_and_www
203 $this->assertEquals('http', $results[0]['scheme']);
204 $this->assertEquals('', $results[0]['userinfo']);
205 $this->assertEquals('example.org', $results[0]['host']);
206 $this->assertEquals('', $results[0]['port']);
207 $this->assertEquals('/', $results[0]['path']);
208 $this->assertEquals('', $results[0]['file']);
209 $this->assertEquals('', $results[0]['query']);
210 $this->assertEquals('ttp_and_www', $results[0]['fragment']);
212 // https://nasty.example.org:443/foo/xxx#port443/slash
213 $this->assertEquals('https', $results[1]['scheme']);
214 $this->assertEquals('', $results[1]['userinfo']);
215 $this->assertEquals('nasty.example.org', $results[1]['host']);
216 $this->assertEquals('', $results[1]['port']);
217 $this->assertEquals('/foo/', $results[1]['path']);
218 $this->assertEquals('xxx', $results[1]['file']);
219 $this->assertEquals('', $results[1]['query']);
220 $this->assertEquals('port443', $results[1]['fragment']);
222 // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
223 $this->assertEquals('sftp', $results[2]['scheme']);
224 $this->assertEquals('', $results[2]['userinfo']);
225 $this->assertEquals('foobar.example.org', $results[2]['host']);
226 $this->assertEquals('80', $results[2]['port']);
227 $this->assertEquals('/', $results[2]['path']);
228 $this->assertEquals('dfsdfs', $results[2]['file']);
229 $this->assertEquals('', $results[2]['query']);
230 $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
232 // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
233 $this->assertEquals('ftp', $results[3]['scheme']);
234 $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
235 $this->assertEquals('10.0.0.1', $results[3]['host']);
236 $this->assertEquals('', $results[3]['port']);
237 $this->assertEquals('/', $results[3]['path']);
238 $this->assertEquals('top_story.htm', $results[3]['file']);
239 $this->assertEquals('', $results[3]['query']);
240 $this->assertEquals('', $results[3]['fragment']);
243 function testFunc_scheme_normalize()
246 foreach($this->setup_string_null() as $key => $value){
247 $this->assertEquals('', scheme_normalize($value), $key);
251 $this->assertEquals('http', scheme_normalize('HTTP'));
254 $this->assertEquals('pop3', scheme_normalize('pop'));
255 $this->assertEquals('nntp', scheme_normalize('news'));
256 $this->assertEquals('imap', scheme_normalize('imap4'));
257 $this->assertEquals('nntps', scheme_normalize('snntp'));
258 $this->assertEquals('nntps', scheme_normalize('snews'));
259 $this->assertEquals('pop3s', scheme_normalize('spop3'));
260 $this->assertEquals('pop3s', scheme_normalize('pops'));
263 $this->assertEquals('http', scheme_normalize('ttp'));
264 $this->assertEquals('https', scheme_normalize('ttps'));
266 // Abbrevs considererd harmless
267 $this->assertEquals('', scheme_normalize('ttp', FALSE));
268 $this->assertEquals('', scheme_normalize('ttps', FALSE));
271 function testFunc_host_normalize()
274 foreach($this->setup_string_null() as $key => $value){
275 $this->assertEquals('', host_normalize($value), $key);
278 // Hostname is case-insensitive
279 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
281 // Cut 'www' (destructive)
282 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
285 function testFunc_port_normalize()
287 $scheme = 'dont_care';
289 // 1st argument: Null
290 $this->assertEquals('', port_normalize(NULL, $scheme));
291 $this->assertEquals('', port_normalize(TRUE, $scheme));
292 $this->assertEquals('', port_normalize(FALSE, $scheme));
293 $this->assertEquals('', port_normalize(array('foobar'), $scheme));
294 $this->assertEquals('', port_normalize('', $scheme));
296 // 1st argument: Known port
297 $this->assertEquals('', port_normalize(-1, $scheme));
298 $this->assertEquals(0, port_normalize(0, $scheme));
299 $this->assertEquals(1, port_normalize(1, $scheme));
300 $this->assertEquals('', port_normalize( 21, 'ftp'));
301 $this->assertEquals('', port_normalize( 22, 'ssh'));
302 $this->assertEquals('', port_normalize( 23, 'telnet'));
303 $this->assertEquals('', port_normalize( 25, 'smtp'));
304 $this->assertEquals('', port_normalize( 69, 'tftp'));
305 $this->assertEquals('', port_normalize( 70, 'gopher'));
306 $this->assertEquals('', port_normalize( 79, 'finger'));
307 $this->assertEquals('', port_normalize( 80, 'http'));
308 $this->assertEquals('', port_normalize( 110, 'pop3'));
309 $this->assertEquals('', port_normalize( 115, 'sftp'));
310 $this->assertEquals('', port_normalize( 119, 'nntp'));
311 $this->assertEquals('', port_normalize( 143, 'imap'));
312 $this->assertEquals('', port_normalize( 194, 'irc'));
313 $this->assertEquals('', port_normalize( 210, 'wais'));
314 $this->assertEquals('', port_normalize( 443, 'https'));
315 $this->assertEquals('', port_normalize( 563, 'nntps'));
316 $this->assertEquals('', port_normalize( 873, 'rsync'));
317 $this->assertEquals('', port_normalize( 990, 'ftps'));
318 $this->assertEquals('', port_normalize( 992, 'telnets'));
319 $this->assertEquals('', port_normalize( 993, 'imaps'));
320 $this->assertEquals('', port_normalize( 994, 'ircs'));
321 $this->assertEquals('', port_normalize( 995, 'pop3s'));
322 $this->assertEquals('', port_normalize(3306, 'mysql'));
323 $this->assertEquals(8080, port_normalize( 8080, $scheme));
324 $this->assertEquals(65535, port_normalize(65535, $scheme));
325 $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
327 // 1st argument: Invalid type
328 $this->assertEquals('1x', port_normalize('001', $scheme) . 'x');
329 $this->assertEquals('', port_normalize('+0', $scheme));
330 $this->assertEquals('', port_normalize('0-1', $scheme)); // intval() says '0'
331 $this->assertEquals('', port_normalize('str', $scheme));
333 // 2nd and 3rd argument: Null
334 $this->assertEquals(80, port_normalize(80, NULL, TRUE));
335 $this->assertEquals(80, port_normalize(80, TRUE, TRUE));
336 $this->assertEquals(80, port_normalize(80, FALSE, TRUE));
337 $this->assertEquals(80, port_normalize(80, array('foobar'), TRUE));
338 $this->assertEquals(80, port_normalize(80, '', TRUE));
340 // 2nd and 3rd argument: Do $scheme_normalize
341 $this->assertEquals('', port_normalize(80, 'TTP', TRUE));
342 $this->assertEquals('', port_normalize(110, 'POP', TRUE));
343 $this->assertEquals(80, port_normalize(80, 'HTTP', FALSE));
346 function testFunc_path_normalize()
348 // 1st argument: Null
349 foreach($this->setup_string_null() as $key => $value){
350 $this->assertEquals('/', path_normalize($value), $key);
353 // 1st argument: CASE sensitive
354 $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
355 $this->assertEquals('/#hoge', path_normalize('#hoge'));
356 $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
357 $this->assertEquals('/b/', path_normalize('/a/../../../b/'));
360 $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
361 $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
362 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
363 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
364 $this->assertEquals('', path_normalize(array('a'), array('b')));
367 function testFunc_file_normalize()
369 // 1st argument: Null
370 foreach($this->setup_string_null() as $key => $value){
371 $this->assertEquals('', file_normalize($value), $key);
374 // 1st argument: Cut DirectoryIndexes (Destructive)
394 // Apache 2.0.59 default 'index.html' variants
396 'index.html.cz.iso8859-2',
405 'index.html.he.iso8859-8',
406 'index.html.hr.iso8859-2',
408 'index.html.ja.iso2022-jp',
409 'index.html.ko.euc-kr',
410 'index.html.lb.utf8',
414 'index.html.po.iso8859-2',
417 'index.html.ru.cp866',
418 'index.html.ru.cp-1251',
419 'index.html.ru.iso-ru',
420 'index.html.ru.koi8-r',
421 'index.html.ru.utf8',
423 'index.html.var', // default
424 'index.html.zh-cn.gb2312',
425 'index.html.zh-tw.big5',
427 'index.html.po.iso8859-2',
428 'index.html.zh-tw.big5',
430 'index.ja.en.de.html',
434 'index.html.en.ja.ca.z',
436 $this->assertEquals('', file_normalize($arg));
439 //$this->assertEquals('foo/', file_normalize('foo/index.html'));
441 //$this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
442 //$this->assertEquals('exe.exe', file_normalize('exe.exe'));
443 //$this->assertEquals('sample.html', file_normalize('sample.html.en'));
444 //$this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
445 //$this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
446 //$this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
449 function testFunc_query_normalize()
451 // 1st argument: Null
452 foreach($this->setup_string_null() as $key => $value){
453 $this->assertEquals('', query_normalize($value), $key);
456 $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
457 $this->assertEquals('eg=foobar', query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
460 function testFunc_generate_glob_regex()
462 // 1st argument: Null
463 foreach($this->setup_string_null() as $key => $value){
464 $this->assertEquals('', generate_glob_regex($value), $key);
467 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
468 $this->assertEquals('A.A', generate_glob_regex('A?A'));
471 function testFunc_generate_host_regex()
473 // 1st argument: Null
474 foreach($this->setup_string_null() as $key => $value){
475 $this->assertEquals('', generate_host_regex($value), $key);
478 $this->assertEquals('localhost', generate_host_regex('localhost'));
479 $this->assertEquals('example\.org', generate_host_regex('example.org'));
480 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
481 $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org'));
482 $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org'));
483 $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40'));
485 // Should match with 192.168.0.0/16
486 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
489 function testFunc_get_blocklist()
491 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
492 $this->fail('SPAM_INI_FILE not defined or not found');
496 // get_blocklist_add()
499 get_blocklist_add($array, 'foo', 'bar');
500 $this->assertEquals(1, count($array));
501 $this->assertEquals('bar', $array['foo']);
503 get_blocklist_add($array, 'hoge', 'fuga');
504 $this->assertEquals(2, count($array));
505 $this->assertEquals('bar', $array['foo']);
506 $this->assertEquals('fuga', $array['hoge']);
508 get_blocklist_add($array, -1, '*.txt');
509 $this->assertEquals(3, count($array));
510 $this->assertEquals('bar', $array['foo']);
511 $this->assertEquals('fuga', $array['hoge']);
512 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
516 $array = get_blocklist();
517 $this->assertTrue(isset($array['C']));
518 $this->assertTrue(isset($array['goodhost']));
520 $array = get_blocklist('B-1');
521 $this->assertTrue(isset($array['*.blogspot.com']));
523 $array = get_blocklist('goodhost');
524 $this->assertTrue(isset($array['IANA-examples']));
527 function testFunc_is_badhost()
530 $this->assertEquals(FALSE, is_badhost(array(), FALSE, TRUE));
531 $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
534 $this->assertEquals(TRUE, is_badhost('=.blogspot.com', FALSE, TRUE));
538 '*.blogspot.com' => array(
543 is_badhost('=.blogspot.com', FALSE, FALSE)