mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-22 10:10:06 +00:00
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.
166 lines
4.1 KiB
C
166 lines
4.1 KiB
C
/*
|
|
* 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 <limits.h>
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
#include <setjmp.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define UNIT_TESTING
|
|
#include <cmocka.h>
|
|
|
|
#include <isc/attributes.h>
|
|
#include <isc/dir.h>
|
|
#include <isc/mem.h>
|
|
#include <isc/result.h>
|
|
#include <isc/types.h>
|
|
#include <isc/util.h>
|
|
|
|
noreturn void
|
|
_fail(const char *const file, const int line);
|
|
|
|
#include <ns/hooks.h>
|
|
|
|
#include <ns/test.h>
|
|
|
|
/*%
|
|
* Structure containing parameters for run_full_path_test().
|
|
*/
|
|
typedef struct {
|
|
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 */
|
|
} ns_plugin_expandpath_test_params_t;
|
|
|
|
/*%
|
|
* Perform a single ns_plugin_expandpath() check using given parameters.
|
|
*/
|
|
static void
|
|
run_full_path_test(const ns_plugin_expandpath_test_params_t *test,
|
|
void **state) {
|
|
char **target = (char **)state;
|
|
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().
|
|
*/
|
|
result = ns_plugin_expandpath(test->input, *target, test->output_size);
|
|
|
|
/*
|
|
* Check return value.
|
|
*/
|
|
if (result != test->result) {
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
"expected result %d (%s), got %d (%s)",
|
|
test->id.description, test->id.lineno, test->result,
|
|
isc_result_totext(test->result), result,
|
|
isc_result_totext(result));
|
|
}
|
|
|
|
/*
|
|
* 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\"",
|
|
test->id.description, test->id.lineno, test->output,
|
|
*target);
|
|
}
|
|
|
|
isc_mem_free(mctx, *target);
|
|
}
|
|
|
|
/* test ns_plugin_expandpath() */
|
|
ISC_RUN_TEST_IMPL(ns_plugin_expandpath) {
|
|
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,
|
|
.output = NAMED_PLUGINDIR "/foo.so",
|
|
},
|
|
{
|
|
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,
|
|
},
|
|
{
|
|
NS_TEST_ID("target buffer too small to fit full path"),
|
|
.input = "foo.so",
|
|
.output_size = 7,
|
|
.result = ISC_R_NOSPACE,
|
|
},
|
|
};
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
run_full_path_test(&tests[i], state);
|
|
}
|
|
}
|
|
|
|
ISC_TEST_LIST_START
|
|
|
|
ISC_TEST_ENTRY_CUSTOM(ns_plugin_expandpath, setup_managers, teardown_managers)
|
|
|
|
ISC_TEST_LIST_END
|
|
|
|
ISC_TEST_MAIN
|