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\APL; |
17
|
|
|
use Badcow\DNS\Rdata\Factory; |
18
|
|
|
use PhpIP\IPBlock; |
19
|
|
|
use PhpIP\IPv4Block; |
20
|
|
|
use PhpIP\IPv6Block; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
|
23
|
|
|
class AplTest extends TestCase |
24
|
|
|
{ |
25
|
|
View Code Duplication |
public function testOutput(): void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$includedRanges = [ |
28
|
|
|
IPBlock::create('192.168.0.0/23'), |
29
|
|
|
IPBlock::create('2001:acad:1::/112'), |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
$excludedRanges = [ |
33
|
|
|
IPBlock::create('192.168.1.64/28'), |
34
|
|
|
IPBlock::create('2001:acad:1::8/128'), |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$apl = Factory::APL($includedRanges, $excludedRanges); |
38
|
|
|
|
39
|
|
|
$expectation = '1:192.168.0.0/23 2:2001:acad:1::/112 !1:192.168.1.64/28 !2:2001:acad:1::8/128'; |
40
|
|
|
$this->assertEquals($expectation, $apl->toText()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testGetters(): void |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$includedRanges = [ |
46
|
|
|
IPBlock::create('192.168.0.0/23'), |
47
|
|
|
IPBlock::create('2001:acad:1::/112'), |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$excludedRanges = [ |
51
|
|
|
IPBlock::create('192.168.1.64/28'), |
52
|
|
|
IPBlock::create('2001:acad:1::8/128'), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$apl = Factory::APL($includedRanges, $excludedRanges); |
56
|
|
|
|
57
|
|
|
$this->assertEquals($includedRanges, $apl->getIncludedAddressRanges()); |
58
|
|
|
$this->assertEquals($excludedRanges, $apl->getExcludedAddressRanges()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public function testFromText(): void |
65
|
|
|
{ |
66
|
|
|
$text = '1:192.168.0.0/23 2:2001:acad:1::/112 !1:192.168.1.64/28 !2:2001:acad:1::8/128'; |
67
|
|
|
$expectation_incl = [ |
68
|
|
|
new IPv4Block('192.168.0.0', 23), |
69
|
|
|
new IPv6Block('2001:acad:1::', 112), |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$expectation_excl = [ |
73
|
|
|
new IPv4Block('192.168.1.64', 28), |
74
|
|
|
new IPv6Block('2001:acad:1::8', 128), |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** @var APL $apl */ |
78
|
|
|
$apl = new APL(); |
79
|
|
|
$apl->fromText($text); |
80
|
|
|
|
81
|
|
|
$this->assertCount(2, $apl->getIncludedAddressRanges()); |
|
|
|
|
82
|
|
|
$this->assertCount(2, $apl->getExcludedAddressRanges()); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->assertEquals($expectation_incl, $apl->getIncludedAddressRanges()); |
85
|
|
|
$this->assertEquals($expectation_excl, $apl->getExcludedAddressRanges()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testWire(): void |
89
|
|
|
{ |
90
|
|
|
$expectation = pack('nCCC4nCC', |
91
|
|
|
1, //Address Family |
92
|
|
|
24, //Prefix |
93
|
|
|
0 + //N: "!" is present |
94
|
|
|
4, //AFD Length |
95
|
|
|
255, 255, 255, 255, //AFDPart |
96
|
|
|
|
97
|
|
|
2, //Address Family |
98
|
|
|
64, //Prefix |
99
|
|
|
128 + //N: "!" is present |
100
|
|
|
16 //AFD Length |
101
|
|
|
).inet_pton('2001:acad:dead:beef::'); //AFDPart |
102
|
|
|
|
103
|
|
|
$apl = new APL(); |
104
|
|
|
$apl->addAddressRange(IPBlock::create('255.255.255.255/24'), true); |
105
|
|
|
$apl->addAddressRange(IPBlock::create('2001:acad:dead:beef::/64'), false); |
106
|
|
|
|
107
|
|
|
$this->assertEquals($expectation, $apl->toWire()); |
108
|
|
|
$aplFromWire = new APL(); |
109
|
|
|
$aplFromWire->fromWire($expectation); |
110
|
|
|
|
111
|
|
|
$this->assertCount(1, $apl->getIncludedAddressRanges()); |
|
|
|
|
112
|
|
|
$this->assertCount(1, $apl->getExcludedAddressRanges()); |
|
|
|
|
113
|
|
|
$this->assertEquals($apl, $aplFromWire); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.