|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Badcow DNS Library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Samuel Williams <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Badcow\DNS\Tests; |
|
13
|
|
|
|
|
14
|
|
|
use Badcow\DNS\Classes; |
|
15
|
|
|
use Badcow\DNS\Rdata\NS; |
|
16
|
|
|
use Badcow\DNS\Validator; |
|
17
|
|
|
use Badcow\DNS\Rdata\Factory; |
|
18
|
|
|
use Badcow\DNS\ResourceRecord; |
|
19
|
|
|
|
|
20
|
|
|
class ValidatorTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
public function testValidateResourceRecordName() |
|
23
|
|
|
{ |
|
24
|
|
|
//Pass cases |
|
25
|
|
|
$fqdn1 = 'example.com.'; |
|
26
|
|
|
$fqdn2 = 'www.example.com.'; |
|
27
|
|
|
$fqdn3 = 'ex-ample.com.'; |
|
28
|
|
|
$fqdn4 = 'ex-ampl3.com.au.'; |
|
29
|
|
|
$fqdn5 = '@'; |
|
30
|
|
|
$fqdn6 = 'alt2.aspmx.l.google.com.'; |
|
31
|
|
|
$fqdn7 = 'www.eXAMple.cOm.'; |
|
32
|
|
|
$fqdn8 = '3xample.com.'; |
|
33
|
|
|
$fqdn9 = '_example.com.'; |
|
34
|
|
|
|
|
35
|
|
|
//Fail cases |
|
36
|
|
|
$fqdn10 = '-example.com.'; |
|
37
|
|
|
|
|
38
|
|
|
//Pass cases |
|
39
|
|
|
$uqdn1 = 'example.com'; |
|
40
|
|
|
$uqdn2 = 'www.example.com'; |
|
41
|
|
|
$uqdn3 = 'example'; |
|
42
|
|
|
$uqdn4 = '@'; |
|
43
|
|
|
$uqdn5 = 'wWw.EXample.com'; |
|
44
|
|
|
|
|
45
|
|
|
//Fail cases |
|
46
|
|
|
$uqdn6 = 'exam*ple.com'; |
|
47
|
|
|
$uqdn7 = 'wheres-wally?.com'; |
|
48
|
|
|
$uqdn9 = '-example.com.'; |
|
49
|
|
|
|
|
50
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn1)); |
|
51
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn2)); |
|
52
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn3)); |
|
53
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn4)); |
|
54
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn5)); |
|
55
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn6)); |
|
56
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn7)); |
|
57
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn8)); |
|
58
|
|
|
$this->assertTrue(Validator::resourceRecordName($fqdn9)); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertFalse(Validator::resourceRecordName($fqdn10)); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertTrue(Validator::resourceRecordName($uqdn1)); |
|
63
|
|
|
$this->assertTrue(Validator::resourceRecordName($uqdn2)); |
|
64
|
|
|
$this->assertTrue(Validator::resourceRecordName($uqdn3)); |
|
65
|
|
|
$this->assertTrue(Validator::resourceRecordName($uqdn4)); |
|
66
|
|
|
$this->assertTrue(Validator::resourceRecordName($uqdn5)); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertFalse(Validator::resourceRecordName($uqdn6)); |
|
69
|
|
|
$this->assertFalse(Validator::resourceRecordName($uqdn7)); |
|
70
|
|
|
$this->assertFalse(Validator::resourceRecordName($uqdn9)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testValidateIpv4Address() |
|
74
|
|
|
{ |
|
75
|
|
|
$valid1 = '119.15.101.102'; |
|
76
|
|
|
$valid2 = '255.0.0.255'; |
|
77
|
|
|
$valid3 = '192.168.0.0'; |
|
78
|
|
|
$valid4 = '0.0.0.0'; |
|
79
|
|
|
|
|
80
|
|
|
$invalid1 = '192.168.1.'; |
|
81
|
|
|
$invalid2 = '172.10.256.1'; |
|
82
|
|
|
$invalid3 = '255.244'; |
|
83
|
|
|
$invalid4 = '::1'; |
|
84
|
|
|
$invalid5 = '2001:db8::ff00:42:8329'; |
|
85
|
|
|
|
|
86
|
|
|
$this->assertTrue(Validator::ipv4($valid1)); |
|
87
|
|
|
$this->assertTrue(Validator::ipv4($valid2)); |
|
88
|
|
|
$this->assertTrue(Validator::ipv4($valid3)); |
|
89
|
|
|
$this->assertTrue(Validator::ipv4($valid4)); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertFalse(Validator::ipv4($invalid1)); |
|
92
|
|
|
$this->assertFalse(Validator::ipv4($invalid2)); |
|
93
|
|
|
$this->assertFalse(Validator::ipv4($invalid3)); |
|
94
|
|
|
$this->assertFalse(Validator::ipv4($invalid4)); |
|
95
|
|
|
$this->assertFalse(Validator::ipv4($invalid5)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testValidateIpv6Address() |
|
99
|
|
|
{ |
|
100
|
|
|
$valid1 = '2001:0db8:0000:0000:0000:ff00:0042:8329'; |
|
101
|
|
|
$valid2 = '2001:db8:0:0:0:ff00:42:8329'; |
|
102
|
|
|
$valid3 = '2001:db8::ff00:42:8329'; |
|
103
|
|
|
$valid4 = '::1'; |
|
104
|
|
|
|
|
105
|
|
|
$invalid1 = 'fffff:0db8:0000:0000:0000:ff00:0042:8329'; |
|
106
|
|
|
$invalid2 = '172.10.255.1'; |
|
107
|
|
|
$invalid3 = '192.168.0.0'; |
|
108
|
|
|
|
|
109
|
|
|
$this->assertTrue(Validator::ipv6($valid1)); |
|
110
|
|
|
$this->assertTrue(Validator::ipv6($valid2)); |
|
111
|
|
|
$this->assertTrue(Validator::ipv6($valid3)); |
|
112
|
|
|
$this->assertTrue(Validator::ipv6($valid4)); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertFalse(Validator::ipv6($invalid1)); |
|
115
|
|
|
$this->assertFalse(Validator::ipv6($invalid2)); |
|
116
|
|
|
$this->assertFalse(Validator::ipv6($invalid3)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testValidateIpAddress() |
|
120
|
|
|
{ |
|
121
|
|
|
$valid1 = '2001:0db8:0000:0000:0000:ff00:0042:8329'; |
|
122
|
|
|
$valid2 = '2001:db8:0:0:0:ff00:42:8329'; |
|
123
|
|
|
$valid3 = '2001:db8::ff00:42:8329'; |
|
124
|
|
|
$valid4 = '::1'; |
|
125
|
|
|
$valid5 = '119.15.101.102'; |
|
126
|
|
|
$valid6 = '255.0.0.255'; |
|
127
|
|
|
$valid7 = '192.168.0.0'; |
|
128
|
|
|
$valid8 = '0.0.0.0'; |
|
129
|
|
|
|
|
130
|
|
|
$invalid1 = '192.168.1.'; |
|
131
|
|
|
$invalid2 = '172.10.256.1'; |
|
132
|
|
|
$invalid3 = '255.244'; |
|
133
|
|
|
$invalid4 = 'fffff:0db8:0000:0000:0000:ff00:0042:8329'; |
|
134
|
|
|
|
|
135
|
|
|
$this->assertTrue(Validator::ipAddress($valid1)); |
|
136
|
|
|
$this->assertTrue(Validator::ipAddress($valid2)); |
|
137
|
|
|
$this->assertTrue(Validator::ipAddress($valid3)); |
|
138
|
|
|
$this->assertTrue(Validator::ipAddress($valid4)); |
|
139
|
|
|
$this->assertTrue(Validator::ipAddress($valid5)); |
|
140
|
|
|
$this->assertTrue(Validator::ipAddress($valid6)); |
|
141
|
|
|
$this->assertTrue(Validator::ipAddress($valid7)); |
|
142
|
|
|
$this->assertTrue(Validator::ipAddress($valid8)); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertFalse(Validator::ipAddress($invalid1)); |
|
145
|
|
|
$this->assertFalse(Validator::ipAddress($invalid2)); |
|
146
|
|
|
$this->assertFalse(Validator::ipAddress($invalid3)); |
|
147
|
|
|
$this->assertFalse(Validator::ipAddress($invalid4)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @expectedExceptionMessage There must be exactly one SOA record, 2 given. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function testValidateNumberOfSoa() |
|
154
|
|
|
{ |
|
155
|
|
|
$zone = $this->buildTestZone(); |
|
156
|
|
|
$soa = new ResourceRecord(); |
|
157
|
|
|
$soa->setClass(Classes::INTERNET); |
|
158
|
|
|
$soa->setName('@'); |
|
159
|
|
|
$soa->setRdata(Factory::Soa( |
|
160
|
|
|
'example.com.', |
|
161
|
|
|
'postmaster.example.com.', |
|
162
|
|
|
date('Ymd01'), |
|
163
|
|
|
3600, |
|
164
|
|
|
14400, |
|
165
|
|
|
604800, |
|
166
|
|
|
3600 |
|
167
|
|
|
)); |
|
168
|
|
|
$zone->addResourceRecord($soa); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertEquals(Validator::ZONE_TOO_MANY_SOA, Validator::zone($zone)); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @expectedExceptionMessage There must be exactly one type of class, 2 given. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function testValidateNumberOfClasses() |
|
177
|
|
|
{ |
|
178
|
|
|
$zone = $this->buildTestZone(); |
|
179
|
|
|
$a = new ResourceRecord(); |
|
180
|
|
|
$a->setName('test'); |
|
181
|
|
|
$a->setClass(Classes::CHAOS); |
|
182
|
|
|
$a->setRdata(Factory::A('192.168.0.1')); |
|
183
|
|
|
$a->setComment('This class does not belong here'); |
|
184
|
|
|
$zone->addResourceRecord($a); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertEquals(Validator::ZONE_TOO_MANY_CLASSES, Validator::zone($zone)); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function testValidateZone() |
|
190
|
|
|
{ |
|
191
|
|
|
$zone = $this->buildTestZone(); |
|
192
|
|
|
|
|
193
|
|
|
//Remove the NS records. |
|
194
|
|
|
foreach ($zone as $resourceRecord) { |
|
195
|
|
|
if (NS::TYPE === $resourceRecord->getType()) { |
|
196
|
|
|
$zone->remove($resourceRecord); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$soa = new ResourceRecord(); |
|
201
|
|
|
$soa->setClass(Classes::INTERNET); |
|
202
|
|
|
$soa->setName('@'); |
|
203
|
|
|
$soa->setRdata(Factory::Soa( |
|
204
|
|
|
'example.com.', |
|
205
|
|
|
'postmaster.example.com.', |
|
206
|
|
|
date('Ymd01'), |
|
207
|
|
|
3600, |
|
208
|
|
|
14400, |
|
209
|
|
|
604800, |
|
210
|
|
|
3600 |
|
211
|
|
|
)); |
|
212
|
|
|
$a = new ResourceRecord(); |
|
213
|
|
|
$a->setName('test'); |
|
214
|
|
|
$a->setClass(Classes::CHAOS); |
|
215
|
|
|
$a->setRdata(Factory::A('192.168.0.1')); |
|
216
|
|
|
$a->setComment('This class does not belong here'); |
|
217
|
|
|
|
|
218
|
|
|
$zone->addResourceRecord($a); |
|
219
|
|
|
$zone->addResourceRecord($soa); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertTrue((bool) (Validator::ZONE_TOO_MANY_CLASSES & Validator::zone($zone))); |
|
222
|
|
|
$this->assertTrue((bool) (Validator::ZONE_NO_NS & Validator::zone($zone))); |
|
223
|
|
|
$expectation = Validator::ZONE_NO_NS | Validator::ZONE_TOO_MANY_CLASSES | Validator::ZONE_TOO_MANY_SOA; |
|
224
|
|
|
$this->assertEquals($expectation, Validator::zone($zone)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testZone() |
|
228
|
|
|
{ |
|
229
|
|
|
$zone = $this->buildTestZone(); |
|
230
|
|
|
$this->assertEquals(Validator::ZONE_OKAY, Validator::zone($zone)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function testWildcard() |
|
234
|
|
|
{ |
|
235
|
|
|
$valid_1 = '*.example.com.'; |
|
236
|
|
|
$valid_2 = '*'; |
|
237
|
|
|
$valid_3 = '*.sub'; |
|
238
|
|
|
$valid_4 = '*.sub.domain'; |
|
239
|
|
|
$valid_5 = '*.sub.example.com.'; |
|
240
|
|
|
|
|
241
|
|
|
$invalid_1 = '*abc.example.com.'; |
|
242
|
|
|
$invalid_2 = 'domain.*.example.com.'; |
|
243
|
|
|
$invalid_3 = 'example.com.*'; |
|
244
|
|
|
$invalid_4 = '*.'; |
|
245
|
|
|
|
|
246
|
|
|
$this->assertTrue(Validator::resourceRecordName($valid_1)); |
|
247
|
|
|
$this->assertTrue(Validator::resourceRecordName($valid_2)); |
|
248
|
|
|
$this->assertTrue(Validator::resourceRecordName($valid_3)); |
|
249
|
|
|
$this->assertTrue(Validator::resourceRecordName($valid_4)); |
|
250
|
|
|
$this->assertTrue(Validator::resourceRecordName($valid_5)); |
|
251
|
|
|
|
|
252
|
|
|
$this->assertFalse(Validator::resourceRecordName($invalid_1)); |
|
253
|
|
|
$this->assertFalse(Validator::resourceRecordName($invalid_2)); |
|
254
|
|
|
$this->assertFalse(Validator::resourceRecordName($invalid_3)); |
|
255
|
|
|
$this->assertFalse(Validator::resourceRecordName($invalid_4)); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function testReverseIpv4() |
|
259
|
|
|
{ |
|
260
|
|
|
$valid_01 = '10.IN-ADDR.ARPA.'; |
|
261
|
|
|
$valid_02 = '10.IN-ADDR.ARPA.'; |
|
262
|
|
|
$valid_03 = '18.IN-addr.ARPA.'; |
|
263
|
|
|
$valid_04 = '26.IN-ADdr.ArpA.'; |
|
264
|
|
|
$valid_05 = '22.0.2.10.IN-ADDR.ARPA.'; |
|
265
|
|
|
$valid_06 = '103.0.0.26.IN-ADDR.ARPA.'; |
|
266
|
|
|
$valid_07 = '77.0.0.10.IN-ADDR.ARPA.'; |
|
267
|
|
|
$valid_08 = '4.0.10.18.IN-ADDR.ARPA.'; |
|
268
|
|
|
$valid_09 = '103.0.3.26.IN-ADDR.ARPA.'; |
|
269
|
|
|
$valid_10 = '6.0.0.10.IN-ADDR.ARPA.'; |
|
270
|
|
|
|
|
271
|
|
|
$invalid_01 = '10.IN-ADDR.ARPA'; |
|
272
|
|
|
$invalid_02 = '10.20.ARPA.'; |
|
273
|
|
|
$invalid_03 = '10.123.0.1.INADDR.ARPA.'; |
|
274
|
|
|
$invalid_04 = '10.1.1.1.1.in-addr.arpa.'; |
|
275
|
|
|
$invalid_05 = '10.1.256.7.in-addr.arpa.'; |
|
276
|
|
|
|
|
277
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_01)); |
|
278
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_02)); |
|
279
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_03)); |
|
280
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_04)); |
|
281
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_05)); |
|
282
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_06)); |
|
283
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_07)); |
|
284
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_08)); |
|
285
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_09)); |
|
286
|
|
|
$this->assertTrue(Validator::reverseIpv4($valid_10)); |
|
287
|
|
|
|
|
288
|
|
|
$this->assertFalse(Validator::reverseIpv4($invalid_01)); |
|
289
|
|
|
$this->assertFalse(Validator::reverseIpv4($invalid_02)); |
|
290
|
|
|
$this->assertFalse(Validator::reverseIpv4($invalid_03)); |
|
291
|
|
|
$this->assertFalse(Validator::reverseIpv4($invalid_04)); |
|
292
|
|
|
$this->assertFalse(Validator::reverseIpv4($invalid_05)); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function testReverseIpv6() |
|
296
|
|
|
{ |
|
297
|
|
|
$valid_01 = 'b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.'; |
|
298
|
|
|
$valid_02 = '1.0.0.0.6.8.7.0.6.5.a.0.4.0.5.1.2.0.0.3.8.f.0.1.0.0.2.ip6.arpa.'; |
|
299
|
|
|
$invalid_01 = 'b.a.9.8.7.6.5.0.0.g.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.'; |
|
300
|
|
|
|
|
301
|
|
|
$this->assertTrue(Validator::reverseIpv6($valid_01)); |
|
302
|
|
|
$this->assertTrue(Validator::reverseIpv6($valid_02)); |
|
303
|
|
|
$this->assertFalse(Validator::reverseIpv6($invalid_01)); |
|
304
|
|
|
|
|
305
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($valid_01)); |
|
306
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($valid_02)); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
public function testResourceRecordName() |
|
310
|
|
|
{ |
|
311
|
|
|
$case_1 = '*.'; |
|
312
|
|
|
$case_2 = '*.hello.com'; |
|
313
|
|
|
$case_3 = 'www.*.hello.com'; |
|
314
|
|
|
|
|
315
|
|
|
$this->assertFalse(Validator::resourceRecordName($case_1)); |
|
316
|
|
|
$this->assertTrue(Validator::resourceRecordName($case_2)); |
|
317
|
|
|
$this->assertFalse(Validator::resourceRecordName($case_3)); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
public function testFqdn() |
|
321
|
|
|
{ |
|
322
|
|
|
//Pass cases |
|
323
|
|
|
$fqdn1 = 'example.com.'; |
|
324
|
|
|
$fqdn2 = 'www.example.com.'; |
|
325
|
|
|
$fqdn3 = 'ex-ample.com.'; |
|
326
|
|
|
$fqdn4 = 'ex-ampl3.com.au.'; |
|
327
|
|
|
$fqdn5 = 'alt2.aspmx.l.google.com.'; |
|
328
|
|
|
$fqdn6 = 'www.eXAMple.cOm.'; |
|
329
|
|
|
$fqdn7 = '3xample.com.'; |
|
330
|
|
|
|
|
331
|
|
|
//Fail cases |
|
332
|
|
|
$fqdn8 = '_example.com.'; |
|
333
|
|
|
$fqdn9 = '-example.com.'; |
|
334
|
|
|
$fqdn10 = 'example.com'; |
|
335
|
|
|
$fqdn11 = 'e&le.com.'; |
|
336
|
|
|
|
|
337
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn1)); |
|
338
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn2)); |
|
339
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn3)); |
|
340
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn4)); |
|
341
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn5)); |
|
342
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn6)); |
|
343
|
|
|
$this->assertTrue(Validator::fullyQualifiedDomainName($fqdn7)); |
|
344
|
|
|
|
|
345
|
|
|
$this->assertFalse(Validator::fullyQualifiedDomainName($fqdn8)); |
|
346
|
|
|
$this->assertFalse(Validator::fullyQualifiedDomainName($fqdn9)); |
|
347
|
|
|
$this->assertFalse(Validator::fullyQualifiedDomainName($fqdn10)); |
|
348
|
|
|
$this->assertFalse(Validator::fullyQualifiedDomainName($fqdn11)); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
public function testHostName() |
|
352
|
|
|
{ |
|
353
|
|
|
$this->assertTrue(Validator::hostName('ya-hoo123')); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|