rsdns 0.23.0

DNS Client Library
Documentation
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
use crate::{
    message::{RecordsSection, reader::*},
    names::{InlineName, Name},
    records::{Class, Type, data::*},
};
use std::{
    net::{Ipv4Addr, Ipv6Addr},
    str::FromStr,
};

// ; <<>> ch4 0.6.0 git:34a0bcd <<>> --read bbc.com.json
// ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2099
// ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 8, ADDITIONAL: 12
//
// ;; QUESTION SECTION:
// ;bbc.com.                      IN     A
//
// ;; ANSWER SECTION:
// bbc.com.                300    IN     A      151.101.128.81
// bbc.com.                300    IN     A      151.101.192.81
// bbc.com.                300    IN     A      151.101.64.81
// bbc.com.                300    IN     A      151.101.0.81
//
// ;; AUTHORITY SECTION:
// bbc.com.                106241 IN     NS     ddns1.bbc.com.
// bbc.com.                106241 IN     NS     dns0.bbc.com.
// bbc.com.                106241 IN     NS     ddns0.bbc.com.
// bbc.com.                106241 IN     NS     ddns1.bbc.co.uk.
// bbc.com.                106241 IN     NS     dns1.bbc.co.uk.
// bbc.com.                106241 IN     NS     ddns0.bbc.co.uk.
// bbc.com.                106241 IN     NS     dns1.bbc.com.
// bbc.com.                106241 IN     NS     dns0.bbc.co.uk.
//
// ;; ADDITIONAL SECTION:
// dns0.bbc.co.uk.         106241 IN     A      198.51.44.9
// dns0.bbc.com.           106241 IN     A      198.51.44.73
// dns1.bbc.co.uk.         106241 IN     A      198.51.45.9
// dns1.bbc.com.           106241 IN     A      198.51.45.73
// ddns0.bbc.co.uk.        106241 IN     A      148.163.199.1
// ddns0.bbc.com.          106241 IN     A      148.163.199.129
// ddns1.bbc.co.uk.        106241 IN     A      148.163.199.65
// ddns1.bbc.com.          79756  IN     A      148.163.199.193
// dns0.bbc.co.uk.         169870 IN     AAAA   2620:4d:4000:6259:7:9:0:1
// dns1.bbc.co.uk.         169870 IN     AAAA   2a00:edc0:6259:7:9::2
// ddns0.bbc.co.uk.        169870 IN     AAAA   2607:f740:e04e::1
// ddns1.bbc.co.uk.        169870 IN     AAAA   2607:f740:e04e:4::1
#[rustfmt::skip]
const M0: [u8; 494] = [
    0x08, 0x33, 0x81, 0x80, 0x00, 0x01, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, // |.3..........| 0
    0x03, 0x62, 0x62, 0x63, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, // |.bbc.com....| 12
    0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, // |..........,.| 24
    0x04, 0x97, 0x65, 0x80, 0x51, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, // |..e.Q.......| 36
    0x00, 0x01, 0x2c, 0x00, 0x04, 0x97, 0x65, 0xc0, 0x51, 0xc0, 0x0c, 0x00, // |..,...e.Q...| 48
    0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x97, 0x65, 0x40, // |......,...e@| 60
    0x51, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, // |Q.........,.| 72
    0x04, 0x97, 0x65, 0x00, 0x51, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, // |..e.Q.......| 84
    0x01, 0x9f, 0x01, 0x00, 0x08, 0x05, 0x64, 0x64, 0x6e, 0x73, 0x31, 0xc0, // |......ddns1.| 96
    0x0c, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, // |............| 108
    0x07, 0x04, 0x64, 0x6e, 0x73, 0x30, 0xc0, 0x0c, 0xc0, 0x0c, 0x00, 0x02, // |..dns0......| 120
    0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x08, 0x05, 0x64, 0x64, 0x6e, // |.........ddn| 132
    0x73, 0x30, 0xc0, 0x0c, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, // |s0..........| 144
    0x9f, 0x01, 0x00, 0x11, 0x05, 0x64, 0x64, 0x6e, 0x73, 0x31, 0x03, 0x62, // |.....ddns1.b| 156
    0x62, 0x63, 0x02, 0x63, 0x6f, 0x02, 0x75, 0x6b, 0x00, 0xc0, 0x0c, 0x00, // |bc.co.uk....| 168
    0x02, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x07, 0x04, 0x64, 0x6e, // |..........dn| 180
    0x73, 0x31, 0xc0, 0xa6, 0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, // |s1..........| 192
    0x9f, 0x01, 0x00, 0x08, 0x05, 0x64, 0x64, 0x6e, 0x73, 0x30, 0xc0, 0xa6, // |.....ddns0..| 204
    0xc0, 0x0c, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x07, // |............| 216
    0x04, 0x64, 0x6e, 0x73, 0x31, 0xc0, 0x0c, 0xc0, 0x0c, 0x00, 0x02, 0x00, // |.dns1.......| 228
    0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x07, 0x04, 0x64, 0x6e, 0x73, 0x30, // |........dns0| 240
    0xc0, 0xa6, 0xc0, 0xf7, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, // |............| 252
    0x00, 0x04, 0xc6, 0x33, 0x2c, 0x09, 0xc0, 0x79, 0x00, 0x01, 0x00, 0x01, // |...3,..y....| 264
    0x00, 0x01, 0x9f, 0x01, 0x00, 0x04, 0xc6, 0x33, 0x2c, 0x49, 0xc0, 0xbd, // |.......3,I..| 276
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x04, 0xc6, 0x33, // |...........3| 288
    0x2d, 0x09, 0xc0, 0xe4, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, // |-...........| 300
    0x00, 0x04, 0xc6, 0x33, 0x2d, 0x49, 0xc0, 0xd0, 0x00, 0x01, 0x00, 0x01, // |...3-I......| 312
    0x00, 0x01, 0x9f, 0x01, 0x00, 0x04, 0x94, 0xa3, 0xc7, 0x01, 0xc0, 0x8c, // |............| 324
    0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, 0x00, 0x04, 0x94, 0xa3, // |............| 336
    0xc7, 0x81, 0xc0, 0xa0, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x9f, 0x01, // |............| 348
    0x00, 0x04, 0x94, 0xa3, 0xc7, 0x41, 0xc0, 0x65, 0x00, 0x01, 0x00, 0x01, // |.....A.e....| 360
    0x00, 0x01, 0x37, 0x8c, 0x00, 0x04, 0x94, 0xa3, 0xc7, 0xc1, 0xc0, 0xf7, // |..7.........| 372
    0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0x97, 0x8e, 0x00, 0x10, 0x26, 0x20, // |..........& | 384
    0x00, 0x4d, 0x40, 0x00, 0x62, 0x59, 0x00, 0x07, 0x00, 0x09, 0x00, 0x00, // |.M@.bY......| 396
    0x00, 0x01, 0xc0, 0xbd, 0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0x97, 0x8e, // |............| 408
    0x00, 0x10, 0x2a, 0x00, 0xed, 0xc0, 0x62, 0x59, 0x00, 0x07, 0x00, 0x09, // |..*...bY....| 420
    0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xc0, 0xd0, 0x00, 0x1c, 0x00, 0x01, // |............| 432
    0x00, 0x02, 0x97, 0x8e, 0x00, 0x10, 0x26, 0x07, 0xf7, 0x40, 0xe0, 0x4e, // |......&..@.N| 444
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0xa0, // |............| 456
    0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0x97, 0x8e, 0x00, 0x10, 0x26, 0x07, // |..........&.| 468
    0xf7, 0x40, 0xe0, 0x4e, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // |.@.N........| 480
    0x00, 0x01, /*                                                       */ // |..| 492
];

// ; <<>> ch4 0.7.0 <<>> --read cnn.ch4
// ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30724
// ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
//
// ;; QUESTION SECTION:
// ;cnn.com.                      IN     A
//
// ;; ANSWER SECTION:
// cnn.com.                51     IN     A      151.101.129.67
// cnn.com.                51     IN     A      151.101.65.67
// cnn.com.                51     IN     A      151.101.1.67
// cnn.com.                51     IN     A      151.101.193.67
//
// ;; Query time: 358.084µs
// ;; SERVER: 127.0.0.53:53
// ;; MSG SIZE rcvd: 89
#[rustfmt::skip]
const M1: [u8; 89] = [
    0x78, 0x04, 0x81, 0x80, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, // |x...........| 0
    0x03, 0x63, 0x6e, 0x6e, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, // |.cnn.com....| 12
    0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x33, 0x00, // |..........3.| 24
    0x04, 0x97, 0x65, 0x81, 0x43, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, // |..e.C.......| 36
    0x00, 0x00, 0x33, 0x00, 0x04, 0x97, 0x65, 0x41, 0x43, 0xc0, 0x0c, 0x00, // |..3...eAC...| 48
    0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x33, 0x00, 0x04, 0x97, 0x65, 0x01, // |......3...e.| 60
    0x43, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x33, 0x00, // |C.........3.| 72
    0x04, 0x97, 0x65, 0xc1, 0x43, /*                                     */ // |..e.C| 84
];

// ; <<>> SRV query for _sip._tcp.example.com.
// ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4660
// ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
//
// ;; QUESTION SECTION:
// ;_sip._tcp.example.com.        IN     SRV
//
// ;; ANSWER SECTION:
// _sip._tcp.example.com.  300    IN     SRV    10 60 5060 sipserver.example.com.
// _sip._tcp.example.com.  300    IN     SRV    20 10 5060 backup.example.com.
#[rustfmt::skip]
const M2: [u8; 96] = [
    0x12, 0x34, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, // |.4..........| 0
    0x04, 0x5f, 0x73, 0x69, 0x70, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x07, 0x65, // |._sip._tcp.e| 12
    0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, // |xample.com..| 24
    0x21, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x01, // |!.....!.....| 36
    0x2c, 0x00, 0x12, 0x00, 0x0a, 0x00, 0x3c, 0x13, 0xc4, 0x09, 0x73, 0x69, // |,.....<...si| 48
    0x70, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0xc0, 0x16, 0xc0, 0x0c, 0x00, // |pserver.....| 60
    0x21, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x0f, 0x00, 0x14, 0x00, // |!.....,.....| 72
    0x0a, 0x13, 0xc4, 0x06, 0x62, 0x61, 0x63, 0x6b, 0x75, 0x70, 0xc0, 0x16, // |....backup..| 84
];

#[test]
fn test_whole_message() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReader");
    let header = mr.header().expect("failed to read the header");
    assert_eq!(header.id, 0x833);

    let question = mr.the_question_ref().expect("the_question_ref failed");
    assert_eq!(question.qtype, Type::A);
    assert_eq!(question.qclass, Class::IN);

    let mut headers = Vec::new();

    assert_eq!(mr.records_count(), 24);
    while mr.has_records() {
        let header = mr.record_header_ref().expect("record_header_ref failed");
        mr.skip_record_data(header.marker())
            .expect("skip_record_data failed");

        match header.section() {
            RecordsSection::Answer => assert!(question.qname.eq(header.name()).expect("eq failed")),
            RecordsSection::Additional => {
                assert!(question.qname.ne(header.name()).expect("ne failed"))
            }
            _ => {}
        }

        headers.push(header);
    }

    for h in headers {
        if h.section() != RecordsSection::Authority {
            continue;
        }

        assert_eq!(h.rtype(), Type::NS);
        assert_eq!(h.rclass(), Class::IN);
        assert_eq!(h.ttl(), 106241);

        let name: Name = h.name().try_into().expect("name_ref::try_into failed");
        assert_eq!(name.as_str(), "bbc.com.");

        mr.record_data_at::<Ns>(h.marker())
            .expect("record_data_at failed");
    }
}

#[test]
fn test_answer_section() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReader");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Answer).expect("seek failed");

    let mut records = Vec::new();
    let mut headers = Vec::new();

    loop {
        if !mr.has_records() {
            break;
        }

        let header = mr
            .record_header::<InlineName>()
            .expect("failed to read record header");

        if header.section() != RecordsSection::Answer {
            break;
        }

        assert_eq!(header.name().as_str(), "bbc.com.");
        assert_eq!(header.rtype(), Type::A);
        assert_eq!(header.rclass(), Class::IN);
        assert_eq!(header.section(), RecordsSection::Answer);
        assert_eq!(header.ttl(), 300);
        assert_eq!(header.rdlen(), 4);

        records.push(
            mr.record_data::<A>(header.marker())
                .expect("failed to read record data"),
        );
        headers.push(header);
    }

    assert_eq!(headers.len(), 4);
    assert_eq!(records.len(), 4);
    assert_eq!(
        records,
        vec![
            A {
                address: Ipv4Addr::from_str("151.101.128.81").unwrap()
            },
            A {
                address: Ipv4Addr::from_str("151.101.192.81").unwrap()
            },
            A {
                address: Ipv4Addr::from_str("151.101.64.81").unwrap()
            },
            A {
                address: Ipv4Addr::from_str("151.101.0.81").unwrap()
            },
        ]
    );

    for (h, d) in headers.iter().zip(records.iter()) {
        assert_eq!(
            *d,
            mr.record_data_at::<A>(h.marker())
                .expect("record_data_at failed")
        );
    }
}

#[test]
fn test_data_bytes() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReader");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Additional).expect("seek failed");

    let mut markers = Vec::new();
    let mut data = Vec::new();

    while mr.has_records() {
        let marker = mr.record_marker().expect("record_marker failed");
        if marker.rtype() == Type::AAAA {
            data.push(
                mr.record_data_bytes(&marker)
                    .expect("record_data_bytes failed"),
            );
            markers.push(marker);
        } else {
            mr.skip_record_data(&marker)
                .expect("skip_record_data failed");
        }
    }

    let addresses = [
        Ipv6Addr::from_str("2620:4d:4000:6259:7:9:0:1").unwrap(),
        Ipv6Addr::from_str("2a00:edc0:6259:7:9::2").unwrap(),
        Ipv6Addr::from_str("2607:f740:e04e::1").unwrap(),
        Ipv6Addr::from_str("2607:f740:e04e:4::1").unwrap(),
    ];
    assert_eq!(data.len(), addresses.len());

    for (d, a) in data.iter().zip(addresses.iter()) {
        assert_eq!(d, &a.octets());
    }

    for (d, m) in data.iter().zip(markers.iter()) {
        assert_eq!(
            &mr.record_data_bytes_at(m)
                .expect("record_data_bytes_at failed"),
            d
        );
    }
}

#[test]
fn test_seek() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.skip_questions().expect("skip_questions failed");

    // read the whole message to discover all section offsets
    while mr.has_records() {
        let marker = mr.record_marker().expect("marker failed");
        mr.skip_record_data(&marker)
            .expect("skip_record_data failed");
    }

    mr.seek(RecordsSection::Answer)
        .expect("seek(Answer) failed");
    assert_eq!(mr.records_count(), 24);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "bbc.com.");
    let a_record = mr.record_data::<A>(record_header.marker()).unwrap();
    assert_eq!(
        a_record.address,
        Ipv4Addr::from_str("151.101.128.81").unwrap()
    );

    mr.seek(RecordsSection::Authority)
        .expect("seek(Authority) failed");
    assert_eq!(mr.records_count(), 20);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "bbc.com.");
    let ns_record = mr.record_data::<Ns>(record_header.marker()).unwrap();
    assert_eq!(ns_record.nsdname.as_str(), "ddns1.bbc.com.");

    mr.seek(RecordsSection::Additional)
        .expect("seek(Additional) failed");
    assert_eq!(mr.records_count(), 12);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "dns0.bbc.co.uk.");
    let a_record = mr.record_data::<A>(record_header.marker()).unwrap();
    assert_eq!(a_record.address, Ipv4Addr::from_str("198.51.44.9").unwrap());
}

#[test]
fn test_answers_offset_is_known_after_skip_questions() {
    let mut mr = MessageReader::new(&M1[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.skip_questions().expect("skip_questions failed");
    mr.seek(RecordsSection::Answer).expect("seek failed");
    assert_eq!(mr.records_count(), 4);
}

#[test]
fn test_seek_empty_section() {
    let mut mr = MessageReader::new(&M1[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.skip_questions().expect("skip_questions failed");

    // read the whole message to discover all section offsets
    while mr.has_records() {
        let marker = mr.record_marker().expect("marker failed");
        mr.skip_record_data(&marker)
            .expect("skip_record_data failed");
    }

    mr.seek(RecordsSection::Answer).expect("seek failed");
    assert_eq!(mr.records_count(), 4);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "cnn.com.");
    assert_eq!(record_header.marker.section, RecordsSection::Answer);
    assert_eq!(record_header.marker.rtype, Type::A);
    assert_eq!(record_header.marker.rclass, Class::IN);
    assert_eq!(record_header.marker.ttl, 51);
    let a_record = mr.record_data::<A>(record_header.marker()).unwrap();
    assert_eq!(
        a_record.address,
        Ipv4Addr::from_str("151.101.129.67").unwrap()
    );

    mr.seek(RecordsSection::Authority)
        .expect("seek(Authority) failed");
    assert_eq!(mr.records_count(), 0);

    mr.seek(RecordsSection::Additional)
        .expect("seek(Additional) failed");
    assert_eq!(mr.records_count(), 0);
}

#[test]
fn test_seek_answer() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Answer)
        .expect("seek(Answer) failed");

    assert_eq!(mr.records_count(), 24);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "bbc.com.");
    let a_record = mr.record_data::<A>(record_header.marker()).unwrap();
    assert_eq!(
        a_record.address,
        Ipv4Addr::from_str("151.101.128.81").unwrap()
    );
}

#[test]
fn test_seek_authority() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Authority)
        .expect("seek(Authority) failed");

    assert_eq!(mr.records_count(), 20);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "bbc.com.");
    let ns_record = mr.record_data::<Ns>(record_header.marker()).unwrap();
    assert_eq!(ns_record.nsdname.as_str(), "ddns1.bbc.com.");
}

#[test]
fn test_srv_records() {
    let mut mr = MessageReader::new(&M2[..]).expect("failed to create MessageReader");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Answer).expect("seek failed");

    let mut records = Vec::new();

    while mr.has_records() {
        let header = mr
            .record_header::<Name>()
            .expect("failed to read record header");

        if header.section() != RecordsSection::Answer {
            break;
        }

        assert_eq!(header.name().as_str(), "_sip._tcp.example.com.");
        assert_eq!(header.rtype(), Type::SRV);
        assert_eq!(header.rclass(), Class::IN);
        assert_eq!(header.ttl(), 300);

        records.push(
            mr.record_data::<Srv>(header.marker())
                .expect("failed to read SRV record data"),
        );
    }

    assert_eq!(records.len(), 2);
    assert_eq!(
        records,
        vec![
            Srv {
                priority: 10,
                weight: 60,
                port: 5060,
                target: Name::from_str("sipserver.example.com.").unwrap(),
            },
            Srv {
                priority: 20,
                weight: 10,
                port: 5060,
                target: Name::from_str("backup.example.com.").unwrap(),
            },
        ]
    );
}

#[test]
fn test_seek_additional() {
    let mut mr = MessageReader::new(&M0[..]).expect("failed to create MessageReder");
    mr.header().expect("failed to read the header");
    mr.seek(RecordsSection::Additional)
        .expect("seek(Additional) failed");

    assert_eq!(mr.records_count(), 12);

    let record_header = mr.record_header::<Name>().unwrap();
    assert_eq!(record_header.name.as_str(), "dns0.bbc.co.uk.");
    let a_record = mr.record_data::<A>(record_header.marker()).unwrap();
    assert_eq!(a_record.address, Ipv4Addr::from_str("198.51.44.9").unwrap());
}