OSDN Git Service

* Separate: spam.php => spam.php and spam_pickup.php, SpamTest.php => SpamTest.php...
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
1 <?php
2 // $Id: SpamTest.php,v 1.18 2007/07/02 14:51:40 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         // And array_unique_recursive()
143         function testPhPFunc_array_merge_recursive()
144         {
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
152                 $array1 = array(2);
153                 $array2 = array(1);
154                 $result = array(2, 1);
155                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
156
157                 // All NUMERIC keys are always renumbered from 0
158                 $array1 = array('10' => 'f3');
159                 $array2 = array('10' => 'f4');
160                 $result = array('f3', 'f4');
161                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
162
163                 // One more thing ...
164                 $array1 = array('20' => 'f5');
165                 $array2 = array();
166                 $result = array('f5');
167                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
168
169                 // Non-numeric keys and values will be marged as you think
170                 $array1 = array('a' => 'f1');
171                 $array2 = array('a' => 'f2');
172                 $result = array('a' => array('f1', 'f2'));
173                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
174
175                 // Non-numeric keys: An array and a value will be marged
176                 $array1 = array('b' => array('k1'));
177                 $array2 = array('b' => 'k2');
178                 $result = array('b' => array('k1', 'k2'));
179                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
180
181                 // Combination
182                 $array1 = array(
183                         2,
184                         'a' => 'f1',
185                         '10' => 'f3',
186                         '20' => 'f5',
187                         'b' => array('k1'),
188                 );
189                 $array2 = array(
190                         1,
191                         'a' => 'f2',
192                         '10' => 'f4',
193                         'b' => 'k2',
194                 );
195                 $result = array (
196                         2,
197                         'a' => array (
198                                 'f1',
199                                 'f2',
200                         ),
201                         'f3',
202                         'f5',
203                         'b' => array (
204                                 'k1',
205                                 'k2',
206                         ),
207                         1,
208                         'f4',
209                 );
210                 $this->assertEquals($result, array_merge_recursive($array1, $array2));
211
212                 // Values will not be unique
213                 $array1 = array(5, 4);
214                 $array2 = array(4, 5);
215                 $result = array_merge_recursive($array1, $array2);
216                 $this->assertEquals(array(5, 4, 4, 5), $result);
217                 $result = array_unique_recursive($result);
218                 $this->assertEquals(array(5, 4),       $result);
219
220                 // One more thing ...
221                 $array1 = array('b' => array('k3'));
222                 $array2 = array('b' => 'k3');
223                 $result = array_merge_recursive($array1, $array2);
224                 $this->assertEquals(array('b' => array('k3', 'k3')), $result);
225                 $result = array_unique_recursive($result);
226                 $this->assertEquals(array('b' => array('k3')),       $result);
227         }
228
229         function testFunc_generate_glob_regex()
230         {
231                 // 1st argument: Null
232                 foreach($this->setup_string_null() as $key => $value){
233                         $this->assertEquals('', generate_glob_regex($value), $key);
234                 }
235
236                 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
237                 $this->assertEquals('A.A',     generate_glob_regex('A?A'));
238         }
239
240         function testFunc_generate_host_regex()
241         {
242                 // 1st argument: Null
243                 foreach($this->setup_string_null() as $key => $value){
244                         $this->assertEquals('', generate_host_regex($value), $key);
245                 }
246
247                 $this->assertEquals('localhost',             generate_host_regex('localhost'));
248                 $this->assertEquals('example\.org',          generate_host_regex('example.org'));
249                 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
250                 $this->assertEquals('.*\.example\.org',      generate_host_regex('*.example.org'));
251                 $this->assertEquals('.*\..*\.example\.org',  generate_host_regex('*.*.example.org'));
252                 $this->assertEquals('10\.20\.30\.40',        generate_host_regex('10.20.30.40'));
253
254                 // Should match with 192.168.0.0/16
255                 //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
256         }
257
258         function testFunc_get_blocklist()
259         {
260                 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
261                         $this->fail('SPAM_INI_FILE not defined or not found');
262                         return;
263                 }
264
265                 // get_blocklist_add()
266                 $array = array();
267
268                 get_blocklist_add($array,   'foo', 'bar');
269                 $this->assertEquals(1,      count($array));
270                 $this->assertEquals('bar',  $array['foo']);
271
272                 get_blocklist_add($array,   'hoge', 'fuga');
273                 $this->assertEquals(2,      count($array));
274                 $this->assertEquals('bar',  $array['foo']);
275                 $this->assertEquals('fuga', $array['hoge']);
276
277                 get_blocklist_add($array,   -1, '*.txt');
278                 $this->assertEquals(3,      count($array));
279                 $this->assertEquals('bar',  $array['foo']);
280                 $this->assertEquals('fuga', $array['hoge']);
281                 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
282
283                 // get_blocklist()
284                 // ALL
285                 $array = get_blocklist();
286                 $this->assertTrue(isset($array['C']));
287                 $this->assertTrue(isset($array['goodhost']));
288                 // badhost
289                 $array = get_blocklist('B-1');
290                 $this->assertTrue(isset($array['*.blogspot.com']));
291                 // goodhost
292                 $array = get_blocklist('goodhost');
293                 $this->assertTrue(isset($array['IANA-examples']));
294         }
295
296         function testFunc_whois_responsibility()
297         {
298                 // 1st argument: Null
299                 foreach($this->setup_string_null() as $key => $value){
300                         $this->assertEquals('',        whois_responsibility($value), $key);
301                 }
302
303                 // 'act.edu.au' is known as 3rd level domain
304                 $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
305                 $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
306                 $this->AssertEquals('act.edu.au',  whois_responsibility('act.edu.au'));
307                 $this->AssertEquals('edu.au',      whois_responsibility('edu.au'));
308                 $this->AssertEquals('au',          whois_responsibility('au'));
309
310                 // 'co.uk' is known as 2nd level domain
311                 $this->AssertEquals('bar.co.uk',   whois_responsibility('foo.bar.co.uk'));
312                 $this->AssertEquals('bar.co.uk',   whois_responsibility('bar.co.uk'));
313                 $this->AssertEquals('co.uk',       whois_responsibility('co.uk'));
314                 $this->AssertEquals('uk',          whois_responsibility('uk'));
315
316                 // 'bar.uk' is not 2nd level (implicit responsibility)
317                 $this->AssertEquals('bar.uk',      whois_responsibility('foo.bar.uk'));
318                 $this->AssertEquals('bar.uk',      whois_responsibility('bar.uk'));
319
320                 // IPv4
321                 $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
322
323                 // Invalid Top-Level Domain (With implicit)
324                 $this->AssertEquals('bar.local',  whois_responsibility('foo.bar.local'));       // Implicit responsibility
325                 $this->AssertEquals('bar.local',  whois_responsibility('bar.local'));
326                 $this->AssertEquals('local',      whois_responsibility('local'));
327                 $this->AssertEquals('localhost',  whois_responsibility('localhost'));
328                 $this->AssertEquals('s',          whois_responsibility('s'));
329         }
330 }
331
332 ?>