summaryrefslogtreecommitdiffstats
path: root/tests/Inc/IpUtilTest.php
blob: 11f30a5cfc4674021be61632a8011b9d668384b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

use PHPUnit\Framework\TestCase;

class IpUtilTest extends TestCase
{
	public function testParseCidrWithSlashNotation(): void
	{
		$res = IpUtil::parseCidr('192.168.1/24');
		$this->assertNotNull($res);
		$this->assertSame(ip2long('192.168.1.0'), $res['start']);
		$this->assertSame(ip2long('192.168.1.255'), $res['end']);
	}

	public function testParseCidrSingleIp(): void
	{
		$res = IpUtil::parseCidr('10.0.0.1');
		$this->assertNotNull($res);
		$this->assertSame(ip2long('10.0.0.1'), $res['start']);
		$this->assertSame(ip2long('10.0.0.1'), $res['end']);
	}

	public function testParseCidrInvalid(): void
	{
		$this->assertNull(IpUtil::parseCidr('1.2.3.4/33'));
		$this->assertNull(IpUtil::parseCidr('not-an-ip'));
	}

	public function testIsValidSubnetRange(): void
	{
		$start = ip2long('192.168.0.0');
		$end = ip2long('192.168.0.255');
		$this->assertTrue(IpUtil::isValidSubnetRange($start, $end));

		$start = ip2long('192.168.0.1');
		$end = ip2long('192.168.0.254');
		$this->assertFalse(IpUtil::isValidSubnetRange($start, $end));

		// single IP should be considered a valid /32 range
		$ip = ip2long('10.0.0.1');
		$this->assertTrue(IpUtil::isValidSubnetRange($ip, $ip));
	}

	public function testRangeToCidrValid(): void
	{
		$start = ip2long('192.168.2.0');
		$end = ip2long('192.168.2.255');
		$this->assertSame('192.168.2.0/24', IpUtil::rangeToCidr($start, $end));
	}

	public function testRangeToCidrNotSubnet(): void
	{
		$start = ip2long('192.168.5.1');
		$end = ip2long('192.168.5.254');
		$this->assertSame(
			'NOT SUBNET: 192.168.5.1-192.168.5.254',
			IpUtil::rangeToCidr($start, $end)
		);
	}

	public function testNormalizeIp(): void
	{
		$this->assertSame('1.0.0.1', IpUtil::normalizeIp('1.1'));
		$this->assertSame('1.2.2.88', IpUtil::normalizeIp('1.2.600'));
		$this->assertSame('1.219.1.1', IpUtil::normalizeIp('1.0333.1.1'));
		$this->assertSame('127.0.0.1', IpUtil::normalizeIp('::ffff:127.0.0.1'));
		$this->assertSame('127.0.0.1', IpUtil::normalizeIp('0x7f.0.0.1'));
		$this->assertSame('127.0.0.1', IpUtil::normalizeIp('0177.0.0.1'));
		$this->assertSame('127.0.0.1', IpUtil::normalizeIp('2130706433'));
		$this->assertSame('127.0.0.1', IpUtil::normalizeIp('0x7f000001'));
		$this->assertSame('::1', IpUtil::normalizeIp('::1'));
		$this->assertNull(IpUtil::normalizeIp('256.1.1.1'));
		$this->assertNull(IpUtil::normalizeIp('1.2.3.4.5'));
		$this->assertNull(IpUtil::normalizeIp('0x100000000'));
		$this->assertNull(IpUtil::normalizeIp('not an ip'));
		$this->assertNull(IpUtil::normalizeIp('::ffff:256.1.1.1'));
	}

	public function testIsValidIp(): void
	{
		$this->assertTrue(IpUtil::isValidIp('1.1'));
		$this->assertTrue(IpUtil::isValidIp('127.0.0.1'));
		$this->assertTrue(IpUtil::isValidIp('::1'));
		$this->assertFalse(IpUtil::isValidIp('256.1.1.1'));
		$this->assertFalse(IpUtil::isValidIp('not an ip'));
	}
}