|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Test for IPv4CIDR |
|
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 IPv4CIDRTest extends DefinitionTest |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @expectedException InvalidArgumentException |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testEmptyDefinition() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->_definition = new \Whitelist\Definition\IPv4CIDR(''); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Test various combinations of CIDR's (valid and invalid) |
|
23
|
|
|
* |
|
24
|
|
|
* @return null |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testValidate() |
|
27
|
|
|
{ |
|
28
|
|
|
$pass = false; |
|
29
|
|
|
|
|
30
|
|
|
// Sorry have to do this instead of using @dataProvider to avoid breaking the contract. |
|
31
|
|
|
foreach ($this->cidrProvider() as $cidr) { |
|
32
|
|
|
list ($expected, $address) = $cidr; |
|
33
|
|
|
try { |
|
34
|
|
|
$this->_definition = new \Whitelist\Definition\IPv4CIDR($address); |
|
35
|
|
|
$pass = true; |
|
36
|
|
|
} catch (Exception $e) { |
|
37
|
|
|
$pass = false; |
|
38
|
|
|
} |
|
39
|
|
|
$this->assertEquals($expected, $pass); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function cidrProvider() |
|
45
|
|
|
{ |
|
46
|
|
|
return array( |
|
47
|
|
|
array(false, '10.10.0.3'), |
|
48
|
|
|
array(false, '10.10.0.0/23445'), |
|
49
|
|
|
array(true, '10.10.0.0/16'), |
|
50
|
|
|
array(true, '0.0.0.0/0'), |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @dataProvider provider |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testMatch($expected, $address) |
|
58
|
|
|
{ |
|
59
|
|
|
// testing if address matches CIDR |
|
60
|
|
|
$this->_definition = new \Whitelist\Definition\IPv4CIDR('10.10.0.0/16'); |
|
61
|
|
|
$this->assertEquals($expected, $this->_definition->match($address)); |
|
62
|
|
|
|
|
63
|
|
|
// testing that all of them pass zero CIDR |
|
64
|
|
|
$this->_definition = new \Whitelist\Definition\IPv4CIDR('0.0.0.0/0'); |
|
65
|
|
|
$this->assertEquals(true, $this->_definition->match($address)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function provider() |
|
69
|
|
|
{ |
|
70
|
|
|
return array( |
|
71
|
|
|
array(true, '10.10.1.1'), |
|
72
|
|
|
array(true, '10.10.76.1'), |
|
73
|
|
|
array(false, '110.1.76.1'), |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|