2016-10-27 13:17:58 +11:00
|
|
|
/*
|
2016-10-28 11:27:49 +11:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2016-10-27 13:17:58 +11:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2016-10-27 13:17:58 +11: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
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2016-10-27 13:17:58 +11:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2023-05-18 15:12:23 +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 */
|
2018-10-24 09:22:15 -07:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2016-10-27 13:17:58 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-24 09:22:15 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2016-10-27 13:17:58 +11:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <irs/resconf.h>
|
|
|
|
|
2024-12-09 13:04:05 +11:00
|
|
|
#include <tests/isc.h>
|
2016-10-27 13:17:58 +11:00
|
|
|
|
2024-12-10 17:35:16 +11:00
|
|
|
static isc_result_t
|
|
|
|
check_nameserver(irs_resconf_t *resconf, const char *expected) {
|
|
|
|
char buf[ISC_SOCKADDR_FORMATSIZE];
|
|
|
|
isc_sockaddrlist_t *servers = irs_resconf_getnameservers(resconf);
|
|
|
|
isc_sockaddr_t *entry = ISC_LIST_HEAD(*servers);
|
|
|
|
assert_true(entry != NULL);
|
|
|
|
isc_sockaddr_format(entry, buf, sizeof(buf));
|
|
|
|
assert_string_equal(buf, expected);
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_ns4(irs_resconf_t *resconf) {
|
|
|
|
return check_nameserver(resconf, "10.0.0.1#53");
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_ns6(irs_resconf_t *resconf) {
|
|
|
|
return check_nameserver(resconf, "2001:db8::1#53");
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_scoped(irs_resconf_t *resconf) {
|
|
|
|
return check_nameserver(resconf, "fe80::1%1#53");
|
|
|
|
}
|
|
|
|
|
2021-07-20 19:34:42 +02:00
|
|
|
static isc_result_t
|
|
|
|
check_number(unsigned int n, unsigned int expected) {
|
|
|
|
return (n == expected) ? ISC_R_SUCCESS : ISC_R_BADNUMBER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_attempts(irs_resconf_t *resconf) {
|
|
|
|
return check_number(irs_resconf_getattempts(resconf), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_timeout(irs_resconf_t *resconf) {
|
|
|
|
return check_number(irs_resconf_gettimeout(resconf), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
check_ndots(irs_resconf_t *resconf) {
|
|
|
|
return check_number(irs_resconf_getndots(resconf), 2);
|
|
|
|
}
|
|
|
|
|
2024-12-09 13:04:05 +11:00
|
|
|
static isc_result_t
|
|
|
|
search_example(irs_resconf_t *resconf) {
|
|
|
|
irs_resconf_search_t *entry;
|
|
|
|
irs_resconf_searchlist_t *list;
|
|
|
|
list = irs_resconf_getsearchlist(resconf);
|
|
|
|
if (list == NULL) {
|
|
|
|
return ISC_R_NOTFOUND;
|
|
|
|
}
|
|
|
|
entry = ISC_LIST_HEAD(*list);
|
|
|
|
assert_true(entry != NULL && entry->domain != NULL);
|
|
|
|
assert_string_equal(entry->domain, "example.com");
|
|
|
|
|
|
|
|
entry = ISC_LIST_TAIL(*list);
|
|
|
|
assert_true(entry != NULL && entry->domain != NULL);
|
|
|
|
assert_string_equal(entry->domain, "example.net");
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-07-20 19:34:42 +02:00
|
|
|
static isc_result_t
|
|
|
|
check_options(irs_resconf_t *resconf) {
|
|
|
|
if (irs_resconf_getattempts(resconf) != 3) {
|
2024-10-17 13:22:48 -07:00
|
|
|
return ISC_R_BADNUMBER; /* default value only */
|
2021-07-20 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (irs_resconf_getndots(resconf) != 2) {
|
2024-10-17 13:22:48 -07:00
|
|
|
return ISC_R_BADNUMBER;
|
2021-07-20 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (irs_resconf_gettimeout(resconf) != 1) {
|
2024-10-17 13:22:48 -07:00
|
|
|
return ISC_R_BADNUMBER;
|
2021-07-20 19:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ISC_R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:22:15 -07:00
|
|
|
/* test irs_resconf_load() */
|
2024-12-09 13:04:05 +11:00
|
|
|
ISC_RUN_TEST_IMPL(irs_resconf_load) {
|
2016-10-27 13:17:58 +11:00
|
|
|
isc_result_t result;
|
|
|
|
irs_resconf_t *resconf = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
struct {
|
|
|
|
const char *file;
|
|
|
|
isc_result_t loadres;
|
|
|
|
isc_result_t (*check)(irs_resconf_t *resconf);
|
|
|
|
isc_result_t checkres;
|
2024-12-11 12:28:40 +11:00
|
|
|
} tests[] = { { "testdata/resconf/domain.conf", ISC_R_SUCCESS, NULL,
|
|
|
|
ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/nameserver-v4.conf", ISC_R_SUCCESS,
|
|
|
|
check_ns4, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/nameserver-v6.conf", ISC_R_SUCCESS,
|
|
|
|
check_ns6, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/nameserver-v6-scoped.conf",
|
|
|
|
ISC_R_SUCCESS, check_scoped, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-attempts.conf", ISC_R_SUCCESS,
|
|
|
|
check_attempts, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-debug.conf", ISC_R_SUCCESS,
|
|
|
|
NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-ndots.conf", ISC_R_SUCCESS,
|
|
|
|
check_ndots, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-timeout.conf", ISC_R_SUCCESS,
|
|
|
|
check_timeout, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-unknown.conf", ISC_R_SUCCESS,
|
|
|
|
NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options.conf", ISC_R_SUCCESS,
|
|
|
|
check_options, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-bad-ndots.conf", ISC_R_RANGE,
|
|
|
|
NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/options-empty.conf",
|
|
|
|
ISC_R_UNEXPECTEDEND, NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/port.conf", ISC_R_SUCCESS, NULL,
|
|
|
|
ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/resolv.conf", ISC_R_SUCCESS, NULL,
|
|
|
|
ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/search.conf", ISC_R_SUCCESS,
|
|
|
|
search_example, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/sortlist-v4.conf", ISC_R_SUCCESS,
|
|
|
|
NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/timeout.conf", ISC_R_SUCCESS, NULL,
|
|
|
|
ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/unknown-with-value.conf",
|
|
|
|
ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/unknown-without-value.conf",
|
|
|
|
ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
|
|
|
|
{ "testdata/resconf/unknown+search.conf", ISC_R_SUCCESS,
|
|
|
|
search_example, ISC_R_SUCCESS } };
|
2016-10-27 13:17:58 +11:00
|
|
|
|
2018-10-24 09:22:15 -07:00
|
|
|
UNUSED(state);
|
2016-10-27 13:17:58 +11:00
|
|
|
|
2024-12-09 13:04:05 +11:00
|
|
|
assert_return_code(chdir(TESTS_DIR), 0);
|
2016-10-27 13:17:58 +11:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
|
2024-12-09 13:04:05 +11:00
|
|
|
if (debug) {
|
|
|
|
fprintf(stderr, "# testing '%s'\n", tests[i].file);
|
|
|
|
}
|
2016-10-27 13:17:58 +11:00
|
|
|
result = irs_resconf_load(mctx, tests[i].file, &resconf);
|
2018-10-24 09:22:15 -07:00
|
|
|
if (result != tests[i].loadres) {
|
|
|
|
fail_msg("# unexpected result %s loading %s",
|
|
|
|
isc_result_totext(result), tests[i].file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS && resconf == NULL) {
|
|
|
|
fail_msg("# NULL on success loading %s", tests[i].file);
|
|
|
|
} else if (result != ISC_R_SUCCESS && resconf != NULL) {
|
|
|
|
fail_msg("# non-NULL on failure loading %s",
|
|
|
|
tests[i].file);
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:17:58 +11:00
|
|
|
if (resconf != NULL && tests[i].check != NULL) {
|
|
|
|
result = (tests[i].check)(resconf);
|
2018-10-24 09:22:15 -07:00
|
|
|
if (result != tests[i].checkres) {
|
|
|
|
fail_msg("# unexpected result %s loading %s",
|
|
|
|
isc_result_totext(result),
|
2016-10-27 13:17:58 +11:00
|
|
|
tests[i].file);
|
2018-10-24 09:22:15 -07:00
|
|
|
}
|
2016-10-27 13:17:58 +11:00
|
|
|
}
|
2018-10-24 09:22:15 -07:00
|
|
|
if (resconf != NULL) {
|
2016-10-27 13:17:58 +11:00
|
|
|
irs_resconf_destroy(&resconf);
|
2018-10-24 09:22:15 -07:00
|
|
|
}
|
2016-10-27 13:17:58 +11:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 09:22:15 -07:00
|
|
|
|
2024-12-09 13:04:05 +11:00
|
|
|
ISC_TEST_LIST_START
|
|
|
|
ISC_TEST_ENTRY(irs_resconf_load)
|
|
|
|
ISC_TEST_LIST_END
|
2018-10-24 09:22:15 -07:00
|
|
|
|
2024-12-09 13:04:05 +11:00
|
|
|
ISC_TEST_MAIN
|