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