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, int $expected, string $message = ''): void |
16
|
|
|
{ |
17
|
|
|
$version = IpHelper::getIpVersion($value); |
18
|
|
|
$this->assertSame($expected, $version, $message); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getIpVersionProvider(): array |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
['192.168.0.1', IpHelper::IPV4], |
25
|
|
|
['192.168.0.1/24', IpHelper::IPV4, 'IPv4 with CIDR is resolved correctly'], |
26
|
|
|
['fb01::1', IpHelper::IPV6], |
27
|
|
|
['fb01::1/24', IpHelper::IPV6, 'IPv6 with CIDR is resolved correctly'], |
28
|
|
|
['', IpHelper::IPV4, 'Empty string is treated as IPv4'], |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @dataProvider expandIpv6Provider |
34
|
|
|
*/ |
35
|
|
|
public function testExpandIpv6(string $value, string $expected): void |
36
|
|
|
{ |
37
|
|
|
$expanded = IpHelper::expandIPv6($value); |
38
|
|
|
$this->assertSame($expected, $expanded); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function expandIpv6Provider(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
['fa01::1', 'fa01:0000:0000:0000:0000:0000:0000:0001'], |
45
|
|
|
['2001:db0:1:2::7', '2001:0db0:0001:0002:0000:0000:0000:0007'], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testIpv6ExpandingWithInvalidValue(): void |
50
|
|
|
{ |
51
|
|
|
$this->expectException(\InvalidArgumentException::class); |
52
|
|
|
IpHelper::expandIPv6('fa01::1/64'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider ip2binProvider |
57
|
|
|
*/ |
58
|
|
|
public function testIp2bin(string $value, string $expected): void |
59
|
|
|
{ |
60
|
|
|
$result = IpHelper::ip2bin($value); |
61
|
|
|
$this->assertSame($expected, $result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function ip2binProvider(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
['192.168.1.1', '11000000101010000000000100000001'], |
68
|
|
|
['', '00000000000000000000000000000000'], |
69
|
|
|
['fa01:0000:0000:0000:0000:0000:0000:0001', '11111010000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'], |
70
|
|
|
['fa01::1', '11111010000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'], |
71
|
|
|
['2620:0:2d0:200::7', '00100110001000000000000000000000000000101101000000000010000000000000000000000000000000000000000000000000000000000000000000000111'], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider inRangeProvider |
77
|
|
|
*/ |
78
|
|
|
public function testInRange(string $value, string $range, bool $expected): void |
79
|
|
|
{ |
80
|
|
|
$result = IpHelper::inRange($value, $range); |
81
|
|
|
$this->assertSame($expected, $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function inRangeProvider(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
['192.168.1.1/24', '192.168.0.0/23', true], |
88
|
|
|
['192.168.1.1/24', '192.168.0.0/24', false], |
89
|
|
|
['192.168.1.1/24', '0.0.0.0/0', true], |
90
|
|
|
['192.168.1.1/32', '192.168.1.1', true], |
91
|
|
|
['192.168.1.1/32', '192.168.1.1/32', true], |
92
|
|
|
['192.168.1.1', '192.168.1.1/32', true], |
93
|
|
|
['fa01::1/128', 'fa01::/64', true], |
94
|
|
|
['fa01::1/128', 'fa01::1/128', true], |
95
|
|
|
['fa01::1/64', 'fa01::1/128', false], |
96
|
|
|
['2620:0:0:0:0:0:0:0', '2620:0:2d0:200::7/32', true], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getCidrBitsDataProvider(): array |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
'invalidEmpty' => ['', null, \InvalidArgumentException::class], |
104
|
|
|
'invalidTooShort' => ['1', null, \InvalidArgumentException::class], |
105
|
|
|
'invalidIp' => ['999.999.999.999', null, \InvalidArgumentException::class], |
106
|
|
|
'invalidIpCidr' => ['999.999.999.999/22', null, \InvalidArgumentException::class], |
107
|
|
|
'shortestIp' => ['::', 128], |
108
|
|
|
'ipv4' => ['127.0.0.1', 32], |
109
|
|
|
'ipv6' => ['::1', 128], |
110
|
|
|
'ipv4-negative' => ['127.0.0.1/-1', null, \InvalidArgumentException::class], |
111
|
|
|
'ipv4-min' => ['127.0.0.1/0', 0], |
112
|
|
|
'ipv4-normal' => ['127.0.0.1/13', 13], |
113
|
|
|
'ipv4-max' => ['127.0.0.1/32', 32], |
114
|
|
|
'ipv4-overflow' => ['127.0.0.1/33', null, \InvalidArgumentException::class], |
115
|
|
|
'ipv6-negative' => ['::1/-1', null, \InvalidArgumentException::class], |
116
|
|
|
'ipv6-min' => ['::1/0', 0], |
117
|
|
|
'ipv6-normal' => ['::1/72', 72], |
118
|
|
|
'ipv6-normalExpanded' => ['2001:0db8:85a3:0000:0000:8a2e:0370:7334/23', 23], |
119
|
|
|
'ipv6-normalIpv4Mapped' => ['::ffff:192.0.2.128/109', 109], |
120
|
|
|
'ipv6-max' => ['::1/128', 128], |
121
|
|
|
'ipv6-overflow' => ['::1/129', null, \InvalidArgumentException::class], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @dataProvider getCidrBitsDataProvider |
127
|
|
|
*/ |
128
|
|
|
public function testGetCidrBits(string $ip, ?int $expectedCidr, ?string $expectedException = null): void |
129
|
|
|
{ |
130
|
|
|
if ($expectedException !== null) { |
131
|
|
|
$this->expectException($expectedException); |
132
|
|
|
} |
133
|
|
|
$this->assertSame($expectedCidr, IpHelper::getCidrBits($ip)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|