1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AndrewAndante\SubMuncher\Test; |
4
|
|
|
|
5
|
|
|
use AndrewAndante\SubMuncher\Util; |
6
|
|
|
|
7
|
|
|
class UtilTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
public function testIsIPAddress() |
10
|
|
|
{ |
11
|
|
|
$this->assertTrue(Util::is_ipaddr('10.10.10.10')); |
12
|
|
|
$this->assertTrue(Util::is_ipaddr('255.255.255.255')); |
13
|
|
|
$this->assertTrue(Util::is_ipaddr('0.0.0.0')); |
14
|
|
|
|
15
|
|
|
$this->assertFalse(Util::is_ipaddr(10)); |
16
|
|
|
$this->assertFalse(Util::is_ipaddr('10.100.1000.100000')); |
17
|
|
|
$this->assertFalse(Util::is_ipaddr('10.10.10')); |
18
|
|
|
$this->assertFalse(Util::is_ipaddr('IP ADDRESS')); |
19
|
|
|
$this->assertFalse(Util::is_ipaddr(['10.10.10.10'])); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testIPLessThan() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->assertTrue(Util::ip_less_than('10.10.10.0', '10.10.10.1')); |
25
|
|
|
$this->assertTrue(Util::ip_less_than('10.10.10.0', '10.10.20.0')); |
26
|
|
|
$this->assertTrue(Util::ip_less_than('10.10.10.10', '10.10.20.0')); |
27
|
|
|
$this->assertFalse(Util::ip_less_than('10.10.10.2', '10.10.10.1')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testIPGreaterThan() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->assertFalse(Util::ip_greater_than('10.10.10.0', '10.10.10.1')); |
33
|
|
|
$this->assertTrue(Util::ip_greater_than('10.10.10.2', '10.10.10.1')); |
34
|
|
|
$this->assertTrue(Util::ip_greater_than('10.10.20.0', '10.10.10.0')); |
35
|
|
|
$this->assertTrue(Util::ip_greater_than('10.10.20.0', '10.10.10.20')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function testIPAfter() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->assertEquals('10.10.10.1', Util::ip_after('10.10.10.0')); |
41
|
|
|
$this->assertEquals('10.10.10.2', Util::ip_after('10.10.10.0', 2)); |
42
|
|
|
$this->assertEquals('10.10.11.0', Util::ip_after('10.10.10.255')); |
43
|
|
|
$this->assertEquals('10.10.11.1', Util::ip_after('10.10.10.255', 2)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
public function testIPBefore() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->assertEquals('10.10.10.0', Util::ip_before('10.10.10.1')); |
49
|
|
|
$this->assertEquals('10.10.10.0', Util::ip_before('10.10.10.2', 2)); |
50
|
|
|
$this->assertEquals('10.10.9.255', Util::ip_before('10.10.10.0')); |
51
|
|
|
$this->assertEquals('10.10.9.255', Util::ip_before('10.10.10.1', 2)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testFindSmallestCidr() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals(31, Util::find_smallest_cidr(2)); |
57
|
|
|
$this->assertEquals(24, Util::find_smallest_cidr(248)); |
58
|
|
|
$this->assertEquals(23, Util::find_smallest_cidr(259)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testIPRangeSize() |
62
|
|
|
{ |
63
|
|
|
$this->assertEquals(2, Util::ip_range_size('10.10.10.0', '10.10.10.1')); |
64
|
|
|
$this->assertEquals(16, Util::ip_range_size('10.10.10.0', '10.10.10.15')); |
65
|
|
|
$this->assertEquals(3, Util::ip_range_size('127.255.255.255', '128.0.0.1')); |
66
|
|
|
$this->assertEquals(512, Util::ip_range_size('10.10.10.0', '10.10.11.255')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSortIPAddresses() |
70
|
|
|
{ |
71
|
|
|
$orderedIPs = $shuffledIPs = [ |
72
|
|
|
'10.10.0.0', |
73
|
|
|
'10.10.10.0', |
74
|
|
|
'10.10.10.1', |
75
|
|
|
'10.255.255.0', |
76
|
|
|
'255.255.255.0', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
while ($orderedIPs === $shuffledIPs) { |
80
|
|
|
shuffle($shuffledIPs); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->assertEquals($orderedIPs, Util::sort_addresses($shuffledIPs)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSubnetRangeSize() |
87
|
|
|
{ |
88
|
|
|
$this->assertEquals(1, Util::subnet_range_size(32)); |
89
|
|
|
$this->assertEquals(2, Util::subnet_range_size(31)); |
90
|
|
|
$this->assertEquals(256, Util::subnet_range_size(24)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testCidrToIPsArray() |
94
|
|
|
{ |
95
|
|
|
$this->assertEquals( |
96
|
|
|
[ |
97
|
|
|
'10.10.10.0', |
98
|
|
|
'10.10.10.1', |
99
|
|
|
'10.10.10.2', |
100
|
|
|
'10.10.10.3', |
101
|
|
|
], |
102
|
|
|
Util::cidr_to_ips_array('10.10.10.0/30') |
103
|
|
|
); |
104
|
|
|
$this->assertEquals(['10.10.10.0'], Util::cidr_to_ips_array('10.10.10.0')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetSingleSubnet() |
108
|
|
|
{ |
109
|
|
|
$this->assertEquals('10.10.10.0/30', Util::get_single_subnet('10.10.10.0', '10.10.10.3')); |
110
|
|
|
$this->assertEquals('0.0.0.0/1', Util::get_single_subnet('10.10.10.0', '100.100.100.30')); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.