1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Tests\Parser; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Classes; |
17
|
|
|
use Badcow\DNS\Parser\ParseException; |
18
|
|
|
use Badcow\DNS\Parser\Parser; |
19
|
|
|
use Badcow\DNS\Rdata\A; |
20
|
|
|
use Badcow\DNS\Rdata\PolymorphicRdata; |
21
|
|
|
use Badcow\DNS\Rdata\UnknownType; |
22
|
|
|
use Badcow\DNS\ResourceRecord; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
class ParseUnknownTypesTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @throws ParseException |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function testUnknownType(): void |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$text = 'dickens.example.com. CLASS45 1800 TYPE1859 \# 20 412054616c65206f662054776f20436974696573'; |
33
|
|
|
$binData = hex2bin('412054616c65206f662054776f20436974696573'); |
34
|
|
|
$zone = Parser::parse('example.com.', $text); |
35
|
|
|
$this->assertCount(1, $zone); |
36
|
|
|
/** @var ResourceRecord $rr */ |
37
|
|
|
$rr = $zone[0]; |
38
|
|
|
|
39
|
|
|
$this->assertEquals('dickens.example.com.', $rr->getName()); |
40
|
|
|
$this->assertEquals('CLASS45', $rr->getClass()); |
41
|
|
|
$this->assertEquals(45, $rr->getClassId()); |
42
|
|
|
$this->assertEquals('TYPE1859', $rr->getType()); |
43
|
|
|
$this->assertEquals(1800, $rr->getTtl()); |
44
|
|
|
$this->assertInstanceOf(UnknownType::class, $rr->getRdata()); |
45
|
|
|
$this->assertEquals($binData, $rr->getRdata()->getData()); |
|
|
|
|
46
|
|
|
$this->assertEquals(1859, $rr->getRdata()->getTypeCode()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ParseException |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function testPolymorphicType(): void |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$text = 'dickens.example.com. IN 1800 RESERVED "A Tale of Two Cities"'; |
55
|
|
|
$zone = Parser::parse('example.com.', $text); |
56
|
|
|
$this->assertCount(1, $zone); |
57
|
|
|
/** @var ResourceRecord $rr */ |
58
|
|
|
$rr = $zone[0]; |
59
|
|
|
|
60
|
|
|
$this->assertEquals('dickens.example.com.', $rr->getName()); |
61
|
|
|
$this->assertEquals('IN', $rr->getClass()); |
62
|
|
|
$this->assertEquals(1, $rr->getClassId()); |
63
|
|
|
$this->assertEquals('RESERVED', $rr->getType()); |
64
|
|
|
$this->assertEquals(0xffff, $rr->getRdata()->getTypeCode()); |
65
|
|
|
$this->assertEquals(1800, $rr->getTtl()); |
66
|
|
|
$this->assertInstanceOf(PolymorphicRdata::class, $rr->getRdata()); |
67
|
|
|
$this->assertEquals('"A Tale of Two Cities"', $rr->getRdata()->getData()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testSupportedTypeInUnknownFormatIsOutputtedAsCorrectType(): void |
71
|
|
|
{ |
72
|
|
|
// files.example.com. IN 3600 IN A 192.168.1.100 |
73
|
|
|
$record = 'files.example.com. IN 3600 TYPE1 \# 4 c0 a8 01 64'; |
74
|
|
|
$zone = Parser::parse('example.com.', $record); |
75
|
|
|
$this->assertCount(1, $zone); |
76
|
|
|
/** @var ResourceRecord $rr */ |
77
|
|
|
$rr = $zone[0]; |
78
|
|
|
|
79
|
|
|
$this->assertEquals('files.example.com.', $rr->getName()); |
80
|
|
|
$this->assertEquals(Classes::INTERNET, $rr->getClass()); |
81
|
|
|
$this->assertEquals(3600, $rr->getTtl()); |
82
|
|
|
$this->assertInstanceOf(A::class, $rr->getRdata()); |
83
|
|
|
$this->assertEquals('192.168.1.100', $rr->getRdata()->getAddress()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.