Debian Codegraph @

tests/dns/dnstap_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 #include <fstrm.h>
26
27 #include <protobuf-c/protobuf-c.h>
28
29 #include <isc/buffer.h>
30 #include <isc/file.h>
31 #include <isc/stdio.h>
32 #include <isc/types.h>
33 #include <isc/util.h>
34
35 #include <dns/dnstap.h>
36 #include <dns/view.h>
37
38 #include <tests/dns.h>
39
40 #define TAPFILE TESTS_DIR "/testdata/dnstap/dnstap.file"
41 #define TAPSOCK TESTS_DIR "/testdata/dnstap/dnstap.sock"
42
43 #define TAPSAVED TESTS_DIR "/testdata/dnstap/dnstap.saved"
44 #define TAPTEXT TESTS_DIR "/testdata/dnstap/dnstap.text"
45
46 static void
47 cleanup(void **state ISC_ATTR_UNUSED) {
48 (void)isc_file_remove(TAPFILE);
49 (void)isc_file_remove(TAPSOCK);
50 }
51
52 static int
53 setup(void **state) {
54 /*
55 * Make sure files are cleaned up before the test runs.
56 */
57 cleanup(state);
58
59 /*
60 * Make sure text conversions match the time zone in which
61 * the testdata was originally generated.
62 */
63 setenv("TZ", "PDT8", 1);
64
65 setup_loopmgr(state);
66
67 return 0;
68 }
69
70 static int
71 teardown(void **state) {
72 cleanup(state);
73
74 teardown_loopmgr(state);
75
76 return 0;
77 }
78
79 /* set up dnstap environment */
80 ISC_LOOP_TEST_IMPL(dns_dt_create) {
81 isc_result_t result;
82 dns_dtenv_t *dtenv = NULL;
83 struct fstrm_iothr_options *fopt;
84
85 fopt = fstrm_iothr_options_init();
86 assert_non_null(fopt);
87 fstrm_iothr_options_set_num_input_queues(fopt, 1);
88
89 result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL,
90 &dtenv);
91 assert_int_equal(result, ISC_R_SUCCESS);
92 if (dtenv != NULL) {
93 dns_dt_detach(&dtenv);
94 }
95 if (fopt != NULL) {
96 fstrm_iothr_options_destroy(&fopt);
97 }
98
99 assert_true(isc_file_exists(TAPFILE));
100
101 fopt = fstrm_iothr_options_init();
102 assert_non_null(fopt);
103 fstrm_iothr_options_set_num_input_queues(fopt, 1);
104
105 result = dns_dt_create(mctx, dns_dtmode_unix, TAPSOCK, &fopt, NULL,
106 &dtenv);
107 assert_int_equal(result, ISC_R_SUCCESS);
108 if (dtenv != NULL) {
109 dns_dt_detach(&dtenv);
110 }
111 if (fopt != NULL) {
112 fstrm_iothr_options_destroy(&fopt);
113 }
114
115 /* 'create' should succeed, but the file shouldn't exist yet */
116 assert_false(isc_file_exists(TAPSOCK));
117
118 fopt = fstrm_iothr_options_init();
119 assert_non_null(fopt);
120 fstrm_iothr_options_set_num_input_queues(fopt, 1);
121
122 result = dns_dt_create(mctx, 33, TAPSOCK, &fopt, NULL, &dtenv);
123 assert_int_equal(result, ISC_R_FAILURE);
124 assert_null(dtenv);
125 if (dtenv != NULL) {
126 dns_dt_detach(&dtenv);
127 }
128 if (fopt != NULL) {
129 fstrm_iothr_options_destroy(&fopt);
130 }
131
132 isc_loopmgr_shutdown(loopmgr);
133 }
134
135 /* send dnstap messages */
136 ISC_LOOP_TEST_IMPL(dns_dt_send) {
137 isc_result_t result;
138 dns_dtenv_t *dtenv = NULL;
139 dns_dthandle_t *handle = NULL;
140 uint8_t *data;
141 size_t dsize;
142 unsigned char zone[DNS_NAME_MAXWIRE];
143 unsigned char qambuffer[4096], rambuffer[4096];
144 unsigned char qrmbuffer[4096], rrmbuffer[4096];
145 isc_buffer_t zb, qamsg, ramsg, qrmsg, rrmsg;
146 size_t qasize, qrsize, rasize, rrsize;
147 dns_fixedname_t zfname;
148 dns_name_t *zname;
149 dns_dtmsgtype_t dt;
150 dns_view_t *view = NULL;
151 dns_compress_t cctx;
152 isc_region_t zr;
153 isc_sockaddr_t qaddr;
154 isc_sockaddr_t raddr;
155 struct in_addr in;
156 isc_stdtime_t now = isc_stdtime_now();
157
158 isc_time_t p, f;
159 struct fstrm_iothr_options *fopt;
160
161 result = dns_test_makeview("test", false, false, &view);
162 assert_int_equal(result, ISC_R_SUCCESS);
163
164 fopt = fstrm_iothr_options_init();
165 assert_non_null(fopt);
166 fstrm_iothr_options_set_num_input_queues(fopt, 1);
167
168 result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL,
169 &dtenv);
170 assert_int_equal(result, ISC_R_SUCCESS);
171
172 dns_dt_attach(dtenv, &view->dtenv);
173 view->dttypes = DNS_DTTYPE_ALL;
174
175 /*
176 * Set up some test data
177 */
178 zname = dns_fixedname_initname(&zfname);
179 isc_buffer_constinit(&zb, "example.com.", 12);
180 isc_buffer_add(&zb, 12);
181 result = dns_name_fromtext(zname, &zb, NULL, 0, NULL);
182 assert_int_equal(result, ISC_R_SUCCESS);
183
184 memset(&zr, 0, sizeof(zr));
185 isc_buffer_init(&zb, zone, sizeof(zone));
186 dns_compress_init(&cctx, mctx, 0);
187 dns_compress_setpermitted(&cctx, false);
188 result = dns_name_towire(zname, &cctx, &zb, NULL);
189 assert_int_equal(result, ISC_R_SUCCESS);
190 dns_compress_invalidate(&cctx);
191 isc_buffer_usedregion(&zb, &zr);
192
193 in.s_addr = inet_addr("10.53.0.1");
194 isc_sockaddr_fromin(&qaddr, &in, 2112);
195 in.s_addr = inet_addr("10.53.0.2");
196 isc_sockaddr_fromin(&raddr, &in, 2112);
197
198 isc_time_set(&p, now - 3600, 0); /* past */
199 isc_time_set(&f, now + 3600, 0); /* future */
200
201 result = dns_test_getdata(TESTS_DIR "/testdata/dnstap/query.auth",
202 qambuffer, sizeof(qambuffer), &qasize);
203 assert_int_equal(result, ISC_R_SUCCESS);
204 isc_buffer_init(&qamsg, qambuffer, qasize);
205 isc_buffer_add(&qamsg, qasize);
206
207 result = dns_test_getdata(TESTS_DIR "/testdata/dnstap/response.auth",
208 rambuffer, sizeof(rambuffer), &rasize);
209 assert_int_equal(result, ISC_R_SUCCESS);
210 isc_buffer_init(&ramsg, rambuffer, rasize);
211 isc_buffer_add(&ramsg, rasize);
212
213 result = dns_test_getdata(TESTS_DIR "/testdata/dnstap/query.recursive",
214 qrmbuffer, sizeof(qrmbuffer), &qrsize);
215 assert_int_equal(result, ISC_R_SUCCESS);
216 isc_buffer_init(&qrmsg, qrmbuffer, qrsize);
217 isc_buffer_add(&qrmsg, qrsize);
218
219 result = dns_test_getdata(TESTS_DIR
220 "/testdata/dnstap/response.recursive",
221 rrmbuffer, sizeof(rrmbuffer), &rrsize);
222 assert_int_equal(result, ISC_R_SUCCESS);
223 isc_buffer_init(&rrmsg, rrmbuffer, rrsize);
224 isc_buffer_add(&rrmsg, rrsize);
225
226 for (dt = DNS_DTTYPE_SQ; dt <= DNS_DTTYPE_TR; dt <<= 1) {
227 isc_buffer_t *m;
228 isc_sockaddr_t *q = &qaddr, *r = &raddr;
229
230 switch (dt) {
231 case DNS_DTTYPE_AQ:
232 m = &qamsg;
233 break;
234 case DNS_DTTYPE_AR:
235 m = &ramsg;
236 break;
237 default:
238 m = &qrmsg;
239 if ((dt & DNS_DTTYPE_RESPONSE) != 0) {
240 m = &ramsg;
241 }
242 break;
243 }
244
245 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_UDP, &zr, &p, &f, m);
246 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_UDP, &zr, NULL, &f,
247 m);
248 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_UDP, &zr, &p, NULL,
249 m);
250 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_UDP, &zr, NULL, NULL,
251 m);
252 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_TCP, &zr, &p, &f, m);
253 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_TCP, &zr, NULL, &f,
254 m);
255 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_TCP, &zr, &p, NULL,
256 m);
257 dns_dt_send(view, dt, q, r, DNS_TRANSPORT_TCP, &zr, NULL, NULL,
258 m);
259 }
260
261 dns_dt_detach(&view->dtenv);
262 dns_dt_detach(&dtenv);
263 dns_view_detach(&view);
264
265 result = dns_dt_open(TAPFILE, dns_dtmode_file, mctx, &handle);
266 assert_int_equal(result, ISC_R_SUCCESS);
267
268 while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
269 dns_dtdata_t *dtdata = NULL;
270 isc_region_t r;
271 static dns_dtmsgtype_t expected = DNS_DTTYPE_SQ;
272 static int n = 0;
273
274 r.base = data;
275 r.length = dsize;
276
277 result = dns_dt_parse(mctx, &r, &dtdata);
278 assert_int_equal(result, ISC_R_SUCCESS);
279 if (result != ISC_R_SUCCESS) {
280 n++;
281 continue;
282 }
283
284 assert_int_equal(dtdata->type, expected);
285 if (++n % 8 == 0) {
286 expected <<= 1;
287 }
288
289 dns_dtdata_free(&dtdata);
290 }
291
292 if (fopt != NULL) {
293 fstrm_iothr_options_destroy(&fopt);
294 }
295 if (handle != NULL) {
296 dns_dt_close(&handle);
297 }
298
299 isc_loopmgr_shutdown(loopmgr);
300 }
301
302 /* dnstap message to text */
303 ISC_LOOP_TEST_IMPL(dns_dt_totext) {
304 isc_result_t result;
305 dns_dthandle_t *handle = NULL;
306 uint8_t *data;
307 size_t dsize;
308 FILE *fp = NULL;
309
310 result = dns_dt_open(TAPSAVED, dns_dtmode_file, mctx, &handle);
311 assert_int_equal(result, ISC_R_SUCCESS);
312
313 result = isc_stdio_open(TAPTEXT, "r", &fp);
314 assert_int_equal(result, ISC_R_SUCCESS);
315
316 while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
317 dns_dtdata_t *dtdata = NULL;
318 isc_buffer_t *b = NULL;
319 isc_region_t r;
320 char s[BUFSIZ], *p;
321
322 r.base = data;
323 r.length = dsize;
324
325 /* read the corresponding line of text */
326 p = fgets(s, sizeof(s), fp);
327 assert_ptr_equal(p, s);
328 if (p == NULL) {
329 break;
330 }
331
332 p = strchr(p, '\n');
333 if (p != NULL) {
334 *p = '\0';
335 }
336
337 /* parse dnstap frame */
338 result = dns_dt_parse(mctx, &r, &dtdata);
339 assert_int_equal(result, ISC_R_SUCCESS);
340 if (result != ISC_R_SUCCESS) {
341 continue;
342 }
343
344 isc_buffer_allocate(mctx, &b, 2048);
345 assert_non_null(b);
346 if (b == NULL) {
347 break;
348 }
349
350 /* convert to text and compare */
351 result = dns_dt_datatotext(dtdata, &b);
352 assert_int_equal(result, ISC_R_SUCCESS);
353
354 assert_string_equal((char *)isc_buffer_base(b), s);
355
356 dns_dtdata_free(&dtdata);
357 isc_buffer_free(&b);
358 }
359
360 if (handle != NULL) {
361 dns_dt_close(&handle);
362 }
363 isc_loopmgr_shutdown(loopmgr);
364 }
365
366 ISC_TEST_LIST_START
367
368 ISC_TEST_ENTRY_CUSTOM(dns_dt_create, setup, teardown)
369 ISC_TEST_ENTRY_CUSTOM(dns_dt_send, setup, teardown)
370 ISC_TEST_ENTRY_CUSTOM(dns_dt_totext, setup, teardown)
371
372 ISC_TEST_LIST_END
373
374 ISC_TEST_MAIN