2 // $Id: SpamTest.php,v 1.20 2007/07/31 14:19:42 henoheno Exp $
3 // Copyright (C) 2007 heno
5 // Design test case for spam.php (called from runner.php)
7 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
9 require_once('spam.php');
10 require_once('PHPUnit/PHPUnit.php');
12 class SpamTest extends PHPUnit_TestCase
14 function setup_string_null()
20 '[array(foobar)]' => array('foobar'),
27 function testFunc_strings()
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));
43 $test = $t1 . $t2 . $t3 . $t4 . $t5;
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));
55 // Preserve the last newline
56 $this->assertEquals($t4 . $t5, strings($test , 4));
57 $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4));
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 ));
65 function testFunc_array_count_leaves()
67 // Empty array = 0, if option is not set
69 $this->assertEquals(0, array_count_leaves($array, FALSE));
70 $this->assertEquals(1, array_count_leaves($array, TRUE));
76 $this->assertEquals(0, array_count_leaves($array, FALSE));
77 $this->assertEquals(1, array_count_leaves($array, TRUE));
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));
89 array(), // Empty array
95 'k3' => array(), // Empty array
100 'k6' => array(), // Empty array
105 'k9' => array(), // Empty array
110 'k12' => array(), // Empty array
113 $this->assertEquals(14, array_count_leaves($array, FALSE));
114 $this->assertEquals(19, array_count_leaves($array, TRUE));
117 function testPhpFunc_array_unique()
119 $this->assertEquals(array(1), array_unique(array(1, 1)));
121 // Keys are preserved, array()s inside are preserved
123 array(0, 2 => array(1, 1), 3 => 2),
125 array(0, 0, array(1, 1), 2, 2)
129 // Keys are preserved
131 array(0, 2 => array(1, 1), 3 => 2),
132 array_unique(array(0, 0, array(1, 1), 2, 2))
135 // ONLY the first array() is preserved
137 array(0 => array(1, 1)),
138 array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
142 function testFunc_array_merge_leaves()
144 // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
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);
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);
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);
169 // One more thing ...
170 $array1 = array('20' => 'f5');
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);
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);
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);
229 $this->assertEquals($result, array_merge_recursive($array1, $array2));
230 $this->assertEquals($result2, array_merge_leaves($array1, $array2));
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));
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);
250 // Preserve numeric keys?
251 $array1 = array('a' => array('' => NULL));
252 $array2 = array('a' => array(5 => NULL));
253 $array3 = array('a' => array(8 => NULL));
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?
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?
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);
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));
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);
283 function testFunc_generate_glob_regex()
285 // 1st argument: Null
286 foreach($this->setup_string_null() as $key => $value){
287 $this->assertEquals('', generate_glob_regex($value), $key);
290 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
291 $this->assertEquals('A.A', generate_glob_regex('A?A'));
294 function testFunc_generate_host_regex()
296 // 1st argument: Null
297 foreach($this->setup_string_null() as $key => $value){
298 $this->assertEquals('', generate_host_regex($value), $key);
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'));
308 // Should match with 192.168.0.0/16
309 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
312 function testFunc_get_blocklist()
314 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
315 $this->fail('SPAM_INI_FILE not defined or not found');
319 // get_blocklist_add()
322 get_blocklist_add($array, 'foo', 'bar');
323 $this->assertEquals(1, count($array));
324 $this->assertEquals('bar', $array['foo']);
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']);
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']);
339 $array = get_blocklist();
340 $this->assertTrue(isset($array['C']));
341 $this->assertTrue(isset($array['goodhost']));
343 $array = get_blocklist('B-1');
344 $this->assertTrue(isset($array['*.blogspot.com']));
346 $array = get_blocklist('goodhost');
347 $this->assertTrue(isset($array['IANA-examples']));
350 function testFunc_whois_responsibility()
352 // 1st argument: Null
353 foreach($this->setup_string_null() as $key => $value){
354 $this->assertEquals('', whois_responsibility($value), $key);
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'));
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'));
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'));
375 $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
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'));