OSDN Git Service

df98e9513cad416ed34ce22dfd392a03175f4f6d
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
1 <?php
2 // $Id: SpamTest.php,v 1.8 2007/05/04 14:54:21 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_array_count_leaves()
28         {
29                 // Empty array = 0, if option is not set
30                 $array = array();
31                 $this->assertEquals(0, array_count_leaves($array, FALSE));
32                 $this->assertEquals(1, array_count_leaves($array, TRUE));
33                 $array = array(
34                         array(
35                                 array()
36                         )
37                 );
38                 $this->assertEquals(0, array_count_leaves($array, FALSE));
39                 $this->assertEquals(1, array_count_leaves($array, TRUE));
40
41                 // One leaf = 1
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));
45                 }
46
47                 // Compisite
48                 $array = array(
49                         1,
50                         'v1',
51                         array(),        // Empty array
52                         array(
53                                 2,
54                                 'v2',
55                                 'k1' => TRUE,
56                                 'k2' => FALSE,
57                                 'k3' => array(),        // Empty array
58                                 'k4' => array(
59                                         3,
60                                         'v3',
61                                         'k5' => NULL,
62                                         'k6' => array(),        // Empty array
63                                 ),
64                         ),
65                         'k7'  => 4,
66                         'k8'  => 'v4',
67                         'k9'  => array(),       // Empty array
68                         'k10' => array(
69                                 5,
70                                 'v5',
71                                 'k11' => NULL,
72                                 'k12' => array(),       // Empty array
73                         ),
74                 );
75                 $this->assertEquals(14, array_count_leaves($array, FALSE));
76                 $this->assertEquals(19, array_count_leaves($array, TRUE));
77         }
78
79         function testFunc_array_merge_leaves()
80         {
81                 $array1 = array(2);
82                 $array2 = array(1);
83                 $result = array(2, 1);
84                 $this->assertEquals($result, array_merge_leaves($array1, $array2));
85
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));
91
92                 // One more thing ...
93                 $array1 = array('20' => 'f5');
94                 $array2 = array();
95                 $result = array('f5');
96                 $this->assertEquals($result, array_merge_leaves($array1, $array2));
97
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));
103
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));
109
110                 // Combination
111                 $array1 = array(
112                         2,
113                         'a' => 'f1',
114                         '10' => 'f3',
115                         '20' => 'f5',
116                         'b' => array('k1'),
117                 );
118                 $array2 = array(
119                         1,
120                         'a' => 'f2',
121                         '10' => 'f4',
122                         'b' => 'k2',
123                 );
124                 $result = array (
125                         2,
126                         'a' => array (
127                                 'f1',
128                                 'f2',
129                         ),
130                         'f3',
131                         'f5',
132                         'b' => array (
133                                 'k1',
134                                 'k2',
135                         ),
136                         1,
137                         'f4',
138                 );
139                 $this->assertEquals($result, array_merge_leaves($array1, $array2));
140
141                 // ----
142
143                 // Values will not be unique now
144                 $array1 = array(5, 4);
145                 $array2 = array(4, 5);
146                 $result = array(5, 4, 4, 5);
147                 $this->assertEquals($result, array_merge_leaves($array1, $array2));
148
149                 // One more thing ...
150                 $array1 = array('b' => array('k3'));
151                 $array2 = array('b' => 'k3');
152                 $result = array('b' => array('k3', 'k3'));
153                 $this->assertEquals($result, array_merge_leaves($array1, $array2));
154         }
155
156         function testFunc_uri_pickup()
157         {
158                 // 1st argument: Null
159                 foreach($this->setup_string_null() as $key => $value){
160                         $this->assertEquals(0, count(uri_pickup($value)), $key);
161                 }
162
163                 // 1st argument: Some
164                 $test_string = <<<EOF
165                         TTP://wwW.Example.Org#TTP_and_www
166                         https://nasty.example.org:443/foo/xxx#port443/slash
167                         sftp://foobar.example.org:80/dfsdfs#ftp_bat_port80
168                         ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
169                         http://192.168.1.4:443#IPv4
170 EOF;
171                 $results = uri_pickup_normalize(uri_pickup($test_string));
172                 $this->assertEquals(5, count($results));
173
174                 // ttp://wwW.Example.Org:80#TTP_and_www
175                 $this->assertEquals('http',           $results[0]['scheme']);
176                 $this->assertEquals('',               $results[0]['userinfo']);
177                 $this->assertEquals('example.org',    $results[0]['host']);
178                 $this->assertEquals('',               $results[0]['port']);
179                 $this->assertEquals('/',              $results[0]['path']);
180                 $this->assertEquals('',               $results[0]['file']);
181                 $this->assertEquals('',               $results[0]['query']);
182                 $this->assertEquals('ttp_and_www',    $results[0]['fragment']);
183
184                 // https://nasty.example.org:443/foo/xxx#port443/slash
185                 $this->assertEquals('https',          $results[1]['scheme']);
186                 $this->assertEquals('',               $results[1]['userinfo']);
187                 $this->assertEquals('nasty.example.org', $results[1]['host']);
188                 $this->assertEquals('',               $results[1]['port']);
189                 $this->assertEquals('/foo/',          $results[1]['path']);
190                 $this->assertEquals('xxx',            $results[1]['file']);
191                 $this->assertEquals('',               $results[1]['query']);
192                 $this->assertEquals('port443',        $results[1]['fragment']);
193
194                 // sftp://foobar.example.org:80/dfsdfs#sftp_bat_port80
195                 $this->assertEquals('sftp',           $results[2]['scheme']);
196                 $this->assertEquals('',               $results[2]['userinfo']);
197                 $this->assertEquals('foobar.example.org', $results[2]['host']);
198                 $this->assertEquals('80',             $results[2]['port']);
199                 $this->assertEquals('/',              $results[2]['path']);
200                 $this->assertEquals('dfsdfs',         $results[2]['file']);
201                 $this->assertEquals('',               $results[2]['query']);
202                 $this->assertEquals('ftp_bat_port80', $results[2]['fragment']);
203
204                 // ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm
205                 $this->assertEquals('ftp',            $results[3]['scheme']);
206                 $this->assertEquals('cnn.example.com&story=breaking_news', $results[3]['userinfo']);
207                 $this->assertEquals('10.0.0.1',       $results[3]['host']);
208                 $this->assertEquals('',               $results[3]['port']);
209                 $this->assertEquals('/',              $results[3]['path']);
210                 $this->assertEquals('top_story.htm',  $results[3]['file']);
211                 $this->assertEquals('',               $results[3]['query']);
212                 $this->assertEquals('',               $results[3]['fragment']);
213         }
214
215         function testFunc_scheme_normalize()
216         {
217                 // Null
218                 foreach($this->setup_string_null() as $key => $value){
219                         $this->assertEquals('', scheme_normalize($value), $key);
220                 }
221
222                 // CASE
223                 $this->assertEquals('http', scheme_normalize('HTTP'));
224
225                 // Aliases
226                 $this->assertEquals('pop3',  scheme_normalize('pop'));
227                 $this->assertEquals('nntp',  scheme_normalize('news'));
228                 $this->assertEquals('imap',  scheme_normalize('imap4'));
229                 $this->assertEquals('nntps', scheme_normalize('snntp'));
230                 $this->assertEquals('nntps', scheme_normalize('snews'));
231                 $this->assertEquals('pop3s', scheme_normalize('spop3'));
232                 $this->assertEquals('pop3s', scheme_normalize('pops'));
233                 
234                 // Abbrevs
235                 $this->assertEquals('http',  scheme_normalize('ttp'));
236                 $this->assertEquals('https', scheme_normalize('ttps'));
237
238                 // Abbrevs considererd harmless
239                 $this->assertEquals('', scheme_normalize('ttp',  FALSE));
240                 $this->assertEquals('', scheme_normalize('ttps', FALSE));
241         }
242
243         function testFunc_host_normalize()
244         {
245                 // Null
246                 foreach($this->setup_string_null() as $key => $value){
247                         $this->assertEquals('', host_normalize($value), $key);
248                 }
249
250                 // Hostname is case-insensitive
251                 $this->assertEquals('example.org', host_normalize('ExAMPle.ORG'));
252
253                 // Cut 'www' (destructive)
254                 $this->assertEquals('example.org', host_normalize('WWW.example.org'));
255         }
256
257         function testFunc_port_normalize()
258         {
259                 $scheme = 'dont_care';
260
261                 // 1st argument: Null
262                 $this->assertEquals('', port_normalize(NULL, $scheme));
263                 $this->assertEquals('', port_normalize(TRUE, $scheme));
264                 $this->assertEquals('', port_normalize(FALSE, $scheme));
265                 $this->assertEquals('', port_normalize(array('foobar'), $scheme));
266                 $this->assertEquals('', port_normalize('',   $scheme));
267
268                 // 1st argument: Known port
269                 $this->assertEquals('',    port_normalize(-1,   $scheme));
270                 $this->assertEquals(0,     port_normalize(0,    $scheme));
271                 $this->assertEquals(1,     port_normalize(1,    $scheme));
272                 $this->assertEquals('',    port_normalize(  21, 'ftp'));
273                 $this->assertEquals('',    port_normalize(  22, 'ssh'));
274                 $this->assertEquals('',    port_normalize(  23, 'telnet'));
275                 $this->assertEquals('',    port_normalize(  25, 'smtp'));
276                 $this->assertEquals('',    port_normalize(  69, 'tftp'));
277                 $this->assertEquals('',    port_normalize(  70, 'gopher'));
278                 $this->assertEquals('',    port_normalize(  79, 'finger'));
279                 $this->assertEquals('',    port_normalize(  80, 'http'));
280                 $this->assertEquals('',    port_normalize( 110, 'pop3'));
281                 $this->assertEquals('',    port_normalize( 115, 'sftp'));
282                 $this->assertEquals('',    port_normalize( 119, 'nntp'));
283                 $this->assertEquals('',    port_normalize( 143, 'imap'));
284                 $this->assertEquals('',    port_normalize( 194, 'irc'));
285                 $this->assertEquals('',    port_normalize( 210, 'wais'));
286                 $this->assertEquals('',    port_normalize( 443, 'https'));
287                 $this->assertEquals('',    port_normalize( 563, 'nntps'));
288                 $this->assertEquals('',    port_normalize( 873, 'rsync'));
289                 $this->assertEquals('',    port_normalize( 990, 'ftps'));
290                 $this->assertEquals('',    port_normalize( 992, 'telnets'));
291                 $this->assertEquals('',    port_normalize( 993, 'imaps'));
292                 $this->assertEquals('',    port_normalize( 994, 'ircs'));
293                 $this->assertEquals('',    port_normalize( 995, 'pop3s'));
294                 $this->assertEquals('',    port_normalize(3306, 'mysql'));
295                 $this->assertEquals(8080,  port_normalize( 8080, $scheme));
296                 $this->assertEquals(65535, port_normalize(65535, $scheme));
297                 $this->assertEquals(65536, port_normalize(65536, $scheme)); // Seems not invalid in RFC
298
299                 // 1st argument: Invalid type
300                 $this->assertEquals('1x',  port_normalize('001', $scheme) . 'x');
301                 $this->assertEquals('',    port_normalize('+0',  $scheme));
302                 $this->assertEquals('',    port_normalize('0-1', $scheme)); // intval() says '0'
303                 $this->assertEquals('',    port_normalize('str', $scheme));
304
305                 // 2nd and 3rd argument: Null
306                 $this->assertEquals(80,    port_normalize(80, NULL,  TRUE));
307                 $this->assertEquals(80,    port_normalize(80, TRUE,  TRUE));
308                 $this->assertEquals(80,    port_normalize(80, FALSE, TRUE));
309                 $this->assertEquals(80,    port_normalize(80, array('foobar'), TRUE));
310                 $this->assertEquals(80,    port_normalize(80, '', TRUE));
311
312                 // 2nd and 3rd argument: Do $scheme_normalize
313                 $this->assertEquals('',    port_normalize(80,  'TTP',  TRUE));
314                 $this->assertEquals('',    port_normalize(110, 'POP',  TRUE));
315                 $this->assertEquals(80,    port_normalize(80,  'HTTP', FALSE));
316         }
317
318         function testFunc_path_normalize()
319         {
320                 // 1st argument: Null
321                 foreach($this->setup_string_null() as $key => $value){
322                         $this->assertEquals('/', path_normalize($value), $key);
323                 }
324
325                 // 1st argument: CASE sensitive
326                 $this->assertEquals('/ExAMPle', path_normalize('ExAMPle'));
327                 $this->assertEquals('/#hoge',   path_normalize('#hoge'));
328                 $this->assertEquals('/a/b/c/d', path_normalize('/a/b/./c////./d'));
329                 $this->assertEquals('/b/',      path_normalize('/a/../../../b/'));
330
331                 // 2nd argument
332                 $this->assertEquals('\\b\\c\\d\\', path_normalize('\\a\\..\\b\\.\\c\\\\.\\d\\', '\\'));
333                 $this->assertEquals('str1str3str', path_normalize('str1strstr2str..str3str', 'str'));
334                 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', TRUE));
335                 $this->assertEquals('/do/../nothing/', path_normalize('/do/../nothing/', array('a')));
336                 $this->assertEquals('',            path_normalize(array('a'), array('b')));
337         }
338
339         function testFunc_file_normalize()
340         {
341                 // 1st argument: Null
342                 foreach($this->setup_string_null() as $key => $value){
343                         $this->assertEquals('', file_normalize($value), $key);
344                 }
345
346                 // 1st argument: Cut DirectoryIndexes (Destructive)
347                 foreach(array(
348                         'default.htm',
349                         'default.html',
350                         'default.asp',
351                         'default.aspx',
352 \r                       'index',
353                         'index.htm',
354                         'index.html',
355                         'index.shtml',
356                         'index.jsp',
357                         'index.php',
358                         'index.php',
359                         'index.php3',
360                         'index.php4',
361                         'index.pl',
362                         'index.py',
363                         'index.rb',
364                         'index.cgi',
365
366                         // Apache 2.0.59 default 'index.html' variants
367                         'index.html.ca',
368                         'index.html.cz.iso8859-2',
369                         'index.html.de',
370                         'index.html.dk',
371                         'index.html.ee',
372                         'index.html.el',
373                         'index.html.en',
374                         'index.html.es',
375                         'index.html.et',
376                         'index.html.fr',
377                         'index.html.he.iso8859-8',
378                         'index.html.hr.iso8859-2',
379                         'index.html.it',
380                         'index.html.ja.iso2022-jp',
381                         'index.html.ko.euc-kr',
382                         'index.html.lb.utf8',
383                         'index.html.nl',
384                         'index.html.nn',
385                         'index.html.no',
386                         'index.html.po.iso8859-2',
387                         'index.html.pt',
388                         'index.html.pt-br',
389                         'index.html.ru.cp866',
390                         'index.html.ru.cp-1251',
391                         'index.html.ru.iso-ru',
392                         'index.html.ru.koi8-r',
393                         'index.html.ru.utf8',
394                         'index.html.sv',
395                         'index.html.var',       // default
396                         'index.html.zh-cn.gb2312',
397                         'index.html.zh-tw.big5',
398
399                         'index.html.po.iso8859-2',
400                         'index.html.zh-tw.big5',
401
402                         'index.ja.en.de.html',
403                 
404                         // .gz
405                         'index.html.ca.gz',
406                         'index.html.en.ja.ca.z',
407                 ) as $arg){
408                         $this->assertEquals('', file_normalize($arg));
409                 }
410
411                 //$this->assertEquals('foo/', file_normalize('foo/index.html'));
412
413                 //$this->assertEquals('ExAMPle', file_normalize('ExAMPle'));
414                 //$this->assertEquals('exe.exe', file_normalize('exe.exe'));
415                 //$this->assertEquals('sample.html', file_normalize('sample.html.en'));
416                 //$this->assertEquals('sample.html', file_normalize('sample.html.pt-br'));
417                 //$this->assertEquals('sample.html', file_normalize('sample.html.po.iso8859-2'));
418                 //$this->assertEquals('sample.html', file_normalize('sample.html.zh-tw.big5'));
419         }
420
421         function testFunc_query_normalize()
422         {
423                 // 1st argument: Null
424                 foreach($this->setup_string_null() as $key => $value){
425                         $this->assertEquals('', query_normalize($value), $key);
426                 }
427
428                 $this->assertEquals('a=0dd&b&c&d&f=d', query_normalize('&&&&f=d&b&d&c&a=0dd'));
429                 $this->assertEquals('eg=foobar',       query_normalize('nothing==&eg=dummy&eg=padding&eg=foobar'));
430         }
431
432         function testFunc_generate_glob_regex()
433         {
434                 // 1st argument: Null
435                 foreach($this->setup_string_null() as $key => $value){
436                         $this->assertEquals('', generate_glob_regex($value), $key);
437                 }
438
439                 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
440                 $this->assertEquals('A.A',     generate_glob_regex('A?A'));
441         }
442
443         function testFunc_generate_host_regex()
444         {
445                 // 1st argument: Null
446                 foreach($this->setup_string_null() as $key => $value){
447                         $this->assertEquals('', generate_host_regex($value), $key);
448                 }
449
450                 $this->assertEquals('localhost',             generate_host_regex('localhost'));
451                 $this->assertEquals('example\.org',          generate_host_regex('example.org'));
452                 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
453                 $this->assertEquals('.*\.example\.org',      generate_host_regex('*.example.org'));
454                 $this->assertEquals('.*\..*\.example\.org',  generate_host_regex('*.*.example.org'));
455                 $this->assertEquals('10\.20\.30\.40',        generate_host_regex('10.20.30.40'));
456
457                 // Should match with 192.168.0.0/16
458                 //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
459         }
460
461         function testFunc_get_blocklist()
462         {
463                 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
464                         $this->fail('SPAM_INI_FILE not defined or not found');
465                         return;
466                 }
467
468                 // get_blocklist_add()
469                 $array = array();
470
471                 get_blocklist_add($array,   'foo', 'bar');
472                 $this->assertEquals(1,      count($array));
473                 $this->assertEquals('bar',  $array['foo']);
474
475                 get_blocklist_add($array,   'hoge', 'fuga');
476                 $this->assertEquals(2,      count($array));
477                 $this->assertEquals('bar',  $array['foo']);
478                 $this->assertEquals('fuga', $array['hoge']);
479
480                 get_blocklist_add($array,   -1, '*.txt');
481                 $this->assertEquals(3,      count($array));
482                 $this->assertEquals('bar',  $array['foo']);
483                 $this->assertEquals('fuga', $array['hoge']);
484                 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
485
486                 // get_blocklist()
487                 // ALL
488                 $array = get_blocklist();
489                 $this->assertTrue(isset($array['C']));
490                 $this->assertTrue(isset($array['goodhost']));
491                 // badhost
492                 $array = get_blocklist('B-1');
493                 $this->assertTrue(isset($array['*.blogspot.com']));
494                 // goodhost
495                 $array = get_blocklist('goodhost');
496                 $this->assertTrue(isset($array['IANA-examples']));
497         }
498
499         function testFunc_is_badhost()
500         {
501                 // FALSE (Nothing)
502                 $this->assertEquals(FALSE,   is_badhost(array(), FALSE, TRUE));
503                 $this->assertEquals(array(), is_badhost(array(), FALSE, FALSE));
504
505                 // TRUE
506                 $this->assertEquals(TRUE,    is_badhost('=.blogspot.com', FALSE, TRUE));
507                 $this->assertEquals(
508                         array(
509                                 'B-1' => array(
510                                         '*.blogspot.com' => array(
511                                                 '=.blogspot.com'
512                                         )
513                                 )
514                         ),
515                         is_badhost('=.blogspot.com', FALSE, FALSE)
516                 );
517         }
518 }
519
520 ?>