dnstap_test.c 8.98 KB
Newer Older
Evan Hunt's avatar
Evan Hunt committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Evan Hunt's avatar
Evan Hunt committed
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Evan Hunt's avatar
Evan Hunt committed
10
11
12
13
14
15
16
17
18
 */

/*! \file */

#include <config.h>

#include <atf-c.h>

#include <unistd.h>
19
#include <inttypes.h>
Evan Hunt's avatar
Evan Hunt committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>

#include <isc/buffer.h>
#include <isc/file.h>
#include <isc/stdio.h>
#include <isc/print.h>
#include <isc/types.h>

#include <dns/dnstap.h>
#include <dns/view.h>

#include "dnstest.h"

#ifdef HAVE_DNSTAP
#include <dns/dnstap.pb-c.h>
#include <protobuf-c/protobuf-c.h>

#define TAPFILE "testdata/dnstap/dnstap.file"
#define TAPSOCK "testdata/dnstap/dnstap.sock"

#define TAPSAVED "testdata/dnstap/dnstap.saved"
#define TAPTEXT "testdata/dnstap/dnstap.text"

/*
 * Helper functions
 */
static void
cleanup() {
	(void) isc_file_remove(TAPFILE);
	(void) isc_file_remove(TAPSOCK);
}

/*
 * Individual unit tests
 */

ATF_TC(create);
ATF_TC_HEAD(create, tc) {
	atf_tc_set_md_var(tc, "descr", "set up dnstap environment");
}
ATF_TC_BODY(create, tc) {
	isc_result_t result;
	dns_dtenv_t *dtenv = NULL;
63
	struct fstrm_iothr_options *fopt;
Evan Hunt's avatar
Evan Hunt committed
64
65
66
67
68

	UNUSED(tc);

	cleanup();

69
	result = dns_test_begin(NULL, true);
Evan Hunt's avatar
Evan Hunt committed
70
71
	ATF_REQUIRE(result == ISC_R_SUCCESS);

72
73
74
75
	fopt = fstrm_iothr_options_init();
	ATF_REQUIRE(fopt != NULL);
	fstrm_iothr_options_set_num_input_queues(fopt, 1);

76
	result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL, &dtenv);
Evan Hunt's avatar
Evan Hunt committed
77
78
79
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	if (dtenv != NULL)
		dns_dt_detach(&dtenv);
80
81
	if (fopt != NULL)
		fstrm_iothr_options_destroy(&fopt);
Evan Hunt's avatar
Evan Hunt committed
82
83
84

	ATF_CHECK(isc_file_exists(TAPFILE));

85
86
87
88
	fopt = fstrm_iothr_options_init();
	ATF_REQUIRE(fopt != NULL);
	fstrm_iothr_options_set_num_input_queues(fopt, 1);

89
	result = dns_dt_create(mctx, dns_dtmode_unix, TAPSOCK, &fopt, NULL, &dtenv);
Evan Hunt's avatar
Evan Hunt committed
90
91
92
	ATF_CHECK_EQ(result, ISC_R_SUCCESS);
	if (dtenv != NULL)
		dns_dt_detach(&dtenv);
93
94
	if (fopt != NULL)
		fstrm_iothr_options_destroy(&fopt);
Evan Hunt's avatar
Evan Hunt committed
95
96
97
98

	/* 'create' should succeed, but the file shouldn't exist yet */
	ATF_CHECK(!isc_file_exists(TAPSOCK));

99
100
101
102
	fopt = fstrm_iothr_options_init();
	ATF_REQUIRE(fopt != NULL);
	fstrm_iothr_options_set_num_input_queues(fopt, 1);

103
	result = dns_dt_create(mctx, 33, TAPSOCK, &fopt, NULL, &dtenv);
Evan Hunt's avatar
Evan Hunt committed
104
105
	ATF_CHECK_EQ(result, ISC_R_FAILURE);
	ATF_CHECK_EQ(dtenv, NULL);
106
107
108
109
	if (dtenv != NULL)
		dns_dt_detach(&dtenv);
	if (fopt != NULL)
		fstrm_iothr_options_destroy(&fopt);
Evan Hunt's avatar
Evan Hunt committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123

	cleanup();

	dns_dt_shutdown();
	dns_test_end();
}

ATF_TC(send);
ATF_TC_HEAD(send, tc) {
	atf_tc_set_md_var(tc, "descr", "send dnstap messages");
}
ATF_TC_BODY(send, tc) {
	isc_result_t result;
	dns_dtenv_t *dtenv = NULL;
124
	dns_dthandle_t *handle = NULL;
125
	uint8_t *data;
Evan Hunt's avatar
Evan Hunt committed
126
127
128
129
130
131
132
133
134
135
136
137
	size_t dsize;
	unsigned char zone[DNS_NAME_MAXWIRE];
	unsigned char qambuffer[4096], rambuffer[4096];
	unsigned char qrmbuffer[4096], rrmbuffer[4096];
	isc_buffer_t zb, qamsg, ramsg, qrmsg, rrmsg;
	size_t qasize, qrsize, rasize, rrsize;
	dns_fixedname_t zfname;
	dns_name_t *zname;
	dns_dtmsgtype_t dt;
	dns_view_t *view = NULL;
	dns_compress_t cctx;
	isc_region_t zr;
138
139
	isc_sockaddr_t qaddr;
	isc_sockaddr_t raddr;
Evan Hunt's avatar
Evan Hunt committed
140
141
142
	struct in_addr in;
	isc_stdtime_t now;
	isc_time_t p, f;
143
	struct fstrm_iothr_options *fopt;
Evan Hunt's avatar
Evan Hunt committed
144
145
146
147
148

	UNUSED(tc);

	cleanup();

149
	result = dns_test_begin(NULL, true);
Evan Hunt's avatar
Evan Hunt committed
150
151
152
153
	ATF_REQUIRE(result == ISC_R_SUCCESS);

	result = dns_test_makeview("test", &view);

154
155
156
157
	fopt = fstrm_iothr_options_init();
	ATF_REQUIRE(fopt != NULL);
	fstrm_iothr_options_set_num_input_queues(fopt, 1);

158
	result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL, &dtenv);
Evan Hunt's avatar
Evan Hunt committed
159
160
161
162
163
164
165
166
	ATF_REQUIRE(result == ISC_R_SUCCESS);

	dns_dt_attach(dtenv, &view->dtenv);
	view->dttypes = DNS_DTTYPE_ALL;

	/*
	 * Set up some test data
	 */
167
	zname = dns_fixedname_initname(&zfname);
Evan Hunt's avatar
Evan Hunt committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	isc_buffer_constinit(&zb, "example.com.", 12);
	isc_buffer_add(&zb, 12);
	result = dns_name_fromtext(zname, &zb, NULL, 0, NULL);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	memset(&zr, 0, sizeof(zr));
	isc_buffer_init(&zb, zone, sizeof(zone));
	result = dns_compress_init(&cctx, -1, mctx);
	dns_compress_setmethods(&cctx, DNS_COMPRESS_NONE);
	result = dns_name_towire(zname, &cctx, &zb);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	dns_compress_invalidate(&cctx);
	isc_buffer_usedregion(&zb, &zr);

	in.s_addr = inet_addr("10.53.0.1");
183
184
185
	isc_sockaddr_fromin(&qaddr, &in, 2112);
	in.s_addr = inet_addr("10.53.0.2");
	isc_sockaddr_fromin(&raddr, &in, 2112);
Evan Hunt's avatar
Evan Hunt committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

	isc_stdtime_get(&now);
	isc_time_set(&p, now - 3600, 0); /* past */
	isc_time_set(&f, now + 3600, 0); /* future */

	result = dns_test_getdata("testdata/dnstap/query.auth",
				  qambuffer, sizeof(qambuffer), &qasize);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_buffer_init(&qamsg, qambuffer, qasize);
	isc_buffer_add(&qamsg, qasize);

	result = dns_test_getdata("testdata/dnstap/response.auth",
				  rambuffer, sizeof(rambuffer), &rasize);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_buffer_init(&ramsg, rambuffer, rasize);
	isc_buffer_add(&ramsg, rasize);

	result = dns_test_getdata("testdata/dnstap/query.recursive", qrmbuffer,
				  sizeof(qrmbuffer), &qrsize);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_buffer_init(&qrmsg, qrmbuffer, qrsize);
	isc_buffer_add(&qrmsg, qrsize);

	result = dns_test_getdata("testdata/dnstap/response.recursive",
				  rrmbuffer, sizeof(rrmbuffer), &rrsize);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_buffer_init(&rrmsg, rrmbuffer, rrsize);
	isc_buffer_add(&rrmsg, rrsize);

	for (dt = DNS_DTTYPE_SQ; dt <= DNS_DTTYPE_TR; dt <<= 1) {
		isc_buffer_t *m;
217
		isc_sockaddr_t *q = &qaddr, *r = &raddr;
Evan Hunt's avatar
Evan Hunt committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

		switch (dt) {
		case DNS_DTTYPE_AQ:
			m = &qamsg;
			break;
		case DNS_DTTYPE_AR:
			m = &ramsg;
			break;
		default:
			m = &qrmsg;
			if ((dt & DNS_DTTYPE_RESPONSE) != 0)
				m = &ramsg;
			break;
		}

233
234
235
236
237
238
239
240
		dns_dt_send(view, dt, q, r, false, &zr, &p, &f, m);
		dns_dt_send(view, dt, q, r, false, &zr, NULL, &f, m);
		dns_dt_send(view, dt, q, r, false, &zr, &p, NULL, m);
		dns_dt_send(view, dt, q, r, false, &zr, NULL, NULL, m);
		dns_dt_send(view, dt, q, r, true, &zr, &p, &f, m);
		dns_dt_send(view, dt, q, r, true, &zr, NULL, &f, m);
		dns_dt_send(view, dt, q, r, true, &zr, &p, NULL, m);
		dns_dt_send(view, dt, q, r, true, &zr, NULL, NULL, m);
Evan Hunt's avatar
Evan Hunt committed
241
242
243
244
245
246
247
248
249
250
251
	}

	dns_dt_detach(&view->dtenv);
	dns_dt_detach(&dtenv);
	dns_dt_shutdown();
	dns_view_detach(&view);

	/*
	 * XXX now read back and check content.
	 */

252
	result = dns_dt_open(TAPFILE, dns_dtmode_file, mctx, &handle);
Evan Hunt's avatar
Evan Hunt committed
253
254
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

255
	while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
Evan Hunt's avatar
Evan Hunt committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
		dns_dtdata_t *dtdata = NULL;
		isc_region_t r;
		static dns_dtmsgtype_t expected = DNS_DTTYPE_SQ;
		static int n = 0;

		r.base = data;
		r.length = dsize;

		result = dns_dt_parse(mctx, &r, &dtdata);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
		if (result != ISC_R_SUCCESS) {
			n++;
			continue;
		}

		ATF_CHECK_EQ(dtdata->type, expected);
		if (++n % 8 == 0)
			expected <<= 1;

		dns_dtdata_free(&dtdata);
	}

278
279
280
281
	if (fopt != NULL)
		fstrm_iothr_options_destroy(&fopt);
	if (handle != NULL)
		dns_dt_close(&handle);
Evan Hunt's avatar
Evan Hunt committed
282
283
284
285
286
287
288
289
290
291
292
	cleanup();

	dns_test_end();
}

ATF_TC(totext);
ATF_TC_HEAD(totext, tc) {
	atf_tc_set_md_var(tc, "descr", "dnstap message to text");
}
ATF_TC_BODY(totext, tc) {
	isc_result_t result;
293
	dns_dthandle_t *handle = NULL;
294
	uint8_t *data;
Evan Hunt's avatar
Evan Hunt committed
295
296
297
298
299
	size_t dsize;
	FILE *fp = NULL;

	UNUSED(tc);

300
301
302
	/* make sure text conversion gets the right local time */
	setenv("TZ", "PST8", 1);

303
	result = dns_test_begin(NULL, true);
Evan Hunt's avatar
Evan Hunt committed
304
305
	ATF_REQUIRE(result == ISC_R_SUCCESS);

306
	result = dns_dt_open(TAPSAVED, dns_dtmode_file, mctx, &handle);
Evan Hunt's avatar
Evan Hunt committed
307
308
309
310
311
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_stdio_open(TAPTEXT, "r", &fp);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

312
	while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
Evan Hunt's avatar
Evan Hunt committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
		dns_dtdata_t *dtdata = NULL;
		isc_buffer_t *b = NULL;
		isc_region_t r;
		char s[BUFSIZ], *p;

		r.base = data;
		r.length = dsize;

		/* read the corresponding line of text */
		p = fgets(s, sizeof(s), fp);
		ATF_CHECK_EQ(p, s);
		if (p == NULL)
			break;

		p = strchr(p, '\n');
		if (p != NULL)
			*p = '\0';

		/* parse dnstap frame */
		result = dns_dt_parse(mctx, &r, &dtdata);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
		if (result != ISC_R_SUCCESS)
			continue;

		isc_buffer_allocate(mctx, &b, 2048);
		ATF_CHECK(b != NULL);
		if (b == NULL)
			break;

		/* convert to text and compare */
		result = dns_dt_datatotext(dtdata, &b);
		ATF_CHECK_EQ(result, ISC_R_SUCCESS);

		ATF_CHECK_STREQ((char *) isc_buffer_base(b), s);

		dns_dtdata_free(&dtdata);
		isc_buffer_free(&b);
	}

352
353
	if (handle != NULL)
		dns_dt_close(&handle);
Evan Hunt's avatar
Evan Hunt committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
	cleanup();

	dns_test_end();
}

#else
ATF_TC(untested);
ATF_TC_HEAD(untested, tc) {
	atf_tc_set_md_var(tc, "descr", "skipping dnstap test");
}
ATF_TC_BODY(untested, tc) {
	UNUSED(tc);
	atf_tc_skip("dnstap not available");
}
#endif

/*
 * Main
 */
ATF_TP_ADD_TCS(tp) {
#ifdef HAVE_DNSTAP
	ATF_TP_ADD_TC(tp, create);
	ATF_TP_ADD_TC(tp, send);
	ATF_TP_ADD_TC(tp, totext);
#else
	ATF_TP_ADD_TC(tp, untested);
#endif

	return (atf_no_error());
}