2019-02-12 15:59:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* 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 http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <limits.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>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.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 <stdlib.h>
|
2019-07-30 21:08:40 +02:00
|
|
|
#include <string.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
|
|
|
|
2019-02-12 15:59:54 +01:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
|
|
|
#include <isc/mem.h>
|
2019-03-07 16:45:04 +11:00
|
|
|
#include <isc/platform.h>
|
2019-02-12 15:59:54 +01:00
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/types.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
ISC_PLATFORM_NORETURN_PRE void _fail(const char *const file,
|
|
|
|
const int line) ISC_PLATFORM_NORETURN_POST;
|
2019-03-07 16:45:04 +11:00
|
|
|
|
2019-02-12 15:59:54 +01:00
|
|
|
#include <ns/hooks.h>
|
|
|
|
|
|
|
|
#include "nstest.h"
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2019-02-12 15:59:54 +01:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = ns_test_begin(NULL, false);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2019-02-12 15:59:54 +01:00
|
|
|
if (*state != NULL) {
|
|
|
|
isc_mem_free(mctx, *state);
|
|
|
|
}
|
|
|
|
|
|
|
|
ns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Structure containing parameters for run_full_path_test().
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-13 14:44:37 -08:00
|
|
|
const ns_test_id_t id; /* libns test identifier */
|
|
|
|
const char *input; /* source string - plugin name or path
|
|
|
|
* */
|
|
|
|
size_t output_size; /* size of target char array to
|
|
|
|
* allocate */
|
|
|
|
isc_result_t result; /* expected return value */
|
|
|
|
const char *output; /* expected output string */
|
2019-02-12 15:59:54 +01:00
|
|
|
} ns_plugin_expandpath_test_params_t;
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Perform a single ns_plugin_expandpath() check using given parameters.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
run_full_path_test(const ns_plugin_expandpath_test_params_t *test,
|
|
|
|
void **state) {
|
|
|
|
char **target = (char **)state;
|
2019-02-12 15:59:54 +01:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(test != NULL);
|
|
|
|
REQUIRE(test->id.description != NULL);
|
|
|
|
REQUIRE(test->input != NULL);
|
|
|
|
REQUIRE(test->result != ISC_R_SUCCESS || test->output != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare a target buffer of given size. Store it in 'state' so that
|
|
|
|
* it can get cleaned up by _teardown() if the test fails.
|
|
|
|
*/
|
|
|
|
*target = isc_mem_allocate(mctx, test->output_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call ns_plugin_expandpath().
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
result = ns_plugin_expandpath(test->input, *target, test->output_size);
|
2019-02-12 15:59:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check return value.
|
|
|
|
*/
|
|
|
|
if (result != test->result) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
|
|
"expected result %d (%s), got %d (%s)",
|
2020-02-12 13:59:18 +01:00
|
|
|
test->id.description, test->id.lineno, test->result,
|
|
|
|
isc_result_totext(test->result), result,
|
|
|
|
isc_result_totext(result));
|
2019-02-12 15:59:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check output string if return value indicates success.
|
|
|
|
*/
|
|
|
|
if (result == ISC_R_SUCCESS && strcmp(*target, test->output) != 0) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
|
|
"expected output \"%s\", got \"%s\"",
|
2020-02-12 13:59:18 +01:00
|
|
|
test->id.description, test->id.lineno, test->output,
|
|
|
|
*target);
|
2019-02-12 15:59:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_free(mctx, *target);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test ns_plugin_expandpath() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
ns_plugin_expandpath_test(void **state) {
|
2019-02-12 15:59:54 +01:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const ns_plugin_expandpath_test_params_t tests[] = {
|
|
|
|
{
|
|
|
|
NS_TEST_ID("correct use with an absolute path"),
|
|
|
|
.input = "/usr/lib/named/foo.so",
|
|
|
|
.output_size = PATH_MAX,
|
|
|
|
.result = ISC_R_SUCCESS,
|
|
|
|
.output = "/usr/lib/named/foo.so",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NS_TEST_ID("correct use with a relative path"),
|
|
|
|
.input = "../../foo.so",
|
|
|
|
.output_size = PATH_MAX,
|
|
|
|
.result = ISC_R_SUCCESS,
|
|
|
|
.output = "../../foo.so",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NS_TEST_ID("correct use with a filename"),
|
|
|
|
.input = "foo.so",
|
|
|
|
.output_size = PATH_MAX,
|
|
|
|
.result = ISC_R_SUCCESS,
|
|
|
|
#ifndef WIN32
|
|
|
|
.output = NAMED_PLUGINDIR "/foo.so",
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifndef WIN32 */
|
2019-02-12 15:59:54 +01:00
|
|
|
.output = "foo.so",
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef WIN32 */
|
2019-02-12 15:59:54 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
NS_TEST_ID("no space at all in target buffer"),
|
|
|
|
.input = "/usr/lib/named/foo.so",
|
|
|
|
.output_size = 0,
|
|
|
|
.result = ISC_R_NOSPACE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NS_TEST_ID("target buffer too small to fit input"),
|
|
|
|
.input = "/usr/lib/named/foo.so",
|
|
|
|
.output_size = 1,
|
|
|
|
.result = ISC_R_NOSPACE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NS_TEST_ID("target buffer too small to fit NULL byte"),
|
|
|
|
.input = "/foo.so",
|
|
|
|
.output_size = 7,
|
|
|
|
.result = ISC_R_NOSPACE,
|
|
|
|
},
|
|
|
|
#ifndef WIN32
|
|
|
|
{
|
|
|
|
NS_TEST_ID("target buffer too small to fit full path"),
|
|
|
|
.input = "foo.so",
|
|
|
|
.output_size = 7,
|
|
|
|
.result = ISC_R_NOSPACE,
|
|
|
|
},
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifndef WIN32 */
|
2019-02-12 15:59:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
|
|
run_full_path_test(&tests[i], state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2019-02-12 15:59:54 +01:00
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(ns_plugin_expandpath_test,
|
|
|
|
_setup, _teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2019-02-12 15:59:54 +01:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|