Failed Conditions
Pull Request — master (#24)
by Adrien
03:04
created

IPRangeTest::IPv4Data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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