Passed
Push — master ( 8ead76...65920b )
by Alexander
09:05
created

IpHelperTest::ip2binProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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