OSDN Git Service

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