OSDN Git Service

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