Skip to content

Commit

Permalink
Add all tests for FQDN
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 20, 2025
1 parent 62ea78f commit f5ec001
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/Fields/FQDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ class FQDN implements Field{
* @throws DNSFieldException
*/
public function __construct(string... $value){
$totalLength = 0;
foreach($value AS $label){
if(!is_string($label)){
throw new DNSFieldException('Only strings allowed.');
if(strlen($label)>=64){
throw new DNSFieldException('Label too long.');
}
$totalLength += 1 + strlen($label);
}
if($totalLength>=256){
throw new DNSFieldException('Domain name too long.');
}
$this->value = $value;
}
Expand Down
99 changes: 99 additions & 0 deletions tests/Fields/FQDNTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
namespace YOCLIB\DNS\Tests\Fields;

use PHPUnit\Framework\TestCase;

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

class FQDNTest extends TestCase{

/**
* @return void
* @throws DNSFieldException
*/
public function testConstructor(): void{
self::assertInstanceOf(FQDN::class,new FQDN('example','com'));
self::assertInstanceOf(FQDN::class,new FQDN('example','com',''));
self::assertInstanceOf(FQDN::class,new FQDN('www','example','com'));
self::assertInstanceOf(FQDN::class,new FQDN('www','example','com',''));
}

/**
* @return void
* @throws DNSFieldException
*/
public function testTooLongLabel(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Label too long.');

new FQDN('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef','example','com','');
}

/**
* @return void
* @throws DNSFieldException
*/
public function testTooLongDomainName(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Domain name too long.');

new FQDN('0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','0123456789abcdef0123456789abcdef','example','com','');
}

/**
* @return void
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame(['example','com'],(new FQDN('example','com'))->getValue());
self::assertSame(['example','com',''],(new FQDN('example','com',''))->getValue());
self::assertSame(['www','example','com'],(new FQDN('www','example','com'))->getValue());
self::assertSame(['www','example','com',''],(new FQDN('www','example','com',''))->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame('example.com',(new FQDN('example','com'))->serializeToPresentationFormat());
self::assertSame('example.com.',(new FQDN('example','com',''))->serializeToPresentationFormat());
self::assertSame('www.example.com',(new FQDN('www','example','com'))->serializeToPresentationFormat());
self::assertSame('www.example.com.',(new FQDN('www','example','com',''))->serializeToPresentationFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x07example\x03com\x40",(new FQDN('example','com'))->serializeToWireFormat());
self::assertSame("\x07example\x03com\x00",(new FQDN('example','com',''))->serializeToWireFormat());
self::assertSame("\x03www\x07example\x03com\x40",(new FQDN('www','example','com'))->serializeToWireFormat());
self::assertSame("\x03www\x07example\x03com\x00",(new FQDN('www','example','com',''))->serializeToWireFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromPresentationFormat(): void{
self::assertSame(['example','com'],FQDN::deserializeFromPresentationFormat('example.com')->getValue());
self::assertSame(['example','com',''],FQDN::deserializeFromPresentationFormat('example.com.')->getValue());
self::assertSame(['www','example','com'],FQDN::deserializeFromPresentationFormat('www.example.com')->getValue());
self::assertSame(['www','example','com',''],FQDN::deserializeFromPresentationFormat('www.example.com.')->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromWireFormat(): void{
self::assertSame(['example','com'],FQDN::deserializeFromWireFormat("\x07example\x03com\x40")->getValue());
self::assertSame(['example','com',''],FQDN::deserializeFromWireFormat("\x07example\x03com\x00")->getValue());
self::assertSame(['www','example','com'],FQDN::deserializeFromWireFormat("\x03www\x07example\x03com\x40")->getValue());
self::assertSame(['www','example','com',''],FQDN::deserializeFromWireFormat("\x03www\x07example\x03com\x00")->getValue());
}

}
17 changes: 0 additions & 17 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\FQDN;
use YOCLIB\DNS\Fields\IPv4Address;
use YOCLIB\DNS\Fields\IPv6Address;
use YOCLIB\DNS\Fields\UnsignedInteger16;
Expand All @@ -18,8 +17,6 @@ class FieldsTest extends TestCase{
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame(['example','com'],(new FQDN('example','com'))->getValue());
self::assertSame(['example','com',''],(new FQDN('example','com',''))->getValue());
self::assertSame('1.2.3.4',(new IPv4Address('1.2.3.4'))->getValue());
self::assertSame('::',(new IPv6Address('::'))->getValue());
self::assertSame(123,(new UnsignedInteger8(123))->getValue());
Expand All @@ -32,8 +29,6 @@ public function testGetValue(): void{
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame("example.com",(new FQDN('example','com'))->serializeToPresentationFormat());
self::assertSame("example.com.",(new FQDN('example','com',''))->serializeToPresentationFormat());
self::assertSame('1.2.3.4',(new IPv4Address('1.2.3.4'))->serializeToPresentationFormat());
self::assertSame('::',(new IPv6Address('::'))->serializeToPresentationFormat());
self::assertSame('123',(new UnsignedInteger8(123))->serializeToPresentationFormat());
Expand All @@ -46,8 +41,6 @@ public function testSerializeToPresentationFormat(): void{
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x07example\x03com\x40",(new FQDN('example','com'))->serializeToWireFormat());
self::assertSame("\x07example\x03com\x00",(new FQDN('example','com',''))->serializeToWireFormat());
self::assertSame("\x01\x02\x03\x04",(new IPv4Address('1.2.3.4'))->serializeToWireFormat());
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());
Expand All @@ -60,16 +53,6 @@ public function testSerializeToWireFormat(): void{
* @throws DNSFieldException
*/
public function testAll(): void{
self::assertEquals("\x07example\x03com\x00",FQDN::deserializeFromPresentationFormat('example.com.')->serializeToWireFormat());
self::assertEquals("example.com.",FQDN::deserializeFromWireFormat("\x07example\x03com\x00")->serializeToPresentationFormat());
self::assertEquals("\x03www\x07example\x03com\x00",FQDN::deserializeFromPresentationFormat('www.example.com.')->serializeToWireFormat());
self::assertEquals("www.example.com.",FQDN::deserializeFromWireFormat("\x03www\x07example\x03com\x00")->serializeToPresentationFormat());

self::assertEquals("\x07example\x03com\x40",FQDN::deserializeFromPresentationFormat('example.com')->serializeToWireFormat());
self::assertEquals("example.com",FQDN::deserializeFromWireFormat("\x07example\x03com\x40")->serializeToPresentationFormat());
self::assertEquals("\x03www\x07example\x03com\x40",FQDN::deserializeFromPresentationFormat('www.example.com')->serializeToWireFormat());
self::assertEquals("www.example.com",FQDN::deserializeFromWireFormat("\x03www\x07example\x03com\x40")->serializeToPresentationFormat());

self::assertEquals("\x01\x02\x03\x04",IPv4Address::deserializeFromPresentationFormat('1.2.3.4')->serializeToWireFormat());
self::assertEquals("1.2.3.4",IPv4Address::deserializeFromWireFormat("\x01\x02\x03\x04")->serializeToPresentationFormat());

Expand Down

0 comments on commit f5ec001

Please sign in to comment.