Completed
Push — member-groupset-delete ( a90a9a )
by Loz
11:22
created

IPUtilsTest::testIPv6()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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