index/tests/IPAddressRangeTest.php

61 lines
1.8 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// IPAddressRangeTest.php
// Created: 2021-04-27
// Updated: 2024-08-01
2022-09-13 13:13:11 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
use Index\Net\{IPAddress,IPAddressRange};
2022-09-13 13:13:11 +00:00
#[CoversClass(IPAddressRange::class)]
#[UsesClass(IPAddress::class)]
2022-09-13 13:13:11 +00:00
final class IPAddressRangeTest extends TestCase {
public function testParsing(): void {
$v4 = IPAddressRange::parse('127.0.0.1/23');
$v6 = IPAddressRange::parse('::1/128');
$this->expectException(InvalidArgumentException::class);
$v0 = IPAddressRange::parse('12.4:424::1/128');
}
public function testToString(): void {
$v4 = IPAddressRange::parse('53.64.123.86/18');
$this->assertEquals('53.64.123.86/18', (string)$v4);
$v6 = IPAddressRange::parse('abcd:1234::43:211a/86');
$this->assertEquals('abcd:1234::43:211a/86', (string)$v6);
}
public function testMatch(): void {
$v41 = IPAddress::parse('53.64.123.86');
$v42 = IPAddress::parse('53.64.130.86');
$v61 = IPAddress::parse('abcd:1234::43:211a');
$v62 = IPAddress::parse('bbce:1535::73:212a');
$r4 = IPAddressRange::parse('53.64.123.0/22');
$r6 = IPAddressRange::parse('abcd:1234::43:211a/86');
$this->assertTrue($r4->match($v41));
$this->assertFalse($r4->match($v42));
$this->assertTrue($r6->match($v61));
$this->assertFalse($r6->match($v62));
}
public function testSerializable(): void {
$cidrs = [
'53.64.123.0/22',
'abcd:1234::43:211a/86',
];
foreach($cidrs as $cidr) {
$obj = IPAddressRange::parse($cidr);
$ser = serialize($obj);
$unser = unserialize($ser);
$this->assertTrue($obj->equals($unser));
}
}
}