-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordTest.php
More file actions
485 lines (419 loc) · 16.6 KB
/
RecordTest.php
File metadata and controls
485 lines (419 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
namespace Tests\Unit\Utopia\DNS\Message;
use PHPUnit\Framework\TestCase;
use Utopia\DNS\Message\Record;
final class RecordTest extends TestCase
{
public function testEncodeARecordMatchesBytes(): void
{
$record = new Record(
name: 'example.com',
type: Record::TYPE_A,
class: Record::CLASS_IN,
ttl: 300,
rdata: '93.184.216.34'
);
// Raw RR: example.com. 300 IN A 93.184.216.34
$expected = "\x07example\x03com\x00"
. "\x00\x01"
. "\x00\x01"
. "\x00\x00\x01\x2C"
. "\x00\x04"
. "\x5D\xB8\xD8\x22";
$this->assertSame($expected, $record->encode());
}
public function testDecodeARecordParsesFields(): void
{
// Raw RR: example.com. 300 IN A 93.184.216.34
$data = "\x07example\x03com\x00"
. "\x00\x01"
. "\x00\x01"
. "\x00\x00\x01\x2C"
. "\x00\x04"
. "\x5D\xB8\xD8\x22";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('example.com', $record->name);
$this->assertSame(Record::TYPE_A, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(300, $record->ttl);
$this->assertSame('93.184.216.34', $record->rdata);
$this->assertNull($record->priority);
$this->assertNull($record->weight);
$this->assertNull($record->port);
$this->assertSame(strlen($data), $offset);
}
public function testEncodeMxRecordMatchesBytes(): void
{
$record = new Record(
name: 'mail.example.com',
type: Record::TYPE_MX,
class: Record::CLASS_IN,
ttl: 3600,
rdata: 'mail.exchange.example.com',
priority: 10
);
// Raw RR: mail.example.com. 3600 IN MX 10 mail.exchange.example.com.
$expected = "\x04mail\x07example\x03com\x00"
. "\x00\x0F"
. "\x00\x01"
. "\x00\x00\x0E\x10"
. "\x00\x1D"
. "\x00\x0A"
. "\x04mail\x08exchange\x07example\x03com\x00";
$this->assertSame($expected, $record->encode());
}
public function testDecodeMxRecordParsesFields(): void
{
// Raw RR: mail.example.com. 3600 IN MX 10 mail.exchange.example.com.
$data = "\x04mail\x07example\x03com\x00"
. "\x00\x0F"
. "\x00\x01"
. "\x00\x00\x0E\x10"
. "\x00\x1D"
. "\x00\x0A"
. "\x04mail\x08exchange\x07example\x03com\x00";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('mail.example.com', $record->name);
$this->assertSame(Record::TYPE_MX, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(3600, $record->ttl);
$this->assertSame(10, $record->priority);
$this->assertSame('mail.exchange.example.com', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testEncodeSrvRecordMatchesBytes(): void
{
$record = new Record(
name: '_sip._tcp.example.com',
type: Record::TYPE_SRV,
class: Record::CLASS_IN,
ttl: 7200,
rdata: 'sip.example.com',
priority: 5,
weight: 10,
port: 5060
);
// Raw RR: _sip._tcp.example.com. 7200 IN SRV 5 10 5060 sip.example.com.
$expected = "\x04_sip\x04_tcp\x07example\x03com\x00"
. "\x00\x21"
. "\x00\x01"
. "\x00\x00\x1C\x20"
. "\x00\x17"
. "\x00\x05\x00\x0A\x13\xC4"
. "\x03sip\x07example\x03com\x00";
$this->assertSame($expected, $record->encode());
}
public function testDecodeSrvRecordParsesFields(): void
{
// Raw RR: _sip._tcp.example.com. 7200 IN SRV 5 10 5060 sip.example.com.
$data = "\x04_sip\x04_tcp\x07example\x03com\x00"
. "\x00\x21"
. "\x00\x01"
. "\x00\x00\x1C\x20"
. "\x00\x17"
. "\x00\x05\x00\x0A\x13\xC4"
. "\x03sip\x07example\x03com\x00";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('_sip._tcp.example.com', $record->name);
$this->assertSame(Record::TYPE_SRV, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(7200, $record->ttl);
$this->assertSame(5, $record->priority);
$this->assertSame(10, $record->weight);
$this->assertSame(5060, $record->port);
$this->assertSame('sip.example.com', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testEncodeTxtRecordMatchesBytes(): void
{
$record = new Record(
name: 'example.com',
type: Record::TYPE_TXT,
class: Record::CLASS_IN,
ttl: 600,
rdata: 'hello'
);
// Raw RR: example.com. 600 IN TXT "hello"
$expected = "\x07example\x03com\x00"
. "\x00\x10"
. "\x00\x01"
. "\x00\x00\x02\x58"
. "\x00\x06"
. "\x05hello";
$this->assertSame($expected, $record->encode());
}
public function testDecodeTxtRecordParsesFields(): void
{
// Raw RR: example.com. 600 IN TXT "hello"
$data = "\x07example\x03com\x00"
. "\x00\x10"
. "\x00\x01"
. "\x00\x00\x02\x58"
. "\x00\x06"
. "\x05hello";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('example.com', $record->name);
$this->assertSame(Record::TYPE_TXT, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(600, $record->ttl);
$this->assertSame('hello', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testDecodeCnameRecordParsesNameRdata(): void
{
// Raw RR: www.example.com. 4000 IN CNAME cdn.example.com.
$data = "\x03www\x07example\x03com\x00"
. "\x00\x05"
. "\x00\x01"
. "\x00\x00\x0F\xA0"
. "\x00\x11"
. "\x03cdn\x07example\x03com\x00";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('www.example.com', $record->name);
$this->assertSame(Record::TYPE_CNAME, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(4000, $record->ttl);
$this->assertSame('cdn.example.com', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testDecodeUnknownRecordKeepsHexData(): void
{
// Raw RR: example.com. 60 IN TYPE65400 RDATA=0x0aff
$data = "\x07example\x03com\x00"
. "\xFE\xF8"
. "\x00\x01"
. "\x00\x00\x00\x3C"
. "\x00\x02"
. "\x0A\xFF";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('example.com', $record->name);
$this->assertSame(0xFEF8, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(60, $record->ttl);
$this->assertSame('0aff', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testDecodeSoaRecordParsesFields(): void
{
// Raw RR: example.com. 3600 IN SOA ns1.example.com. admin.example.com. 2024102701 7200 3600 1209600 86400
$data = "\x07example\x03com\x00"
. "\x00\x06" // TYPE_SOA
. "\x00\x01" // CLASS_IN
. "\x00\x00\x0E\x10" // TTL: 3600
. "\x00\x38" // RDLENGTH: 56 bytes (17 + 19 + 20)
// MNAME: ns1.example.com (17 bytes)
. "\x03ns1\x07example\x03com\x00"
// RNAME: admin.example.com (19 bytes)
. "\x05admin\x07example\x03com\x00"
// Serial: 2024102701 = 0x78a55b2d
. "\x78\xa5\x5b\x2d"
// Refresh: 7200
. "\x00\x00\x1C\x20"
// Retry: 3600
. "\x00\x00\x0E\x10"
// Expire: 1209600
. "\x00\x12\x75\x00"
// Minimum: 86400
. "\x00\x01\x51\x80";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('example.com', $record->name);
$this->assertSame(Record::TYPE_SOA, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(3600, $record->ttl);
$this->assertSame(
'ns1.example.com admin.example.com 2024102701 7200 3600 1209600 86400',
$record->rdata
);
$this->assertSame(strlen($data), $offset);
}
public function testEncodeSoaRecordMatchesBytes(): void
{
$record = new Record(
name: 'example.com',
type: Record::TYPE_SOA,
class: Record::CLASS_IN,
ttl: 3600,
rdata: 'ns1.example.com admin.example.com 2024102701 7200 3600 1209600 86400'
);
$expected = "\x07example\x03com\x00"
. "\x00\x06"
. "\x00\x01"
. "\x00\x00\x0E\x10"
. "\x00\x38" // RDLENGTH: 56 bytes
. "\x03ns1\x07example\x03com\x00"
. "\x05admin\x07example\x03com\x00"
. "\x78\xa5\x5b\x2d"
. "\x00\x00\x1C\x20"
. "\x00\x00\x0E\x10"
. "\x00\x12\x75\x00"
. "\x00\x01\x51\x80";
$this->assertSame($expected, $record->encode());
}
public function testDecodeTxtRecordWithMultipleChunks(): void
{
// TXT with two chunks: "hello" (5 bytes) + "world" (5 bytes)
$data = "\x07example\x03com\x00"
. "\x00\x10" // TYPE_TXT
. "\x00\x01" // CLASS_IN
. "\x00\x00\x02\x58" // TTL: 600
. "\x00\x0C" // RDLENGTH: 12 bytes (1+5+1+5)
. "\x05hello"
. "\x05world";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('example.com', $record->name);
$this->assertSame(Record::TYPE_TXT, $record->type);
$this->assertSame(Record::CLASS_IN, $record->class);
$this->assertSame(600, $record->ttl);
$this->assertSame('helloworld', $record->rdata);
$this->assertSame(strlen($data), $offset);
}
public function testDecodeTxtRecordWithThreeChunks(): void
{
// TXT with three chunks: 1+3 + 1+3 + 1+3 = 12 bytes
$data = "\x07example\x03com\x00"
. "\x00\x10"
. "\x00\x01"
. "\x00\x00\x02\x58"
. "\x00\x0C" // RDLENGTH: 12 bytes (not 15)
. "\x03foo"
. "\x03bar"
. "\x03baz";
$offset = 0;
$record = Record::decode($data, $offset);
$this->assertSame('foobarbaz', $record->rdata);
}
public function testDecodeSoaRecordRoundTrip(): void
{
// Original SOA RR for round-trip comparison: example.com. 3600 IN SOA ns1.example.com. admin.example.com. 2024102701 7200 3600 1209600 86400
$original = "\x07example\x03com\x00"
. "\x00\x06"
. "\x00\x01"
. "\x00\x00\x0E\x10"
. "\x00\x38" // RDLENGTH: 56 bytes
. "\x03ns1\x07example\x03com\x00"
. "\x05admin\x07example\x03com\x00"
. "\x78\xa5\x5b\x2d"
. "\x00\x00\x1C\x20"
. "\x00\x00\x0E\x10"
. "\x00\x12\x75\x00"
. "\x00\x01\x51\x80";
$offset = 0;
$record = Record::decode($original, $offset);
$encoded = $record->encode();
$this->assertSame($original, $encoded);
}
public function testEncodeTxtRecordWithMultipleChunks(): void
{
// Test that a TXT record with rdata exactly 256 bytes gets split into 2 chunks
// (255 bytes + 1 byte)
$exactly256Bytes = str_repeat('a', 256);
$record = new Record(
name: 'example.com',
type: Record::TYPE_TXT,
class: Record::CLASS_IN,
ttl: 600,
rdata: $exactly256Bytes
);
$encoded = $record->encode();
// Extract RDATA portion to verify chunking
$nameLen = strlen("\x07example\x03com\x00");
$headerLen = $nameLen + 2 + 2 + 4 + 2; // name + type + class + ttl + rdlength
$rdataEncoded = substr($encoded, $headerLen);
// Should have 2 chunks: 255 bytes + 1 byte = 256 bytes total
// First chunk: chr(255) + 255 bytes = 256 bytes
// Second chunk: chr(1) + 1 byte = 2 bytes
// Total RDATA: 256 + 2 = 258 bytes
$this->assertSame(258, strlen($rdataEncoded)); // Total RDATA length
$this->assertSame(255, ord($rdataEncoded[0])); // First chunk is 255 bytes
$this->assertSame(1, ord($rdataEncoded[256])); // Second chunk is 1 byte
// Verify round-trip
$offset = 0;
$decoded = Record::decode($encoded, $offset);
$this->assertSame($exactly256Bytes, $decoded->rdata);
}
public function testEncodeTxtRecordWithLongString(): void
{
// Create a TXT record with rdata > 255 bytes to test chunking
$longString = str_repeat('a', 300); // 300 bytes
$record = new Record(
name: 'example.com',
type: Record::TYPE_TXT,
class: Record::CLASS_IN,
ttl: 600,
rdata: $longString
);
$encoded = $record->encode();
$offset = 0;
$decoded = Record::decode($encoded, $offset);
// Verify round-trip: decoded rdata should match original
$this->assertSame($longString, $decoded->rdata);
$this->assertSame(Record::TYPE_TXT, $decoded->type);
$this->assertSame(600, $decoded->ttl);
// Verify the encoded data has multiple chunks
// Extract RDATA portion (skip header: name + type + class + ttl + rdlength)
$nameLen = strlen("\x07example\x03com\x00");
$headerLen = $nameLen + 2 + 2 + 4 + 2; // name + type + class + ttl + rdlength
$rdataEncoded = substr($encoded, $headerLen);
// Should have 2 chunks: 255 bytes + 45 bytes = 300 bytes total
// First chunk: chr(255) + 255 bytes = 256 bytes
// Second chunk: chr(45) + 45 bytes = 46 bytes
// Total RDATA: 256 + 46 = 302 bytes
$this->assertSame(302, strlen($rdataEncoded)); // Total RDATA length
$this->assertSame(255, ord($rdataEncoded[0])); // First chunk is 255 bytes
$this->assertSame(45, ord($rdataEncoded[256])); // Second chunk is 45 bytes
}
public function testEncodeTxtRecordRoundTripWithMultipleChunks(): void
{
// Test round-trip encoding/decoding of multi-chunk TXT record
$original = "\x07example\x03com\x00"
. "\x00\x10" // TYPE_TXT
. "\x00\x01" // CLASS_IN
. "\x00\x00\x02\x58" // TTL: 600
. "\x00\x0C" // RDLENGTH: 12 bytes (1+3+1+3+1+3)
. "\x03foo"
. "\x03bar"
. "\x03baz";
$offset = 0;
$record = Record::decode($original, $offset);
$encoded = $record->encode();
// Decode again to verify
$offset2 = 0;
$record2 = Record::decode($encoded, $offset2);
$this->assertSame('foobarbaz', $record2->rdata);
$this->assertSame(Record::TYPE_TXT, $record2->type);
$this->assertSame(600, $record2->ttl);
}
public function testEncodeTxtRecordWithEmptyRdata(): void
{
// Test that empty TXT rdata is encoded as a single zero-length character-string
$record = new Record(
name: 'example.com',
type: Record::TYPE_TXT,
class: Record::CLASS_IN,
ttl: 600,
rdata: ''
);
$encoded = $record->encode();
// Extract RDATA portion
$nameLen = strlen("\x07example\x03com\x00");
$headerLen = $nameLen + 2 + 2 + 4 + 2; // name + type + class + ttl + rdlength
$rdataEncoded = substr($encoded, $headerLen);
// Should be a single zero-length character-string: chr(0)
$this->assertSame(1, strlen($rdataEncoded)); // RDLENGTH should be 1
$this->assertSame(0, ord($rdataEncoded[0])); // First (and only) chunk is 0 bytes
// Verify round-trip: decode should work and return empty string
$offset = 0;
$decoded = Record::decode($encoded, $offset);
$this->assertSame('', $decoded->rdata);
$this->assertSame(Record::TYPE_TXT, $decoded->type);
$this->assertSame(600, $decoded->ttl);
}
}