| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <limits.h> |
| 16 |
#include <sched.h> /* IWYU pragma: keep */ |
| 17 |
#include <setjmp.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stdbool.h> |
| 20 |
#include <stddef.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#define UNIT_TESTING |
| 25 |
#include <cmocka.h> |
| 26 |
|
| 27 |
#include <isc/attributes.h> |
| 28 |
#include <isc/dir.h> |
| 29 |
#include <isc/mem.h> |
| 30 |
#include <isc/result.h> |
| 31 |
#include <isc/types.h> |
| 32 |
#include <isc/util.h> |
| 33 |
|
| 34 |
#include <ns/hooks.h> |
| 35 |
|
| 36 |
#include <tests/ns.h> |
| 37 |
|
| 38 |
/*% |
| 39 |
* Structure containing parameters for run_full_path_test(). |
| 40 |
*/ |
| 41 |
typedef struct { |
| 42 |
const ns_test_id_t id; /* libns test identifier */ |
| 43 |
const char *input; /* source string - plugin name or path |
| 44 |
* */ |
| 45 |
size_t output_size; /* size of target char array to |
| 46 |
* allocate */ |
| 47 |
isc_result_t result; /* expected return value */ |
| 48 |
const char *output; /* expected output string */ |
| 49 |
} ns_plugin_expandpath_test_params_t; |
| 50 |
|
| 51 |
/*% |
| 52 |
* Perform a single ns_plugin_expandpath() check using given parameters. |
| 53 |
*/ |
| 54 |
static void |
| 55 |
run_full_path_test(const ns_plugin_expandpath_test_params_t *test, |
| 56 |
void **state) { |
| 57 |
char **target = (char **)state; |
| 58 |
isc_result_t result; |
| 59 |
|
| 60 |
REQUIRE(test != NULL); |
| 61 |
REQUIRE(test->id.description != NULL); |
| 62 |
REQUIRE(test->input != NULL); |
| 63 |
REQUIRE(test->result != ISC_R_SUCCESS || test->output != NULL); |
| 64 |
|
| 65 |
/* |
| 66 |
* Prepare a target buffer of given size. Store it in 'state' so that |
| 67 |
* it can get cleaned up by _teardown() if the test fails. |
| 68 |
*/ |
| 69 |
*target = isc_mem_allocate(mctx, test->output_size); |
| 70 |
|
| 71 |
/* |
| 72 |
* Call ns_plugin_expandpath(). |
| 73 |
*/ |
| 74 |
result = ns_plugin_expandpath(test->input, *target, test->output_size); |
| 75 |
|
| 76 |
/* |
| 77 |
* Check return value. |
| 78 |
*/ |
| 79 |
if (result != test->result) { |
| 80 |
fail_msg("# test \"%s\" on line %d: " |
| 81 |
"expected result %d (%s), got %d (%s)", |
| 82 |
test->id.description, test->id.lineno, test->result, |
| 83 |
isc_result_totext(test->result), result, |
| 84 |
isc_result_totext(result)); |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
* Check output string if return value indicates success. |
| 89 |
*/ |
| 90 |
if (result == ISC_R_SUCCESS && strcmp(*target, test->output) != 0) { |
| 91 |
fail_msg("# test \"%s\" on line %d: " |
| 92 |
"expected output \"%s\", got \"%s\"", |
| 93 |
test->id.description, test->id.lineno, test->output, |
| 94 |
*target); |
| 95 |
} |
| 96 |
|
| 97 |
isc_mem_free(mctx, *target); |
| 98 |
} |
| 99 |
|
| 100 |
/* test ns_plugin_expandpath() */ |
| 101 |
ISC_RUN_TEST_IMPL(ns_plugin_expandpath) { |
| 102 |
size_t i; |
| 103 |
|
| 104 |
const ns_plugin_expandpath_test_params_t tests[] = { |
| 105 |
{ |
| 106 |
NS_TEST_ID("correct use with an absolute path"), |
| 107 |
.input = "/usr/lib/named/foo.so", |
| 108 |
.output_size = PATH_MAX, |
| 109 |
.result = ISC_R_SUCCESS, |
| 110 |
.output = "/usr/lib/named/foo.so", |
| 111 |
}, |
| 112 |
{ |
| 113 |
NS_TEST_ID("correct use with a relative path"), |
| 114 |
.input = "../../foo.so", |
| 115 |
.output_size = PATH_MAX, |
| 116 |
.result = ISC_R_SUCCESS, |
| 117 |
.output = "../../foo.so", |
| 118 |
}, |
| 119 |
{ |
| 120 |
NS_TEST_ID("correct use with a filename"), |
| 121 |
.input = "foo.so", |
| 122 |
.output_size = PATH_MAX, |
| 123 |
.result = ISC_R_SUCCESS, |
| 124 |
.output = NAMED_PLUGINDIR "/foo.so", |
| 125 |
}, |
| 126 |
{ |
| 127 |
NS_TEST_ID("no space at all in target buffer"), |
| 128 |
.input = "/usr/lib/named/foo.so", |
| 129 |
.output_size = 0, |
| 130 |
.result = ISC_R_NOSPACE, |
| 131 |
}, |
| 132 |
{ |
| 133 |
NS_TEST_ID("target buffer too small to fit input"), |
| 134 |
.input = "/usr/lib/named/foo.so", |
| 135 |
.output_size = 1, |
| 136 |
.result = ISC_R_NOSPACE, |
| 137 |
}, |
| 138 |
{ |
| 139 |
NS_TEST_ID("target buffer too small to fit NULL byte"), |
| 140 |
.input = "/foo.so", |
| 141 |
.output_size = 7, |
| 142 |
.result = ISC_R_NOSPACE, |
| 143 |
}, |
| 144 |
{ |
| 145 |
NS_TEST_ID("target buffer too small to fit full path"), |
| 146 |
.input = "foo.so", |
| 147 |
.output_size = 7, |
| 148 |
.result = ISC_R_NOSPACE, |
| 149 |
}, |
| 150 |
}; |
| 151 |
|
| 152 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 153 |
run_full_path_test(&tests[i], state); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
ISC_TEST_LIST_START |
| 158 |
ISC_TEST_ENTRY(ns_plugin_expandpath) |
| 159 |
ISC_TEST_LIST_END |
| 160 |
|
| 161 |
ISC_TEST_MAIN |