Skip to content

Commit

Permalink
Add all tests for IPv6 address
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 20, 2025
1 parent b25006f commit a91a750
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 7 deletions.
110 changes: 110 additions & 0 deletions tests/Fields/IPv6AddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
namespace YOCLIB\DNS\Tests\Fields;

use PHPUnit\Framework\TestCase;

use YOCLIB\DNS\Exceptions\DNSFieldException;
use YOCLIB\DNS\Fields\IPv6Address;

class IPv6AddressTest extends TestCase{

/**
* @return void
* @throws DNSFieldException
*/
public function testConstructor(): void{
self::assertInstanceOf(IPv6Address::class,new IPv6Address('::'));
self::assertInstanceOf(IPv6Address::class,new IPv6Address('::1'));
self::assertInstanceOf(IPv6Address::class,new IPv6Address('fe80::'));
self::assertInstanceOf(IPv6Address::class,new IPv6Address('fe80::1'));
}

/**
* @return void
* @throws DNSFieldException
*/
public function testNotIPv4(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Human readable IPv6 address isn\'t valid.');

new IPv6Address('0.0.0.0');
}

/**
* @return void
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame('::',(new IPv6Address('::'))->getValue());
self::assertSame('::1',(new IPv6Address('::1'))->getValue());
self::assertSame('fe80::',(new IPv6Address('fe80::'))->getValue());
self::assertSame('fe80::1',(new IPv6Address('fe80::1'))->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame('::',(new IPv6Address('::'))->serializeToPresentationFormat());
self::assertSame('::1',(new IPv6Address('::1'))->serializeToPresentationFormat());
self::assertSame('fe80::',(new IPv6Address('fe80::'))->serializeToPresentationFormat());
self::assertSame('fe80::1',(new IPv6Address('fe80::1'))->serializeToPresentationFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",(new IPv6Address('::'))->serializeToWireFormat());
self::assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",(new IPv6Address('::1'))->serializeToWireFormat());
self::assertSame("\xFE\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",(new IPv6Address('fe80::'))->serializeToWireFormat());
self::assertSame("\xFE\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",(new IPv6Address('fe80::1'))->serializeToWireFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromPresentationFormat(): void{
self::assertSame('::',IPv6Address::deserializeFromPresentationFormat('::')->getValue());
self::assertSame('::1',IPv6Address::deserializeFromPresentationFormat('::1')->getValue());
self::assertSame('fe80::',IPv6Address::deserializeFromPresentationFormat('fe80::')->getValue());
self::assertSame('fe80::1',IPv6Address::deserializeFromPresentationFormat('fe80::1')->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromWireFormat(): void{
self::assertSame('::',IPv6Address::deserializeFromWireFormat("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")->getValue());
self::assertSame('::1',IPv6Address::deserializeFromWireFormat("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")->getValue());
self::assertSame('fe80::',IPv6Address::deserializeFromWireFormat("\xFE\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")->getValue());
self::assertSame('fe80::1',IPv6Address::deserializeFromWireFormat("\xFE\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromWireFormatTooLessData(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Binary IPv6 address should be 16 octets.');

IPv6Address::deserializeFromWireFormat("\xFE\x80\x00\x00\x00\x00\x00\x00\x00");
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromWireFormatTooMuchData(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Binary IPv6 address should be 16 octets.');

IPv6Address::deserializeFromWireFormat("\xFE\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01");
}

}
7 changes: 0 additions & 7 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use PHPUnit\Framework\TestCase;

use YOCLIB\DNS\Exceptions\DNSFieldException;
use YOCLIB\DNS\Fields\IPv6Address;
use YOCLIB\DNS\Fields\UnsignedInteger16;
use YOCLIB\DNS\Fields\UnsignedInteger32;
use YOCLIB\DNS\Fields\UnsignedInteger8;
Expand All @@ -16,7 +15,6 @@ class FieldsTest extends TestCase{
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame('::',(new IPv6Address('::'))->getValue());
self::assertSame(123,(new UnsignedInteger8(123))->getValue());
self::assertSame(1234,(new UnsignedInteger16(1234))->getValue());
self::assertSame(12345678,(new UnsignedInteger32(12345678))->getValue());
Expand All @@ -27,7 +25,6 @@ public function testGetValue(): void{
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame('::',(new IPv6Address('::'))->serializeToPresentationFormat());
self::assertSame('123',(new UnsignedInteger8(123))->serializeToPresentationFormat());
self::assertSame('1234',(new UnsignedInteger16(1234))->serializeToPresentationFormat());
self::assertSame('12345678',(new UnsignedInteger32(12345678))->serializeToPresentationFormat());
Expand All @@ -38,7 +35,6 @@ public function testSerializeToPresentationFormat(): void{
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",(new IPv6Address('::'))->serializeToWireFormat());
self::assertSame(chr(123),(new UnsignedInteger8(123))->serializeToWireFormat());
self::assertSame(pack('n',1234),(new UnsignedInteger16(1234))->serializeToWireFormat());
self::assertSame(pack('N',12345678),(new UnsignedInteger32(12345678))->serializeToWireFormat());
Expand All @@ -49,9 +45,6 @@ public function testSerializeToWireFormat(): void{
* @throws DNSFieldException
*/
public function testAll(): void{
self::assertEquals("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",IPv6Address::deserializeFromPresentationFormat('::')->serializeToWireFormat());
self::assertEquals("::",IPv6Address::deserializeFromWireFormat("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")->serializeToPresentationFormat());

self::assertEquals("\x7F",UnsignedInteger8::deserializeFromPresentationFormat('127')->serializeToWireFormat());
self::assertEquals("127",UnsignedInteger8::deserializeFromWireFormat("\x7F")->serializeToPresentationFormat());

Expand Down

0 comments on commit a91a750

Please sign in to comment.