2017-09-08 13:39:09 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-09-08 13:39:09 -07:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2017-09-08 13:39:09 -07: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.
|
2017-09-08 13:39:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/file.h>
|
|
|
|
#include <isc/hash.h>
|
2022-07-26 13:03:45 +02:00
|
|
|
#include <isc/job.h>
|
|
|
|
#include <isc/loop.h>
|
2021-04-27 00:07:43 +02:00
|
|
|
#include <isc/managers.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/mem.h>
|
2019-11-05 15:34:35 -08:00
|
|
|
#include <isc/netmgr.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/os.h>
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <isc/random.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/stdio.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/string.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/timer.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <dns/cache.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/fixedname.h>
|
|
|
|
#include <dns/log.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/view.h>
|
|
|
|
#include <dns/zone.h>
|
|
|
|
|
|
|
|
#include <ns/client.h>
|
2018-08-03 14:16:41 -07:00
|
|
|
#include <ns/hooks.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <ns/interfacemgr.h>
|
|
|
|
#include <ns/server.h>
|
|
|
|
|
2022-05-03 11:37:31 +02:00
|
|
|
#include <tests/ns.h>
|
Use the TLS context cache for server-side contexts
Using the TLS context cache for server-side contexts could reduce the
number of contexts to initialise in the configurations when e.g. the
same 'tls' entry is used in multiple 'listen-on' statements for the
same DNS transport, binding to multiple IP addresses.
In such a case, only one TLS context will be created, instead of a
context per IP address, which could reduce the initialisation time, as
initialising even a non-ephemeral TLS context introduces some delay,
which can be *visually* noticeable by log activity.
Also, this change lays down a foundation for Mutual TLS (when the
server validates a client certificate, additionally to a client
validating the server), as the TLS context cache can be extended to
store additional data required for validation (like intermediates CA
chain).
Additionally to the above, the change ensures that the contexts are
not being changed after initialisation, as such a practice is frowned
upon. Previously we would set the supported ALPN tags within
isc_nm_listenhttp() and isc_nm_listentlsdns(). We do not do that for
client-side contexts, so that appears to be an overlook. Now we set
the supported ALPN tags right after server-side contexts creation,
similarly how we do for client-side ones.
2021-12-23 12:01:34 +02:00
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
dns_dispatchmgr_t *dispatchmgr = NULL;
|
|
|
|
ns_interfacemgr_t *interfacemgr = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_server_t *sctx = NULL;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
matchview(isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr,
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_message_t *message, dns_aclenv_t *env, isc_result_t *sigresultp,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_view_t **viewp) {
|
2017-09-08 13:39:09 -07:00
|
|
|
UNUSED(srcaddr);
|
|
|
|
UNUSED(destaddr);
|
|
|
|
UNUSED(message);
|
2018-02-05 20:24:14 +01:00
|
|
|
UNUSED(env);
|
2017-09-08 13:39:09 -07:00
|
|
|
UNUSED(sigresultp);
|
|
|
|
UNUSED(viewp);
|
|
|
|
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
static void
|
|
|
|
scan_interfaces(void *arg) {
|
|
|
|
UNUSED(arg);
|
|
|
|
ns_interfacemgr_scan(interfacemgr, true, false);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
int
|
|
|
|
setup_server(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
ns_listenlist_t *listenon = NULL;
|
|
|
|
in_port_t port = 5300 + isc_random8();
|
2017-09-08 13:39:09 -07: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
|
|
|
setup_managers(state);
|
2017-09-08 13:39:09 -07: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
|
|
|
ns_server_create(mctx, matchview, &sctx);
|
2017-09-08 13:39:09 -07: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
|
|
|
result = dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2022-07-26 13:03:45 +02:00
|
|
|
goto cleanup;
|
2017-09-08 13:39:09 -07:00
|
|
|
}
|
|
|
|
|
2022-10-29 14:22:56 -07:00
|
|
|
result = ns_interfacemgr_create(mctx, sctx, loopmgr, netmgr,
|
|
|
|
dispatchmgr, NULL, false,
|
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
|
|
|
&interfacemgr);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2022-07-26 13:03:45 +02:00
|
|
|
goto cleanup;
|
2019-11-05 15:34:35 -08:00
|
|
|
}
|
2021-04-27 00:07:43 +02:00
|
|
|
|
2023-01-05 22:18:55 -08:00
|
|
|
result = ns_listenlist_default(mctx, port, true, AF_INET, &listenon);
|
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
|
|
|
if (result != ISC_R_SUCCESS) {
|
2022-07-26 13:03:45 +02:00
|
|
|
goto cleanup;
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
ns_interfacemgr_setlistenon4(interfacemgr, listenon);
|
|
|
|
ns_listenlist_detach(&listenon);
|
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
isc_loop_setup(mainloop, scan_interfaces, NULL);
|
2017-09-08 13:39:09 -07: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
|
|
|
return (0);
|
2022-07-26 13:03:45 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
teardown_server(state);
|
|
|
|
return (-1);
|
2017-09-08 13:39:09 -07:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
void
|
2023-01-10 10:20:44 +01:00
|
|
|
shutdown_interfacemgr(void *arg ISC_ATTR_UNUSED) {
|
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
|
|
|
if (interfacemgr != NULL) {
|
|
|
|
ns_interfacemgr_shutdown(interfacemgr);
|
|
|
|
ns_interfacemgr_detach(&interfacemgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2022-07-26 13:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
teardown_server(void **state) {
|
|
|
|
shutdown_interfacemgr(NULL);
|
2017-09-08 13:39:09 -07: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
|
|
|
if (dispatchmgr != NULL) {
|
|
|
|
dns_dispatchmgr_detach(&dispatchmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2017-09-08 13:39:09 -07: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
|
|
|
if (sctx != NULL) {
|
|
|
|
ns_server_detach(&sctx);
|
2018-10-25 17:02:46 -07: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
|
|
|
teardown_managers(state);
|
|
|
|
return (0);
|
2017-09-08 13:39:09 -07: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
|
|
|
static dns_zone_t *served_zone = NULL;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_test_serve_zone(const char *zonename, const char *filename,
|
|
|
|
dns_view_t *view) {
|
2017-10-11 15:02:50 -07:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_db_t *db = NULL;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare zone structure for further processing.
|
|
|
|
*/
|
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_test_makezone(zonename, &served_zone, view, false);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start zone manager.
|
|
|
|
*/
|
2022-10-29 14:22:56 -07:00
|
|
|
dns_test_setupzonemgr();
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the zone to the zone manager.
|
|
|
|
*/
|
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_test_managezone(served_zone);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto close_zonemgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
view->nocookieudp = 512;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set path to the master file for the zone and then load it.
|
|
|
|
*/
|
2018-04-03 16:34:41 +02:00
|
|
|
dns_zone_setfile(served_zone, filename, dns_masterformat_text,
|
|
|
|
&dns_master_style_default);
|
2018-07-10 18:43:55 +02:00
|
|
|
result = dns_zone_load(served_zone, false);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto release_zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The zone should now be loaded; test it.
|
|
|
|
*/
|
|
|
|
result = dns_zone_getdb(served_zone, &db);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto release_zone;
|
|
|
|
}
|
|
|
|
if (db != NULL) {
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
release_zone:
|
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
|
|
|
dns_test_releasezone(served_zone);
|
2017-10-11 15:02:50 -07:00
|
|
|
close_zonemgr:
|
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
|
|
|
dns_test_closezonemgr();
|
2017-10-11 15:02:50 -07:00
|
|
|
dns_zone_detach(&served_zone);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_test_cleanup_zone(void) {
|
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
|
|
|
dns_test_releasezone(served_zone);
|
|
|
|
dns_test_closezonemgr();
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
dns_zone_detach(&served_zone);
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_test_getclient(ns_interface_t *ifp0, bool tcp, ns_client_t **clientp) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t result;
|
2022-07-26 13:03:45 +02:00
|
|
|
ns_client_t *client;
|
|
|
|
ns_clientmgr_t *clientmgr;
|
2020-02-13 14:44:37 -08:00
|
|
|
int i;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2019-11-05 15:34:35 -08:00
|
|
|
UNUSED(ifp0);
|
|
|
|
UNUSED(tcp);
|
|
|
|
|
2022-07-26 13:03:45 +02:00
|
|
|
clientmgr = ns_interfacemgr_getclientmgr(interfacemgr);
|
|
|
|
|
|
|
|
client = isc_mem_get(clientmgr->mctx, sizeof(*client));
|
2019-11-05 15:34:35 -08:00
|
|
|
result = ns__client_setup(client, clientmgr, true);
|
|
|
|
|
2019-12-09 14:39:38 +01:00
|
|
|
for (i = 0; i < 32; i++) {
|
2020-02-12 13:59:18 +01:00
|
|
|
if (atomic_load(&client_addrs[i]) == (uintptr_t)NULL ||
|
2020-02-13 14:44:37 -08:00
|
|
|
atomic_load(&client_addrs[i]) == (uintptr_t)client)
|
|
|
|
{
|
2019-11-05 15:34:35 -08:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
}
|
2019-12-09 14:39:38 +01:00
|
|
|
REQUIRE(i < 32);
|
2019-11-05 15:34:35 -08:00
|
|
|
|
|
|
|
atomic_store(&client_refs[i], 2);
|
2020-02-12 13:59:18 +01:00
|
|
|
atomic_store(&client_addrs[i], (uintptr_t)client);
|
|
|
|
client->handle = (isc_nmhandle_t *)client; /* Hack */
|
2019-11-05 15:34:35 -08:00
|
|
|
*clientp = client;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
/*%
|
|
|
|
* Synthesize a DNS message based on supplied QNAME, QTYPE and flags, then
|
|
|
|
* parse it and store the results in client->message.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
|
|
|
attach_query_msg_to_client(ns_client_t *client, const char *qnamestr,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdatatype_t qtype, unsigned int qflags) {
|
2017-10-11 15:02:50 -07:00
|
|
|
dns_rdataset_t *qrdataset = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_message_t *message = NULL;
|
|
|
|
unsigned char query[65536];
|
|
|
|
dns_name_t *qname = NULL;
|
|
|
|
isc_buffer_t querybuf;
|
|
|
|
dns_compress_t cctx;
|
|
|
|
isc_result_t result;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
REQUIRE(client != NULL);
|
|
|
|
REQUIRE(qnamestr != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new DNS message holding a query.
|
|
|
|
*/
|
2020-09-25 11:51:36 +02:00
|
|
|
dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &message);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set query ID to a random value.
|
|
|
|
*/
|
2018-05-28 15:22:23 +02:00
|
|
|
message->id = isc_random16();
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set query flags as requested by the caller.
|
|
|
|
*/
|
|
|
|
message->flags = qflags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate structures required to construct the query.
|
|
|
|
*/
|
2022-05-16 13:28:13 +02:00
|
|
|
dns_message_gettemprdataset(message, &qrdataset);
|
|
|
|
dns_message_gettempname(message, &qname);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert "qnamestr" to a DNS name, create a question rdataset of
|
|
|
|
* class IN and type "qtype", link the two and add the result to the
|
|
|
|
* QUESTION section of the query.
|
|
|
|
*/
|
|
|
|
result = dns_name_fromstring(qname, qnamestr, 0, mctx);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto put_name;
|
|
|
|
}
|
|
|
|
dns_rdataset_makequestion(qrdataset, dns_rdataclass_in, qtype);
|
|
|
|
ISC_LIST_APPEND(qname->list, qrdataset, link);
|
|
|
|
dns_message_addname(message, qname, DNS_SECTION_QUESTION);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Render the query.
|
|
|
|
*/
|
2022-06-23 21:53:46 +01:00
|
|
|
dns_compress_init(&cctx, mctx, 0);
|
2017-10-11 15:02:50 -07:00
|
|
|
isc_buffer_init(&querybuf, query, sizeof(query));
|
|
|
|
result = dns_message_renderbegin(message, &cctx, &querybuf);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto destroy_message;
|
|
|
|
}
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto destroy_message;
|
|
|
|
}
|
|
|
|
result = dns_message_renderend(message);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto destroy_message;
|
|
|
|
}
|
|
|
|
dns_compress_invalidate(&cctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the created message as it was rendered into "querybuf" and
|
|
|
|
* the latter is all we are going to need from now on.
|
|
|
|
*/
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&message);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the rendered query, storing results in client->message.
|
|
|
|
*/
|
|
|
|
isc_buffer_first(&querybuf);
|
|
|
|
return (dns_message_parse(client->message, &querybuf, 0));
|
|
|
|
|
|
|
|
put_name:
|
|
|
|
dns_message_puttempname(message, &qname);
|
|
|
|
dns_message_puttemprdataset(message, &qrdataset);
|
|
|
|
destroy_message:
|
2020-09-21 16:16:15 -03:00
|
|
|
dns_message_detach(&message);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
2018-08-13 21:08:08 -07:00
|
|
|
* A hook action which stores the query context pointed to by "arg" at
|
|
|
|
* "data". Causes execution to be interrupted at hook insertion
|
2017-10-11 15:02:50 -07:00
|
|
|
* point.
|
|
|
|
*/
|
2018-12-06 11:36:30 +01:00
|
|
|
static ns_hookresult_t
|
2020-02-13 14:44:37 -08:00
|
|
|
extract_qctx(void *arg, void *data, isc_result_t *resultp) {
|
2017-10-11 15:02:50 -07:00
|
|
|
query_ctx_t **qctxp;
|
2020-02-13 14:44:37 -08:00
|
|
|
query_ctx_t *qctx;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
2018-08-13 21:08:08 -07:00
|
|
|
REQUIRE(arg != NULL);
|
|
|
|
REQUIRE(data != NULL);
|
2017-10-11 15:02:50 -07:00
|
|
|
REQUIRE(resultp != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* qctx is a stack variable in lib/ns/query.c. Its contents need to be
|
|
|
|
* duplicated or otherwise they will become invalidated once the stack
|
|
|
|
* gets unwound.
|
|
|
|
*/
|
|
|
|
qctx = isc_mem_get(mctx, sizeof(*qctx));
|
|
|
|
if (qctx != NULL) {
|
2018-08-13 21:08:08 -07:00
|
|
|
memmove(qctx, (query_ctx_t *)arg, sizeof(*qctx));
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 21:08:08 -07:00
|
|
|
qctxp = (query_ctx_t **)data;
|
2017-10-11 15:02:50 -07:00
|
|
|
/*
|
|
|
|
* If memory allocation failed, the supplied pointer will simply be set
|
|
|
|
* to NULL. We rely on the user of this hook to react properly.
|
|
|
|
*/
|
|
|
|
*qctxp = qctx;
|
|
|
|
*resultp = ISC_R_UNSET;
|
|
|
|
|
2018-12-06 11:36:30 +01:00
|
|
|
return (NS_HOOK_RETURN);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Initialize a query context for "client" and store it in "qctxp".
|
|
|
|
*
|
|
|
|
* Requires:
|
|
|
|
*
|
|
|
|
* \li "client->message" to hold a parsed DNS query.
|
|
|
|
*/
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
create_qctx_for_client(ns_client_t *client, query_ctx_t **qctxp) {
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_t *saved_hook_table = NULL, *query_hooks = NULL;
|
|
|
|
const ns_hook_t hook = {
|
2018-08-13 21:08:08 -07:00
|
|
|
.action = extract_qctx,
|
|
|
|
.action_data = qctxp,
|
2017-10-11 15:02:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
REQUIRE(client != NULL);
|
|
|
|
REQUIRE(qctxp != NULL);
|
|
|
|
REQUIRE(*qctxp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call ns_query_start() to initialize a query context for given
|
|
|
|
* client, but first hook into query_setup() so that we can just
|
|
|
|
* extract an initialized query context, without kicking off any
|
|
|
|
* further processing. Make sure we do not overwrite any previously
|
|
|
|
* set hooks.
|
|
|
|
*/
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_create(mctx, &query_hooks);
|
|
|
|
ns_hook_add(query_hooks, mctx, NS_QUERY_SETUP, &hook);
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2018-08-12 11:19:36 -07:00
|
|
|
saved_hook_table = ns__hook_table;
|
2018-09-19 23:38:23 -07:00
|
|
|
ns__hook_table = query_hooks;
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2020-09-03 13:31:27 -07:00
|
|
|
ns_query_start(client, client->handle);
|
2018-08-12 11:19:36 -07:00
|
|
|
|
|
|
|
ns__hook_table = saved_hook_table;
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_free(mctx, (void **)&query_hooks);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_detach(&client->reqhandle);
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
if (*qctxp == NULL) {
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
} else {
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_test_qctx_create(const ns_test_qctx_create_params_t *params,
|
2020-02-13 14:44:37 -08:00
|
|
|
query_ctx_t **qctxp) {
|
2017-10-11 15:02:50 -07:00
|
|
|
ns_client_t *client = NULL;
|
|
|
|
isc_result_t result;
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_t *handle = NULL;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
REQUIRE(params != NULL);
|
|
|
|
REQUIRE(params->qname != NULL);
|
|
|
|
REQUIRE(qctxp != NULL);
|
|
|
|
REQUIRE(*qctxp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and initialize a client structure.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
result = ns_test_getclient(NULL, false, &client);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
2023-03-31 00:12:33 +02:00
|
|
|
client->tnow = isc_time_now();
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Every client needs to belong to a view.
|
|
|
|
*/
|
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_test_makeview("view", params->with_cache, &client->view);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto detach_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synthesize a DNS query using given QNAME, QTYPE and flags, storing
|
|
|
|
* it in client->message.
|
|
|
|
*/
|
|
|
|
result = attach_query_msg_to_client(client, params->qname,
|
|
|
|
params->qtype, params->qflags);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2019-05-22 10:58:41 +02:00
|
|
|
goto detach_view;
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow recursion for the client. As NS_CLIENTATTR_RA normally gets
|
2022-01-11 23:35:22 -08:00
|
|
|
* set in ns_client_request(), i.e. earlier than the unit tests hook
|
2017-10-11 15:02:50 -07:00
|
|
|
* into the call chain, just set it manually.
|
|
|
|
*/
|
|
|
|
client->attributes |= NS_CLIENTATTR_RA;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a query context for a client sending the previously
|
|
|
|
* synthesized query.
|
|
|
|
*/
|
|
|
|
result = create_qctx_for_client(client, qctxp);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-09-21 16:16:15 -03:00
|
|
|
goto detach_query;
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-09-03 13:31:27 -07:00
|
|
|
* The reference count for "client" is now at 2, so we need to
|
|
|
|
* decrement it in order for it to drop to zero when "qctx" gets
|
|
|
|
* destroyed.
|
2017-10-11 15:02:50 -07:00
|
|
|
*/
|
2020-09-03 13:31:27 -07:00
|
|
|
handle = client->handle;
|
|
|
|
isc_nmhandle_detach(&handle);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-09-21 16:16:15 -03:00
|
|
|
detach_query:
|
|
|
|
dns_message_detach(&client->message);
|
2019-05-22 10:58:41 +02:00
|
|
|
detach_view:
|
|
|
|
dns_view_detach(&client->view);
|
2017-10-11 15:02:50 -07:00
|
|
|
detach_client:
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_detach(&client->handle);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_test_qctx_destroy(query_ctx_t **qctxp) {
|
2017-10-11 15:02:50 -07:00
|
|
|
query_ctx_t *qctx;
|
|
|
|
|
|
|
|
REQUIRE(qctxp != NULL);
|
|
|
|
REQUIRE(*qctxp != NULL);
|
|
|
|
|
|
|
|
qctx = *qctxp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*qctxp = NULL;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
if (qctx->zone != NULL) {
|
|
|
|
dns_zone_detach(&qctx->zone);
|
|
|
|
}
|
|
|
|
if (qctx->db != NULL) {
|
|
|
|
dns_db_detach(&qctx->db);
|
|
|
|
}
|
2019-11-05 15:34:35 -08:00
|
|
|
if (qctx->client != NULL) {
|
2020-09-03 13:31:27 -07:00
|
|
|
isc_nmhandle_detach(&qctx->client->handle);
|
2019-11-05 15:34:35 -08:00
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
isc_mem_put(mctx, qctx, sizeof(*qctx));
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:36:30 +01:00
|
|
|
ns_hookresult_t
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_test_hook_catch_call(void *arg, void *data, isc_result_t *resultp) {
|
2018-08-13 21:08:08 -07:00
|
|
|
UNUSED(arg);
|
|
|
|
UNUSED(data);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
*resultp = ISC_R_UNSET;
|
|
|
|
|
2018-12-06 11:36:30 +01:00
|
|
|
return (NS_HOOK_RETURN);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t
|
|
|
|
ns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin,
|
2020-02-13 14:44:37 -08:00
|
|
|
const char *testfile) {
|
|
|
|
isc_result_t result;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_fixedname_t fixed;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *name;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fixed);
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
result = dns_name_fromstring(name, origin, 0, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_db_create(mctx, "rbt", name, dbtype, dns_rdataclass_in, 0,
|
|
|
|
NULL, db);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2017-09-08 13:39:09 -07:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-04-03 13:22:09 +02:00
|
|
|
result = dns_db_load(*db, testfile, dns_masterformat_text, 0);
|
2017-09-08 13:39:09 -07:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
fromhex(char c) {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
2017-09-08 13:39:09 -07:00
|
|
|
return (c - '0');
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
2017-09-08 13:39:09 -07:00
|
|
|
return (c - 'a' + 10);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
2017-09-08 13:39:09 -07:00
|
|
|
return (c - 'A' + 10);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
printf("bad input format: %02x\n", c);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
ns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz,
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t *sizep) {
|
|
|
|
isc_result_t result;
|
2017-09-08 13:39:09 -07:00
|
|
|
unsigned char *bp;
|
2020-02-13 14:44:37 -08:00
|
|
|
char *rp, *wp;
|
|
|
|
char s[BUFSIZ];
|
|
|
|
size_t len, i;
|
|
|
|
FILE *f = NULL;
|
|
|
|
int n;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
result = isc_stdio_open(file, "r", &f);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2017-09-08 13:39:09 -07:00
|
|
|
return (result);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
bp = buf;
|
|
|
|
while (fgets(s, sizeof(s), f) != NULL) {
|
|
|
|
rp = s;
|
|
|
|
wp = s;
|
|
|
|
len = 0;
|
|
|
|
while (*rp != '\0') {
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*rp == '#') {
|
2017-09-08 13:39:09 -07:00
|
|
|
break;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (*rp != ' ' && *rp != '\t' && *rp != '\r' &&
|
2022-11-02 19:33:14 +01:00
|
|
|
*rp != '\n')
|
|
|
|
{
|
2017-09-08 13:39:09 -07:00
|
|
|
*wp++ = *rp;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
rp++;
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
if (len == 0U) {
|
2017-09-08 13:39:09 -07:00
|
|
|
continue;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (len % 2 != 0U) {
|
2022-07-26 13:03:45 +02:00
|
|
|
result = ISC_R_UNEXPECTEDEND;
|
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (len > bufsiz * 2) {
|
2022-07-26 13:03:45 +02:00
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
rp = s;
|
|
|
|
for (i = 0; i < len; i += 2) {
|
|
|
|
n = fromhex(*rp++);
|
|
|
|
n *= 16;
|
|
|
|
n += fromhex(*rp++);
|
|
|
|
*bp++ = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*sizep = bp - buf;
|
|
|
|
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_stdio_close(f);
|
|
|
|
return (result);
|
|
|
|
}
|