2 // $Id: SpamTest.php,v 1.19 2007/07/28 12:47:28 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(), and array_merge_leaves()
148 $result = array_merge_recursive($array1, $array2);
149 $this->assertEquals(array(1, 1), $result);
150 $result = array_unique_recursive($result);
151 $this->assertEquals(array(1), $result);
152 $result = array_merge_leaves($array1, $array2);
153 $this->assertEquals(array(1), $result);
158 $result = array_merge_recursive($array1, $array2);
159 $this->assertEquals(array(2, 1), $result);
160 $result = array_merge_leaves($array1, $array2);
161 $this->assertEquals(array(1), $result);
163 // All NUMERIC keys are always renumbered from 0
164 $array1 = array('10' => 'f3');
165 $array2 = array('10' => 'f4');
166 $result = array_merge_recursive($array1, $array2);
167 $this->assertEquals(array(0 => 'f3', 1 => 'f4'), $result);
168 $result = array_merge_leaves($array1, $array2);
169 $this->assertEquals(array(10 => 'f4'), $result);
171 // One more thing ...
172 $array1 = array('20' => 'f5');
174 $result = array_merge_recursive($array1, $array2);
175 $this->assertEquals(array(0 => 'f5'), $result);
176 $result = array_merge_leaves($array1, $array2);
177 $this->assertEquals(array(20 => 'f5'), $result);
179 // Non-numeric keys and values will be marged as you think
180 $array1 = array('a' => 'f1');
181 $array2 = array('a' => 'f2');
182 $result = array_merge_recursive($array1, $array2);
183 $this->assertEquals(array('a' => array('f1', 'f2')), $result);
184 $result = array_merge_leaves($array1, $array2);
185 $this->assertEquals(array('a' => 'f2'), $result);
187 // Non-numeric keys: An array and a value will be marged
188 $array1 = array('b' => array('k1'));
189 $array2 = array('b' => 'k2');
190 $result = array_merge_recursive($array1, $array2);
191 $this->assertEquals(array('b' => array(0 => 'k1', 1 => 'k2')), $result);
192 $result = array_merge_leaves($array1, $array2);
193 $this->assertEquals(array('b' => array(0 => 'k1')), $result);
231 $this->assertEquals($result, array_merge_recursive($array1, $array2));
232 $this->assertEquals($result2, array_merge_leaves($array1, $array2));
234 // Values will not be unique
235 $array1 = array(5, 4);
236 $array2 = array(4, 5);
237 $result = array_merge_recursive($array1, $array2);
238 $this->assertEquals(array(5, 4, 4, 5), $result);
239 $result = array_unique_recursive($result);
240 $this->assertEquals(array(5, 4), $result);
241 $result = array_merge_leaves($array1, $array2);
242 $this->assertEquals(array(4, 5), $result);
244 // One more thing ...
245 $array1 = array('b' => array('k3'));
246 $array2 = array('b' => 'k3');
247 $result = array_merge_recursive($array1, $array2);
248 $this->assertEquals(array('b' => array('k3', 'k3')), $result);
249 $result = array_unique_recursive($result);
250 $this->assertEquals(array('b' => array('k3')), $result);
251 $result = array_merge_leaves($array1, $array2);
252 $this->assertEquals(array('b' => array('k3')), $result);
254 // Preserve numeric keys
255 $array1 = array('a' => array('' => NULL));
256 $array2 = array('a' => array(5 => NULL));
257 $array3 = array('a' => array(8 => NULL));
259 // BAD: PHP array_merge_recursive() don't preserve numeric keys
260 $result = array_merge_recursive($array1, $array2);
261 $this->assertEquals(array('a' => array('' => NULL, 0 => NULL)), $result); // 0?
262 $result = array_merge_recursive($array2, $array3);
263 $this->assertEquals(array('a' => array(5 => NULL, 6 => NULL)), $result); // 6?
265 $result = array_merge_leaves($array1, $array2);
266 $this->assertEquals(array('a' => array('' => NULL, 5 => NULL)), $result); // 0?
267 $result = array_merge_leaves($array2, $array3);
268 $this->assertEquals(array('a' => array(5 => NULL, 8 => NULL)), $result); // 6?
272 $array1 = array('a' => TRUE);
273 $array2 = array('b' => FALSE);
274 $result = array_merge_leaves($array1, $array2);
275 $this->assertEquals(array('a' => TRUE, 'b' => FALSE), $result);
277 $array1 = array('a' => TRUE);
278 $array2 = array('a' => array('aa' => TRUE));
279 $this->assertEquals($array2, array_merge_leaves($array1, $array2));
280 $this->assertEquals($array2, array_merge_leaves($array2, $array1));
282 $array1 = array('a' => array('a1' => TRUE));
283 $array2 = array('a' => array('a2' => FALSE));
284 $result = array_merge_leaves($array1, $array2);
285 $this->assertEquals(array('a' => array('a1' => TRUE, 'a2' => FALSE)), $result);
288 function testFunc_generate_glob_regex()
290 // 1st argument: Null
291 foreach($this->setup_string_null() as $key => $value){
292 $this->assertEquals('', generate_glob_regex($value), $key);
295 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
296 $this->assertEquals('A.A', generate_glob_regex('A?A'));
299 function testFunc_generate_host_regex()
301 // 1st argument: Null
302 foreach($this->setup_string_null() as $key => $value){
303 $this->assertEquals('', generate_host_regex($value), $key);
306 $this->assertEquals('localhost', generate_host_regex('localhost'));
307 $this->assertEquals('example\.org', generate_host_regex('example.org'));
308 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
309 $this->assertEquals('.*\.example\.org', generate_host_regex('*.example.org'));
310 $this->assertEquals('.*\..*\.example\.org', generate_host_regex('*.*.example.org'));
311 $this->assertEquals('10\.20\.30\.40', generate_host_regex('10.20.30.40'));
313 // Should match with 192.168.0.0/16
314 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
317 function testFunc_get_blocklist()
319 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
320 $this->fail('SPAM_INI_FILE not defined or not found');
324 // get_blocklist_add()
327 get_blocklist_add($array, 'foo', 'bar');
328 $this->assertEquals(1, count($array));
329 $this->assertEquals('bar', $array['foo']);
331 get_blocklist_add($array, 'hoge', 'fuga');
332 $this->assertEquals(2, count($array));
333 $this->assertEquals('bar', $array['foo']);
334 $this->assertEquals('fuga', $array['hoge']);
336 get_blocklist_add($array, -1, '*.txt');
337 $this->assertEquals(3, count($array));
338 $this->assertEquals('bar', $array['foo']);
339 $this->assertEquals('fuga', $array['hoge']);
340 $this->assertEquals('/^.*\.txt$/i', $array['*.txt']);
344 $array = get_blocklist();
345 $this->assertTrue(isset($array['C']));
346 $this->assertTrue(isset($array['goodhost']));
348 $array = get_blocklist('B-1');
349 $this->assertTrue(isset($array['*.blogspot.com']));
351 $array = get_blocklist('goodhost');
352 $this->assertTrue(isset($array['IANA-examples']));
355 function testFunc_whois_responsibility()
357 // 1st argument: Null
358 foreach($this->setup_string_null() as $key => $value){
359 $this->assertEquals('', whois_responsibility($value), $key);
362 // 'act.edu.au' is known as 3rd level domain
363 $this->AssertEquals('bar.act.edu.au', whois_responsibility('foo.bar.act.edu.au'));
364 $this->AssertEquals('bar.act.edu.au', whois_responsibility('bar.act.edu.au'));
365 $this->AssertEquals('act.edu.au', whois_responsibility('act.edu.au'));
366 $this->AssertEquals('edu.au', whois_responsibility('edu.au'));
367 $this->AssertEquals('au', whois_responsibility('au'));
369 // 'co.uk' is known as 2nd level domain
370 $this->AssertEquals('bar.co.uk', whois_responsibility('foo.bar.co.uk'));
371 $this->AssertEquals('bar.co.uk', whois_responsibility('bar.co.uk'));
372 $this->AssertEquals('co.uk', whois_responsibility('co.uk'));
373 $this->AssertEquals('uk', whois_responsibility('uk'));
375 // 'bar.uk' is not 2nd level (implicit responsibility)
376 $this->AssertEquals('bar.uk', whois_responsibility('foo.bar.uk'));
377 $this->AssertEquals('bar.uk', whois_responsibility('bar.uk'));
380 $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
382 // Invalid Top-Level Domain (With implicit)
383 $this->AssertEquals('bar.local', whois_responsibility('foo.bar.local')); // Implicit responsibility
384 $this->AssertEquals('bar.local', whois_responsibility('bar.local'));
385 $this->AssertEquals('local', whois_responsibility('local'));
386 $this->AssertEquals('localhost', whois_responsibility('localhost'));
387 $this->AssertEquals('s', whois_responsibility('s'));