2 // $Id: SpamTest.php,v 1.18 2007/07/02 14:51:40 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 // And array_unique_recursive()
143 function testPhPFunc_array_merge_recursive()
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);
154 $result = array(2, 1);
155 $this->assertEquals($result, array_merge_recursive($array1, $array2));
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));
163 // One more thing ...
164 $array1 = array('20' => 'f5');
166 $result = array('f5');
167 $this->assertEquals($result, array_merge_recursive($array1, $array2));
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));
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));
210 $this->assertEquals($result, array_merge_recursive($array1, $array2));
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);
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);
229 function testFunc_generate_glob_regex()
231 // 1st argument: Null
232 foreach($this->setup_string_null() as $key => $value){
233 $this->assertEquals('', generate_glob_regex($value), $key);
236 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
237 $this->assertEquals('A.A', generate_glob_regex('A?A'));
240 function testFunc_generate_host_regex()
242 // 1st argument: Null
243 foreach($this->setup_string_null() as $key => $value){
244 $this->assertEquals('', generate_host_regex($value), $key);
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'));
254 // Should match with 192.168.0.0/16
255 //$this->assertEquals('192\.168\.', generate_host_regex('192.168.'));
258 function testFunc_get_blocklist()
260 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
261 $this->fail('SPAM_INI_FILE not defined or not found');
265 // get_blocklist_add()
268 get_blocklist_add($array, 'foo', 'bar');
269 $this->assertEquals(1, count($array));
270 $this->assertEquals('bar', $array['foo']);
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']);
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']);
285 $array = get_blocklist();
286 $this->assertTrue(isset($array['C']));
287 $this->assertTrue(isset($array['goodhost']));
289 $array = get_blocklist('B-1');
290 $this->assertTrue(isset($array['*.blogspot.com']));
292 $array = get_blocklist('goodhost');
293 $this->assertTrue(isset($array['IANA-examples']));
296 function testFunc_whois_responsibility()
298 // 1st argument: Null
299 foreach($this->setup_string_null() as $key => $value){
300 $this->assertEquals('', whois_responsibility($value), $key);
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'));
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'));
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'));
321 $this->AssertEquals('192.168.0.1', whois_responsibility('192.168.0.1'));
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'));