2 // $Id: SpamTest.php,v 1.25 2009/01/02 10:37:47 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 testFunc_delimiter_reverse()
17 $this->assertEquals('com.example.bar.foo',
18 delimiter_reverse('foo.bar.example.com'));
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, '.', '|'));
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, '.', '|'));
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, '.', '|'));
36 $this->assertEquals('100', delimiter_reverse('100'));
37 $this->assertEquals(array(), delimiter_reverse(array()));
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));
49 function setup_string_null()
55 '[array(foobar)]' => array('foobar'),
62 function testFunc_strings()
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));
78 $test = $t1 . $t2 . $t3 . $t4 . $t5;
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));
90 // Preserve the last newline
91 $this->assertEquals($t4 . $t5, strings($test , 4));
92 $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4));
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 ));
100 function testFunc_array_count_leaves()
102 // Empty array = 0, if option is not set
104 $this->assertEquals(0, array_count_leaves($array, FALSE));
105 $this->assertEquals(1, array_count_leaves($array, TRUE));
111 $this->assertEquals(0, array_count_leaves($array, FALSE));
112 $this->assertEquals(1, array_count_leaves($array, TRUE));
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));
124 array(), // Empty array
130 'k3' => array(), // Empty array
135 'k6' => array(), // Empty array
140 'k9' => array(), // Empty array
145 'k12' => array(), // Empty array
148 $this->assertEquals(14, array_count_leaves($array, FALSE));
149 $this->assertEquals(19, array_count_leaves($array, TRUE));
152 function testPhpFunc_array_unique()
154 $this->assertEquals(array(1), array_unique(array(1, 1)));
156 // Keys are preserved, array()s inside are preserved
158 array(0, 2 => array(1, 1), 3 => 2),
160 array(0, 0, array(1, 1), 2, 2)
164 // Keys are preserved
166 array(0, 2 => array(1, 1), 3 => 2),
167 array_unique(array(0, 0, array(1, 1), 2, 2))
170 // ONLY the first array() is preserved
172 array(0 => array(1, 1)),
173 array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
177 function testFunc_array_merge_leaves()
179 // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
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);
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);
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);
204 // One more thing ...
205 $array1 = array('20' => 'f5');
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);
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);
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);
264 $this->assertEquals($result, array_merge_recursive($array1, $array2));
265 $this->assertEquals($result2, array_merge_leaves($array1, $array2));
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));
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);
285 // Preserve numeric keys?
286 $array1 = array('a' => array('' => NULL));
287 $array2 = array('a' => array(5 => NULL));
288 $array3 = array('a' => array(8 => NULL));
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?
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?
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);
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));
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);
318 function testFunc_generate_glob_regex()
320 // 1st argument: Null
321 foreach($this->setup_string_null() as $key => $value){
322 $this->assertEquals('', generate_glob_regex($value), $key);
325 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
326 $this->assertEquals('A.A', generate_glob_regex('A?A'));
329 function testFunc_generate_host_regex()
331 // 1st argument: Null
332 foreach($this->setup_string_null() as $key => $value){
333 $this->assertEquals('', generate_host_regex($value), $key);
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'));
343 // Should match with 192.168.0.0/16
344 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
347 function testFunc_get_blocklist()
349 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
350 $this->fail('SPAM_INI_FILE not defined or not found');
354 // get_blocklist_add()
357 get_blocklist_add($array, 'foo', 'bar');
358 $this->assertEquals(1, count($array));
359 $this->assertEquals('bar', $array['foo']);
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']);
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']);
374 $array = get_blocklist();
375 $this->assertTrue(isset($array['C']));
376 $this->assertTrue(isset($array['goodhost']));
378 $array = get_blocklist('B-1');
379 $this->assertTrue(isset($array['Google.com']));
381 $array = get_blocklist('goodhost');
382 $this->assertTrue(isset($array['IANA-examples']));