forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpTest.php
188 lines (164 loc) · 9.46 KB
/
IpTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* IpTest.php
*
* Tests Util\IP classes
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*
* @copyright 2017 Tony Murray
* @author Tony Murray <[email protected]>
*/
namespace LibreNMS\Tests;
use LibreNMS\Util\IP;
use LibreNMS\Util\IPv4;
use LibreNMS\Util\IPv6;
class IpTest extends TestCase
{
public function testIsValid(): void
{
$this->assertTrue(IP::isValid('192.168.0.1'));
$this->assertTrue(IP::isValid('192.168.0.1'));
$this->assertTrue(IP::isValid('2001:4860:4860::8888'));
$this->assertTrue(IPv4::isValid('192.168.0.1'));
$this->assertTrue(IPv6::isValid('2001:4860:4860::8888'));
$this->assertFalse(IPv4::isValid('2001:4860:4860::8888'));
$this->assertFalse(IPv6::isValid('192.168.0.1'));
$this->assertFalse(IP::isValid('not_an_ip'));
$this->assertTrue(IPv4::isValid('8.8.8.8', true));
$this->assertTrue(IP::isValid('8.8.8.8', true));
$this->assertTrue(IPv4::isValid('192.168.0.1', true));
$this->assertTrue(IPv6::isValid('FF81::', true));
$this->assertFalse(IPv4::isValid('127.0.0.1', true));
$this->assertFalse(IPv6::isValid('::1', true));
$this->assertFalse(IP::isValid('169.254.1.1', true));
$this->assertFalse(IP::isValid('fe80::1', true));
$this->assertFalse(IPv4::isValid('fe80::1', true));
$this->assertFalse(IP::isValid('Falafel', true));
}
/**
* See https://github.com/librenms/librenms/pull/13468 for more info
*
* @requires PHP >= 7.4
*/
public function testIsValidIPv6ExcludeReserved(): void
{
$this->assertFalse(IPv6::isValid('2001:db8:85a3::8a2e:370:7334', true));
}
public function testIpParse(): void
{
$this->assertEquals('192.168.0.1', IP::parse('192.168.0.1'));
$this->assertEquals('127.0.0.1', IP::parse('127.0.0.1'));
$this->assertEquals('2001:db8:85a3::8a2e:370:7334', IP::parse('2001:db8:85a3::8a2e:370:7334'));
$this->assertEquals('::1', IP::parse('::1'));
$this->assertEquals('192.168.0.1', new IPv4('192.168.0.1'));
$this->assertEquals('127.0.0.1', new IPv4('127.0.0.1'));
$this->assertEquals('2001:db8:85a3::8a2e:370:7334', new IPv6('2001:db8:85a3::8a2e:370:7334'));
$this->assertEquals('::1', new IPv6('::1'));
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
new IPv6('192.168.0.1');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
new IPv6('127.0.0.1');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
new IPv4('2001:db8:85a3::8a2e:370:7334');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
new IPv4('::1');
}
public function testHexToIp(): void
{
$this->assertEquals('192.168.1.254', IP::fromHexString('c0 a8 01 fe'));
$this->assertEquals('192.168.1.254', IP::fromHexString('c0a801fe'));
$this->assertEquals('192.168.1.254', IP::fromHexString('c0 a8 01 fe '));
$this->assertEquals('192.168.1.254', IP::fromHexString('"c0 a8 01 fe"'));
$this->assertEquals('192.168.1.254', IP::fromHexString('192.168.1.254'));
$this->assertEquals('62.40.125.125', IP::fromHexString('>(}}')); // stupid ascii encoded
$this->assertEquals('2001:db8::2:1', IP::fromHexString('2001:db8::2:1'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20:01:0d:b8:00:00:00:00:00:00:00:00:00:02:00:01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20.01.0d.b8.00.00.00.00.00.00.00.00.00.02.00.01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20010db8000000000000000000020001'));
$this->assertEquals('3e28:7d7d:3e28:7d7d:3e28:7d7d:3e28:7d7d', IP::fromHexString('>(}}>(}}>(}}>(}}')); // stupid ascii encoded
$this->assertEquals('::', IP::fromHexString('00000000000000000000000000000000'));
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
IP::fromHexString('c0 a8 01 01 fe');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 00 00 01');
}
public function testNetmask2Cidr(): void
{
$this->assertSame(32, IPv4::netmask2cidr('255.255.255.255'));
$this->assertSame(30, IPv4::netmask2cidr('255.255.255.252'));
$this->assertSame(26, IPv4::netmask2cidr('255.255.255.192'));
$this->assertSame(16, IPv4::netmask2cidr('255.255.0.0'));
}
public function testIpInNetwork(): void
{
$this->assertTrue(IP::parse('192.168.1.0')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.32')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.254')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.255')->inNetwork('192.168.1.0/24'));
$this->assertFalse(IP::parse('192.168.1.1')->inNetwork('192.168.1.0'));
$this->assertFalse(IP::parse('10.4.3.2')->inNetwork('192.168.1.0/16'));
$this->assertTrue(IP::parse('::1')->inNetwork('::/64'));
$this->assertTrue(IP::parse('2001:db7:85a3::8a2e:370:7334')->inNetwork('::/0'));
$this->assertFalse(IP::parse('2001:db7:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::/64'));
$this->assertTrue(IP::parse('2001:db8:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::/64'));
$this->assertTrue(IP::parse('2001:db8:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::8a2e:370:7334/128'));
$this->assertFalse(IP::parse('2001:db8:85a3::8a2e:370:7335')->inNetwork('2001:db8:85a3::8a2e:370:7334/128'));
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('42')->inNetwork('192.168.1.0/4');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('192.168.1.256')->inNetwork('192.168.1.0/24');
$this->expectException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('192.168.1.0')->inNetwork('192.168.1.0');
}
public function testIpv6Compress(): void
{
$this->assertEquals('::1', IP::parse('0:0:0:0:0:0:0:1'));
$this->assertSame('::1', IP::parse('0:0:0:0:0:0:0:1')->compressed());
$this->assertSame('::', IP::parse('0:0:0:0:0:0:0:0')->compressed());
$this->assertSame('::', IP::parse('0000:0000:0000:0000:0000:0000:0000:0000')->compressed());
$this->assertSame('2001:db8:85a3::8a2e:370:7334', IP::parse('2001:0db8:85a3:0000:0000:8a2e:0370:7334')->compressed());
}
public function testIpv6Uncompress(): void
{
$this->assertSame('0000:0000:0000:0000:0000:0000:0000:0001', IP::parse('::1')->uncompressed());
$this->assertSame('0000:0000:0000:0000:0000:0000:0000:0000', IP::parse('::')->uncompressed());
$this->assertSame('2001:0db8:85a3:0000:0000:8a2e:0370:7334', IP::parse('2001:db8:85a3::8a2e:370:7334')->uncompressed());
$this->assertSame('2001:0db8:85a3:0001:0001:8a2e:0370:7334', IP::parse('2001:db8:85a3:1:1:8a2e:370:7334')->uncompressed());
}
public function testNetworkFromIp(): void
{
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.34')->getNetwork(24));
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.0/24')->getNetwork());
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.255/24')->getNetwork());
$this->assertSame('192.168.1.0', IP::parse('192.168.1.34')->getNetworkAddress(24));
$this->assertSame('192.168.16.0/20', IP::parse('192.168.23.45')->getNetwork(20));
$this->assertSame('2001:db8:85a3::/64', IP::parse('2001:db8:85a3:0:341a:8a2e:0370:7334')->getNetwork(64));
$this->assertSame('2001:db8:85a3:3400::/54', IP::parse('2001:db8:85a3:369a::370:7334/54')->getNetwork());
$this->assertSame('2001:db8:85a3:3600::/55', IP::parse('2001:db8:85a3:369a::370:7334/55')->getNetwork());
$this->assertSame('2001:db8:85a3:341a::370:7334/128', IP::parse('2001:db8:85a3:341a::370:7334')->getNetwork());
$this->assertSame('2001:db8:85a3:341a::370:7334', IP::parse('2001:db8:85a3:341a::370:7334/128')->getNetworkAddress());
}
public function testToSnmpIndex(): void
{
$this->assertSame('192.168.1.5', IP::parse('192.168.1.5')->toSnmpIndex());
$this->assertSame('32.1.8.120.224.0.130.226.134.161.0.0.0.0.0.0', IP::parse('2001:878:e000:82e2:86a1:0000:0000:0000')->toSnmpIndex());
$this->assertSame('0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1', IP::parse('::1')->toSnmpIndex());
$this->assertSame('32.1.8.120.0.0.224.0.0.130.0.226.0.136.0.161', IP::parse('2001:0878:0000:e000:0082:00e2:0088:00a1')->toSnmpIndex());
}
}