index/tests/IpAddressRangeTest.php

61 lines
1.8 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
2024-10-02 01:35:05 +00:00
// IpAddressRangeTest.php
2022-09-13 13:13:11 +00:00
// Created: 2021-04-27
2024-10-02 01:35:05 +00:00
// Updated: 2024-10-02
2022-09-13 13:13:11 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
2024-10-02 01:35:05 +00:00
use Index\Net\{IpAddress,IpAddressRange};
2022-09-13 13:13:11 +00:00
2024-10-02 01:35:05 +00:00
#[CoversClass(IpAddressRange::class)]
#[UsesClass(IpAddress::class)]
final class IpAddressRangeTest extends TestCase {
2022-09-13 13:13:11 +00:00
public function testParsing(): void {
2024-10-02 01:35:05 +00:00
$v4 = IpAddressRange::parse('127.0.0.1/23');
$v6 = IpAddressRange::parse('::1/128');
2022-09-13 13:13:11 +00:00
$this->expectException(InvalidArgumentException::class);
2024-10-02 01:35:05 +00:00
$v0 = IpAddressRange::parse('12.4:424::1/128');
2022-09-13 13:13:11 +00:00
}
public function testToString(): void {
2024-10-02 01:35:05 +00:00
$v4 = IpAddressRange::parse('53.64.123.86/18');
2022-09-13 13:13:11 +00:00
$this->assertEquals('53.64.123.86/18', (string)$v4);
2024-10-02 01:35:05 +00:00
$v6 = IpAddressRange::parse('abcd:1234::43:211a/86');
2022-09-13 13:13:11 +00:00
$this->assertEquals('abcd:1234::43:211a/86', (string)$v6);
}
public function testMatch(): void {
2024-10-02 01:35:05 +00:00
$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');
2022-09-13 13:13:11 +00:00
2024-10-02 01:35:05 +00:00
$r4 = IpAddressRange::parse('53.64.123.0/22');
$r6 = IpAddressRange::parse('abcd:1234::43:211a/86');
2022-09-13 13:13:11 +00:00
$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) {
2024-10-02 01:35:05 +00:00
$obj = IpAddressRange::parse($cidr);
2022-09-13 13:13:11 +00:00
$ser = serialize($obj);
$unser = unserialize($ser);
$this->assertTrue($obj->equals($unser));
}
}
}