From 8d347788b0fb1c511d808042082e18c9cd08afe5 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Fri, 26 Oct 2018 08:46:28 -0700 Subject: [PATCH] convert dispatch_test --- lib/dns/tests/Kyuafile | 2 +- lib/dns/tests/Makefile.in | 6 +- lib/dns/tests/dispatch_test.c | 184 +++++++++++++++++++--------------- 3 files changed, 108 insertions(+), 84 deletions(-) diff --git a/lib/dns/tests/Kyuafile b/lib/dns/tests/Kyuafile index d18a48c24c..939f51fa6e 100644 --- a/lib/dns/tests/Kyuafile +++ b/lib/dns/tests/Kyuafile @@ -7,7 +7,7 @@ tap_test_program{name='dbdiff_test'} tap_test_program{name='dbiterator_test'} atf_test_program{name='dbversion_test'} tap_test_program{name='dh_test'} -atf_test_program{name='dispatch_test'} +tap_test_program{name='dispatch_test'} tap_test_program{name='dnstap_test'} atf_test_program{name='dst_test'} tap_test_program{name='geoip_test'} diff --git a/lib/dns/tests/Makefile.in b/lib/dns/tests/Makefile.in index 380bec3b9e..4021f3c0ef 100644 --- a/lib/dns/tests/Makefile.in +++ b/lib/dns/tests/Makefile.in @@ -128,9 +128,9 @@ dh_test@EXEEXT@: dh_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${DNSLIBS} ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} dispatch_test@EXEEXT@: dispatch_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} - ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \ - dispatch_test.@O@ dnstest.@O@ ${DNSLIBS} \ - ${ISCLIBS} ${LIBS} + ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ + ${LDFLAGS} -o $@ dispatch_test.@O@ dnstest.@O@ \ + ${DNSLIBS} ${ISCLIBS} ${LIBS} ${CMOCKA_LIBS} dnstap_test@EXEEXT@: dnstap_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS} ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${CMOCKA_CFLAGS} \ diff --git a/lib/dns/tests/dispatch_test.c b/lib/dns/tests/dispatch_test.c index ae6fb665db..68a9cd46ce 100644 --- a/lib/dns/tests/dispatch_test.c +++ b/lib/dns/tests/dispatch_test.c @@ -9,21 +9,29 @@ * information regarding copyright ownership. */ -/*! \file */ - #include +#if HAVE_CMOCKA + +#include +#include +#include + #include #include +#include +#include #include -#include +#define UNIT_TESTING +#include #include #include #include #include #include +#include #include #include @@ -34,6 +42,27 @@ dns_dispatchmgr_t *dispatchmgr = NULL; dns_dispatchset_t *dset = NULL; +static int +_setup(void **state) { + isc_result_t result; + + UNUSED(state); + + result = dns_test_begin(NULL, true); + assert_int_equal(result, ISC_R_SUCCESS); + + return (0); +} + +static int +_teardown(void **state) { + UNUSED(state); + + dns_test_end(); + + return (0); +} + static isc_result_t make_dispatchset(unsigned int ndisps) { isc_result_t result; @@ -61,54 +90,41 @@ make_dispatchset(unsigned int ndisps) { } static void -teardown(void) { - if (dset != NULL) +reset(void) { + if (dset != NULL) { dns_dispatchset_destroy(&dset); - if (dispatchmgr != NULL) + } + if (dispatchmgr != NULL) { dns_dispatchmgr_destroy(&dispatchmgr); + } } -/* - * Individual unit tests - */ -ATF_TC(dispatchset_create); -ATF_TC_HEAD(dispatchset_create, tc) { - atf_tc_set_md_var(tc, "descr", "create dispatch set"); -} -ATF_TC_BODY(dispatchset_create, tc) { +/* create dispatch set */ +static void +dispatchset_create(void **state) { isc_result_t result; - UNUSED(tc); - - result = dns_test_begin(NULL, true); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + UNUSED(state); result = make_dispatchset(1); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); - teardown(); + assert_int_equal(result, ISC_R_SUCCESS); + reset(); result = make_dispatchset(10); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); - teardown(); - - dns_test_end(); + assert_int_equal(result, ISC_R_SUCCESS); + reset(); } -ATF_TC(dispatchset_get); -ATF_TC_HEAD(dispatchset_get, tc) { - atf_tc_set_md_var(tc, "descr", "test dispatch set round-robin"); -} -ATF_TC_BODY(dispatchset_get, tc) { +/* test dispatch set round-robin */ +static void +dispatchset_get(void **state) { isc_result_t result; dns_dispatch_t *d1, *d2, *d3, *d4, *d5; - UNUSED(tc); - - result = dns_test_begin(NULL, true); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + UNUSED(state); result = make_dispatchset(1); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); d1 = dns_dispatchset_get(dset); d2 = dns_dispatchset_get(dset); @@ -116,15 +132,15 @@ ATF_TC_BODY(dispatchset_get, tc) { d4 = dns_dispatchset_get(dset); d5 = dns_dispatchset_get(dset); - ATF_CHECK_EQ(d1, d2); - ATF_CHECK_EQ(d2, d3); - ATF_CHECK_EQ(d3, d4); - ATF_CHECK_EQ(d4, d5); + assert_int_equal(d1, d2); + assert_int_equal(d2, d3); + assert_int_equal(d3, d4); + assert_int_equal(d4, d5); - teardown(); + reset(); result = make_dispatchset(4); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); d1 = dns_dispatchset_get(dset); d2 = dns_dispatchset_get(dset); @@ -132,14 +148,13 @@ ATF_TC_BODY(dispatchset_get, tc) { d4 = dns_dispatchset_get(dset); d5 = dns_dispatchset_get(dset); - ATF_CHECK_EQ(d1, d5); - ATF_CHECK(d1 != d2); - ATF_CHECK(d2 != d3); - ATF_CHECK(d3 != d4); - ATF_CHECK(d4 != d5); + assert_int_equal(d1, d5); + assert_true(d1 != d2); + assert_true(d2 != d3); + assert_true(d3 != d4); + assert_true(d4 != d5); - teardown(); - dns_test_end(); + reset(); } static void @@ -219,7 +234,7 @@ response(isc_task_t *task, isc_event_t *event) { if (wasfirst) { result = dns_dispatch_getnext(dispentry, &devent); - ATF_CHECK_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); } else { dns_dispatch_removeresponse(&dispentry, &devent); isc_app_shutdown(); @@ -234,15 +249,13 @@ startit(isc_task_t *task, isc_event_t *event) { isc_socket_attach(dns_dispatch_getsocket(dispatch), &sock); result = isc_socket_sendto(sock, event->ev_arg, task, senddone, sock, &local, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); isc_event_free(&event); } -ATF_TC(dispatch_getnext); -ATF_TC_HEAD(dispatch_getnext, tc) { - atf_tc_set_md_var(tc, "descr", "test dispatch getnext"); -} -ATF_TC_BODY(dispatch_getnext, tc) { +/* test dispatch getnext */ +static void +dispatch_getnext(void **state) { isc_region_t region; isc_result_t result; isc_socket_t *sock = NULL; @@ -253,19 +266,16 @@ ATF_TC_BODY(dispatch_getnext, tc) { unsigned int attrs; unsigned char rbuf[12]; - UNUSED(tc); + UNUSED(state); result = isc_mutex_init(&lock); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); - - result = dns_test_begin(NULL, true); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = isc_task_create(taskmgr, 0, &task); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_dispatchmgr_create(mctx, &dispatchmgr); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); ina.s_addr = htonl(INADDR_LOOPBACK); isc_sockaddr_fromin(&local, &ina, 0); @@ -273,32 +283,32 @@ ATF_TC_BODY(dispatch_getnext, tc) { result = dns_dispatch_getudp(dispatchmgr, socketmgr, taskmgr, &local, 512, 6, 1024, 17, 19, attrs, attrs, &dispatch); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); /* * Create a local udp nameserver on the loopback. */ result = isc_socket_create(socketmgr, AF_INET, isc_sockettype_udp, &sock); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); ina.s_addr = htonl(INADDR_LOOPBACK); isc_sockaddr_fromin(&local, &ina, 0); result = isc_socket_bind(sock, &local, 0); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = isc_socket_getsockname(sock, &local); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); first = true; region.base = rbuf; region.length = sizeof(rbuf); result = isc_socket_recv(sock, ®ion, 1, task, nameserver, sock); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = dns_dispatch_addresponse(dispatch, 0, &local, task, response, NULL, &id, &dispentry, NULL); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); memset(message, 0, sizeof(message)); message[0] = (id >> 8) & 0xff; @@ -307,12 +317,12 @@ ATF_TC_BODY(dispatch_getnext, tc) { region.base = message; region.length = sizeof(message); result = isc_app_onrun(mctx, task, startit, ®ion); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); result = isc_app_run(); - ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); + assert_int_equal(result, ISC_R_SUCCESS); - ATF_CHECK_EQ(responses, 2); + assert_int_equal(responses, 2); /* * Shutdown nameserver. @@ -326,16 +336,30 @@ ATF_TC_BODY(dispatch_getnext, tc) { */ dns_dispatch_detach(&dispatch); dns_dispatchmgr_destroy(&dispatchmgr); - - dns_test_end(); } -/* - * Main - */ -ATF_TP_ADD_TCS(tp) { - ATF_TP_ADD_TC(tp, dispatchset_create); - ATF_TP_ADD_TC(tp, dispatchset_get); - ATF_TP_ADD_TC(tp, dispatch_getnext); - return (atf_no_error()); +int +main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup_teardown(dispatchset_create, + _setup, _teardown), + cmocka_unit_test_setup_teardown(dispatchset_get, + _setup, _teardown), + cmocka_unit_test_setup_teardown(dispatch_getnext, + _setup, _teardown), + }; + + return (cmocka_run_group_tests(tests, NULL, NULL)); } + +#else /* HAVE_CMOCKA */ + +#include + +int +main(void) { + printf("1..0 # Skipped: cmocka not available\n"); + return (0); +} + +#endif