1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tests for the Check class |
5
|
|
|
* |
6
|
|
|
* @author Sam Stenvall <[email protected]> |
7
|
|
|
* @copyright Copyright © Sam Stenvall 2014- |
8
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
9
|
|
|
*/ |
10
|
|
|
class CheckTest extends PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var Whitelist\Check the check instance |
15
|
|
|
*/ |
16
|
|
|
protected $_checker; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Initialize the checker |
20
|
|
|
*/ |
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->_checker = new \Whitelist\Check(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Test invalid objects passed to whitelist() |
28
|
|
|
* @expectedException InvalidArgumentException |
29
|
|
|
*/ |
30
|
|
|
public function testInvalidDefinitionObject() |
31
|
|
|
{ |
32
|
|
|
$this->_checker->whitelist(array( |
33
|
|
|
new stdClass(), |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test unparsable definition passed to whitelist() |
39
|
|
|
* @expectedException InvalidArgumentException |
40
|
|
|
*/ |
41
|
|
|
public function testUnknownDefinition() |
42
|
|
|
{ |
43
|
|
|
$this->_checker->whitelist(array( |
44
|
|
|
'ag?', // definition class should not be able to be determined |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This test also tests that the whitelist definitions are valid, ie. they |
50
|
|
|
* don't throw an exception |
51
|
|
|
* @dataProvider matchDataprovider |
52
|
|
|
*/ |
53
|
|
|
public function testMatch($expected, $expression) |
54
|
|
|
{ |
55
|
|
|
$this->_checker->whitelist(array( |
56
|
|
|
'10.2.3.1', |
57
|
|
|
'10.0.0.0/16', |
58
|
|
|
'2001:14d8:100:934b::3:1', |
59
|
|
|
'2001:14b8:100:934b::/64', |
60
|
|
|
'test.com', |
61
|
|
|
'example-domain.com', |
62
|
|
|
'*.another-example-domain.com', |
63
|
|
|
'*.example.com', |
64
|
|
|
new Whitelist\Definition\Domain('sub.example.com'), |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
$this->assertEquals($expected, $this->_checker->check($expression)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function matchDataProvider() |
71
|
|
|
{ |
72
|
|
|
return array( |
73
|
|
|
array(true, '10.2.3.1'), |
74
|
|
|
array(false, '10.2.3.2'), |
75
|
|
|
array(true, '10.0.1.1'), |
76
|
|
|
array(false, '10.1.1.1'), |
77
|
|
|
array(true, '2001:14d8:100:934b::3:1'), |
78
|
|
|
array(false, '2001:14d8:100:934b::3:2'), |
79
|
|
|
array(true, '2001:14b8:100:934b::12b1:1'), |
80
|
|
|
array(false, '2001:14c8:100:934b::12b1:1'), |
81
|
|
|
array(true, 'test.com'), |
82
|
|
|
array(true, 'anything.goes.example.com'), |
83
|
|
|
array(true, 'sub.example.com'), |
84
|
|
|
array(false, 'test.example2.com'), |
85
|
|
|
array(true, 'example-domain.com'), |
86
|
|
|
array(true, 'test.another-example-domain.com') |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|