-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d1d9ce
commit a55f1e1
Showing
4 changed files
with
637 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tests\ArrayAccess\DnsRecord\Packet; | ||
|
||
use ArrayAccess\DnsRecord\Exceptions\LengthException; | ||
use ArrayAccess\DnsRecord\Packet\Header; | ||
use PHPUnit\Framework\TestCase; | ||
use Throwable; | ||
use function sprintf; | ||
|
||
class HeaderTest extends TestCase | ||
{ | ||
/** | ||
* Just test header exception | ||
* | ||
* @see \Tests\ArrayAccess\DnsRecord\Utils\BufferTest::testCreateQuestionMessage() | ||
*/ | ||
public function testFromMessage() | ||
{ | ||
try { | ||
Header::fromMessage(''); | ||
} catch (Throwable $e) { | ||
$this->assertInstanceOf( | ||
LengthException::class, | ||
$e, | ||
sprintf( | ||
'%1$s::fromMessage("") should throw %2$s', | ||
Header::class, | ||
LengthException::class | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tests\ArrayAccess\DnsRecord\Packet; | ||
|
||
use ArrayAccess\DnsRecord\Packet\Message; | ||
use PHPUnit\Framework\TestCase; | ||
use function md5; | ||
use function microtime; | ||
use function sprintf; | ||
|
||
class MessageTest extends TestCase | ||
{ | ||
public function testToString() | ||
{ | ||
$message = md5(microtime()); | ||
$this->assertSame( | ||
(string) new Message($message), | ||
$message, | ||
sprintf( | ||
'%s->__tostring() should identical "%2$s"', | ||
Message::class, | ||
$message | ||
) | ||
); | ||
} | ||
|
||
public function testGetMessage() | ||
{ | ||
$message = md5(microtime()); | ||
$this->assertSame( | ||
(new Message($message))->getMessage(), | ||
$message, | ||
sprintf( | ||
'%s->getMessage() should identical "%2$s"', | ||
Message::class, | ||
$message | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tests\ArrayAccess\DnsRecord\Utils; | ||
|
||
use ArrayAccess\DnsRecord\Packet\Header; | ||
use ArrayAccess\DnsRecord\Packet\Question; | ||
use ArrayAccess\DnsRecord\Utils\Buffer; | ||
use ArrayAccess\DnsRecord\Utils\Lookup; | ||
use PHPUnit\Framework\TestCase; | ||
use function sprintf; | ||
use function strlen; | ||
|
||
class BufferTest extends TestCase | ||
{ | ||
|
||
public function testRead() | ||
{ | ||
$printed = 'printed'; | ||
$skipped = '_skipped'; | ||
$buffer = $printed.$skipped; | ||
$readLength = 7; | ||
$offset = 0; | ||
$read = Buffer::read($buffer, $offset, $readLength); | ||
$this->assertSame( | ||
$printed, | ||
$read, | ||
sprintf( | ||
'%s::read("%2$s", $offset, 7) should return "%3$s"', | ||
Buffer::class, | ||
$buffer, | ||
$printed | ||
) | ||
); | ||
$this->assertSame( | ||
$readLength, | ||
$offset, | ||
sprintf( | ||
'Offset from %s::read("%2$s", $offset, 7) should %3$d', | ||
Buffer::class, | ||
$buffer, | ||
$readLength | ||
) | ||
); | ||
// offset maximum should strlen($buffer) | ||
$read = Buffer::read($buffer, $offset, 1000); | ||
$this->assertSame( | ||
$skipped, | ||
$read, | ||
sprintf( | ||
'%s::read("%2$s", $offset, 1000) should return "%3$s"', | ||
Buffer::class, | ||
$buffer, | ||
$skipped | ||
) | ||
); | ||
$this->assertSame( | ||
strlen($buffer), | ||
$offset, | ||
sprintf( | ||
'Offset from %s::read("%2$s", $offset, 1000) should %3$d', | ||
Buffer::class, | ||
$buffer, | ||
strlen($buffer) | ||
) | ||
); | ||
} | ||
|
||
public function testCreateHeaderMessage() | ||
{ | ||
$header = Header::createQueryHeader(); | ||
$this->assertSame( | ||
$header->getMessage(), | ||
Buffer::createHeaderMessage($header), | ||
sprintf( | ||
'%1$s::createHeaderMessage($header) should identical with $header->getMessage()', | ||
Buffer::class | ||
) | ||
); | ||
} | ||
|
||
public function testCompressLabel() | ||
{ | ||
$domain = 'example.com'; | ||
$label = Buffer::compressLabel($domain); | ||
$this->assertNotSame( | ||
$domain, | ||
$label, | ||
sprintf( | ||
'%1$s::compressLabel("%2$s") should not identical with "%2$s"', | ||
Buffer::class, | ||
$domain | ||
) | ||
); | ||
} | ||
|
||
public function testReadLabel() | ||
{ | ||
$offset = 0; | ||
$domain = 'example.com'; | ||
$label = Buffer::compressLabel($domain); | ||
$read = Buffer::readLabel($label, $offset); | ||
$this->assertGreaterThan( | ||
0, | ||
$offset, | ||
sprintf( | ||
'Offset from %1$s::readLabel($label, $offset) should greater than 0', | ||
Buffer::class | ||
) | ||
); | ||
$this->assertNotSame( | ||
$label, | ||
$read, | ||
sprintf( | ||
'%1$s::readLabel($label, $offset) should not identical with $label', | ||
Buffer::class | ||
) | ||
); | ||
$this->assertSame( | ||
$domain, | ||
$read, | ||
sprintf( | ||
'%1$s::readLabel($label, $offset) should identical with $label', | ||
Buffer::class | ||
) | ||
); | ||
$this->assertEmpty( | ||
Buffer::readLabel($label, $offset), | ||
sprintf( | ||
'With next offset of %1$s::readLabel($label, $offset) should empty', | ||
Buffer::class | ||
) | ||
); | ||
$underscore = 'example_com'; // underscore separator | ||
// fallback offset | ||
$offset = 0; | ||
$read = Buffer::readLabel($label, $offset, '_'); | ||
$this->assertGreaterThan( | ||
0, | ||
$offset, | ||
sprintf( | ||
'Offset from %1$s::readLabel($label, $offset, "_") should greater than 0', | ||
Buffer::class | ||
) | ||
); | ||
$this->assertNotSame( | ||
$label, | ||
$read, | ||
sprintf( | ||
'%1$s::readLabel($label, $offset, "_") should not identical with $label', | ||
Buffer::class | ||
) | ||
); | ||
$this->assertNotSame( | ||
$domain, | ||
$read, | ||
sprintf( | ||
'%1$s::readLabel($label, $offset, "_") should not identical with "%2$s"', | ||
Buffer::class, | ||
$domain | ||
) | ||
); | ||
$this->assertSame( | ||
$underscore, | ||
$read, | ||
sprintf( | ||
'%1$s::readLabel($label, $offset) should identical with "%2$s"', | ||
Buffer::class, | ||
$underscore | ||
) | ||
); | ||
} | ||
|
||
public function testCompressHeader() | ||
{ | ||
$header = Buffer::compressHeader( | ||
'A', | ||
'IN' | ||
); | ||
$this->assertGreaterThanOrEqual( | ||
10, | ||
strlen($header), | ||
sprintf( | ||
'%1$s::compressHeader("A", "IN") length should 10 or greater', | ||
Buffer::class | ||
) | ||
); | ||
} | ||
|
||
public function testCreateQuestionMessage() | ||
{ | ||
$question = Question::fromFilteredResponse( | ||
'example.com', | ||
Lookup::RR_TYPES['A'], | ||
Lookup::QCLASS_IN | ||
); | ||
$header = Buffer::compressHeader( | ||
$question->getType(), | ||
$question->getClass() | ||
); | ||
$message = Buffer::createQuestionMessage($question); | ||
// header is on the end of binary string | ||
$this->assertStringEndsWith( | ||
$header, | ||
$message, | ||
sprintf( | ||
'%1$s::createQuestionMessage(%2$s) should ending with header', | ||
Buffer::class, | ||
Question::class, | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.