mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-22 10:10:06 +00:00
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.
198 lines
4.6 KiB
C
198 lines
4.6 KiB
C
/*
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
*
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
*
|
|
* 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 https://mozilla.org/MPL/2.0/.
|
|
*
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
* information regarding copyright ownership.
|
|
*/
|
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
#include <setjmp.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#define UNIT_TESTING
|
|
#include <cmocka.h>
|
|
|
|
#include <isc/app.h>
|
|
#include <isc/buffer.h>
|
|
#include <isc/net.h>
|
|
#include <isc/print.h>
|
|
#include <isc/task.h>
|
|
#include <isc/timer.h>
|
|
#include <isc/util.h>
|
|
|
|
#include <dns/dispatch.h>
|
|
#include <dns/name.h>
|
|
#include <dns/resolver.h>
|
|
#include <dns/view.h>
|
|
|
|
#include <dns/test.h>
|
|
|
|
static dns_dispatchmgr_t *dispatchmgr = NULL;
|
|
static dns_dispatch_t *dispatch = NULL;
|
|
static dns_view_t *view = NULL;
|
|
|
|
static int
|
|
_setup(void **state) {
|
|
isc_result_t result;
|
|
isc_sockaddr_t local;
|
|
|
|
setup_managers(state);
|
|
|
|
result = dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr);
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
result = dns_test_makeview("view", true, &view);
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
isc_sockaddr_any(&local);
|
|
result = dns_dispatch_createudp(dispatchmgr, &local, &dispatch);
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
return (0);
|
|
}
|
|
|
|
static int
|
|
_teardown(void **state) {
|
|
dns_dispatch_detach(&dispatch);
|
|
dns_view_detach(&view);
|
|
dns_dispatchmgr_detach(&dispatchmgr);
|
|
|
|
teardown_managers(state);
|
|
|
|
return (0);
|
|
}
|
|
|
|
static void
|
|
mkres(dns_resolver_t **resolverp) {
|
|
isc_result_t result;
|
|
|
|
result = dns_resolver_create(view, taskmgr, 1, netmgr, timermgr, 0,
|
|
dispatchmgr, dispatch, NULL, resolverp);
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
}
|
|
|
|
static void
|
|
destroy_resolver(dns_resolver_t **resolverp) {
|
|
dns_resolver_shutdown(*resolverp);
|
|
dns_resolver_detach(resolverp);
|
|
}
|
|
|
|
/* dns_resolver_create */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_create) {
|
|
dns_resolver_t *resolver = NULL;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
/* dns_resolver_gettimeout */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_gettimeout) {
|
|
dns_resolver_t *resolver = NULL;
|
|
unsigned int timeout;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_true(timeout > 0);
|
|
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
/* dns_resolver_settimeout */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_settimeout) {
|
|
dns_resolver_t *resolver = NULL;
|
|
unsigned int default_timeout, timeout;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
|
|
default_timeout = dns_resolver_gettimeout(resolver);
|
|
dns_resolver_settimeout(resolver, default_timeout + 1);
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_true(timeout == default_timeout + 1);
|
|
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
/* dns_resolver_settimeout */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_default) {
|
|
dns_resolver_t *resolver = NULL;
|
|
unsigned int default_timeout, timeout;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
|
|
default_timeout = dns_resolver_gettimeout(resolver);
|
|
dns_resolver_settimeout(resolver, default_timeout + 100);
|
|
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_int_equal(timeout, default_timeout + 100);
|
|
|
|
dns_resolver_settimeout(resolver, 0);
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_int_equal(timeout, default_timeout);
|
|
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
/* dns_resolver_settimeout below minimum */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_belowmin) {
|
|
dns_resolver_t *resolver = NULL;
|
|
unsigned int default_timeout, timeout;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
|
|
default_timeout = dns_resolver_gettimeout(resolver);
|
|
dns_resolver_settimeout(resolver, 9000);
|
|
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_int_equal(timeout, default_timeout);
|
|
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
/* dns_resolver_settimeout over maximum */
|
|
ISC_RUN_TEST_IMPL(dns_resolver_settimeout_overmax) {
|
|
dns_resolver_t *resolver = NULL;
|
|
unsigned int timeout;
|
|
|
|
UNUSED(state);
|
|
|
|
mkres(&resolver);
|
|
|
|
dns_resolver_settimeout(resolver, 4000000);
|
|
timeout = dns_resolver_gettimeout(resolver);
|
|
assert_in_range(timeout, 0, 3999999);
|
|
destroy_resolver(&resolver);
|
|
}
|
|
|
|
ISC_TEST_LIST_START
|
|
|
|
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)
|
|
|
|
ISC_TEST_LIST_END
|
|
|
|
ISC_TEST_MAIN
|