Completed
Push — master ( ef9665...74ecad )
by Daniel
12:14
created

IPUtilsTest::testAnIPv6WithOptionDisabledIPv6()   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 0
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\Dev\SapphireTest;
12
use SilverStripe\Control\Util\IPUtils;
13
14
class IPUtilsTest extends SapphireTest {
15
	/**
16
	 * @dataProvider testIPv4Provider
17
	 */
18
	public function testIPv4($matches, $remoteAddr, $cidr)
19
	{
20
		$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr));
21
	}
22
23
	public function testIPv4Provider()
24
	{
25
		return array(
26
			array(true, '192.168.1.1', '192.168.1.1'),
27
			array(true, '192.168.1.1', '192.168.1.1/1'),
28
			array(true, '192.168.1.1', '192.168.1.0/24'),
29
			array(false, '192.168.1.1', '1.2.3.4/1'),
30
			array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
31
			array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
32
			array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
33
			array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
34
			array(true, '1.2.3.4', '0.0.0.0/0'),
35
			array(true, '1.2.3.4', '192.168.1.0/0'),
36
			array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
37
			array(false, 'an_invalid_ip', '192.168.1.0/24'),
38
		);
39
	}
40
41
	/**
42
	 * @dataProvider testIPv6Provider
43
	 */
44
	public function testIPv6($matches, $remoteAddr, $cidr)
45
	{
46
		if (!defined('AF_INET6')) {
47
			$this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
48
		}
49
50
		$this->assertSame($matches, IPUtils::checkIP($remoteAddr, $cidr));
51
	}
52
53
	public function testIPv6Provider()
54
	{
55
		return array(
56
			array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
57
			array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
58
			array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
59
			array(true, '0:0:0:0:0:0:0:1', '::1'),
60
			array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
61
			array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
62
			array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
63
			array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
64
			array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
65
			array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
66
		);
67
	}
68
69
	/**
70
	 * @expectedException \RuntimeException
71
	 * @requires extension sockets
72
	 */
73
	public function testAnIPv6WithOptionDisabledIPv6()
74
	{
75
		if (defined('AF_INET6')) {
76
			$this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
77
		}
78
79
		IPUtils::checkIP('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
80
	}
81
}
82