Passed
Push — master ( b978c0...c5c7e6 )
by Adrien
02:35
created

testMultiplesWillMatchesIfAtLeastOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Validator;
6
7
use Ecodev\Felix\Validator\IPRange;
8
use PHPUnit\Framework\TestCase;
9
10
class IPRangeTest extends TestCase
11
{
12
    /**
13
     * @dataProvider IPv4Data
14
     * @dataProvider IPv6Data
15
     */
16
    public function testMatches(bool $result, string $remoteAddr, string $cidr): void
17
    {
18
        self::assertSame($result, IPRange::matches($remoteAddr, [$cidr]));
19
    }
20
21
    public static function IPv4Data(): iterable
22
    {
23
        return [
24
            'valid - exact (no mask; /32 equiv)' => [true, '192.168.1.1', '192.168.1.1'],
25
            'valid - entirety of class-c (/1)' => [true, '192.168.1.1', '192.168.1.1/1'],
26
            'valid - class-c private subnet (/24)' => [true, '192.168.1.1', '192.168.1.0/24'],
27
            'valid - any subnet (/0)' => [true, '1.2.3.4', '0.0.0.0/0'],
28
            'valid - subnet expands to all' => [true, '1.2.3.4', '192.168.1.0/0'],
29
            'invalid - class-a invalid subnet' => [false, '192.168.1.1', '1.2.3.4/1'],
30
            'invalid - CIDR mask out-of-range' => [false, '192.168.1.1', '192.168.1.1/33'],
31
            'invalid - invalid cidr notation' => [false, '1.2.3.4', '256.256.256/0'],
32
            'invalid - invalid IP address' => [false, 'an_invalid_ip', '192.168.1.0/24'],
33
            'invalid - empty IP address' => [false, '', '1.2.3.4/1'],
34
            'invalid - proxy wildcard' => [false, '192.168.20.13', '*'],
35
            'invalid - proxy missing netmask' => [false, '192.168.20.13', '0.0.0.0'],
36
            'invalid - request IP with invalid proxy wildcard' => [false, '0.0.0.0', '*'],
37
        ];
38
    }
39
40
    public static function IPv6Data(): iterable
41
    {
42
        return [
43
            'valid - ipv4 subnet' => [true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
44
            'valid - exact' => [true, '0:0:0:0:0:0:0:1', '::1'],
45
            'valid - all subnets' => [true, '0:0:603:0:396e:4789:8e99:0001', '::/0'],
46
            'valid - subnet expands to all' => [true, '0:0:603:0:396e:4789:8e99:0001', '2a01:198:603:0::/0'],
47
            'invalid - not in subnet' => [false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
48
            'invalid - does not match exact' => [false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'],
49
            'invalid - compressed notation, does not match exact' => [false, '0:0:603:0:396e:4789:8e99:0001', '::1'],
50
            'invalid - garbage IP' => [false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'],
51
            'invalid - invalid cidr' => [false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'],
52
            'invalid - empty IP address' => [false, '', '::1'],
53
        ];
54
    }
55
56
    public function testMultiplesWillMatchesIfAtLeastOne(): void
57
    {
58
        self::assertTrue(IPRange::matches('192.168.1.1', ['255.255.255.255', '192.168.1.1']));
59
    }
60
}
61