Skip to content

Commit

Permalink
Add all tests for character string
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Feb 20, 2025
1 parent f6f5caa commit ebd9be3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/Fields/CharacterString.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ class CharacterString implements Field{

/**
* @param string $value
* @throws DNSFieldException
*/
public function __construct(string $value){
if(strlen($value)>255){
throw new DNSFieldException("Character string can have 255 characters at most.");
}
$this->value = $value;
}

Expand All @@ -31,7 +35,7 @@ public function serializeToPresentationFormat(): string{
$backslashEscapedValue = str_replace(self::BACKSLASH,self::BACKSLASH.self::BACKSLASH,$this->value);
$escapedValue = str_replace(self::QUOTE,self::BACKSLASH.self::QUOTE,$backslashEscapedValue);
//TODO Check only spaces, or also other whitespaces
if(str_contains($this->value,' ')){
if($this->value==='' || str_contains($this->value,' ')){
return self::QUOTE.($escapedValue).self::QUOTE;
}
return $escapedValue;
Expand Down
6 changes: 3 additions & 3 deletions tests/Fields/BitmapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class BitmapTest extends TestCase{
* @throws DNSFieldException
*/
public function testConstructor(): void{
self::assertInstanceOf(Bitmap::class,(new Bitmap([1,2,3,4])));
self::assertInstanceOf(Bitmap::class,(new Bitmap([1,2,3,4,5])));
self::assertInstanceOf(Bitmap::class,(new Bitmap([1,2,3,4,5,6])));
self::assertInstanceOf(Bitmap::class,new Bitmap([1,2,3,4]));
self::assertInstanceOf(Bitmap::class,new Bitmap([1,2,3,4,5]));
self::assertInstanceOf(Bitmap::class,new Bitmap([1,2,3,4,5,6]));
}

/**
Expand Down
88 changes: 88 additions & 0 deletions tests/Fields/CharacterStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
namespace YOCLIB\DNS\Tests\Fields;

use PHPUnit\Framework\TestCase;

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

class CharacterStringTest extends TestCase{

/**
* @return void
* @throws DNSFieldException
*/
public function testConstructor(): void{
self::assertInstanceOf(CharacterString::class,new CharacterString(''));
self::assertInstanceOf(CharacterString::class,new CharacterString('Text'));
self::assertInstanceOf(CharacterString::class,new CharacterString('Text with space'));
self::assertInstanceOf(CharacterString::class,new CharacterString('Text "with" quote'));
}

/**
* @return void
* @throws DNSFieldException
*/
public function testTooLong(): void{
self::expectException(DNSFieldException::class);
self::expectExceptionMessage('Character string can have 255 characters at most.');

new CharacterString('0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF');
}

/**
* @return void
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame('',(new CharacterString(''))->getValue());
self::assertSame('Text',(new CharacterString('Text'))->getValue());
self::assertSame('Text with space',(new CharacterString('Text with space'))->getValue());
self::assertSame('Text "with" quote',(new CharacterString('Text "with" quote'))->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame('""',(new CharacterString(''))->serializeToPresentationFormat());
self::assertSame('Text',(new CharacterString('Text'))->serializeToPresentationFormat());
self::assertSame('"Text with space"',(new CharacterString('Text with space'))->serializeToPresentationFormat());
self::assertSame('"Text \"with\" quote"',(new CharacterString('Text "with" quote'))->serializeToPresentationFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x00",(new CharacterString(''))->serializeToWireFormat());
self::assertSame("\x04Text",(new CharacterString('Text'))->serializeToWireFormat());
self::assertSame("\x0FText with space",(new CharacterString('Text with space'))->serializeToWireFormat());
self::assertSame("\x11Text \"with\" quote",(new CharacterString('Text "with" quote'))->serializeToWireFormat());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromPresentationFormat(): void{
self::assertSame('',CharacterString::deserializeFromPresentationFormat('""')->getValue());
self::assertSame('Text',CharacterString::deserializeFromPresentationFormat('Text')->getValue());
self::assertSame('Text with space',CharacterString::deserializeFromPresentationFormat('"Text with space"')->getValue());
self::assertSame('Text "with" quote',CharacterString::deserializeFromPresentationFormat('"Text \"with\" quote"')->getValue());
}

/**
* @return void
* @throws DNSFieldException
*/
public function testDeserializeFromWireFormat(): void{
self::assertSame('',CharacterString::deserializeFromWireFormat("\x00")->getValue());
self::assertSame('Text',CharacterString::deserializeFromWireFormat("\x04Text")->getValue());
self::assertSame('Text with space',CharacterString::deserializeFromWireFormat("\x0FText with space")->getValue());
self::assertSame('Text "with" quote',CharacterString::deserializeFromWireFormat("\x11Text \"with\" quote")->getValue());
}

}
11 changes: 0 additions & 11 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\CharacterString;
use YOCLIB\DNS\Fields\FQDN;
use YOCLIB\DNS\Fields\IPv4Address;
use YOCLIB\DNS\Fields\IPv6Address;
Expand All @@ -19,7 +18,6 @@ class FieldsTest extends TestCase{
* @throws DNSFieldException
*/
public function testGetValue(): void{
self::assertSame('This is text',(new CharacterString('This is text'))->getValue());
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());
Expand All @@ -34,7 +32,6 @@ public function testGetValue(): void{
* @throws DNSFieldException
*/
public function testSerializeToPresentationFormat(): void{
self::assertSame('"This is text"',(new CharacterString('This is text'))->serializeToPresentationFormat());
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());
Expand All @@ -49,7 +46,6 @@ public function testSerializeToPresentationFormat(): void{
* @throws DNSFieldException
*/
public function testSerializeToWireFormat(): void{
self::assertSame("\x0CThis is text",(new CharacterString('This is text'))->serializeToWireFormat());
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());
Expand All @@ -64,13 +60,6 @@ public function testSerializeToWireFormat(): void{
* @throws DNSFieldException
*/
public function testAll(): void{
self::assertEquals("\x04Text",CharacterString::deserializeFromPresentationFormat('Text')->serializeToWireFormat());
self::assertEquals("Text",CharacterString::deserializeFromWireFormat("\x04Text")->serializeToPresentationFormat());
self::assertEquals("\x0FText with space",CharacterString::deserializeFromPresentationFormat('"Text with space"')->serializeToWireFormat());
self::assertEquals('"Text with space"',CharacterString::deserializeFromWireFormat("\x0FText with space")->serializeToPresentationFormat());
self::assertEquals("\x11Text \"with\" quote",CharacterString::deserializeFromPresentationFormat('"Text \"with\" quote"')->serializeToWireFormat());
self::assertEquals('"Text \"with\" quote"',CharacterString::deserializeFromWireFormat("\x11Text \"with\" quote")->serializeToPresentationFormat());

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());
Expand Down

0 comments on commit ebd9be3

Please sign in to comment.