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\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Rdata\AAAA; |
17
|
|
|
use Badcow\DNS\Rdata\DecodeException; |
18
|
|
|
use Badcow\DNS\Rdata\Factory; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class AaaaTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
public function testToText(): void |
24
|
|
|
{ |
25
|
|
|
$address = '2003:dead:beef:4dad:23:46:bb:101'; |
26
|
|
|
$aaaa = new AAAA(); |
27
|
|
|
$aaaa->setAddress($address); |
28
|
|
|
|
29
|
|
|
$this->assertEquals($address, $aaaa->toText()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testSetAddress(): void |
33
|
|
|
{ |
34
|
|
|
$address = 'fe80::1'; |
35
|
|
|
$aaaa = new AAAA(); |
36
|
|
|
$aaaa->setAddress($address); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($address, $aaaa->getAddress()); |
39
|
|
|
|
40
|
|
|
$this->expectException(\InvalidArgumentException::class); |
41
|
|
|
$this->expectExceptionMessage('The address "abc" is not a valid IPv6 address.'); |
42
|
|
|
$aaaa->setAddress('abc'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testFromText(): void |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$text = '2003:dead:beef:4dad:23:46:bb:101'; |
48
|
|
|
/** @var AAAA $aaaa */ |
49
|
|
|
$aaaa = new AAAA(); |
50
|
|
|
$aaaa->fromText($text); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($text, $aaaa->getAddress()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws DecodeException |
57
|
|
|
*/ |
58
|
|
|
public function testToWire(): void |
59
|
|
|
{ |
60
|
|
|
$address = '2003:dead:beef:4dad:23:46:bb:101'; |
61
|
|
|
$expectation = inet_pton($address); |
62
|
|
|
/** @var AAAA $aaaa */ |
63
|
|
|
$aaaa = new AAAA(); |
64
|
|
|
$aaaa->fromWire($expectation); |
65
|
|
|
|
66
|
|
|
$this->assertEquals($expectation, $aaaa->toWire()); |
67
|
|
|
$this->assertEquals($address, $aaaa->getAddress()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testToWireThrowsExceptionIfAddressIsMalformed(): void |
71
|
|
|
{ |
72
|
|
|
$aaaa_prime = new class() extends AAAA { |
73
|
|
|
public function __construct() |
74
|
|
|
{ |
75
|
|
|
$this->address = 'abc'; |
76
|
|
|
} |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
$this->expectException(\InvalidArgumentException::class); |
80
|
|
|
$this->expectExceptionMessage('The IP address "abc" cannot be encoded. Check that it is a valid IP address.'); |
81
|
|
|
$aaaa_prime->toWire(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetType(): void |
85
|
|
|
{ |
86
|
|
|
$aaaa = new AAAA(); |
87
|
|
|
$this->assertEquals('AAAA', $aaaa->getType()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testGetTypeCode(): void |
91
|
|
|
{ |
92
|
|
|
$aaaa = new AAAA(); |
93
|
|
|
$this->assertEquals(28, $aaaa->getTypeCode()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws DecodeException |
98
|
|
|
*/ |
99
|
|
|
public function testFromWire(): void |
100
|
|
|
{ |
101
|
|
|
$wire = chr(0x07).inet_pton('beef::1').chr(0x07); |
102
|
|
|
$offset = 1; |
103
|
|
|
$aaaa = new AAAA(); |
104
|
|
|
$aaaa->fromWire($wire, $offset); |
105
|
|
|
|
106
|
|
|
$this->assertEquals('beef::1', $aaaa->getAddress()); |
107
|
|
|
$this->assertEquals(17, $offset); |
108
|
|
|
|
109
|
|
|
$wire = pack('C3', 0x61, 0x62, 0x63); |
110
|
|
|
$aaaa = new AAAA(); |
111
|
|
|
$this->expectException(DecodeException::class); |
112
|
|
|
$aaaa->fromWire($wire); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testFactory(): void |
116
|
|
|
{ |
117
|
|
|
$aaaa = new AAAA(); |
118
|
|
|
$aaaa->setAddress('2001:acad:1::'); |
119
|
|
|
|
120
|
|
|
$this->assertEquals(Factory::AAAA('2001:acad:1::'), $aaaa); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.