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')); } }