1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* These helpful tests were lifted from the Symfony library |
7
|
|
|
* https://github.com/symfony/http-foundation/blob/master/LICENSE |
8
|
|
|
* |
9
|
|
|
* (c) Fabien Potencier <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
15
|
|
|
use SilverStripe\Control\Util\IPUtils; |
16
|
|
|
|
17
|
|
|
class IPUtilsTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @dataProvider iPv4Provider |
21
|
|
|
*/ |
22
|
|
|
public function testIPv4($matches, $remoteAddr, $cidr) |
23
|
|
|
{ |
24
|
|
|
$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function iPv4Provider() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
[true, '192.168.1.1', '192.168.1.1'], |
31
|
|
|
[true, '192.168.1.1', '192.168.1.1/1'], |
32
|
|
|
[true, '192.168.1.1', '192.168.1.0/24'], |
33
|
|
|
[false, '192.168.1.1', '1.2.3.4/1'], |
34
|
|
|
[false, '192.168.1.1', '192.168.1.1/33'], // invalid subnet |
35
|
|
|
[true, '192.168.1.1', ['1.2.3.4/1', '192.168.1.0/24']], |
36
|
|
|
[true, '192.168.1.1', ['192.168.1.0/24', '1.2.3.4/1']], |
37
|
|
|
[false, '192.168.1.1', ['1.2.3.4/1', '4.3.2.1/1']], |
38
|
|
|
[true, '1.2.3.4', '0.0.0.0/0'], |
39
|
|
|
[true, '1.2.3.4', '192.168.1.0/0'], |
40
|
|
|
[false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation |
41
|
|
|
[false, 'an_invalid_ip', '192.168.1.0/24'], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider iPv6Provider |
47
|
|
|
*/ |
48
|
|
|
public function testIPv6($matches, $remoteAddr, $cidr) |
49
|
|
|
{ |
50
|
|
|
if (!defined('AF_INET6')) { |
51
|
|
|
$this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function iPv6Provider() |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
[true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'], |
61
|
|
|
[false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'], |
62
|
|
|
[false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'], |
63
|
|
|
[true, '0:0:0:0:0:0:0:1', '::1'], |
64
|
|
|
[false, '0:0:603:0:396e:4789:8e99:0001', '::1'], |
65
|
|
|
[true, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '2a01:198:603:0::/65']], |
66
|
|
|
[true, '2a01:198:603:0:396e:4789:8e99:890f', ['2a01:198:603:0::/65', '::1']], |
67
|
|
|
[false, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '1a01:198:603:0::/65']], |
68
|
|
|
[false, '}__test|O:21:"JDatabaseDriverMysqli":3:{s:2', '::1'], |
69
|
|
|
[false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @requires extension sockets |
75
|
|
|
*/ |
76
|
|
|
public function testAnIPv6WithOptionDisabledIPv6() |
77
|
|
|
{ |
78
|
|
|
$this->expectException(\RuntimeException::class); |
79
|
|
|
$this->expectExceptionMessage(''); |
80
|
|
|
$this->expectExceptionMessageMatches(''); |
81
|
|
|
if (defined('AF_INET6')) { |
82
|
|
|
$this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
IPUtils::checkIP('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|