OSDN Git Service

is_badhost(): becomes just a sample code
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
1 <?php
2 // $Id: SpamTest.php,v 1.6 2007/05/01 05:04:39 henoheno Exp $
3 // Copyright (C) 2007 heno
4 //
5 // Design test case for spam.php (called from runner.php)
6
7 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
8
9 require_once('spam.php');
10 require_once('PHPUnit/PHPUnit.php');
11
12 class SpamTest extends PHPUnit_TestCase
13 {
14         function setup_string_null()
15         {
16                 return array(
17                         '[NULL]'        => NULL,
18                         '[TRUE]'        => TRUE,
19                         '[FALSE]'       => FALSE,
20                         '[array(foobar)]' => array('foobar'),
21                         '[]'            => '',
22                         '[0]'           => 0,
23                         '[1]'           => 1
24                 );
25         }
26
27         function testFunc_uri_pickup()
28         {
29                 // 1st argument: Null
30                 foreach($this->setup_string_null() as $key => $value){
31                         $this->assertEquals(0, count(uri_pickup($value)), $key);
32                 }
33
34                 // 1st argument: Some
35                 $test_string = <<<EOF
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
41 EOF;
42                 $results = uri_pickup_normalize(uri_pickup($test_string));
43                 $this->assertEquals(5, count($results));
44
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']);
54
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']);
64
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']);
74
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']);
84         }
85
86         function testFunc_scheme_normalize()
87         {
88                 // Null
89                 foreach($this->setup_string_null() as $key => $value){
90                         $this->assertEquals('', scheme_normalize($value), $key);
91                 }
92
93                 // CASE
94                 $this->assertEquals('http', scheme_normalize('HTTP'));
95
96                 // Aliases
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'));
104                 
105                 // Abbrevs
106                 $this->assertEquals('http',  scheme_normalize('ttp'));
107                 $this->assertEquals('https', scheme_normalize('ttps'));
108
109                 // Abbrevs considererd harmless
110                 $this->assertEquals('', scheme_normalize('ttp',  FALSE));
111                 $this->assertEquals('', scheme_normalize('ttps', FALSE));
112         }
113
114         function testFunc_host_normalize()
115         {
116                 // Null
117                 foreach($this->setup_string_null() as $key => $value){
118                         $this->assertEquals('', host_normalize($value), $key);
119                 }
120
121                 // Hostname is case-insensitive
122                 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
123
124                 // Cut 'www' (destructive)
125                 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
126         }
127
128         function testFunc_port_normalize()
129         {
130                 $scheme = 'dont_care';
131
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));
138
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
169
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));
175
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));
182
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));
187         }
188
189         function testFunc_path_normalize()
190         {
191                 // 1st argument: Null
192                 foreach($this->setup_string_null() as $key => $value){
193                         $this->assertEquals('/', path_normalize($value), $key);
194                 }
195
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/'));
201
202                 // 2nd argument
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')));
208         }
209
210         function testFunc_file_normalize()
211         {
212                 // 1st argument: Null
213                 foreach($this->setup_string_null() as $key => $value){
214                         $this->assertEquals('', file_normalize($value), $key);
215                 }
216
217                 // 1st argument: Cut DirectoryIndexes (Destructive)
218                 foreach(array(
219                         'default.htm',
220                         'default.html',
221                         'default.asp',
222                         'default.aspx',
223 \r                       'index',
224                         'index.htm',
225                         'index.html',
226                         'index.shtml',
227                         'index.jsp',
228                         'index.php',
229                         'index.php',
230                         'index.php3',
231                         'index.php4',
232                         'index.pl',
233                         'index.py',
234                         'index.rb',
235                         'index.cgi',
236
237                         // Apache 2.0.59 default 'index.html' variants
238                         'index.html.ca',
239                         'index.html.cz.iso8859-2',
240                         'index.html.de',
241                         'index.html.dk',
242                         'index.html.ee',
243                         'index.html.el',
244                         'index.html.en',
245                         'index.html.es',
246                         'index.html.et',
247                         'index.html.fr',
248                         'index.html.he.iso8859-8',
249                         'index.html.hr.iso8859-2',
250                         'index.html.it',
251                         'index.html.ja.iso2022-jp',
252                         'index.html.ko.euc-kr',
253                         'index.html.lb.utf8',
254                         'index.html.nl',
255                         'index.html.nn',
256                         'index.html.no',
257                         'index.html.po.iso8859-2',
258                         'index.html.pt',
259                         'index.html.pt-br',
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',
265                         'index.html.sv',
266                         'index.html.var',       // default
267                         'index.html.zh-cn.gb2312',
268                         'index.html.zh-tw.big5',
269
270                         'index.html.po.iso8859-2',
271                         'index.html.zh-tw.big5',
272
273                         'index.ja.en.de.html',
274                 
275                         // .gz
276                         'index.html.ca.gz',
277                         'index.html.en.ja.ca.z',
278                 ) as $arg){
279                         $this->assertEquals('', file_normalize($arg));
280                 }
281
282                 //$this->assertEquals('foo/', file_normalize('foo/index.html'));
283
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'));
290         }
291
292         function testFunc_query_normalize()
293         {
294                 // 1st argument: Null
295                 foreach($this->setup_string_null() as $key => $value){
296                         $this->assertEquals('', query_normalize($value), $key);
297                 }
298
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'));
301         }
302
303         function testFunc_generate_glob_regex()
304         {
305                 // 1st argument: Null
306                 foreach($this->setup_string_null() as $key => $value){
307                         $this->assertEquals('', generate_glob_regex($value), $key);
308                 }
309
310                 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
311                 $this->assertEquals('A.A',     generate_glob_regex('A?A'));
312         }
313
314         function testFunc_generate_host_regex()
315         {
316                 // 1st argument: Null
317                 foreach($this->setup_string_null() as $key => $value){
318                         $this->assertEquals('', generate_host_regex($value), $key);
319                 }
320
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'));
327
328                 // Should match with 192.168.0.0/16
329                 //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
330         }
331
332         function testFunc_get_blocklist()
333         {
334                 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
335                         $this->fail('SPAM_INI_FILE not defined or not found');
336                         return;
337                 }
338
339                 // get_blocklist_add()
340                 $array = array();
341
342                 get_blocklist_add($array,   'foo', 'bar');
343                 $this->assertEquals(1,      count($array));
344                 $this->assertEquals('bar',  $array['foo']);
345
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']);
350
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']);
356
357                 // get_blocklist()
358                 // ALL
359                 $array = get_blocklist();
360                 $this->assertTrue(isset($array['C']));
361                 $this->assertTrue(isset($array['goodhost']));
362                 // badhost
363                 $array = get_blocklist('B-1');
364                 $this->assertTrue(isset($array['*.blogspot.com']));
365                 // goodhost
366                 $array = get_blocklist('goodhost');
367                 $this->assertTrue(isset($array['IANA-examples']));
368         }
369
370         function testFunc_is_badhost()
371         {
372                 // FALSE (Nothing)
373                 $this->assertEquals(FALSE,   is_badhost(array(), FALSE, TRUE));
374                 $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
375
376                 // TRUE
377                 $this->assertEquals(TRUE,    is_badhost('=.blogspot.com', FALSE, TRUE));
378                 $this->assertEquals(
379                         array(
380                                 'B-1' => array(
381                                         '*.blogspot.com' => array(
382                                                 '=.blogspot.com'
383                                         )
384                                 )
385                         ),
386                         is_badhost('=.blogspot.com', FALSE, FALSE)
387                 );
388         }
389 }
390
391 ?>