2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-27 04:28:33 +00:00
bind/tests/dns/resolver_test.c

192 lines
4.7 KiB
C
Raw Normal View History

/*
* 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>
2018-11-14 20:24:54 +08:00
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
2018-11-14 20:24:54 +08:00
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/buffer.h>
#include <isc/net.h>
#include <isc/timer.h>
#include <isc/tls.h>
#include <isc/util.h>
#include <dns/dispatch.h>
#include <dns/name.h>
#include <dns/resolver.h>
#include <dns/view.h>
#include <tests/dns.h>
static dns_dispatchmgr_t *dispatchmgr = NULL;
2020-02-13 14:44:37 -08:00
static dns_dispatch_t *dispatch = NULL;
static dns_view_t *view = NULL;
static isc_tlsctx_cache_t *tlsctx_cache = NULL;
2018-11-14 20:24:54 +08:00
static int
setup_test(void **state) {
2020-02-13 14:44:37 -08:00
isc_result_t result;
isc_sockaddr_t local;
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.
2022-05-02 10:56:42 +02:00
setup_managers(state);
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.
2022-05-02 10:56:42 +02:00
result = dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr);
2018-11-14 20:24:54 +08:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_test_makeview("view", false, &view);
2018-11-14 20:24:54 +08:00
assert_int_equal(result, ISC_R_SUCCESS);
isc_sockaddr_any(&local);
dispatch: Clean up connect and recv callbacks - disp_connected() has been split into two functions, udp_connected() (which takes 'resp' as an argument) and tcp_connected() (which takes 'disp', and calls the connect callbacks for all pending resps). - In dns_dispatch_connect(), if a connection is already open, we need to detach the dispentry immediately because we won't be running tcp_connected(). - dns_disptach_cancel() also now calls the connect callbacks for pending TCP responses, and the response callbacks for open TCP connections waiting on read. - If udp_connected() runs after dns_dispatch_cancel() has been called, ensure that the caller's connect callback is run. - If a UDP connection fails with EADDRINUSE, we try again up to five times with a different local port number before giving up. - If a TCP connection is canceled while still pending connection, the connect timeout may still fire. we attach the dispatch before connecting to ensure that it won't be detached too soon in this case. - The dispentry is no longer removed from the pending list when deactivating, so that the connect callback can still be run if dns_dispatch_removeresponse() was run while the connecting was pending. - Rewrote dns_dispatch_gettcp() to avoid a data race. - startrecv() and dispatch_getnext() can be called with a NULL resp when using TCP. - Refactored udp_recv() and tcp_recv() and added result logging. - EOF is now treated the same as CANCELED in response callbacks. - ISC_R_SHUTTINGDOWN is sent to the reponse callbacks for all resps if tcp_recv() is triggered by a netmgr shutdown. (response callbacks are *not* sent by udp_recv() in this case.)
2021-08-04 13:14:11 -07:00
result = dns_dispatch_createudp(dispatchmgr, &local, &dispatch);
2018-11-14 20:24:54 +08:00
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
}
2018-11-14 20:24:54 +08:00
static int
teardown_test(void **state) {
dns_dispatch_detach(&dispatch);
dns_view_detach(&view);
dns_dispatchmgr_detach(&dispatchmgr);
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.
2022-05-02 10:56:42 +02:00
teardown_managers(state);
2018-11-14 20:24:54 +08:00
return (0);
}
static void
2020-02-13 14:44:37 -08:00
mkres(dns_resolver_t **resolverp) {
isc_result_t result;
isc_tlsctx_cache_create(mctx, &tlsctx_cache);
result = dns_resolver_create(view, loopmgr, 1, netmgr, 0, tlsctx_cache,
dispatchmgr, dispatch, NULL, resolverp);
2018-11-14 20:24:54 +08:00
assert_int_equal(result, ISC_R_SUCCESS);
}
static void
2020-02-13 14:44:37 -08:00
destroy_resolver(dns_resolver_t **resolverp) {
dns_resolver_shutdown(*resolverp);
dns_resolver_detach(resolverp);
if (tlsctx_cache != NULL) {
isc_tlsctx_cache_detach(&tlsctx_cache);
}
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_create */
ISC_LOOP_TEST_IMPL(create) {
dns_resolver_t *resolver = NULL;
mkres(&resolver);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_gettimeout */
ISC_LOOP_TEST_IMPL(gettimeout) {
dns_resolver_t *resolver = NULL;
2020-02-13 14:44:37 -08:00
unsigned int timeout;
mkres(&resolver);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_true(timeout > 0);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_settimeout */
ISC_LOOP_TEST_IMPL(settimeout) {
dns_resolver_t *resolver = NULL;
2020-02-13 14:44:37 -08:00
unsigned int default_timeout, timeout;
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, default_timeout + 1);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_true(timeout == default_timeout + 1);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_settimeout */
ISC_LOOP_TEST_IMPL(settimeout_default) {
dns_resolver_t *resolver = NULL;
2020-02-13 14:44:37 -08:00
unsigned int default_timeout, timeout;
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, default_timeout + 100);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_int_equal(timeout, default_timeout + 100);
dns_resolver_settimeout(resolver, 0);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_int_equal(timeout, default_timeout);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_settimeout below minimum */
ISC_LOOP_TEST_IMPL(settimeout_belowmin) {
dns_resolver_t *resolver = NULL;
2020-02-13 14:44:37 -08:00
unsigned int default_timeout, timeout;
mkres(&resolver);
default_timeout = dns_resolver_gettimeout(resolver);
dns_resolver_settimeout(resolver, 9000);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_int_equal(timeout, default_timeout);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
2018-11-14 20:24:54 +08:00
/* dns_resolver_settimeout over maximum */
ISC_LOOP_TEST_IMPL(settimeout_overmax) {
dns_resolver_t *resolver = NULL;
2020-02-13 14:44:37 -08:00
unsigned int timeout;
mkres(&resolver);
dns_resolver_settimeout(resolver, 4000000);
timeout = dns_resolver_gettimeout(resolver);
2018-11-14 20:24:54 +08:00
assert_in_range(timeout, 0, 3999999);
destroy_resolver(&resolver);
isc_loopmgr_shutdown(loopmgr);
}
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.
2022-05-02 10:56:42 +02:00
ISC_TEST_LIST_START
ISC_TEST_ENTRY_CUSTOM(create, setup_test, teardown_test)
ISC_TEST_ENTRY_CUSTOM(gettimeout, setup_test, teardown_test)
ISC_TEST_ENTRY_CUSTOM(settimeout, setup_test, teardown_test)
ISC_TEST_ENTRY_CUSTOM(settimeout_default, setup_test, teardown_test)
ISC_TEST_ENTRY_CUSTOM(settimeout_belowmin, setup_test, teardown_test)
ISC_TEST_ENTRY_CUSTOM(settimeout_overmax, setup_test, teardown_test)
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.
2022-05-02 10:56:42 +02:00
ISC_TEST_LIST_END
2018-11-14 20:24:54 +08: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.
2022-05-02 10:56:42 +02:00
ISC_TEST_MAIN