Debian Codegraph @

tests/dns/qpzone_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/qp.h>
29 #include <dns/rdatalist.h>
30 #include <dns/rdataset.h>
31 #include <dns/rdataslab.h>
32 #include <dns/rdatastruct.h>
33 #define KEEP_BEFORE
34
35 /* Include the main file */
36
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wshadow"
39 #undef CHECK
40 #include "qpzone.c"
41 #pragma GCC diagnostic pop
42
43 #undef CHECK
44 #include <tests/dns.h>
45
46 #define CASESET(header) \
47 ((atomic_load_acquire(&(header)->attributes) & \
48 DNS_SLABHEADERATTR_CASESET) != 0)
49
50 const char *ownercase_vectors[12][2] = {
51 {
52 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",
53 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
54 },
55 {
56 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
57 "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ",
58 },
59 {
60 "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ",
61 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
62 },
63 {
64 "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ",
65 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
66 },
67 {
68 "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVxXyYzZ",
69 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvxxyyzz",
70 },
71 {
72 "WwW.ExAmPlE.OrG",
73 "wWw.eXaMpLe.oRg",
74 },
75 {
76 "_SIP.tcp.example.org",
77 "_sip.TCP.example.org",
78 },
79 {
80 "bind-USERS.lists.example.org",
81 "bind-users.lists.example.org",
82 },
83 {
84 "a0123456789.example.org",
85 "A0123456789.example.org",
86 },
87 {
88 "\\000.example.org",
89 "\\000.example.org",
90 },
91 {
92 "wWw.\\000.isc.org",
93 "www.\\000.isc.org",
94 },
95 {
96 "\255.example.org",
97 "\255.example.ORG",
98 }
99 };
100
101 static bool
102 ownercase_test_one(const char *str1, const char *str2) {
103 isc_result_t result;
104 db_nodelock_t node_locks[1];
105 qpzonedb_t qpdb = {
106 .common.methods = &qpdb_zonemethods,
107 .common.mctx = mctx,
108 .node_locks = node_locks,
109 };
110 qpznode_t node = { .locknum = 0 };
111 dns_slabheader_t header = {
112 .node = &node,
113 .db = (dns_db_t *)&qpdb,
114 };
115 unsigned char *raw = (unsigned char *)(&header) + sizeof(header);
116 dns_rdataset_t rdataset = {
117 .magic = DNS_RDATASET_MAGIC,
118 .slab = { .db = (dns_db_t *)&qpdb, .node = &node, .raw = raw },
119 .methods = &dns_rdataslab_rdatasetmethods,
120 };
121 isc_buffer_t b;
122 dns_fixedname_t fname1, fname2;
123 dns_name_t *name1 = dns_fixedname_initname(&fname1);
124 dns_name_t *name2 = dns_fixedname_initname(&fname2);
125
126 memset(node_locks, 0, sizeof(node_locks));
127 /* Minimal initialization of the mock objects */
128 NODE_INITLOCK(&qpdb.node_locks[0].lock);
129
130 isc_buffer_constinit(&b, str1, strlen(str1));
131 isc_buffer_add(&b, strlen(str1));
132 result = dns_name_fromtext(name1, &b, dns_rootname, 0, NULL);
133 assert_int_equal(result, ISC_R_SUCCESS);
134
135 isc_buffer_constinit(&b, str2, strlen(str2));
136 isc_buffer_add(&b, strlen(str2));
137 result = dns_name_fromtext(name2, &b, dns_rootname, 0, NULL);
138 assert_int_equal(result, ISC_R_SUCCESS);
139
140 /* Store the case from name1 */
141 dns_rdataset_setownercase(&rdataset, name1);
142
143 assert_true(CASESET(&header));
144
145 /* Retrieve the case to name2 */
146 dns_rdataset_getownercase(&rdataset, name2);
147
148 NODE_DESTROYLOCK(&qpdb.node_locks[0].lock);
149
150 return dns_name_caseequal(name1, name2);
151 }
152
153 ISC_RUN_TEST_IMPL(ownercase) {
154 UNUSED(state);
155
156 for (size_t n = 0; n < ARRAY_SIZE(ownercase_vectors); n++) {
157 assert_true(ownercase_test_one(ownercase_vectors[n][0],
158 ownercase_vectors[n][1]));
159 }
160
161 assert_false(ownercase_test_one("W.example.org", "\\000.example.org"));
162
163 /* Ö and ö in ISO Latin 1 */
164 assert_false(ownercase_test_one("\\216", "\\246"));
165 }
166
167 ISC_RUN_TEST_IMPL(setownercase) {
168 isc_result_t result;
169 db_nodelock_t node_locks[1];
170 qpzonedb_t qpdb = {
171 .common.methods = &qpdb_zonemethods,
172 .common.mctx = mctx,
173 .node_locks = node_locks,
174 };
175 qpznode_t node = { .locknum = 0 };
176 dns_slabheader_t header = {
177 .node = &node,
178 .db = (dns_db_t *)&qpdb,
179 };
180 unsigned char *raw = (unsigned char *)(&header) + sizeof(header);
181 dns_rdataset_t rdataset = {
182 .magic = DNS_RDATASET_MAGIC,
183 .slab = { .db = (dns_db_t *)&qpdb, .node = &node, .raw = raw },
184 .methods = &dns_rdataslab_rdatasetmethods,
185 };
186 const char *str1 =
187 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
188 isc_buffer_t b;
189 dns_fixedname_t fname1, fname2;
190 dns_name_t *name1 = dns_fixedname_initname(&fname1);
191 dns_name_t *name2 = dns_fixedname_initname(&fname2);
192
193 UNUSED(state);
194
195 /* Minimal initialization of the mock objects */
196 memset(node_locks, 0, sizeof(node_locks));
197 NODE_INITLOCK(&qpdb.node_locks[0].lock);
198
199 isc_buffer_constinit(&b, str1, strlen(str1));
200 isc_buffer_add(&b, strlen(str1));
201 result = dns_name_fromtext(name1, &b, dns_rootname, 0, NULL);
202 assert_int_equal(result, ISC_R_SUCCESS);
203
204 isc_buffer_constinit(&b, str1, strlen(str1));
205 isc_buffer_add(&b, strlen(str1));
206 result = dns_name_fromtext(name2, &b, dns_rootname, 0, NULL);
207 assert_int_equal(result, ISC_R_SUCCESS);
208
209 assert_false(CASESET(&header));
210
211 /* Retrieve the case to name2 */
212 dns_rdataset_getownercase(&rdataset, name2);
213
214 NODE_DESTROYLOCK(&qpdb.node_locks[0].lock);
215
216 assert_true(dns_name_caseequal(name1, name2));
217 }
218
219 ISC_TEST_LIST_START
220 ISC_TEST_ENTRY(ownercase)
221 ISC_TEST_ENTRY(setownercase)
222 ISC_TEST_LIST_END
223
224 ISC_TEST_MAIN