2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/tests/irs/resconf_test.c

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

182 lines
4.6 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.
*/
#if HAVE_CMOCKA
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>
static isc_mem_t *mctx = NULL;
static void
Complete rewrite the BIND 9 build system The rewrite of BIND 9 build system is a large work and cannot be reasonable split into separate merge requests. Addition of the automake has a positive effect on the readability and maintainability of the build system as it is more declarative, it allows conditional and we are able to drop all of the custom make code that BIND 9 developed over the years to overcome the deficiencies of autoconf + custom Makefile.in files. This squashed commit contains following changes: - conversion (or rather fresh rewrite) of all Makefile.in files to Makefile.am by using automake - the libtool is now properly integrated with automake (the way we used it was rather hackish as the only official way how to use libtool is via automake - the dynamic module loading was rewritten from a custom patchwork to libtool's libltdl (which includes the patchwork to support module loading on different systems internally) - conversion of the unit test executor from kyua to automake parallel driver - conversion of the system test executor from custom make/shell to automake parallel driver - The GSSAPI has been refactored, the custom SPNEGO on the basis that all major KRB5/GSSAPI (mit-krb5, heimdal and Windows) implementations support SPNEGO mechanism. - The various defunct tests from bin/tests have been removed: bin/tests/optional and bin/tests/pkcs11 - The text files generated from the MD files have been removed, the MarkDown has been designed to be readable by both humans and computers - The xsl header is now generated by a simple sed command instead of perl helper - The <irs/platform.h> header has been removed - cleanups of configure.ac script to make it more simpler, addition of multiple macros (there's still work to be done though) - the tarball can now be prepared with `make dist` - the system tests are partially able to run in oot build Here's a list of unfinished work that needs to be completed in subsequent merge requests: - `make distcheck` doesn't yet work (because of system tests oot run is not yet finished) - documentation is not yet built, there's a different merge request with docbook to sphinx-build rst conversion that needs to be rebased and adapted on top of the automake - msvc build is non functional yet and we need to decide whether we will just cross-compile bind9 using mingw-w64 or fix the msvc build - contributed dlz modules are not included neither in the autoconf nor automake
2018-08-07 16:46:53 +02:00
setup_test(void) {
isc_mem_create(&mctx);
/*
* the caller might run from another directory, but tests
* that access test data files must first chdir to the proper
* location.
*/
assert_return_code(chdir(TESTS_DIR), 0);
}
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
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() */
static void
irs_resconf_load_test(void **state) {
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/domain.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
{ "testdata/nameserver-v4.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/nameserver-v6.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/nameserver-v6-scoped.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/options-attempts.conf", ISC_R_SUCCESS,
check_attempts, ISC_R_SUCCESS },
{ "testdata/options-debug.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/options-ndots.conf", ISC_R_SUCCESS, check_ndots,
ISC_R_SUCCESS },
{ "testdata/options-timeout.conf", ISC_R_SUCCESS, check_timeout,
ISC_R_SUCCESS },
{ "testdata/options-unknown.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/options.conf", ISC_R_SUCCESS, check_options,
ISC_R_SUCCESS },
{ "testdata/options-bad-ndots.conf", ISC_R_RANGE, NULL,
ISC_R_SUCCESS },
{ "testdata/options-empty.conf", ISC_R_UNEXPECTEDEND, NULL,
ISC_R_SUCCESS },
{ "testdata/port.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
{ "testdata/resolv.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
{ "testdata/search.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
{ "testdata/sortlist-v4.conf", ISC_R_SUCCESS, NULL,
ISC_R_SUCCESS },
{ "testdata/timeout.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS },
{ "testdata/unknown.conf", ISC_R_SUCCESS, NULL, ISC_R_SUCCESS }
};
UNUSED(state);
setup_test();
for (i = 0; i < sizeof(tests) / sizeof(tests[1]); i++) {
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_mem_detach(&mctx);
}
int
main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(irs_resconf_load_test),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
main(void) {
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
}
#endif /* if HAVE_CMOCKA */