OSDN Git Service

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