2 // $Id: SpamUtilTest.php,v 1.2 2009/01/04 08:56:07 henoheno Exp $
3 // Copyright (C) 2007-2009 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_util.php');
10 require_once('PHPUnit/PHPUnit.php');
12 class SpamUtilTest extends PHPUnit_TestCase
15 function setup_string_null()
21 '[array(foobar)]' => array('foobar'),
28 function testFunc_delimiter_reverse()
31 $this->assertEquals('com.example.bar.foo',
32 delimiter_reverse('foo.bar.example.com'));
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, '.', '|'));
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, '.', '|'));
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, '.', '|'));
50 $this->assertEquals('100', delimiter_reverse('100'));
51 $this->assertEquals(array(), delimiter_reverse(array()));
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));
63 function testFunc_strings()
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));
79 $test = $t1 . $t2 . $t3 . $t4 . $t5;
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));
91 // Preserve the last newline
92 $this->assertEquals($t4 . $t5, strings($test , 4));
93 $this->assertEquals($t4 . $t5 . "\n", strings($test . "\n", 4));
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 ));
101 function testFunc_array_count_leaves()
103 // Empty array = 0, if option is not set
105 $this->assertEquals(0, array_count_leaves($array, FALSE));
106 $this->assertEquals(1, array_count_leaves($array, TRUE));
112 $this->assertEquals(0, array_count_leaves($array, FALSE));
113 $this->assertEquals(1, array_count_leaves($array, TRUE));
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));
125 array(), // Empty array
131 'k3' => array(), // Empty array
136 'k6' => array(), // Empty array
141 'k9' => array(), // Empty array
146 'k12' => array(), // Empty array
149 $this->assertEquals(14, array_count_leaves($array, FALSE));
150 $this->assertEquals(19, array_count_leaves($array, TRUE));
153 function testPhpFunc_array_unique()
155 $this->assertEquals(array(1), array_unique(array(1, 1)));
157 // Keys are preserved, array()s inside are preserved
159 array(0, 2 => array(1, 1), 3 => 2),
161 array(0, 0, array(1, 1), 2, 2)
165 // Keys are preserved
167 array(0, 2 => array(1, 1), 3 => 2),
168 array_unique(array(0, 0, array(1, 1), 2, 2))
171 // ONLY the first array() is preserved
173 array(0 => array(1, 1)),
174 array_unique(array_unique(array(0 => array(1, 1), 'a' => array(2,2), 'b' => array(3, 3))))
178 function testFunc_array_merge_leaves()
180 // PHP array_unique_recursive(), PHP array_merge_leaves(), and array_merge_leaves()
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);
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);
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);
205 // One more thing ...
206 $array1 = array('20' => 'f5');
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);
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);
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);
265 $this->assertEquals($result, array_merge_recursive($array1, $array2));
266 $this->assertEquals($result2, array_merge_leaves($array1, $array2));
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));
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);
286 // Preserve numeric keys?
287 $array1 = array('a' => array('' => NULL));
288 $array2 = array('a' => array(5 => NULL));
289 $array3 = array('a' => array(8 => NULL));
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?
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?
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);
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));
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);
319 function testFunc_generate_glob_regex()
321 // 1st argument: Null
322 foreach($this->setup_string_null() as $key => $value){
323 $this->assertEquals('', generate_glob_regex($value), $key);
326 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
327 $this->assertEquals('A.A', generate_glob_regex('A?A'));
330 function testFunc_generate_host_regex()
332 // 1st argument: Null
333 foreach($this->setup_string_null() as $key => $value){
334 $this->assertEquals('', generate_host_regex($value), $key);
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'));
344 // Should match with 192.168.0.0/16
345 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
348 function testFunc_get_blocklist()
350 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
351 $this->fail('SPAM_INI_FILE not defined or not found');
355 // get_blocklist_add()
358 get_blocklist_add($array, 'foo', 'bar');
359 $this->assertEquals(1, count($array));
360 $this->assertEquals('bar', $array['foo']);
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']);
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']);
375 $array = get_blocklist();
376 $this->assertTrue(isset($array['C']));
377 $this->assertTrue(isset($array['goodhost']));
379 $array = get_blocklist('B-1');
380 $this->assertTrue(isset($array['Google.com']));
382 $array = get_blocklist('goodhost');
383 $this->assertTrue(isset($array['IANA-examples']));