2018-02-27 18:20:01 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-27 18:20:01 -08:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#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>
|
2018-02-27 18:20:01 -08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2018-02-27 18:20:01 -08:00
|
|
|
#include <isc/app.h>
|
|
|
|
#include <isc/buffer.h>
|
2018-11-14 20:24:54 +08:00
|
|
|
#include <isc/print.h>
|
2018-02-27 18:20:01 -08:00
|
|
|
#include <isc/socket.h>
|
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <isc/util.h>
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/resolver.h>
|
|
|
|
#include <dns/view.h>
|
|
|
|
|
|
|
|
#include "dnstest.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;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
2018-02-27 18:20:01 -08:00
|
|
|
isc_sockaddr_t local;
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_test_begin(NULL, true);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_dispatchmgr_create(dt_mctx, &dispatchmgr);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
result = dns_test_makeview("view", &view);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
isc_sockaddr_any(&local);
|
2020-12-16 01:32:06 -08:00
|
|
|
result = dns_dispatch_createudp(dispatchmgr, socketmgr, taskmgr, &local,
|
Dispatch API simplification
- Many dispatch attributes can be set implicitly instead of being passed
in. we can infer whether to set DNS_DISPATCHATTR_TCP or _UDP from
whether we're calling dns_dispatch_createtcp() or _createudp(). we
can also infer DNS_DISPATCHATTR_IPV4 or _IPV6 from the addresses or
the socket that were passed in.
- We no longer use dup'd sockets in UDP dispatches, so the 'dup_socket'
parameter has been removed from dns_dispatch_createudp(), along with
the code implementing it. also removed isc_socket_dup() since it no
longer has any callers.
- The 'buffersize' parameter was ignored and has now been removed;
buffersize is now fixed at 4096.
- Maxbuffers and maxrequests don't need to be passed in on every call to
dns_dispatch_createtcp() and _createudp().
In all current uses, the value for mgr->maxbuffers will either be
raised once from its default of 20000 to 32768, or else left
alone. (passing in a value lower than 20000 does not lower it.) there
isn't enough difference between these values for there to be any need
to configure this.
The value for disp->maxrequests controls both the quota of concurrent
requests for a dispatch and also the size of the dispatch socket
memory pool. it's not clear that this quota is necessary at all. the
memory pool size currently starts at 32768, but is sometimes lowered
to 4096, which is definitely unnecessary.
This commit sets both values permanently to 32768.
- Previously TCP dispatches allocated their own separate QID table,
which didn't incorporate a port table. this commit removes
per-dispatch QID tables and shares the same table between all
dispatches. since dispatches are created for each TCP socket, this may
speed up the dispatch allocation process. there may be a slight
increase in lock contention since all dispatches are sharing a single
QID table, but since TCP sockets are used less often than UDP
sockets (which were already sharing a QID table), it should not be a
substantial change.
- The dispatch port table was being used to determine whether a port was
already in use; if so, then a UDP socket would be bound with
REUSEADDR. this commit removes the port table, and always binds UDP
sockets that way.
2020-12-17 00:43:00 -08:00
|
|
|
0, &dispatch);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
2018-02-27 18:20:01 -08:00
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
|
|
|
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_dispatch_detach(&dispatch);
|
|
|
|
dns_view_detach(&view);
|
|
|
|
dns_dispatchmgr_destroy(&dispatchmgr);
|
|
|
|
dns_test_end();
|
2018-11-14 20:24:54 +08:00
|
|
|
|
|
|
|
return (0);
|
2018-02-27 18:20:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
mkres(dns_resolver_t **resolverp) {
|
2018-02-27 18:20:01 -08:00
|
|
|
isc_result_t result;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_resolver_create(view, taskmgr, 1, 1, socketmgr, timermgr,
|
|
|
|
0, dispatchmgr, dispatch, NULL, resolverp);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2018-02-27 18:20:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
destroy_resolver(dns_resolver_t **resolverp) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_shutdown(*resolverp);
|
|
|
|
dns_resolver_detach(resolverp);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_create */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
create_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
mkres(&resolver);
|
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_gettimeout */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
gettimeout_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int timeout;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
mkres(&resolver);
|
|
|
|
|
|
|
|
timeout = dns_resolver_gettimeout(resolver);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_true(timeout > 0);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_settimeout */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
settimeout_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int default_timeout, timeout;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
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);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_settimeout */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
settimeout_default_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int default_timeout, timeout;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
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);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
dns_resolver_settimeout(resolver, 0);
|
|
|
|
timeout = dns_resolver_gettimeout(resolver);
|
2018-11-14 20:24:54 +08:00
|
|
|
assert_int_equal(timeout, default_timeout);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_settimeout below minimum */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
settimeout_belowmin_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int default_timeout, timeout;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
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);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
/* dns_resolver_settimeout over maximum */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
settimeout_overmax_test(void **state) {
|
2018-02-27 18:20:01 -08:00
|
|
|
dns_resolver_t *resolver = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int timeout;
|
2018-02-27 18:20:01 -08:00
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
UNUSED(state);
|
2018-02-27 18:20:01 -08:00
|
|
|
|
|
|
|
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);
|
2018-02-27 18:20:01 -08:00
|
|
|
destroy_resolver(&resolver);
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:24:54 +08:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-11-14 20:24:54 +08:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
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),
|
2018-11-14 20:24:54 +08:00
|
|
|
cmocka_unit_test_setup_teardown(settimeout_belowmin_test,
|
|
|
|
_setup, _teardown),
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(settimeout_overmax_test, _setup,
|
|
|
|
_teardown),
|
2018-11-14 20:24:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-11-14 20:24:54 +08:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2018-02-27 18:20:01 -08:00
|
|
|
}
|
2018-11-14 20:24:54 +08:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|