2 // $Id: SpamTest.php,v 1.1 2007/04/12 14:37:57 henoheno Exp $
3 // Copyright (C) 2007 heno
5 // Design test case for spam.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 testFunc_uri_pickup()
17 $this->assertEquals(0, count(uri_pickup(NULL)));
18 $this->assertEquals(0, count(uri_pickup(TRUE)));
19 $this->assertEquals(0, count(uri_pickup(FALSE)));
20 $this->assertEquals(0, count(uri_pickup(array('foobar'))));
21 $this->assertEquals(0, count(uri_pickup('')));
22 $this->assertEquals(0, count(uri_pickup(0)));
26 TTP://wwW.Example.Org#TTP_and_www
27 https://nasty.example.org:443/foo/xxx#port443/slash
28 sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
29 ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
30 http://192.168.1.4:443#IPv4
32 $results = uri_pickup_normalize(uri_pickup($test_string));
33 $this->assertEquals(5, count($results));
35 // ttp://wwW.Example.Org:80#TTP_and_www
36 $this->assertEquals('http', $results[0]['scheme']);
37 $this->assertEquals('', $results[0]['userinfo']);
38 $this->assertEquals('example.org', $results[0]['host']);
39 $this->assertEquals('', $results[0]['port']);
40 $this->assertEquals('/', $results[0]['path']);
41 $this->assertEquals('', $results[0]['file']);
42 $this->assertEquals('', $results[0]['query']);
43 $this->assertEquals('ttp_and_www', $results[0]['fragment']);
45 // https://nasty.example.org:443/foo/xxx#port443/slash
46 $this->assertEquals('https', $results[1]['scheme']);
47 $this->assertEquals('', $results[1]['userinfo']);
48 $this->assertEquals('nasty.example.org', $results[1]['host']);
49 $this->assertEquals('', $results[1]['port']);
50 $this->assertEquals('/foo/', $results[1]['path']);
51 $this->assertEquals('xxx', $results[1]['file']);
52 $this->assertEquals('', $results[1]['query']);
53 $this->assertEquals('port443', $results[1]['fragment']);
55 // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
56 $this->assertEquals('sftp', $results[2]['scheme']);
57 $this->assertEquals('', $results[2]['userinfo']);
58 $this->assertEquals('foobar.example.org', $results[2]['host']);
59 $this->assertEquals('80', $results[2]['port']);
60 $this->assertEquals('/', $results[2]['path']);
61 $this->assertEquals('dfsdfs', $results[2]['file']);
62 $this->assertEquals('', $results[2]['query']);
63 $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
65 // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
66 $this->assertEquals('ftp', $results[3]['scheme']);
67 $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
68 $this->assertEquals('10.0.0.1', $results[3]['host']);
69 $this->assertEquals('', $results[3]['port']);
70 $this->assertEquals('/', $results[3]['path']);
71 $this->assertEquals('top_story.htm', $results[3]['file']);
72 $this->assertEquals('', $results[3]['query']);
73 $this->assertEquals('', $results[3]['fragment']);
76 function testFunc_scheme_normalize()
79 $this->assertEquals('', scheme_normalize(NULL));
80 $this->assertEquals('', scheme_normalize(TRUE));
81 $this->assertEquals('', scheme_normalize(FALSE));
82 $this->assertEquals('', scheme_normalize(array('foobar')));
83 $this->assertEquals('', scheme_normalize(''));
84 $this->assertEquals('', scheme_normalize(0));
85 $this->assertEquals('', scheme_normalize(1));
88 $this->assertEquals('http', scheme_normalize('HTTP'));
91 $this->assertEquals('pop3', scheme_normalize('pop'));
92 $this->assertEquals('nntp', scheme_normalize('news'));
93 $this->assertEquals('imap', scheme_normalize('imap4'));
94 $this->assertEquals('nntps', scheme_normalize('snntp'));
95 $this->assertEquals('nntps', scheme_normalize('snews'));
96 $this->assertEquals('pop3s', scheme_normalize('spop3'));
97 $this->assertEquals('pop3s', scheme_normalize('pops'));
100 $this->assertEquals('http', scheme_normalize('ttp'));
101 $this->assertEquals('https', scheme_normalize('ttps'));
103 // Abbrevs considererd harmless
104 $this->assertEquals('', scheme_normalize('ttp', FALSE));
105 $this->assertEquals('', scheme_normalize('ttps', FALSE));
108 function testFunc_host_normalize()
111 $this->assertEquals('', host_normalize(NULL));
112 $this->assertEquals('', host_normalize(TRUE));
113 $this->assertEquals('', host_normalize(FALSE));
114 $this->assertEquals('', host_normalize(array('foobar')));
115 $this->assertEquals('', host_normalize(''));
116 $this->assertEquals('', host_normalize(0));
117 $this->assertEquals('', host_normalize(1));
119 // Hostname is case-insensitive
120 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
122 // Cut 'www' (destructive)
123 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
126 function testFunc_port_normalize()
128 $scheme = 'dont_care';
130 // 1st argument: Null
131 $this->assertEquals('', port_normalize(NULL, $scheme));
132 $this->assertEquals('', port_normalize(TRUE, $scheme));
133 $this->assertEquals('', port_normalize(FALSE, $scheme));
134 $this->assertEquals('', port_normalize(array('foobar'), $scheme));
135 $this->assertEquals('', port_normalize('', $scheme));
137 // 1st argument: Known port
138 $this->assertEquals('', port_normalize(-1, $scheme));
139 $this->assertEquals(0, port_normalize(0, $scheme));
140 $this->assertEquals(1, port_normalize(1, $scheme));
141 $this->assertEquals('', port_normalize( 21, 'ftp'));
142 $this->assertEquals('', port_normalize( 22, 'ssh'));
143 $this->assertEquals('', port_normalize( 23, 'telnet'));
144 $this->assertEquals('', port_normalize( 25, 'smtp'));
145 $this->assertEquals('', port_normalize( 69, 'tftp'));
146 $this->assertEquals('', port_normalize( 70, 'gopher'));
147 $this->assertEquals('', port_normalize( 79, 'finger'));
148 $this->assertEquals('', port_normalize( 80, 'http'));
149 $this->assertEquals('', port_normalize( 110, 'pop3'));
150 $this->assertEquals('', port_normalize( 115, 'sftp'));
151 $this->assertEquals('', port_normalize( 119, 'nntp'));
152 $this->assertEquals('', port_normalize( 143, 'imap'));
153 $this->assertEquals('', port_normalize( 194, 'irc'));
154 $this->assertEquals('', port_normalize( 210, 'wais'));
155 $this->assertEquals('', port_normalize( 443, 'https'));
156 $this->assertEquals('', port_normalize( 563, 'nntps'));
157 $this->assertEquals('', port_normalize( 873, 'rsync'));
158 $this->assertEquals('', port_normalize( 990, 'ftps'));
159 $this->assertEquals('', port_normalize( 992, 'telnets'));
160 $this->assertEquals('', port_normalize( 993, 'imaps'));
161 $this->assertEquals('', port_normalize( 994, 'ircs'));
162 $this->assertEquals('', port_normalize( 995, 'pop3s'));
163 $this->assertEquals('', port_normalize(3306, 'mysql'));
164 $this->assertEquals(8080, port_normalize( 8080, $scheme));
165 $this->assertEquals(65535, port_normalize(65535, $scheme));
166 $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
168 // 1st argument: Invalid type
169 $this->assertEquals('1x', port_normalize('001', $scheme) . 'x');
170 $this->assertEquals('', port_normalize('+0', $scheme));
171 $this->assertEquals('', port_normalize('0-1', $scheme)); // intval() says '0'
172 $this->assertEquals('', port_normalize('str', $scheme));
174 // 2nd and 3rd argument: Null
175 $this->assertEquals(80, port_normalize(80, NULL, TRUE));
176 $this->assertEquals(80, port_normalize(80, TRUE, TRUE));
177 $this->assertEquals(80, port_normalize(80, FALSE, TRUE));
178 $this->assertEquals(80, port_normalize(80, array('foobar'), TRUE));
179 $this->assertEquals(80, port_normalize(80, '', TRUE));
181 // 2nd and 3rd argument: Do $scheme_normalize
182 $this->assertEquals('', port_normalize(80, 'TTP', TRUE));
183 $this->assertEquals('', port_normalize(110, 'POP', TRUE));
184 $this->assertEquals(80, port_normalize(80, 'HTTP', FALSE));
187 function testFunc_path_normalize()
189 // 1st argument: Null
190 $this->assertEquals('/', path_normalize(NULL));
191 $this->assertEquals('/', path_normalize(TRUE));
192 $this->assertEquals('/', path_normalize(FALSE));
193 $this->assertEquals('/', path_normalize(array('foobar')));
194 $this->assertEquals('/', path_normalize(''));
195 $this->assertEquals('/', path_normalize(0));
196 $this->assertEquals('/', path_normalize(1));
198 // 1st argument: CASE sensitive
199 $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
200 $this->assertEquals('/#hoge', path_normalize('#hoge'));
201 $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
202 $this->assertEquals('/b/', path_normalize('/a/../../../b/'));
205 $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
206 $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
207 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
208 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
209 $this->assertEquals('', path_normalize(array('a'), array('b')));
212 function testFunc_file_normalize()
214 // 1st argument: Null
215 $this->assertEquals('', file_normalize(NULL));
216 $this->assertEquals('', file_normalize(TRUE));
217 $this->assertEquals('', file_normalize(FALSE));
218 $this->assertEquals('', file_normalize(array('foobar')));
219 $this->assertEquals('', file_normalize(''));
220 $this->assertEquals('', file_normalize(0));
221 $this->assertEquals('', file_normalize(1));
223 // 1st argument: Cut DirectoryIndexes (Destructive)
224 $this->assertEquals('', file_normalize('default.htm'));
225 $this->assertEquals('', file_normalize('default.html'));
226 $this->assertEquals('', file_normalize('default.asp'));
227 $this->assertEquals('', file_normalize('default.aspx'));
228 \r $this->assertEquals('', file_normalize('index'));
229 $this->assertEquals('', file_normalize('index.htm'));
230 $this->assertEquals('', file_normalize('index.html'));
231 $this->assertEquals('', file_normalize('index.shtml'));
232 $this->assertEquals('', file_normalize('index.jsp'));
233 $this->assertEquals('', file_normalize('index.php'));
234 $this->assertEquals('', file_normalize('index.php'));
235 $this->assertEquals('', file_normalize('index.php3'));
236 $this->assertEquals('', file_normalize('index.php4'));
237 $this->assertEquals('', file_normalize('index.pl'));
238 $this->assertEquals('', file_normalize('index.py'));
239 $this->assertEquals('', file_normalize('index.rb'));
240 $this->assertEquals('', file_normalize('index.cgi'));
242 // Apache 2.0.59 default 'index.html' variants
243 $this->assertEquals('', file_normalize('index.html.ca'));
244 $this->assertEquals('', file_normalize('index.html.cz.iso8859-2'));
245 $this->assertEquals('', file_normalize('index.html.de'));
246 $this->assertEquals('', file_normalize('index.html.dk'));
247 $this->assertEquals('', file_normalize('index.html.ee'));
248 $this->assertEquals('', file_normalize('index.html.el'));
249 $this->assertEquals('', file_normalize('index.html.en'));
250 $this->assertEquals('', file_normalize('index.html.es'));
251 $this->assertEquals('', file_normalize('index.html.et'));
252 $this->assertEquals('', file_normalize('index.html.fr'));
253 $this->assertEquals('', file_normalize('index.html.he.iso8859-8'));
254 $this->assertEquals('', file_normalize('index.html.hr.iso8859-2'));
255 $this->assertEquals('', file_normalize('index.html.it'));
256 $this->assertEquals('', file_normalize('index.html.ja.iso2022-jp'));
257 $this->assertEquals('', file_normalize('index.html.ko.euc-kr'));
258 $this->assertEquals('', file_normalize('index.html.lb.utf8'));
259 $this->assertEquals('', file_normalize('index.html.nl'));
260 $this->assertEquals('', file_normalize('index.html.nn'));
261 $this->assertEquals('', file_normalize('index.html.no'));
262 $this->assertEquals('', file_normalize('index.html.po.iso8859-2'));
263 $this->assertEquals('', file_normalize('index.html.pt'));
264 $this->assertEquals('', file_normalize('index.html.pt-br'));
265 $this->assertEquals('', file_normalize('index.html.ru.cp866'));
266 $this->assertEquals('', file_normalize('index.html.ru.cp-1251'));
267 $this->assertEquals('', file_normalize('index.html.ru.iso-ru'));
268 $this->assertEquals('', file_normalize('index.html.ru.koi8-r'));
269 $this->assertEquals('', file_normalize('index.html.ru.utf8'));
270 $this->assertEquals('', file_normalize('index.html.sv'));
271 $this->assertEquals('', file_normalize('index.html.var')); // default
272 $this->assertEquals('', file_normalize('index.html.zh-cn.gb2312'));
273 $this->assertEquals('', file_normalize('index.html.zh-tw.big5'));
275 $this->assertEquals('', file_normalize('index.html.po.iso8859-2'));
276 $this->assertEquals('', file_normalize('index.html.zh-tw.big5'));
278 $this->assertEquals('', file_normalize('index.ja.en.de.html'));
281 $this->assertEquals('', file_normalize('index.html.ca.gz'));
282 $this->assertEquals('', file_normalize('index.html.en.ja.ca.z'));
284 // $this->assertEquals('foo/', file_normalize('foo/index.html'));
286 // $this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
287 // $this->assertEquals('exe.exe', file_normalize('exe.exe'));
288 // $this->assertEquals('sample.html', file_normalize('sample.html.en'));
289 // $this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
290 // $this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
291 // $this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
294 function testFunc_query_normalize()
296 // 1st argument: Null
297 $this->assertEquals('', query_normalize(NULL));
298 $this->assertEquals('', query_normalize(TRUE));
299 $this->assertEquals('', query_normalize(FALSE));
300 $this->assertEquals('', query_normalize(array('foobar')));
301 $this->assertEquals('', query_normalize(''));
302 $this->assertEquals('', query_normalize(0));
303 $this->assertEquals('', query_normalize(1));
305 $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
306 $this->assertEquals('eg=foobar', query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
309 function testFunc_generate_glob_regex()
311 // 1st argument: Null
312 $this->assertEquals('', generate_glob_regex(NULL));
313 $this->assertEquals('', generate_glob_regex(TRUE));
314 $this->assertEquals('', generate_glob_regex(FALSE));
315 $this->assertEquals('', generate_glob_regex(array('foobar')));
316 $this->assertEquals('', generate_glob_regex(''));
317 $this->assertEquals('', generate_glob_regex(0));
318 $this->assertEquals('', generate_glob_regex(1));
320 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
321 $this->assertEquals('A.A', generate_glob_regex('A?A'));
324 function testFunc_generate_host_regex()
326 // 1st argument: Null
327 $this->assertEquals('', generate_host_regex(NULL));
328 $this->assertEquals('', generate_host_regex(TRUE));
329 $this->assertEquals('', generate_host_regex(FALSE));
330 $this->assertEquals('', generate_host_regex(array('foobar')));
331 $this->assertEquals('', generate_host_regex(''));
332 $this->assertEquals('', generate_host_regex(0));
333 $this->assertEquals('', generate_host_regex(1));
335 $this->assertEquals('localhost', generate_host_regex('localhost'));
336 $this->assertEquals('example\.org', generate_host_regex('example.org'));
337 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
338 $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org'));
339 $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org'));
340 $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40'));
342 // Should match with 192.168.0.0/16
343 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
346 function testFunc_get_blocklist()
348 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
349 $this->fail('SPAM_INI_FILE not defined or not found');
353 // get_blocklist_add()
356 get_blocklist_add($array, 'foo', 'bar');
357 $this->assertEquals(1, count($array));
358 $this->assertEquals('bar', $array['foo']);
360 get_blocklist_add($array, 'hoge', 'fuga');
361 $this->assertEquals(2, count($array));
362 $this->assertEquals('bar', $array['foo']);
363 $this->assertEquals('fuga', $array['hoge']);
365 get_blocklist_add($array, -1, '*.txt');
366 $this->assertEquals(3, count($array));
367 $this->assertEquals('bar', $array['foo']);
368 $this->assertEquals('fuga', $array['hoge']);
369 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
373 $array = get_blocklist();
374 $this->assertTrue(isset($array['badhost']));
375 $this->assertTrue(isset($array['goodhost']));
377 $array = get_blocklist('badhost');
378 $this->assertTrue(isset($array['*.blogspot.com']));
380 $array = get_blocklist('goodhost');
381 $this->assertTrue(isset($array['IANA-examples']));
384 function testFunc_is_badhost()
386 // is_badhost_avail()
390 $this->assertTrue(is_badhost('something...blogspot.com', TRUE, $remains));