2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

Give the unit tests a big overhaul

The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.

This commit does several things:

1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
   conditionals.

2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
   implementations, test lists, and the main test routine, so we don't
   have to repeat this all over again.  The macros were modeled after
   libuv test suite but adapted to cmocka as the test driver.

   A simple example of a unit test would be:

    ISC_RUN_TEST_IMPL(test1) { assert_true(true); }

    ISC_TEST_LIST_START
    ISC_TEST_ENTRY(test1)
    ISC_TEST_LIST_END

    ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)

   For more complicated examples including group setup and teardown
   functions, and per-test setup and teardown functions.

3. The macros prefix the test functions and cmocka entries, so the name
   of the test can now match the tested function name, and we don't have
   to append `_test` because `run_test_` is automatically prepended to
   the main test function, and `setup_test_` and `teardown_test_` is
   prepended to setup and teardown function.

4. Update all the unit tests to use the new syntax and fix a few bits
   here and there.

5. In the future, we can separate the test declarations and test
   implementations which are going to greatly help with uncluttering the
   bigger unit tests like doh_test and netmgr_test, because the test
   implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
   and `ISC_RUN_TEST_IMPL` for more details.

NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros.  There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
This commit is contained in:
Ondřej Surý
2022-05-02 10:56:42 +02:00
committed by Evan Hunt
parent 3b757aa749
commit 63fe9312ff
81 changed files with 2288 additions and 5718 deletions

View File

@@ -11,8 +11,6 @@
* information regarding copyright ownership.
*/
#if HAVE_CMOCKA
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <stdarg.h>
@@ -36,7 +34,7 @@
#include <dns/resolver.h>
#include <dns/view.h>
#include "dnstest.h"
#include <dns/test.h>
static dns_dispatchmgr_t *dispatchmgr = NULL;
static dns_dispatch_t *dispatch = NULL;
@@ -47,15 +45,12 @@ _setup(void **state) {
isc_result_t result;
isc_sockaddr_t local;
UNUSED(state);
setup_managers(state);
result = dns_test_begin(NULL, true);
result = dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr);
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_dispatchmgr_create(dt_mctx, netmgr, &dispatchmgr);
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_test_makeview("view", &view);
result = dns_test_makeview("view", true, &view);
assert_int_equal(result, ISC_R_SUCCESS);
isc_sockaddr_any(&local);
@@ -67,12 +62,11 @@ _setup(void **state) {
static int
_teardown(void **state) {
UNUSED(state);
dns_dispatch_detach(&dispatch);
dns_view_detach(&view);
dns_dispatchmgr_detach(&dispatchmgr);
dns_test_end();
teardown_managers(state);
return (0);
}
@@ -93,8 +87,7 @@ destroy_resolver(dns_resolver_t **resolverp) {
}
/* dns_resolver_create */
static void
create_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_create) {
dns_resolver_t *resolver = NULL;
UNUSED(state);
@@ -104,8 +97,7 @@ create_test(void **state) {
}
/* dns_resolver_gettimeout */
static void
gettimeout_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_gettimeout) {
dns_resolver_t *resolver = NULL;
unsigned int timeout;
@@ -120,8 +112,7 @@ gettimeout_test(void **state) {
}
/* dns_resolver_settimeout */
static void
settimeout_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_settimeout) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
@@ -138,8 +129,7 @@ settimeout_test(void **state) {
}
/* dns_resolver_settimeout */
static void
settimeout_default_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_default) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
@@ -161,8 +151,7 @@ settimeout_default_test(void **state) {
}
/* dns_resolver_settimeout below minimum */
static void
settimeout_belowmin_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_belowmin) {
dns_resolver_t *resolver = NULL;
unsigned int default_timeout, timeout;
@@ -180,8 +169,7 @@ settimeout_belowmin_test(void **state) {
}
/* dns_resolver_settimeout over maximum */
static void
settimeout_overmax_test(void **state) {
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_overmax) {
dns_resolver_t *resolver = NULL;
unsigned int timeout;
@@ -195,33 +183,15 @@ settimeout_overmax_test(void **state) {
destroy_resolver(&resolver);
}
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(create_test, _setup, _teardown),
cmocka_unit_test_setup_teardown(gettimeout_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_default_test, _setup,
_teardown),
cmocka_unit_test_setup_teardown(settimeout_belowmin_test,
_setup, _teardown),
cmocka_unit_test_setup_teardown(settimeout_overmax_test, _setup,
_teardown),
};
ISC_TEST_LIST_START
return (cmocka_run_group_tests(tests, NULL, NULL));
}
ISC_TEST_ENTRY_CUSTOM(dns_resolver_create, _setup, _teardown)
ISC_TEST_ENTRY_CUSTOM(dns_resolver_gettimeout, _setup, _teardown)
ISC_TEST_ENTRY_CUSTOM(dns_resolver_settimeout, _setup, _teardown)
ISC_TEST_ENTRY_CUSTOM(dns_resolver_settimeout_default, _setup, _teardown)
ISC_TEST_ENTRY_CUSTOM(dns_resolver_settimeout_belowmin, _setup, _teardown)
ISC_TEST_ENTRY_CUSTOM(dns_resolver_settimeout_overmax, _setup, _teardown)
#else /* HAVE_CMOCKA */
ISC_TEST_LIST_END
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
}
#endif /* if HAVE_CMOCKA */
ISC_TEST_MAIN