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