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; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\AlignedBuilder; |
17
|
|
|
use Badcow\DNS\Classes; |
18
|
|
|
use Badcow\DNS\Rdata\Factory; |
19
|
|
|
use Badcow\DNS\ResourceRecord; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
class AlignedBuilderTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
protected $expected = <<< 'DNS' |
25
|
|
|
$ORIGIN example.com. |
26
|
|
|
$TTL 3600 |
27
|
|
|
@ IN SOA ( |
28
|
|
|
example.com. ; MNAME |
29
|
|
|
postmaster.example.com. ; RNAME |
30
|
|
|
2015050801 ; SERIAL |
31
|
|
|
3600 ; REFRESH |
32
|
|
|
14400 ; RETRY |
33
|
|
|
604800 ; EXPIRE |
34
|
|
|
3600 ; MINIMUM |
35
|
|
|
) |
36
|
|
|
|
37
|
|
|
; NS RECORDS |
38
|
|
|
@ 14400 IN NS ns1.example.net.au. |
39
|
|
|
@ 14400 IN NS ns2.example.net.au. |
40
|
|
|
|
41
|
|
|
; A RECORDS |
42
|
|
|
subdomain.au IN A 192.168.1.2; This is a local ip. |
43
|
|
|
|
44
|
|
|
; AAAA RECORDS |
45
|
|
|
ipv6domain 3600 IN AAAA ::1; This is an IPv6 domain. |
46
|
|
|
|
47
|
|
|
; CNAME RECORDS |
48
|
|
|
alias IN CNAME subdomain.au.example.com. |
49
|
|
|
|
50
|
|
|
; DNAME RECORDS |
51
|
|
|
bar.example.com. IN DNAME foo.example.com. |
52
|
|
|
|
53
|
|
|
; MX RECORDS |
54
|
|
|
@ IN MX 10 mail-gw1.example.net. |
55
|
|
|
@ IN MX 20 mail-gw2.example.net. |
56
|
|
|
@ IN MX 30 mail-gw3.example.net. |
57
|
|
|
|
58
|
|
|
; LOC RECORDS |
59
|
|
|
canberra IN LOC ( |
60
|
|
|
35 18 27.000 S ; LATITUDE |
61
|
|
|
149 7 27.840 E ; LONGITUDE |
62
|
|
|
500.00m ; ALTITUDE |
63
|
|
|
20.12m ; SIZE |
64
|
|
|
200.30m ; HORIZONTAL PRECISION |
65
|
|
|
300.10m ; VERTICAL PRECISION |
66
|
|
|
); This is Canberra |
67
|
|
|
|
68
|
|
|
; HINFO RECORDS |
69
|
|
|
@ IN HINFO "2.7GHz" "Ubuntu 12.04" |
70
|
|
|
|
71
|
|
|
; TXT RECORDS |
72
|
|
|
example.net. IN TXT "v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all" |
73
|
|
|
two.cities IN TXT ( |
74
|
|
|
"It was the best of times, it was the wor" |
75
|
|
|
"st of times, it was the age of wisdom, i" |
76
|
|
|
"t was the age of foolishness, it was the" |
77
|
|
|
" epoch of belief, it was the epoch of in" |
78
|
|
|
"credulity, it was the season of Light, i" |
79
|
|
|
"t was the season of Darkness, it was the" |
80
|
|
|
" spring of hope, it was the winter of de" |
81
|
|
|
"spair, we had everything before us, we h" |
82
|
|
|
"ad nothing before us, we were all going " |
83
|
|
|
"direct to Heaven, we were all going dire" |
84
|
|
|
"ct the other way—in short, the period " |
85
|
|
|
"was so far like the present period, that" |
86
|
|
|
" some of its noisiest authorities insist" |
87
|
|
|
"ed on its being received, for good or fo" |
88
|
|
|
"r evil, in the superlative degree of com" |
89
|
|
|
"parison only." |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
; SRV RECORDS |
93
|
|
|
_ftp._tcp IN SRV 10 10 21 files |
94
|
|
|
|
95
|
|
|
; RRSIG RECORDS |
96
|
|
|
example.com. IN RRSIG A 14 2 3600 ( |
97
|
|
|
20200112073532 20191229133101 12345 example.com. |
98
|
|
|
bDts/7a5qbal6s3ZYzS5puPSjEfys5yI |
99
|
|
|
6R/kprBBRDEfVcT6YwPaDT3VkVjKXdvp |
100
|
|
|
KX2/DwpijNAWkjpfsewCLmeImx3Rgkzf |
101
|
|
|
uxfipRKtBUguiPTBhkj/ft2halJziVXl ) |
102
|
|
|
|
103
|
|
|
DNS; |
104
|
|
|
|
105
|
|
|
protected function getExpected(): string |
106
|
|
|
{ |
107
|
|
|
//This is a fix for Windows systems that may expect a carriage return char. |
108
|
|
|
return str_replace("\r", '', $this->expected); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testCompareResourceRecords(): void |
112
|
|
|
{ |
113
|
|
|
$soa = new ResourceRecord(); |
114
|
|
|
$soa->setClass('IN'); |
115
|
|
|
$soa->setName('@'); |
116
|
|
|
$soa->setRdata(Factory::SOA( |
117
|
|
|
'example.com.', |
118
|
|
|
'postmaster.example.com.', |
119
|
|
|
2015050801, |
120
|
|
|
3600, |
121
|
|
|
14400, |
122
|
|
|
604800, |
123
|
|
|
3600 |
124
|
|
|
)); |
125
|
|
|
|
126
|
|
|
$ns1 = new ResourceRecord(); |
127
|
|
|
$ns1->setClass(Classes::INTERNET); |
128
|
|
|
$ns1->setName('@'); |
129
|
|
|
$ns1->setTtl(14400); |
130
|
|
|
$ns1->setRdata(Factory::NS('ns1.example.net.au.')); |
131
|
|
|
|
132
|
|
|
$ns2 = new ResourceRecord(); |
133
|
|
|
$ns2->setClass('IN'); |
134
|
|
|
$ns2->setName('@'); |
135
|
|
|
$ns2->setTtl(14400); |
136
|
|
|
$ns2->setRdata(Factory::NS('ns2.example.net.au.')); |
137
|
|
|
|
138
|
|
|
$a = new ResourceRecord(); |
139
|
|
|
$a->setName('subdomain.au'); |
140
|
|
|
$a->setRdata(Factory::A('192.168.1.2')); |
141
|
|
|
$a->setComment('This is a local ip.'); |
142
|
|
|
|
143
|
|
|
$cname = new ResourceRecord(); |
144
|
|
|
$cname->setName('alias'); |
145
|
|
|
$cname->setRdata(Factory::CNAME('subdomain.au.example.com.')); |
146
|
|
|
$cname->setClass(Classes::INTERNET); |
147
|
|
|
|
148
|
|
|
$aaaa = ResourceRecord::create('ipv6domain', Factory::AAAA('::1'), 3600); |
149
|
|
|
|
150
|
|
|
$mx1 = new ResourceRecord(); |
151
|
|
|
$mx1->setName('@'); |
152
|
|
|
$mx1->setRdata(Factory::MX(10, 'mailgw01.example.net.')); |
153
|
|
|
|
154
|
|
|
$mx2 = new ResourceRecord(); |
155
|
|
|
$mx2->setName('@'); |
156
|
|
|
$mx2->setRdata(Factory::MX(20, 'mailgw02.example.net.')); |
157
|
|
|
|
158
|
|
|
$txt = new ResourceRecord(); |
159
|
|
|
$txt->setName('example.net.'); |
160
|
|
|
$txt->setRdata(Factory::TXT('v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all')); |
161
|
|
|
|
162
|
|
|
$spf = new ResourceRecord(); |
163
|
|
|
$spf->setName('example.com.'); |
164
|
|
|
$spf->setRdata(Factory::SPF('v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all')); |
165
|
|
|
|
166
|
|
|
$rrsig = new ResourceRecord(); |
167
|
|
|
$rrsig->setName('example.com.'); |
168
|
|
|
$rrsig->setRdata(Factory::RRSIG( |
169
|
|
|
'A', |
170
|
|
|
14, |
171
|
|
|
2, |
172
|
|
|
3600, |
173
|
|
|
new \DateTime('2070-01-01 00:00:00'), |
174
|
|
|
new \DateTime('1970-01-01 00:00:00'), |
175
|
|
|
65535, |
176
|
|
|
'example.com.', |
177
|
|
|
'aaaaaaaaaaaaaaaaaaaaaaaaa' |
178
|
|
|
)); |
179
|
|
|
|
180
|
|
|
$nsec3 = new ResourceRecord(); |
181
|
|
|
$nsec3->setName('example.com.'); |
182
|
|
|
$nsec3->setRdata(Factory::NSEC3PARAM(1, 0, 5, '9474017E')); |
183
|
|
|
|
184
|
|
|
$rp = new ResourceRecord(); |
185
|
|
|
$rp->setRdata(Factory::RP('mail.example.com.', 'example.com.')); |
186
|
|
|
|
187
|
|
|
$spf = new ResourceRecord(); |
188
|
|
|
$spf->setRdata(Factory::SPF('skjdfskjasdfjh')); |
189
|
|
|
|
190
|
|
|
$alignedBuilder = new AlignedBuilder(); |
191
|
|
|
|
192
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($soa, $ns1) < 0); |
193
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($aaaa, $cname) < 0); |
194
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($mx1, $mx2) < 0); |
195
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($mx1, $mx2) < 0); |
196
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($mx1, $spf) < 0); |
197
|
|
|
|
198
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($mx1, $a) > 0); |
199
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($ns2, $ns1) > 0); |
200
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($spf, $txt) > 0); |
201
|
|
|
|
202
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($nsec3, $rrsig) < 0); |
203
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($rrsig, $nsec3) > 0); |
204
|
|
|
|
205
|
|
|
$this->assertTrue($alignedBuilder->compareResourceRecords($rp, $spf) < 0); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testBuild(): void |
209
|
|
|
{ |
210
|
|
|
$zone = TestZone::buildTestZone(); |
211
|
|
|
$twoCities = new ResourceRecord(); |
212
|
|
|
$twoCities->setName('two.cities'); |
213
|
|
|
$twoCities->setRdata(Factory::TXT('It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way—in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.')); |
214
|
|
|
|
215
|
|
|
$zone->addResourceRecord($twoCities); |
216
|
|
|
|
217
|
|
|
$resourceRecord = new ResourceRecord(); |
218
|
|
|
$resourceRecord->setName('null'); |
219
|
|
|
$zone->addResourceRecord($resourceRecord); |
220
|
|
|
|
221
|
|
|
$builder = new AlignedBuilder(); |
222
|
|
|
$output = $builder->build($zone); |
223
|
|
|
|
224
|
|
|
$this->assertEquals($this->getExpected(), $output); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|