1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\NetworkUtilities\Tests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Yiisoft\NetworkUtilities\IpHelper; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class IpHelperTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider getIpVersionProvider |
14
|
|
|
*/ |
15
|
|
|
public function testGetIpVersion(string $value, bool $validation, ?int $expectedVersion, ?string $expectedException = null, string $message = ''): void |
16
|
|
|
{ |
17
|
|
|
if ($expectedException !== null) { |
18
|
|
|
$this->expectException($expectedException); |
19
|
|
|
} |
20
|
|
|
$version = IpHelper::getIpVersion($value, $validation); |
21
|
|
|
if ($expectedException === null) { |
22
|
|
|
$this->assertSame($expectedVersion, $version, $message); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getIpVersionProvider(): array |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'emptyString' => ['', false, null, \InvalidArgumentException::class], |
30
|
|
|
'emptyStringValidate' => ['', true, null, \InvalidArgumentException::class], |
31
|
|
|
'tooShort' => ['1', false, null, \InvalidArgumentException::class], |
32
|
|
|
'tooShortValidate' => ['1', true, null, \InvalidArgumentException::class], |
33
|
|
|
'ipv4Minimal' => ['0.0.0.0', false, IpHelper::IPV4], |
34
|
|
|
'ipv4TooShort' => ['0.0.0.', false, null, \InvalidArgumentException::class], |
35
|
|
|
'ipv4' => ['192.168.0.1', false, IpHelper::IPV4], |
36
|
|
|
'ipv4Max' => ['255.255.255.255', false, IpHelper::IPV4], |
37
|
|
|
'ipv4MaxValidation' => ['255.255.255.255', true, IpHelper::IPV4], |
38
|
|
|
'ipv4OverMax' => ['255.255.255.256', true, null, \InvalidArgumentException::class], |
39
|
|
|
'ipv4Cidr' => ['192.168.0.1/24', false, IpHelper::IPV4, null, 'IPv4 with CIDR is resolved correctly'], |
40
|
|
|
'ipv4CidrValidation' => ['192.168.0.1/24', true, null, \InvalidArgumentException::class], |
41
|
|
|
'ipv6' => ['fb01::1', false, IpHelper::IPV6], |
42
|
|
|
'ipv6Cidr' => ['fb01::1/24', false, IpHelper::IPV6, null, 'IPv6 with CIDR is resolved correctly'], |
43
|
|
|
'ipv6CidrValidation' => ['fb01::1/24', true, null, \InvalidArgumentException::class], |
44
|
|
|
'ipv6Minimal' => ['::', false, IpHelper::IPV6], |
45
|
|
|
'ipv6MinimalValidation' => ['::', true, IpHelper::IPV6], |
46
|
|
|
'ipv6MappedIpv4' => ['::ffff:192.168.0.2', false, IpHelper::IPV6], |
47
|
|
|
'ipv6MappedIpv4Validation' => ['::ffff:192.168.0.2', true, IpHelper::IPV6], |
48
|
|
|
'ipv6Full' => ['fa01:0000:0000:0000:0000:0000:0000:0001', false, IpHelper::IPV6], |
49
|
|
|
'ipv6FullValidation' => ['fa01:0000:0000:0000:0000:0000:0000:0001', true, IpHelper::IPV6], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider expandIpv6Provider |
55
|
|
|
*/ |
56
|
|
|
public function testExpandIpv6(string $value, string $expected): void |
57
|
|
|
{ |
58
|
|
|
$expanded = IpHelper::expandIPv6($value); |
59
|
|
|
$this->assertSame($expected, $expanded); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function expandIpv6Provider(): array |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
['fa01::1', 'fa01:0000:0000:0000:0000:0000:0000:0001'], |
66
|
|
|
['2001:db0:1:2::7', '2001:0db0:0001:0002:0000:0000:0000:0007'], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testIpv6ExpandingWithInvalidValue(): void |
71
|
|
|
{ |
72
|
|
|
$this->expectException(\InvalidArgumentException::class); |
73
|
|
|
IpHelper::expandIPv6('fa01::1/64'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @dataProvider ip2binProvider |
78
|
|
|
*/ |
79
|
|
|
public function testIp2bin(string $value, string $expected): void |
80
|
|
|
{ |
81
|
|
|
$result = IpHelper::ip2bin($value); |
82
|
|
|
$this->assertSame($expected, $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function ip2binProvider(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
['192.168.1.1', '11000000101010000000000100000001'], |
89
|
|
|
['fa01:0000:0000:0000:0000:0000:0000:0001', '11111010000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'], |
90
|
|
|
['fa01::1', '11111010000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'], |
91
|
|
|
['2620:0:2d0:200::7', '00100110001000000000000000000000000000101101000000000010000000000000000000000000000000000000000000000000000000000000000000000111'], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider inRangeProvider |
97
|
|
|
*/ |
98
|
|
|
public function testInRange(string $value, string $range, bool $expected): void |
99
|
|
|
{ |
100
|
|
|
$result = IpHelper::inRange($value, $range); |
101
|
|
|
$this->assertSame($expected, $result); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function inRangeProvider(): array |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
['192.168.1.1/24', '192.168.0.0/23', true], |
108
|
|
|
['192.168.1.1/24', '192.168.0.0/24', false], |
109
|
|
|
['192.168.1.1/24', '0.0.0.0/0', true], |
110
|
|
|
['192.168.1.1/32', '192.168.1.1', true], |
111
|
|
|
['192.168.1.1/32', '192.168.1.1/32', true], |
112
|
|
|
['192.168.1.1', '192.168.1.1/32', true], |
113
|
|
|
['fa01::1/128', 'fa01::/64', true], |
114
|
|
|
['fa01::1/128', 'fa01::1/128', true], |
115
|
|
|
['fa01::1/64', 'fa01::1/128', false], |
116
|
|
|
['2620:0:0:0:0:0:0:0', '2620:0:2d0:200::7/32', true], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getCidrBitsDataProvider(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
'invalidEmpty' => ['', null, \InvalidArgumentException::class], |
124
|
|
|
'invalidTooShort' => ['1', null, \InvalidArgumentException::class], |
125
|
|
|
'invalidIp' => ['999.999.999.999', null, \InvalidArgumentException::class], |
126
|
|
|
'invalidIpCidr' => ['999.999.999.999/22', null, \InvalidArgumentException::class], |
127
|
|
|
'shortestIp' => ['::', 128], |
128
|
|
|
'ipv4' => ['127.0.0.1', 32], |
129
|
|
|
'ipv6' => ['::1', 128], |
130
|
|
|
'ipv4-negative' => ['127.0.0.1/-1', null, \InvalidArgumentException::class], |
131
|
|
|
'ipv4-min' => ['127.0.0.1/0', 0], |
132
|
|
|
'ipv4-normal' => ['127.0.0.1/13', 13], |
133
|
|
|
'ipv4-max' => ['127.0.0.1/32', 32], |
134
|
|
|
'ipv4-overflow' => ['127.0.0.1/33', null, \InvalidArgumentException::class], |
135
|
|
|
'ipv6-negative' => ['::1/-1', null, \InvalidArgumentException::class], |
136
|
|
|
'ipv6-min' => ['::1/0', 0], |
137
|
|
|
'ipv6-normal' => ['::1/72', 72], |
138
|
|
|
'ipv6-normalExpanded' => ['2001:0db8:85a3:0000:0000:8a2e:0370:7334/23', 23], |
139
|
|
|
'ipv6-normalIpv4Mapped' => ['::ffff:192.0.2.128/109', 109], |
140
|
|
|
'ipv6-max' => ['::1/128', 128], |
141
|
|
|
'ipv6-overflow' => ['::1/129', null, \InvalidArgumentException::class], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @dataProvider getCidrBitsDataProvider |
147
|
|
|
*/ |
148
|
|
|
public function testGetCidrBits(string $ip, ?int $expectedCidr, ?string $expectedException = null): void |
149
|
|
|
{ |
150
|
|
|
if ($expectedException !== null) { |
151
|
|
|
$this->expectException($expectedException); |
152
|
|
|
} |
153
|
|
|
$this->assertSame($expectedCidr, IpHelper::getCidrBits($ip)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function ipv4RegexpDataProvider(): array |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
|
|
'min' => ['0.0.0.0', 1], |
160
|
|
|
'max' => ['255.255.255.255', 1], |
161
|
|
|
'overflow' => ['255.255.255.256', 0], |
162
|
|
|
'overflow2' => ['256.0.0.0', 0], |
163
|
|
|
'random' => ['31.88.247.9', 1], |
164
|
|
|
'broken' => ['1.', 0], |
165
|
|
|
'broken2' => ['1.1', 0], |
166
|
|
|
'broken3' => ['1.1.', 0], |
167
|
|
|
'broken4' => ['1.1.1.', 0], |
168
|
|
|
'empty' => ['', 0], |
169
|
|
|
'invalid' => ['apple', 0], |
170
|
|
|
'trailingLeadingSpace' => [' 0.0.0.0 ', 0], |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @dataProvider ipv4RegexpDataProvider |
176
|
|
|
*/ |
177
|
|
|
public function testIpv4Regexp(string $ip, int $result): void |
178
|
|
|
{ |
179
|
|
|
$this->assertSame($result, preg_match(IpHelper::IPV4_REGEXP, $ip)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function ipv6RegexpDataProvider(): array |
183
|
|
|
{ |
184
|
|
|
return [ |
185
|
|
|
'shortest' => ['::', 1], |
186
|
|
|
'full' => ['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 1], |
187
|
|
|
'overflow' => ['ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffg', 0], |
188
|
|
|
'overflow2' => ['fffg:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 0], |
189
|
|
|
'empty' => ['', 0], |
190
|
|
|
'invalid' => ['apple', 0], |
191
|
|
|
'trailingLeadingSpace' => [' :: ', 0], |
192
|
|
|
'mappedIpv4' => ['::ffff:192.168.0.2', 1], |
193
|
|
|
'random' => ['2001:db8:a::123', 1], |
194
|
|
|
'random2' => ['2001:db8:85a3::8a2e:370:7334', 1], |
195
|
|
|
'allHexCharacters' => ['0123:4567:89ab:cdef:CDEF:AB12:1:1', 1], |
196
|
|
|
'variableGroupLength' => ['1:12:123:1234::', 1] |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @dataProvider ipv6RegexpDataProvider |
202
|
|
|
*/ |
203
|
|
|
public function testIpv6Regexp(string $ip, int $result): void |
204
|
|
|
{ |
205
|
|
|
$this->assertSame($result, preg_match(IpHelper::IPV6_REGEXP, $ip)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|