2012-04-27 16:07:24 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2012-04-27 16:07:24 -07:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2012-04-27 16:07:24 -07:00
|
|
|
*/
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
2018-10-26 08:46:28 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-04-27 16:07:24 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2016-07-11 13:36:16 +10:00
|
|
|
#include <isc/app.h>
|
2012-04-27 16:07:24 -07:00
|
|
|
#include <isc/buffer.h>
|
2019-07-01 15:19:29 +02:00
|
|
|
#include <isc/refcount.h>
|
2012-04-27 16:07:24 -07:00
|
|
|
#include <isc/socket.h>
|
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
2018-10-26 08:46:28 -07:00
|
|
|
#include <isc/util.h>
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/view.h>
|
|
|
|
|
|
|
|
#include "dnstest.h"
|
|
|
|
|
|
|
|
dns_dispatchmgr_t *dispatchmgr = NULL;
|
|
|
|
dns_dispatchset_t *dset = NULL;
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-26 08:46:28 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = dns_test_begin(NULL, true);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-26 08:46:28 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
dns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-04-27 16:07:24 -07:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
make_dispatchset(unsigned int ndisps) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_sockaddr_t any;
|
2012-04-27 16:07:24 -07:00
|
|
|
dns_dispatch_t *disp = NULL;
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_dispatchmgr_create(dt_mctx, &dispatchmgr);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2012-04-27 16:07:24 -07:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
isc_sockaddr_any(&any);
|
2020-12-16 01:32:06 -08:00
|
|
|
result = dns_dispatch_createudp(dispatchmgr, socketmgr, taskmgr, &any,
|
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, &disp);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2012-04-27 16:07:24 -07:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2012-04-27 16:07:24 -07:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_dispatchset_create(dt_mctx, socketmgr, taskmgr, disp,
|
2012-04-27 16:07:24 -07:00
|
|
|
&dset, ndisps);
|
|
|
|
dns_dispatch_detach(&disp);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
reset(void) {
|
2018-10-26 08:46:28 -07:00
|
|
|
if (dset != NULL) {
|
2012-04-27 16:07:24 -07:00
|
|
|
dns_dispatchset_destroy(&dset);
|
2018-10-26 08:46:28 -07:00
|
|
|
}
|
|
|
|
if (dispatchmgr != NULL) {
|
2012-04-27 16:07:24 -07:00
|
|
|
dns_dispatchmgr_destroy(&dispatchmgr);
|
2018-10-26 08:46:28 -07:00
|
|
|
}
|
2012-04-27 16:07:24 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
/* create dispatch set */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dispatchset_create(void **state) {
|
2012-04-27 16:07:24 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
UNUSED(state);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
result = make_dispatchset(1);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
reset();
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
result = make_dispatchset(10);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
reset();
|
2012-04-27 16:07:24 -07:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
/* test dispatch set round-robin */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dispatchset_get(void **state) {
|
|
|
|
isc_result_t result;
|
2012-04-27 16:07:24 -07:00
|
|
|
dns_dispatch_t *d1, *d2, *d3, *d4, *d5;
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
UNUSED(state);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
result = make_dispatchset(1);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
d1 = dns_dispatchset_get(dset);
|
|
|
|
d2 = dns_dispatchset_get(dset);
|
|
|
|
d3 = dns_dispatchset_get(dset);
|
|
|
|
d4 = dns_dispatchset_get(dset);
|
|
|
|
d5 = dns_dispatchset_get(dset);
|
|
|
|
|
2020-11-26 13:10:40 +01:00
|
|
|
assert_ptr_equal(d1, d2);
|
|
|
|
assert_ptr_equal(d2, d3);
|
|
|
|
assert_ptr_equal(d3, d4);
|
|
|
|
assert_ptr_equal(d4, d5);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
reset();
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
result = make_dispatchset(4);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
|
|
|
d1 = dns_dispatchset_get(dset);
|
|
|
|
d2 = dns_dispatchset_get(dset);
|
|
|
|
d3 = dns_dispatchset_get(dset);
|
|
|
|
d4 = dns_dispatchset_get(dset);
|
|
|
|
d5 = dns_dispatchset_get(dset);
|
|
|
|
|
2020-11-26 13:10:40 +01:00
|
|
|
assert_ptr_equal(d1, d5);
|
|
|
|
assert_ptr_not_equal(d1, d2);
|
|
|
|
assert_ptr_not_equal(d2, d3);
|
|
|
|
assert_ptr_not_equal(d3, d4);
|
|
|
|
assert_ptr_not_equal(d4, d5);
|
2012-04-27 16:07:24 -07:00
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
reset();
|
2012-04-27 16:07:24 -07:00
|
|
|
}
|
|
|
|
|
2016-07-11 13:36:16 +10:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
senddone(isc_task_t *task, isc_event_t *event) {
|
2016-07-13 01:12:47 -07:00
|
|
|
isc_socket_t *sock = event->ev_arg;
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
2016-07-13 01:12:47 -07:00
|
|
|
isc_socket_detach(&sock);
|
2016-07-11 13:36:16 +10:00
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
nameserver(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_region_t region;
|
|
|
|
isc_socket_t *dummy;
|
|
|
|
isc_socket_t *sock = event->ev_arg;
|
|
|
|
isc_socketevent_t *ev = (isc_socketevent_t *)event;
|
2016-07-11 13:36:16 +10:00
|
|
|
static unsigned char buf1[16];
|
|
|
|
static unsigned char buf2[16];
|
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
memmove(buf1, ev->region.base, 12);
|
2016-07-11 13:36:16 +10:00
|
|
|
memset(buf1 + 12, 0, 4);
|
2020-02-12 13:59:18 +01:00
|
|
|
buf1[2] |= 0x80; /* qr=1 */
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2018-01-22 09:36:12 +11:00
|
|
|
memmove(buf2, ev->region.base, 12);
|
2016-07-11 13:36:16 +10:00
|
|
|
memset(buf2 + 12, 1, 4);
|
2020-02-12 13:59:18 +01:00
|
|
|
buf2[2] |= 0x80; /* qr=1 */
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* send message to be discarded.
|
|
|
|
*/
|
|
|
|
region.base = buf1;
|
|
|
|
region.length = sizeof(buf1);
|
|
|
|
dummy = NULL;
|
2016-07-13 01:12:47 -07:00
|
|
|
isc_socket_attach(sock, &dummy);
|
|
|
|
result = isc_socket_sendto(sock, ®ion, task, senddone, sock,
|
2016-07-11 13:36:16 +10:00
|
|
|
&ev->address, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2016-07-11 13:36:16 +10:00
|
|
|
isc_socket_detach(&dummy);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* send nextitem message.
|
|
|
|
*/
|
|
|
|
region.base = buf2;
|
|
|
|
region.length = sizeof(buf2);
|
|
|
|
dummy = NULL;
|
2016-07-13 01:12:47 -07:00
|
|
|
isc_socket_attach(sock, &dummy);
|
|
|
|
result = isc_socket_sendto(sock, ®ion, task, senddone, sock,
|
2016-07-11 13:36:16 +10:00
|
|
|
&ev->address, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2016-07-11 13:36:16 +10:00
|
|
|
isc_socket_detach(&dummy);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2016-07-11 13:36:16 +10:00
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
static dns_dispatch_t *dispatch = NULL;
|
|
|
|
static dns_dispentry_t *dispentry = NULL;
|
|
|
|
static atomic_bool first = ATOMIC_VAR_INIT(true);
|
|
|
|
static isc_sockaddr_t local;
|
2019-10-09 08:47:44 +02:00
|
|
|
static atomic_uint_fast32_t responses;
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
response(isc_task_t *task, isc_event_t *event) {
|
2016-07-11 13:36:16 +10:00
|
|
|
dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool exp_true = true;
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
2019-10-09 08:47:44 +02:00
|
|
|
atomic_fetch_add_relaxed(&responses, 1);
|
2019-07-01 15:19:29 +02:00
|
|
|
if (atomic_compare_exchange_strong(&first, &exp_true, false)) {
|
|
|
|
isc_result_t result = dns_dispatch_getnext(dispentry, &devent);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
} else {
|
|
|
|
dns_dispatch_removeresponse(&dispentry, &devent);
|
|
|
|
isc_app_shutdown();
|
|
|
|
}
|
2016-08-08 09:35:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
startit(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_result_t result;
|
2016-08-08 09:35:17 +10:00
|
|
|
isc_socket_t *sock = NULL;
|
|
|
|
|
2020-12-16 01:32:06 -08:00
|
|
|
isc_socket_attach(dns_dispatch_getentrysocket(dispentry), &sock);
|
2016-08-08 09:35:17 +10:00
|
|
|
result = isc_socket_sendto(sock, event->ev_arg, task, senddone, sock,
|
|
|
|
&local, NULL);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-08-08 09:35:17 +10:00
|
|
|
isc_event_free(&event);
|
2016-07-11 13:36:16 +10:00
|
|
|
}
|
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
/* test dispatch getnext */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
dispatch_getnext(void **state) {
|
|
|
|
isc_region_t region;
|
|
|
|
isc_result_t result;
|
|
|
|
isc_socket_t *sock = NULL;
|
|
|
|
isc_task_t *task = NULL;
|
|
|
|
uint16_t id;
|
2016-07-11 13:36:16 +10:00
|
|
|
struct in_addr ina;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned char message[12];
|
|
|
|
unsigned char rbuf[12];
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
UNUSED(state);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2019-10-09 08:47:44 +02:00
|
|
|
atomic_init(&responses, 0);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
result = isc_task_create(taskmgr, 0, &task);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_dispatchmgr_create(dt_mctx, &dispatchmgr);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
ina.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
isc_sockaddr_fromin(&local, &ina, 0);
|
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-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a local udp nameserver on the loopback.
|
|
|
|
*/
|
|
|
|
result = isc_socket_create(socketmgr, AF_INET, isc_sockettype_udp,
|
2016-07-13 01:12:47 -07:00
|
|
|
&sock);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
ina.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
isc_sockaddr_fromin(&local, &ina, 0);
|
2016-07-13 01:12:47 -07:00
|
|
|
result = isc_socket_bind(sock, &local, 0);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2016-07-13 01:12:47 -07:00
|
|
|
result = isc_socket_getsockname(sock, &local);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
region.base = rbuf;
|
|
|
|
region.length = sizeof(rbuf);
|
2016-07-13 01:12:47 -07:00
|
|
|
result = isc_socket_recv(sock, ®ion, 1, task, nameserver, sock);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2018-04-03 14:35:07 +02:00
|
|
|
result = dns_dispatch_addresponse(dispatch, 0, &local, task, response,
|
2020-12-16 01:32:06 -08:00
|
|
|
NULL, &id, &dispentry, socketmgr);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
memset(message, 0, sizeof(message));
|
|
|
|
message[0] = (id >> 8) & 0xff;
|
|
|
|
message[1] = id & 0xff;
|
|
|
|
|
|
|
|
region.base = message;
|
|
|
|
region.length = sizeof(message);
|
2019-06-18 15:01:43 +02:00
|
|
|
result = isc_app_onrun(dt_mctx, task, startit, ®ion);
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
result = isc_app_run();
|
2018-10-26 08:46:28 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2019-10-09 08:47:44 +02:00
|
|
|
assert_int_equal(atomic_load_acquire(&responses), 2);
|
2016-07-11 13:36:16 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown nameserver.
|
|
|
|
*/
|
2016-07-13 01:12:47 -07:00
|
|
|
isc_socket_cancel(sock, task, ISC_SOCKCANCEL_RECV);
|
|
|
|
isc_socket_detach(&sock);
|
2016-07-11 13:36:16 +10:00
|
|
|
isc_task_detach(&task);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown the dispatch.
|
|
|
|
*/
|
|
|
|
dns_dispatch_detach(&dispatch);
|
|
|
|
dns_dispatchmgr_destroy(&dispatchmgr);
|
2018-10-26 08:46:28 -07:00
|
|
|
}
|
2016-07-11 13:36:16 +10:00
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-26 08:46:28 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
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),
|
2018-10-26 08:46:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2016-07-11 13:36:16 +10:00
|
|
|
}
|
2012-04-27 16:07:24 -07:00
|
|
|
|
2018-10-26 08:46:28 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-26 08:46:28 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2012-04-27 16:07:24 -07:00
|
|
|
}
|
2018-10-26 08:46:28 -07:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|