OSDN Git Service

145f361315f5ac83cccf0c48f22ff7959cdf3e87
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
1 <?php
2 // $Id: SpamTest.php,v 1.21 2007/08/18 09:10:35 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_strings()
28         {
29                 // 1st argument: Null
30                 $this->assertEquals('',  strings(NULL,  0));
31                 $this->assertEquals('',  strings(TRUE,  0));
32                 $this->assertEquals('',  strings(FALSE, 0));
33                 $this->assertEquals('',  strings('',    0));
34                 $this->assertEquals('0', strings(0,     0));
35                 $this->assertEquals('1', strings(1,     0));
36
37                 // Setup
38                 $t1 = '1'    . "\n";
39                 $t2 = '12'   . "\n";
40                 $t3 = '123'  . "\n";
41                 $t4 = '1234' . "\n";
42                 $t5 = '12345';
43                 $test = $t1 . $t2 . $t3 . $t4 . $t5;
44
45                 // Minimum length
46                 $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test, -1));
47                 $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test,  0));
48                 $this->assertEquals($t1 . $t2 . $t3 . $t4 . $t5, strings($test,  1));
49                 $this->assertEquals(      $t2 . $t3 . $t4 . $t5, strings($test,  2));
50                 $this->assertEquals(            $t3 . $t4 . $t5, strings($test,  3));
51                 $this->assertEquals(                  $t4 . $t5, strings($test,  4));
52                 $this->assertEquals(                  $t4 . $t5, strings($test)); // Default
53                 $this->assertEquals(                        $t5, strings($test,  5));
54
55                 // Preserve the last newline
56                 $this->assertEquals($t4 . $t5,        strings($test       , 4));
57                 $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4));
58
59                 // Ignore sequential spaces, and spaces at the beginning/end of lines
60                 $test = '   A' . '      ' . '   ' . 'B  ';
61                 $this->assertEquals($test, strings($test, 0, FALSE));
62                 $this->assertEquals('A B', strings($test, 0, TRUE ));
63         }
64
65         function testFunc_array_count_leaves()
66         {
67                 // Empty array = 0, if option is not set
68                 $array = array();
69                 $this->assertEquals(0, array_count_leaves($array, FALSE));
70                 $this->assertEquals(1, array_count_leaves($array, TRUE));
71                 $array = array(
72                         array(
73                                 array()
74                         )
75                 );
76                 $this->assertEquals(0, array_count_leaves($array, FALSE));
77                 $this->assertEquals(1, array_count_leaves($array, TRUE));
78
79                 // One leaf = 1
80                 foreach(array(NULL, TRUE, FALSE, -1, 0, 1, '', 'foobar') as $value) {
81                         $this->assertEquals(1, array_count_leaves($value, FALSE));
82                         $this->assertEquals(1, array_count_leaves($value, TRUE));
83                 }
84
85                 // Compisite
86                 $array = array(
87                         1,
88                         'v1',
89                         array(),        // Empty array
90                         array(
91                                 2,
92                                 'v2',
93                                 'k1' => TRUE,
94                                 'k2' => FALSE,
95                                 'k3' => array(),        // Empty array
96                                 'k4' => array(
97                                         3,
98                                         'v3',
99                                         'k5' => NULL,
100                                         'k6' => array(),        // Empty array
101                                 ),
102                         ),
103                         'k7'  => 4,
104                         'k8'  => 'v4',
105                         'k9'  => array(),       // Empty array
106                         'k10' => array(
107                                 5,
108                                 'v5',
109                                 'k11' => NULL,
110                                 'k12' => array(),       // Empty array
111                         ),
112                 );
113                 $this->assertEquals(14, array_count_leaves($array, FALSE));
114                 $this->assertEquals(19, array_count_leaves($array, TRUE));
115         }
116
117         function testPhpFunc_array_unique()
118         {
119                 $this->assertEquals(array(1), array_unique(array(1, 1)));
120
121                 // Keys are preserved, array()s inside are preserved
122                 $this->assertEquals(
123                         array(0, 2 => array(1, 1), 3 => 2),
124                         array_unique(
125                                 array(0, 0, array(1, 1), 2, 2)
126                         )
127                 );
128
129                 // Keys are preserved
130                 $this->assertEquals(
131                         array(0, 2 => array(1, 1), 3 => 2),
132                         array_unique(array(0, 0, array(1, 1), 2, 2))
133                 );
134
135                 // ONLY the first array() is preserved
136                 $this->assertEquals(
137                         array(0 => array(1, 1)),
138                         array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
139                 );
140         }
141
142         function testFunc_array_merge_leaves()
143         {
144                 // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
145                 $array1 = array(1);
146                 $array2 = array(1);
147                 $result = array_merge_recursive($array1, $array2);
148                 $this->assertEquals(array(1, 1), $result);
149                 $result = array_unique_recursive($result);
150                 $this->assertEquals(array(1),    $result);
151                 $result = array_merge_leaves($array1, $array2);
152                 $this->assertEquals(array(1),    $result);
153
154                 $array1 = array(2);
155                 $array2 = array(1);
156                 $result = array_merge_recursive($array1, $array2);
157                 $this->assertEquals(array(2, 1), $result);
158                 $result = array_merge_leaves($array1, $array2);
159                 $this->assertEquals(array(1),    $result);
160
161                 // All NUMERIC keys are always renumbered from 0?
162                 $array1 = array('10' => 'f3');
163                 $array2 = array('10' => 'f4');
164                 $result = array_merge_recursive($array1, $array2);
165                 $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result);
166                 $result = array_merge_leaves($array1, $array2);
167                 $this->assertEquals(array(10 => 'f4'), $result);
168
169                 // One more thing ...
170                 $array1 = array('20' => 'f5');
171                 $array2 = array();
172                 $result = array_merge_recursive($array1, $array2);
173                 $this->assertEquals(array(0 => 'f5'), $result);
174                 $result = array_merge_leaves($array1, $array2);
175                 $this->assertEquals(array(20 => 'f5'), $result);
176
177                 // Non-numeric keys and values will be marged as you think?
178                 $array1 = array('a' => 'f1');
179                 $array2 = array('a' => 'f2');
180                 $result = array_merge_recursive($array1, $array2);
181                 $this->assertEquals(array('a' => array('f1', 'f2')), $result);
182                 $result = array_merge_leaves($array1, $array2);
183                 $this->assertEquals(array('a' => 'f2'), $result);
184
185                 // Non-numeric keys: An array and a value will be marged?
186                 $array1 = array('b' => array('k1'));
187                 $array2 = array('b' => 'k2');
188                 $result = array_merge_recursive($array1, $array2);
189                 $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result);
190                 $result = array_merge_leaves($array1, $array2);
191                 $this->assertEquals(array('b' => array(0 => 'k1')), $result);
192
193                 // Combination?
194                 $array1 = array(
195                         2,
196                         'a'  => 'f1',
197                         '10' => 'f3',
198                         '20' => 'f5',
199                         'b'  => array('k1'),
200                 );
201                 $array2 = array(
202                         1,
203                         'a'  => 'f2',
204                         '10' => 'f4',
205                         'b'  => 'k2',
206                 );
207                 $result = array (
208                         2,
209                         'a' => array (
210                                 'f1',
211                                 'f2',
212                         ),
213                         'f3',
214                         'f5',
215                         'b' => array (
216                                 'k1',
217                                 'k2',
218                         ),
219                         1,
220                         'f4',
221                 );
222                 $result2 = array (
223                          0  => 1,
224                         10  => 'f4',
225                         20  => 'f5',
226                         'a' => 'f2',
227                         'b' => array ('k1'),
228                 );
229                 $this->assertEquals($result,  array_merge_recursive($array1, $array2));
230                 $this->assertEquals($result2, array_merge_leaves($array1, $array2));
231
232                 // Values will not be unique?
233                 $array1 = array(5, 4);
234                 $array2 = array(4, 5);
235                 $result = array_merge_recursive($array1, $array2);
236                 $this->assertEquals(array(5, 4, 4, 5), $result);
237                 $this->assertEquals(array(5, 4),       array_unique_recursive($result));
238                 $this->assertEquals(array(0=>4, 1=>5), array_merge_leaves($array1, $array2));
239
240                 // One more thing ...?
241                 $array1 = array('b' => array('k3'));
242                 $array2 = array('b' => 'k3');
243                 $result = array_merge_recursive($array1, $array2);
244                 $this->assertEquals(array('b' => array('k3', 'k3')), $result);
245                 $result = array_unique_recursive($result);
246                 $this->assertEquals(array('b' => array('k3')),       $result);
247                 $result = array_merge_leaves($array1, $array2);
248                 $this->assertEquals(array('b' => array('k3')), $result);
249
250                 // Preserve numeric keys?
251                 $array1 = array('a' => array('' => NULL));
252                 $array2 = array('a' => array(5  => NULL));
253                 $array3 = array('a' => array(8  => NULL));
254                 //
255                 // BAD: PHP array_merge_recursive() don't preserve numeric keys
256                 $result = array_merge_recursive($array1, $array2);
257                 $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result);       // 0?
258                 $result = array_merge_recursive($array2, $array3);
259                 $this->assertEquals(array('a' => array(5 => NULL,  6 => NULL)), $result);       // 6?
260                 //
261                 $result = array_merge_leaves($array1, $array2);
262                 $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result);       // 0?
263                 $result = array_merge_leaves($array2, $array3);
264                 $this->assertEquals(array('a' => array(5 => NULL,  8 => NULL)), $result);       // 6?
265
266                 // Merging array leaves
267                 $array1 = array('a' => TRUE);
268                 $array2 = array('b' => FALSE);
269                 $result = array_merge_leaves($array1, $array2);
270                 $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result);
271
272                 $array1 = array('a' => TRUE);
273                 $array2 = array('a' => array('aa' => TRUE));
274                 $this->assertEquals($array2, array_merge_leaves($array1, $array2));
275                 $this->assertEquals($array2, array_merge_leaves($array2, $array1));
276
277                 $array1 = array('a' => array('a1' => TRUE));
278                 $array2 = array('a' => array('a2' => FALSE));
279                 $result = array_merge_leaves($array1, $array2);
280                 $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result);
281         }
282
283         function testFunc_generate_glob_regex()
284         {
285                 // 1st argument: Null
286                 foreach($this->setup_string_null() as $key => $value){
287                         $this->assertEquals('', generate_glob_regex($value), $key);
288                 }
289
290                 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
291                 $this->assertEquals('A.A',     generate_glob_regex('A?A'));
292         }
293
294         function testFunc_generate_host_regex()
295         {
296                 // 1st argument: Null
297                 foreach($this->setup_string_null() as $key => $value){
298                         $this->assertEquals('', generate_host_regex($value), $key);
299                 }
300
301                 $this->assertEquals('localhost',             generate_host_regex('localhost'));
302                 $this->assertEquals('example\.org',          generate_host_regex('example.org'));
303                 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
304                 $this->assertEquals('.*\.example\.org',      generate_host_regex('*.example.org'));
305                 $this->assertEquals('.*\..*\.example\.org',  generate_host_regex('*.*.example.org'));
306                 $this->assertEquals('10\.20\.30\.40',        generate_host_regex('10.20.30.40'));
307
308                 // Should match with 192.168.0.0/16
309                 //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
310         }
311
312         function testFunc_get_blocklist()
313         {
314                 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
315                         $this->fail('SPAM_INI_FILE not defined or not found');
316                         return;
317                 }
318
319                 // get_blocklist_add()
320                 $array = array();
321
322                 get_blocklist_add($array,   'foo', 'bar');
323                 $this->assertEquals(1,      count($array));
324                 $this->assertEquals('bar',  $array['foo']);
325
326                 get_blocklist_add($array,   'hoge', 'fuga');
327                 $this->assertEquals(2,      count($array));
328                 $this->assertEquals('bar',  $array['foo']);
329                 $this->assertEquals('fuga', $array['hoge']);
330
331                 get_blocklist_add($array,   -1, '*.txt');
332                 $this->assertEquals(3,      count($array));
333                 $this->assertEquals('bar',  $array['foo']);
334                 $this->assertEquals('fuga', $array['hoge']);
335                 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
336
337                 // get_blocklist()
338                 // ALL
339                 $array = get_blocklist();
340                 $this->assertTrue(isset($array['C']));
341                 $this->assertTrue(isset($array['goodhost']));
342                 // badhost
343                 $array = get_blocklist('B-1');
344                 $this->assertTrue(isset($array['Google.com']));
345                 // goodhost
346                 $array = get_blocklist('goodhost');
347                 $this->assertTrue(isset($array['IANA-examples']));
348         }
349
350         function testFunc_whois_responsibility()
351         {
352                 // 1st argument: Null
353                 foreach($this->setup_string_null() as $key => $value){
354                         $this->assertEquals('',        whois_responsibility($value), $key);
355                 }
356
357                 // 'act.edu.au' is known as 3rd level domain
358                 $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
359                 $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
360                 $this->AssertEquals('act.edu.au',  whois_responsibility('act.edu.au'));
361                 $this->AssertEquals('edu.au',      whois_responsibility('edu.au'));
362                 $this->AssertEquals('au',          whois_responsibility('au'));
363
364                 // 'co.uk' is known as 2nd level domain
365                 $this->AssertEquals('bar.co.uk',   whois_responsibility('foo.bar.co.uk'));
366                 $this->AssertEquals('bar.co.uk',   whois_responsibility('bar.co.uk'));
367                 $this->AssertEquals('co.uk',       whois_responsibility('co.uk'));
368                 $this->AssertEquals('uk',          whois_responsibility('uk'));
369
370                 // 'bar.uk' is not 2nd level (implicit responsibility)
371                 $this->AssertEquals('bar.uk',      whois_responsibility('foo.bar.uk'));
372                 $this->AssertEquals('bar.uk',      whois_responsibility('bar.uk'));
373
374                 // IPv4
375                 $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
376
377                 // Invalid Top-Level Domain (With implicit)
378                 $this->AssertEquals('bar.local',  whois_responsibility('foo.bar.local'));       // Implicit responsibility
379                 $this->AssertEquals('bar.local',  whois_responsibility('bar.local'));
380                 $this->AssertEquals('local',      whois_responsibility('local'));
381                 $this->AssertEquals('localhost',  whois_responsibility('localhost'));
382                 $this->AssertEquals('s',          whois_responsibility('s'));
383         }
384 }
385
386 ?>