OSDN Git Service

2011
[pukiwiki/pukiwiki_sandbox.git] / spam / SpamTest.php
1 <?php
2 // $Id: SpamTest.php,v 1.27 2009/01/04 08:56:07 henoheno Exp $
3 // Copyright (C) 2007-2009 heno
4 //
5 // Design test case for spam.php (called from runner.php)
6
7 if (! defined('SPAM_INI_FILE')) define('SPAM_INI_FILE', 'spam.ini.php');
8
9 require_once('spam.php');
10 require_once('PHPUnit/PHPUnit.php');
11
12 class SpamTest extends PHPUnit_TestCase
13 {
14         // Utility
15         function setup_string_null()
16         {
17                 return array(
18                         '[NULL]'        => NULL,
19                         '[TRUE]'        => TRUE,
20                         '[FALSE]'       => FALSE,
21                         '[array(foobar)]' => array('foobar'),
22                         '[]'            => '',
23                         '[0]'           => 0,
24                         '[1]'           => 1
25                 );
26         }
27
28         function testFunc_generate_glob_regex()
29         {
30                 // 1st argument: Null
31                 foreach($this->setup_string_null() as $key => $value){
32                         $this->assertEquals('', generate_glob_regex($value), $key);
33                 }
34
35                 $this->assertEquals('.*\.txt', generate_glob_regex('*.txt'));
36                 $this->assertEquals('A.A',     generate_glob_regex('A?A'));
37         }
38
39         function testFunc_generate_host_regex()
40         {
41                 // 1st argument: Null
42                 foreach($this->setup_string_null() as $key => $value){
43                         $this->assertEquals('', generate_host_regex($value), $key);
44                 }
45
46                 $this->assertEquals('localhost',             generate_host_regex('localhost'));
47                 $this->assertEquals('example\.org',          generate_host_regex('example.org'));
48                 $this->assertEquals('(?:.*\.)?example\.org', generate_host_regex('.example.org'));
49                 $this->assertEquals('.*\.example\.org',      generate_host_regex('*.example.org'));
50                 $this->assertEquals('.*\..*\.example\.org',  generate_host_regex('*.*.example.org'));
51                 $this->assertEquals('10\.20\.30\.40',        generate_host_regex('10.20.30.40'));
52
53                 // Should match with 192.168.0.0/16
54                 //$this->assertEquals('192\.168\.',       generate_host_regex('192.168.'));
55         }
56
57         function testFunc_get_blocklist()
58         {
59                 if (! defined('SPAM_INI_FILE') || ! file_exists(SPAM_INI_FILE)) {
60                         $this->fail('SPAM_INI_FILE not defined or not found');
61                         return;
62                 }
63
64                 // get_blocklist_add()
65                 $array = array();
66                 //
67                 get_blocklist_add($array,   'foo', 'bar');
68                 $this->assertEquals(1,      count($array));
69                 $this->assertEquals('bar',  $array['foo']);
70                 //
71                 get_blocklist_add($array,   'hoge', 'fuga');
72                 $this->assertEquals(2,      count($array));
73                 $this->assertEquals('bar',  $array['foo']);
74                 $this->assertEquals('fuga', $array['hoge']);
75                 //
76                 get_blocklist_add($array,   -1, '*.txt');
77                 $this->assertEquals(3,      count($array));
78                 $this->assertEquals('bar',  $array['foo']);
79                 $this->assertEquals('fuga', $array['hoge']);
80                 $this->assertEquals('#^.*\.txt$#i', $array['*.txt']);
81
82                 // get_blocklist()
83                 // ALL
84                 $array = get_blocklist();
85                 $this->assertTrue(isset($array['C']));
86                 $this->assertTrue(isset($array['goodhost']));
87                 // badhost
88                 $array = get_blocklist('B-1');
89                 $this->assertTrue(isset($array['Google.com']));
90                 // goodhost
91                 $array = get_blocklist('goodhost');
92                 $this->assertTrue(isset($array['IANA-examples']));
93         }
94
95 }
96
97 ?>