convert dnstap_test
This commit is contained in:
@@ -8,7 +8,7 @@ tap_test_program{name='dbiterator_test'}
|
||||
atf_test_program{name='dbversion_test'}
|
||||
tap_test_program{name='dh_test'}
|
||||
atf_test_program{name='dispatch_test'}
|
||||
atf_test_program{name='dnstap_test'}
|
||||
tap_test_program{name='dnstap_test'}
|
||||
atf_test_program{name='dst_test'}
|
||||
tap_test_program{name='geoip_test'}
|
||||
atf_test_program{name='keytable_test'}
|
||||
|
||||
@@ -133,9 +133,9 @@ dispatch_test@EXEEXT@: dispatch_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS}
|
||||
${ISCLIBS} ${LIBS}
|
||||
|
||||
dnstap_test@EXEEXT@: dnstap_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
|
||||
dnstap_test.@O@ dnstest.@O@ ${DNSLIBS} \
|
||||
${ISCLIBS} ${LIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \
|
||||
${LDFLAGS} -o $@ dnstap_test.@O@ dnstest.@O@ \
|
||||
${DNSLIBS} ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS}
|
||||
|
||||
dst_test@EXEEXT@: dst_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS}
|
||||
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
|
||||
|
||||
@@ -9,21 +9,28 @@
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
/*! \file */
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <atf-c.h>
|
||||
#if HAVE_CMOCKA
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/file.h>
|
||||
#include <isc/stdio.h>
|
||||
#include <isc/print.h>
|
||||
#include <isc/types.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include <dns/dnstap.h>
|
||||
#include <dns/view.h>
|
||||
@@ -40,85 +47,99 @@
|
||||
#define TAPSAVED "testdata/dnstap/dnstap.saved"
|
||||
#define TAPTEXT "testdata/dnstap/dnstap.text"
|
||||
|
||||
/*
|
||||
* Helper functions
|
||||
*/
|
||||
static int
|
||||
_setup(void **state) {
|
||||
isc_result_t result;
|
||||
|
||||
UNUSED(state);
|
||||
|
||||
result = dns_test_begin(NULL, false);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
_teardown(void **state) {
|
||||
UNUSED(state);
|
||||
|
||||
dns_test_end();
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
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) {
|
||||
/* set up dnstap environment */
|
||||
static void
|
||||
create_test(void **state) {
|
||||
isc_result_t result;
|
||||
dns_dtenv_t *dtenv = NULL;
|
||||
struct fstrm_iothr_options *fopt;
|
||||
|
||||
UNUSED(tc);
|
||||
UNUSED(state);
|
||||
|
||||
cleanup();
|
||||
|
||||
result = dns_test_begin(NULL, true);
|
||||
ATF_REQUIRE(result == ISC_R_SUCCESS);
|
||||
|
||||
fopt = fstrm_iothr_options_init();
|
||||
ATF_REQUIRE(fopt != NULL);
|
||||
assert_non_null(fopt);
|
||||
fstrm_iothr_options_set_num_input_queues(fopt, 1);
|
||||
|
||||
result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL, &dtenv);
|
||||
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
||||
if (dtenv != NULL)
|
||||
result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE,
|
||||
&fopt, NULL, &dtenv);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
if (dtenv != NULL) {
|
||||
dns_dt_detach(&dtenv);
|
||||
if (fopt != NULL)
|
||||
}
|
||||
if (fopt != NULL) {
|
||||
fstrm_iothr_options_destroy(&fopt);
|
||||
}
|
||||
|
||||
ATF_CHECK(isc_file_exists(TAPFILE));
|
||||
assert_true(isc_file_exists(TAPFILE));
|
||||
|
||||
fopt = fstrm_iothr_options_init();
|
||||
ATF_REQUIRE(fopt != NULL);
|
||||
assert_non_null(fopt);
|
||||
fstrm_iothr_options_set_num_input_queues(fopt, 1);
|
||||
|
||||
result = dns_dt_create(mctx, dns_dtmode_unix, TAPSOCK, &fopt, NULL, &dtenv);
|
||||
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
||||
if (dtenv != NULL)
|
||||
result = dns_dt_create(mctx, dns_dtmode_unix, TAPSOCK,
|
||||
&fopt, NULL, &dtenv);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
if (dtenv != NULL) {
|
||||
dns_dt_detach(&dtenv);
|
||||
if (fopt != NULL)
|
||||
}
|
||||
if (fopt != NULL) {
|
||||
fstrm_iothr_options_destroy(&fopt);
|
||||
}
|
||||
|
||||
/* 'create' should succeed, but the file shouldn't exist yet */
|
||||
ATF_CHECK(!isc_file_exists(TAPSOCK));
|
||||
assert_false(isc_file_exists(TAPSOCK));
|
||||
|
||||
fopt = fstrm_iothr_options_init();
|
||||
ATF_REQUIRE(fopt != NULL);
|
||||
assert_non_null(fopt);
|
||||
fstrm_iothr_options_set_num_input_queues(fopt, 1);
|
||||
|
||||
result = dns_dt_create(mctx, 33, TAPSOCK, &fopt, NULL, &dtenv);
|
||||
ATF_CHECK_EQ(result, ISC_R_FAILURE);
|
||||
ATF_CHECK_EQ(dtenv, NULL);
|
||||
if (dtenv != NULL)
|
||||
assert_int_equal(result, ISC_R_FAILURE);
|
||||
assert_null(dtenv);
|
||||
if (dtenv != NULL) {
|
||||
dns_dt_detach(&dtenv);
|
||||
if (fopt != NULL)
|
||||
}
|
||||
if (fopt != NULL) {
|
||||
fstrm_iothr_options_destroy(&fopt);
|
||||
}
|
||||
|
||||
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) {
|
||||
/* send dnstap messages */
|
||||
static void
|
||||
send_test(void **state) {
|
||||
isc_result_t result;
|
||||
dns_dtenv_t *dtenv = NULL;
|
||||
dns_dthandle_t *handle = NULL;
|
||||
@@ -142,21 +163,19 @@ ATF_TC_BODY(send, tc) {
|
||||
isc_time_t p, f;
|
||||
struct fstrm_iothr_options *fopt;
|
||||
|
||||
UNUSED(tc);
|
||||
UNUSED(state);
|
||||
|
||||
cleanup();
|
||||
|
||||
result = dns_test_begin(NULL, true);
|
||||
ATF_REQUIRE(result == ISC_R_SUCCESS);
|
||||
|
||||
result = dns_test_makeview("test", &view);
|
||||
|
||||
fopt = fstrm_iothr_options_init();
|
||||
ATF_REQUIRE(fopt != NULL);
|
||||
assert_non_null(fopt);
|
||||
fstrm_iothr_options_set_num_input_queues(fopt, 1);
|
||||
|
||||
result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE, &fopt, NULL, &dtenv);
|
||||
ATF_REQUIRE(result == ISC_R_SUCCESS);
|
||||
result = dns_dt_create(mctx, dns_dtmode_file, TAPFILE,
|
||||
&fopt, NULL, &dtenv);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
dns_dt_attach(dtenv, &view->dtenv);
|
||||
view->dttypes = DNS_DTTYPE_ALL;
|
||||
@@ -168,14 +187,14 @@ ATF_TC_BODY(send, tc) {
|
||||
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);
|
||||
assert_int_equal(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);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
dns_compress_invalidate(&cctx);
|
||||
isc_buffer_usedregion(&zb, &zr);
|
||||
|
||||
@@ -190,25 +209,25 @@ ATF_TC_BODY(send, tc) {
|
||||
|
||||
result = dns_test_getdata("testdata/dnstap/query.auth",
|
||||
qambuffer, sizeof(qambuffer), &qasize);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(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);
|
||||
assert_int_equal(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);
|
||||
assert_int_equal(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);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_buffer_init(&rrmsg, rrmbuffer, rrsize);
|
||||
isc_buffer_add(&rrmsg, rrsize);
|
||||
|
||||
@@ -225,8 +244,9 @@ ATF_TC_BODY(send, tc) {
|
||||
break;
|
||||
default:
|
||||
m = &qrmsg;
|
||||
if ((dt & DNS_DTTYPE_RESPONSE) != 0)
|
||||
if ((dt & DNS_DTTYPE_RESPONSE) != 0) {
|
||||
m = &ramsg;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -245,12 +265,8 @@ ATF_TC_BODY(send, tc) {
|
||||
dns_dt_shutdown();
|
||||
dns_view_detach(&view);
|
||||
|
||||
/*
|
||||
* XXX now read back and check content.
|
||||
*/
|
||||
|
||||
result = dns_dt_open(TAPFILE, dns_dtmode_file, mctx, &handle);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
|
||||
dns_dtdata_t *dtdata = NULL;
|
||||
@@ -262,52 +278,48 @@ ATF_TC_BODY(send, tc) {
|
||||
r.length = dsize;
|
||||
|
||||
result = dns_dt_parse(mctx, &r, &dtdata);
|
||||
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
n++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ATF_CHECK_EQ(dtdata->type, expected);
|
||||
if (++n % 8 == 0)
|
||||
assert_int_equal(dtdata->type, expected);
|
||||
if (++n % 8 == 0) {
|
||||
expected <<= 1;
|
||||
}
|
||||
|
||||
dns_dtdata_free(&dtdata);
|
||||
}
|
||||
|
||||
if (fopt != NULL)
|
||||
if (fopt != NULL) {
|
||||
fstrm_iothr_options_destroy(&fopt);
|
||||
if (handle != NULL)
|
||||
}
|
||||
if (handle != NULL) {
|
||||
dns_dt_close(&handle);
|
||||
}
|
||||
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) {
|
||||
/* dnstap message to text */
|
||||
static void
|
||||
totext_test(void **state) {
|
||||
isc_result_t result;
|
||||
dns_dthandle_t *handle = NULL;
|
||||
uint8_t *data;
|
||||
size_t dsize;
|
||||
FILE *fp = NULL;
|
||||
|
||||
UNUSED(tc);
|
||||
UNUSED(state);
|
||||
|
||||
/* make sure text conversion gets the right local time */
|
||||
setenv("TZ", "PST8", 1);
|
||||
|
||||
result = dns_test_begin(NULL, true);
|
||||
ATF_REQUIRE(result == ISC_R_SUCCESS);
|
||||
|
||||
result = dns_dt_open(TAPSAVED, dns_dtmode_file, mctx, &handle);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
result = isc_stdio_open(TAPTEXT, "r", &fp);
|
||||
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
while (dns_dt_getframe(handle, &data, &dsize) == ISC_R_SUCCESS) {
|
||||
dns_dtdata_t *dtdata = NULL;
|
||||
@@ -320,64 +332,69 @@ ATF_TC_BODY(totext, tc) {
|
||||
|
||||
/* read the corresponding line of text */
|
||||
p = fgets(s, sizeof(s), fp);
|
||||
ATF_CHECK_EQ(p, s);
|
||||
if (p == NULL)
|
||||
assert_ptr_equal(p, s);
|
||||
if (p == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
p = strchr(p, '\n');
|
||||
if (p != NULL)
|
||||
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)
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
isc_buffer_allocate(mctx, &b, 2048);
|
||||
ATF_CHECK(b != NULL);
|
||||
if (b == NULL)
|
||||
assert_non_null(b);
|
||||
if (b == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* convert to text and compare */
|
||||
result = dns_dt_datatotext(dtdata, &b);
|
||||
ATF_CHECK_EQ(result, ISC_R_SUCCESS);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
ATF_CHECK_STREQ((char *) isc_buffer_base(b), s);
|
||||
assert_string_equal((char *) isc_buffer_base(b), s);
|
||||
|
||||
dns_dtdata_free(&dtdata);
|
||||
isc_buffer_free(&b);
|
||||
}
|
||||
|
||||
if (handle != NULL)
|
||||
if (handle != NULL) {
|
||||
dns_dt_close(&handle);
|
||||
}
|
||||
cleanup();
|
||||
|
||||
dns_test_end();
|
||||
}
|
||||
#endif /* HAVE_DNSTAP */
|
||||
|
||||
int
|
||||
main(void) {
|
||||
#if HAVE_DNSTAP
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(create_test, _setup, _teardown),
|
||||
cmocka_unit_test_setup_teardown(send_test, _setup, _teardown),
|
||||
cmocka_unit_test_setup_teardown(totext_test, _setup, _teardown),
|
||||
};
|
||||
|
||||
return (cmocka_run_group_tests(tests, NULL, NULL));
|
||||
#else
|
||||
ATF_TC(untested);
|
||||
ATF_TC_HEAD(untested, tc) {
|
||||
atf_tc_set_md_var(tc, "descr", "skipping dnstap test");
|
||||
print_message("1..0 # Skip dnstap not enabled\n");
|
||||
#endif /* HAVE_DNSTAP */
|
||||
}
|
||||
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
|
||||
#else /* HAVE_CMOCKA */
|
||||
|
||||
return (atf_no_error());
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main(void) {
|
||||
printf("1..0 # Skipped: cmocka not available\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
#endif /* HAVE_CMOCKA */
|
||||
|
||||
Reference in New Issue
Block a user