Debian Codegraph @

tests/dns/master_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/dir.h>
28 #include <isc/string.h>
29 #include <isc/util.h>
30
31 #include <dns/cache.h>
32 #include <dns/callbacks.h>
33 #include <dns/db.h>
34 #include <dns/master.h>
35 #include <dns/masterdump.h>
36 #include <dns/name.h>
37 #include <dns/rdata.h>
38 #include <dns/rdatalist.h>
39 #include <dns/rdataset.h>
40
41 #include <tests/dns.h>
42
43 static void
44 nullmsg(dns_rdatacallbacks_t *cb, const char *fmt, ...) {
45 UNUSED(cb);
46 UNUSED(fmt);
47 }
48
49 #define BUFLEN 255
50 #define BIGBUFLEN (70 * 1024)
51 #define TEST_ORIGIN "test"
52
53 static dns_masterrawheader_t header;
54 static bool headerset;
55
56 dns_name_t dns_origin;
57 char origin[sizeof(TEST_ORIGIN)];
58 unsigned char name_buf[BUFLEN];
59 dns_rdatacallbacks_t callbacks;
60 char *include_file = NULL;
61
62 static void
63 rawdata_callback(dns_zone_t *zone, dns_masterrawheader_t *header);
64
65 static isc_result_t
66 add_callback(void *arg, const dns_name_t *owner,
67 dns_rdataset_t *dataset DNS__DB_FLARG) {
68 char buf[BIGBUFLEN];
69 isc_buffer_t target;
70 isc_result_t result;
71
72 UNUSED(arg);
73
74 isc_buffer_init(&target, buf, BIGBUFLEN);
75 result = dns_rdataset_totext(dataset, owner, false, false, &target);
76 return result;
77 }
78
79 static void
80 rawdata_callback(dns_zone_t *zone, dns_masterrawheader_t *h) {
81 UNUSED(zone);
82 header = *h;
83 headerset = true;
84 }
85
86 static isc_result_t
87 setup_master(void (*warn)(struct dns_rdatacallbacks *, const char *, ...),
88 void (*error)(struct dns_rdatacallbacks *, const char *, ...)) {
89 isc_result_t result;
90 int len;
91 isc_buffer_t source;
92 isc_buffer_t target;
93
94 strlcpy(origin, TEST_ORIGIN, sizeof(origin));
95 len = strlen(origin);
96 isc_buffer_init(&source, origin, len);
97 isc_buffer_add(&source, len);
98 isc_buffer_setactive(&source, len);
99 isc_buffer_init(&target, name_buf, BUFLEN);
100 dns_name_init(&dns_origin, NULL);
101 dns_master_initrawheader(&header);
102
103 result = dns_name_fromtext(&dns_origin, &source, dns_rootname, 0,
104 &target);
105 if (result != ISC_R_SUCCESS) {
106 return result;
107 }
108
109 dns_rdatacallbacks_init_stdio(&callbacks);
110 callbacks.add = add_callback;
111 callbacks.rawdata = rawdata_callback;
112 callbacks.zone = NULL;
113 if (warn != NULL) {
114 callbacks.warn = warn;
115 }
116 if (error != NULL) {
117 callbacks.error = error;
118 }
119 headerset = false;
120 return result;
121 }
122
123 static isc_result_t
124 test_master(const char *workdir, const char *testfile,
125 dns_masterformat_t format,
126 void (*warn)(struct dns_rdatacallbacks *, const char *, ...),
127 void (*error)(struct dns_rdatacallbacks *, const char *, ...)) {
128 isc_result_t result;
129
130 result = setup_master(warn, error);
131 if (result != ISC_R_SUCCESS) {
132 return result;
133 }
134
135 dns_rdatacallbacks_init_stdio(&callbacks);
136 callbacks.add = add_callback;
137 callbacks.rawdata = rawdata_callback;
138 callbacks.zone = NULL;
139 if (warn != NULL) {
140 callbacks.warn = warn;
141 }
142 if (error != NULL) {
143 callbacks.error = error;
144 }
145
146 if (workdir != NULL) {
147 result = isc_dir_chdir(workdir);
148 if (result != ISC_R_SUCCESS) {
149 return result;
150 }
151 }
152
153 result = dns_master_loadfile(testfile, &dns_origin, &dns_origin,
154 dns_rdataclass_in, true, 0, &callbacks,
155 NULL, NULL, mctx, format, 0);
156
157 return result;
158 }
159
160 static void
161 include_callback(const char *filename, void *arg) {
162 char **argp = (char **)arg;
163 *argp = isc_mem_strdup(mctx, filename);
164 }
165
166 /*
167 * Successful load test:
168 * dns_master_loadfile() loads a valid master file and returns success
169 */
170 ISC_RUN_TEST_IMPL(load) {
171 isc_result_t result;
172
173 UNUSED(state);
174
175 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master1.data",
176 dns_masterformat_text, nullmsg, nullmsg);
177 assert_int_equal(result, ISC_R_SUCCESS);
178 }
179
180 /*
181 * Unexpected end of file test:
182 * dns_master_loadfile() returns DNS_R_UNEXPECTED when file ends too soon
183 */
184 ISC_RUN_TEST_IMPL(unexpected) {
185 isc_result_t result;
186
187 UNUSED(state);
188
189 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master2.data",
190 dns_masterformat_text, nullmsg, nullmsg);
191 assert_int_equal(result, ISC_R_UNEXPECTEDEND);
192 }
193
194 /*
195 * No owner test:
196 * dns_master_loadfile() accepts broken zones with no TTL for first record
197 * if it is an SOA
198 */
199 ISC_RUN_TEST_IMPL(noowner) {
200 isc_result_t result;
201
202 UNUSED(state);
203
204 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master3.data",
205 dns_masterformat_text, nullmsg, nullmsg);
206 assert_int_equal(result, DNS_R_NOOWNER);
207 }
208
209 /*
210 * No TTL test:
211 * dns_master_loadfile() returns DNS_R_NOOWNER when no owner name is
212 * specified
213 */
214 ISC_RUN_TEST_IMPL(nottl) {
215 isc_result_t result;
216
217 UNUSED(state);
218
219 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master4.data",
220 dns_masterformat_text, nullmsg, nullmsg);
221 assert_int_equal(result, ISC_R_SUCCESS);
222 }
223
224 /*
225 * Bad class test:
226 * dns_master_loadfile() returns DNS_R_BADCLASS when record class doesn't
227 * match zone class
228 */
229 ISC_RUN_TEST_IMPL(badclass) {
230 isc_result_t result;
231
232 UNUSED(state);
233
234 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master5.data",
235 dns_masterformat_text, nullmsg, nullmsg);
236 assert_int_equal(result, DNS_R_BADCLASS);
237 }
238
239 /*
240 * Too big rdata test:
241 * dns_master_loadfile() returns ISC_R_NOSPACE when record is too big
242 */
243 ISC_RUN_TEST_IMPL(toobig) {
244 isc_result_t result;
245
246 UNUSED(state);
247
248 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master15.data",
249 dns_masterformat_text, nullmsg, nullmsg);
250 assert_int_equal(result, ISC_R_NOSPACE);
251 }
252
253 /*
254 * Maximum rdata test:
255 * dns_master_loadfile() returns ISC_R_SUCCESS when record is maximum size
256 */
257 ISC_RUN_TEST_IMPL(maxrdata) {
258 isc_result_t result;
259
260 UNUSED(state);
261
262 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master16.data",
263 dns_masterformat_text, nullmsg, nullmsg);
264 assert_int_equal(result, ISC_R_SUCCESS);
265 }
266
267 /*
268 * DNSKEY test:
269 * dns_master_loadfile() understands DNSKEY with key material
270 */
271 ISC_RUN_TEST_IMPL(dnskey) {
272 isc_result_t result;
273
274 UNUSED(state);
275
276 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master6.data",
277 dns_masterformat_text, nullmsg, nullmsg);
278 assert_int_equal(result, ISC_R_SUCCESS);
279 }
280
281 /*
282 * DNSKEY with no key material test:
283 * dns_master_loadfile() understands DNSKEY with no key material
284 *
285 * RFC 4034 removed the ability to signal NOKEY, so empty key material should
286 * be rejected.
287 */
288 ISC_RUN_TEST_IMPL(dnsnokey) {
289 isc_result_t result;
290
291 UNUSED(state);
292
293 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master7.data",
294 dns_masterformat_text, nullmsg, nullmsg);
295 assert_int_equal(result, ISC_R_UNEXPECTEDEND);
296 }
297
298 /*
299 * Include test:
300 * dns_master_loadfile() understands $INCLUDE
301 */
302 ISC_RUN_TEST_IMPL(include) {
303 isc_result_t result;
304
305 UNUSED(state);
306
307 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master8.data",
308 dns_masterformat_text, nullmsg, nullmsg);
309 assert_int_equal(result, DNS_R_SEENINCLUDE);
310 }
311
312 /*
313 * Include file list test:
314 * dns_master_loadfile4() returns names of included file
315 */
316 ISC_RUN_TEST_IMPL(master_includelist) {
317 isc_result_t result;
318 char *filename = NULL;
319
320 UNUSED(state);
321
322 result = setup_master(nullmsg, nullmsg);
323 assert_int_equal(result, ISC_R_SUCCESS);
324
325 result = isc_dir_chdir(SRCDIR);
326 assert_int_equal(result, ISC_R_SUCCESS);
327
328 result = dns_master_loadfile(
329 TESTS_DIR "/testdata/master/master8.data", &dns_origin,
330 &dns_origin, dns_rdataclass_in, 0, true, &callbacks,
331 include_callback, &filename, mctx, dns_masterformat_text, 0);
332 assert_int_equal(result, DNS_R_SEENINCLUDE);
333 assert_non_null(filename);
334 if (filename != NULL) {
335 assert_string_equal(filename, "testdata/master/master6.data");
336 isc_mem_free(mctx, filename);
337 }
338 }
339
340 /*
341 * Include failure test:
342 * dns_master_loadfile() understands $INCLUDE failures
343 */
344 ISC_RUN_TEST_IMPL(includefail) {
345 isc_result_t result;
346
347 UNUSED(state);
348
349 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master9.data",
350 dns_masterformat_text, nullmsg, nullmsg);
351 assert_int_equal(result, DNS_R_BADCLASS);
352 }
353
354 /*
355 * Non-empty blank lines test:
356 * dns_master_loadfile() handles non-empty blank lines
357 */
358 ISC_RUN_TEST_IMPL(blanklines) {
359 isc_result_t result;
360
361 UNUSED(state);
362
363 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master10.data",
364 dns_masterformat_text, nullmsg, nullmsg);
365 assert_int_equal(result, ISC_R_SUCCESS);
366 }
367
368 /*
369 * SOA leading zeroes test:
370 * dns_master_loadfile() allows leading zeroes in SOA
371 */
372
373 ISC_RUN_TEST_IMPL(leadingzero) {
374 isc_result_t result;
375
376 UNUSED(state);
377
378 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master11.data",
379 dns_masterformat_text, nullmsg, nullmsg);
380 assert_int_equal(result, ISC_R_SUCCESS);
381 }
382
383 /* masterfile totext tests */
384 ISC_RUN_TEST_IMPL(totext) {
385 isc_result_t result;
386 dns_rdataset_t rdataset;
387 dns_rdatalist_t rdatalist;
388 isc_buffer_t target;
389 unsigned char buf[BIGBUFLEN];
390
391 UNUSED(state);
392
393 /* First, test with an empty rdataset */
394 dns_rdatalist_init(&rdatalist);
395 rdatalist.rdclass = dns_rdataclass_in;
396 rdatalist.type = dns_rdatatype_none;
397 rdatalist.covers = dns_rdatatype_none;
398
399 dns_rdataset_init(&rdataset);
400 dns_rdatalist_tordataset(&rdatalist, &rdataset);
401
402 isc_buffer_init(&target, buf, BIGBUFLEN);
403 result = dns_master_rdatasettotext(dns_rootname, &rdataset,
404 &dns_master_style_debug, NULL,
405 &target);
406 assert_int_equal(result, ISC_R_SUCCESS);
407 assert_int_equal(isc_buffer_usedlength(&target), 0);
408
409 /*
410 * XXX: We will also need to add tests for dumping various
411 * rdata types, classes, etc, and comparing the results against
412 * known-good output.
413 */
414 }
415
416 /*
417 * Raw load test:
418 * dns_master_loadfile() loads a valid raw file and returns success
419 */
420 ISC_RUN_TEST_IMPL(loadraw) {
421 isc_result_t result;
422
423 UNUSED(state);
424
425 /* Raw format version 0 */
426 result = test_master(BUILDDIR, "testdata/master/master12.data",
427 dns_masterformat_raw, nullmsg, nullmsg);
428 assert_string_equal(isc_result_totext(result), "success");
429 assert_true(headerset);
430 assert_int_equal(header.flags, 0);
431
432 /* Raw format version 1, no source serial */
433 result = test_master(BUILDDIR, "testdata/master/master13.data",
434 dns_masterformat_raw, nullmsg, nullmsg);
435 assert_string_equal(isc_result_totext(result), "success");
436 assert_true(headerset);
437 assert_int_equal(header.flags, 0);
438
439 /* Raw format version 1, source serial == 2011120101 */
440 result = test_master(BUILDDIR, "testdata/master/master14.data",
441 dns_masterformat_raw, nullmsg, nullmsg);
442 assert_string_equal(isc_result_totext(result), "success");
443 assert_true(headerset);
444 assert_true((header.flags & DNS_MASTERRAW_SOURCESERIALSET) != 0);
445 assert_int_equal(header.sourceserial, 2011120101);
446 }
447
448 /*
449 * Raw dump test:
450 * dns_master_dump*() functions dump valid raw files
451 */
452 ISC_RUN_TEST_IMPL(dumpraw) {
453 isc_result_t result;
454 dns_db_t *db = NULL;
455 dns_dbversion_t *version = NULL;
456 char myorigin[sizeof(TEST_ORIGIN)];
457 dns_name_t dnsorigin;
458 isc_buffer_t source, target;
459 unsigned char namebuf[BUFLEN];
460 int len;
461
462 UNUSED(state);
463
464 strlcpy(myorigin, TEST_ORIGIN, sizeof(myorigin));
465 len = strlen(myorigin);
466 isc_buffer_init(&source, myorigin, len);
467 isc_buffer_add(&source, len);
468 isc_buffer_setactive(&source, len);
469 isc_buffer_init(&target, namebuf, BUFLEN);
470 dns_name_init(&dnsorigin, NULL);
471 result = dns_name_fromtext(&dnsorigin, &source, dns_rootname, 0,
472 &target);
473 assert_int_equal(result, ISC_R_SUCCESS);
474
475 result = dns_db_create(mctx, ZONEDB_DEFAULT, &dnsorigin,
476 dns_dbtype_zone, dns_rdataclass_in, 0, NULL,
477 &db);
478 assert_int_equal(result, ISC_R_SUCCESS);
479
480 result = isc_dir_chdir(SRCDIR);
481 assert_int_equal(result, ISC_R_SUCCESS);
482
483 result = dns_db_load(db, TESTS_DIR "/testdata/master/master1.data",
484 dns_masterformat_text, 0);
485 assert_int_equal(result, ISC_R_SUCCESS);
486
487 result = isc_dir_chdir(BUILDDIR);
488 assert_int_equal(result, ISC_R_SUCCESS);
489
490 dns_db_currentversion(db, &version);
491
492 result = dns_master_dump(mctx, db, version, &dns_master_style_default,
493 "test.dump", dns_masterformat_raw, NULL);
494 assert_int_equal(result, ISC_R_SUCCESS);
495
496 result = test_master(NULL, "test.dump", dns_masterformat_raw, nullmsg,
497 nullmsg);
498 assert_string_equal(isc_result_totext(result), "success");
499 assert_true(headerset);
500 assert_int_equal(header.flags, 0);
501
502 dns_master_initrawheader(&header);
503 header.sourceserial = 12345;
504 header.flags |= DNS_MASTERRAW_SOURCESERIALSET;
505
506 unlink("test.dump");
507 result = dns_master_dump(mctx, db, version, &dns_master_style_default,
508 "test.dump", dns_masterformat_raw, &header);
509 assert_int_equal(result, ISC_R_SUCCESS);
510
511 result = test_master(NULL, "test.dump", dns_masterformat_raw, nullmsg,
512 nullmsg);
513 assert_string_equal(isc_result_totext(result), "success");
514 assert_true(headerset);
515 assert_true((header.flags & DNS_MASTERRAW_SOURCESERIALSET) != 0);
516 assert_int_equal(header.sourceserial, 12345);
517
518 unlink("test.dump");
519 dns_db_closeversion(db, &version, false);
520 dns_db_detach(&db);
521 }
522
523 static const char *warn_expect_value;
524 static bool warn_expect_result;
525
526 static void
527 warn_expect(struct dns_rdatacallbacks *mycallbacks, const char *fmt, ...) {
528 char buf[4096];
529 va_list ap;
530
531 UNUSED(mycallbacks);
532
533 warn_expect_result = false;
534
535 va_start(ap, fmt);
536 vsnprintf(buf, sizeof(buf), fmt, ap);
537 va_end(ap);
538
539 if (warn_expect_value != NULL && strstr(buf, warn_expect_value) != NULL)
540 {
541 warn_expect_result = true;
542 }
543 }
544
545 /*
546 * Origin change test:
547 * dns_master_loadfile() rejects zones with inherited name following $ORIGIN
548 */
549 ISC_RUN_TEST_IMPL(neworigin) {
550 isc_result_t result;
551
552 UNUSED(state);
553
554 warn_expect_value = "record with inherited owner";
555 result = test_master(SRCDIR, TESTS_DIR "/testdata/master/master17.data",
556 dns_masterformat_text, warn_expect, nullmsg);
557 assert_int_equal(result, ISC_R_SUCCESS);
558 assert_true(warn_expect_result);
559 }
560
561 ISC_TEST_LIST_START
562 ISC_TEST_ENTRY(load)
563 ISC_TEST_ENTRY(unexpected)
564 ISC_TEST_ENTRY(noowner)
565 ISC_TEST_ENTRY(nottl)
566 ISC_TEST_ENTRY(badclass)
567 ISC_TEST_ENTRY(dnskey)
568 ISC_TEST_ENTRY(dnsnokey)
569 ISC_TEST_ENTRY(include)
570 ISC_TEST_ENTRY(master_includelist)
571 ISC_TEST_ENTRY(includefail)
572 ISC_TEST_ENTRY(blanklines)
573 ISC_TEST_ENTRY(leadingzero)
574 ISC_TEST_ENTRY(totext)
575 ISC_TEST_ENTRY(loadraw)
576 ISC_TEST_ENTRY(dumpraw)
577 ISC_TEST_ENTRY(toobig)
578 ISC_TEST_ENTRY(maxrdata)
579 ISC_TEST_ENTRY(neworigin)
580 ISC_TEST_LIST_END
581
582 ISC_TEST_MAIN