2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/tests/dns/resconf_test.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
5.9 KiB
C
Raw Normal View History

/*
2016-10-28 11:27:49 +11:00
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <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 */
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/mem.h>
#include <isc/util.h>
#include <irs/resconf.h>
#include <tests/isc.h>
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");
}
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);
}
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;
}
static isc_result_t
check_options(irs_resconf_t *resconf) {
if (irs_resconf_getattempts(resconf) != 3) {
return ISC_R_BADNUMBER; /* default value only */
}
if (irs_resconf_getndots(resconf) != 2) {
return ISC_R_BADNUMBER;
}
if (irs_resconf_gettimeout(resconf) != 1) {
return ISC_R_BADNUMBER;
}
return ISC_R_SUCCESS;
}
/* test irs_resconf_load() */
ISC_RUN_TEST_IMPL(irs_resconf_load) {
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;
} 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 } };
UNUSED(state);
assert_return_code(chdir(TESTS_DIR), 0);
for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
if (debug) {
fprintf(stderr, "# testing '%s'\n", tests[i].file);
}
result = irs_resconf_load(mctx, tests[i].file, &resconf);
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);
}
if (resconf != NULL && tests[i].check != NULL) {
result = (tests[i].check)(resconf);
if (result != tests[i].checkres) {
fail_msg("# unexpected result %s loading %s",
isc_result_totext(result),
tests[i].file);
}
}
if (resconf != NULL) {
irs_resconf_destroy(&resconf);
}
}
}
ISC_TEST_LIST_START
ISC_TEST_ENTRY(irs_resconf_load)
ISC_TEST_LIST_END
ISC_TEST_MAIN