Debian Codegraph @

tests/dns/dbdiff_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 <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/util.h>
27
28 #include <dns/db.h>
29 #include <dns/dbiterator.h>
30 #include <dns/journal.h>
31 #include <dns/name.h>
32
33 #include <tests/dns.h>
34
35 #define BUFLEN 255
36 #define BIGBUFLEN (64 * 1024)
37 #define TEST_ORIGIN "test"
38
39 static void
40 test_create(const char *oldfile, dns_db_t **old, const char *newfile,
41 dns_db_t **newdb) {
42 isc_result_t result;
43
44 result = dns_test_loaddb(old, dns_dbtype_zone, TEST_ORIGIN, oldfile);
45 assert_int_equal(result, ISC_R_SUCCESS);
46
47 result = dns_test_loaddb(newdb, dns_dbtype_zone, TEST_ORIGIN, newfile);
48 assert_int_equal(result, ISC_R_SUCCESS);
49 }
50
51 /* dns_db_diffx of identical content */
52 ISC_RUN_TEST_IMPL(diffx_same) {
53 dns_db_t *newdb = NULL, *olddb = NULL;
54 isc_result_t result;
55 dns_diff_t diff;
56
57 UNUSED(state);
58
59 test_create(TESTS_DIR "/testdata/diff/zone1.data", &olddb,
60 TESTS_DIR "/testdata/diff/zone1.data", &newdb);
61
62 dns_diff_init(mctx, &diff);
63
64 result = dns_db_diffx(&diff, newdb, NULL, olddb, NULL, NULL);
65 assert_int_equal(result, ISC_R_SUCCESS);
66
67 assert_true(ISC_LIST_EMPTY(diff.tuples));
68
69 dns_diff_clear(&diff);
70 dns_db_detach(&newdb);
71 dns_db_detach(&olddb);
72 }
73
74 /* dns_db_diffx of zone with record added */
75 ISC_RUN_TEST_IMPL(diffx_add) {
76 dns_db_t *newdb = NULL, *olddb = NULL;
77 dns_difftuple_t *tuple;
78 isc_result_t result;
79 dns_diff_t diff;
80 int count = 0;
81
82 UNUSED(state);
83
84 test_create(TESTS_DIR "/testdata/diff/zone1.data", &olddb,
85 TESTS_DIR "/testdata/diff/zone2.data", &newdb);
86
87 dns_diff_init(mctx, &diff);
88
89 result = dns_db_diffx(&diff, newdb, NULL, olddb, NULL, NULL);
90 assert_int_equal(result, ISC_R_SUCCESS);
91
92 assert_false(ISC_LIST_EMPTY(diff.tuples));
93 for (tuple = ISC_LIST_HEAD(diff.tuples); tuple != NULL;
94 tuple = ISC_LIST_NEXT(tuple, link))
95 {
96 assert_int_equal(tuple->op, DNS_DIFFOP_ADD);
97 count++;
98 }
99 assert_int_equal(count, 1);
100
101 dns_diff_clear(&diff);
102 dns_db_detach(&newdb);
103 dns_db_detach(&olddb);
104 }
105
106 /* dns_db_diffx of zone with record removed */
107 ISC_RUN_TEST_IMPL(diffx_remove) {
108 dns_db_t *newdb = NULL, *olddb = NULL;
109 dns_difftuple_t *tuple;
110 isc_result_t result;
111 dns_diff_t diff;
112 int count = 0;
113
114 UNUSED(state);
115
116 test_create(TESTS_DIR "/testdata/diff/zone1.data", &olddb,
117 TESTS_DIR "/testdata/diff/zone3.data", &newdb);
118
119 dns_diff_init(mctx, &diff);
120
121 result = dns_db_diffx(&diff, newdb, NULL, olddb, NULL, NULL);
122 assert_int_equal(result, ISC_R_SUCCESS);
123
124 assert_false(ISC_LIST_EMPTY(diff.tuples));
125 for (tuple = ISC_LIST_HEAD(diff.tuples); tuple != NULL;
126 tuple = ISC_LIST_NEXT(tuple, link))
127 {
128 assert_int_equal(tuple->op, DNS_DIFFOP_DEL);
129 count++;
130 }
131 assert_int_equal(count, 1);
132
133 dns_diff_clear(&diff);
134 dns_db_detach(&newdb);
135 dns_db_detach(&olddb);
136 }
137
138 ISC_TEST_LIST_START
139 ISC_TEST_ENTRY(diffx_same)
140 ISC_TEST_ENTRY(diffx_add)
141 ISC_TEST_ENTRY(diffx_remove)
142 ISC_TEST_LIST_END
143
144 ISC_TEST_MAIN