1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?php
namespace React\Tests\Dns\Protocol;
use PHPUnit\Framework\TestCase;
use React\Dns\Protocol\BinaryDumper;
use React\Dns\Model\Message;
class BinaryDumperTest extends TestCase
{
public function testRequestToBinary()
{
$data = "";
$data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
$data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
$data .= "00 01 00 01"; // question: type A, class IN
$expected = $this->formatHexDump(str_replace(' ', '', $data), 2);
$request = new Message();
$request->header->set('id', 0x7262);
$request->header->set('rd', 1);
$request->questions[] = array(
'name' => 'igor.io',
'type' => Message::TYPE_A,
'class' => Message::CLASS_IN,
);
$request->prepare();
$dumper = new BinaryDumper();
$data = $dumper->toBinary($request);
$data = $this->convertBinaryToHexDump($data);
$this->assertSame($expected, $data);
}
private function convertBinaryToHexDump($input)
{
return $this->formatHexDump(implode('', unpack('H*', $input)));
}
private function formatHexDump($input)
{
return implode(' ', str_split($input, 2));
}
}
|