OSDN Git Service

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