From cdbaddb524193227b028de0a58eb806bccea8952 Mon Sep 17 00:00:00 2001 From: Colin Vidal Date: Mon, 21 Jul 2025 15:58:31 +0200 Subject: [PATCH] ns_plugin_expandpath() auto-extension unit-tests Update existing ns_plugin_expandpath() unit test to cover the logic appending the plugin extension if missing. Because ns_plugin_expandpath() now relies on isc_file_exists() API, a mocked version has been added in tests/ns/plugin_test.c and relies on the linker --wrap mechanism. --- tests/ns/meson.build | 7 +++++ tests/ns/plugin_test.c | 62 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/tests/ns/meson.build b/tests/ns/meson.build index 7d4e2e30fa..ca0270ca37 100644 --- a/tests/ns/meson.build +++ b/tests/ns/meson.build @@ -14,6 +14,12 @@ foreach unit : [ 'plugin', 'query', ] + linkargs = '' + if unit == 'plugin' + linkargs = [ + '-Wl,--wrap=isc_file_exists', + ] + endif test_bin = executable( unit, files(f'@unit@_test.c', 'netmgr_wrap.c'), @@ -31,6 +37,7 @@ foreach unit : [ cmocka_dep, nghttp2_dep, ], + link_args: linkargs, ) test( diff --git a/tests/ns/plugin_test.c b/tests/ns/plugin_test.c index a7427c4c85..1de3e7c989 100644 --- a/tests/ns/plugin_test.c +++ b/tests/ns/plugin_test.c @@ -34,7 +34,16 @@ #include -#include +#include "../ns/hooks.c" + +bool +__wrap_isc_file_exists(const char *pathname); + +bool +__wrap_isc_file_exists(const char *pathname) { + UNUSED(pathname); + return mock(); +} #include @@ -43,8 +52,8 @@ */ typedef struct { const ns_test_id_t id; /* libns test identifier */ - const char *input; /* source string - plugin name or path - * */ + const char *input; /* source string - plugin name or path */ + bool exists; /* return of mocked isc_file_exists() */ size_t output_size; /* size of target char array to * allocate */ isc_result_t result; /* expected return value */ @@ -65,6 +74,10 @@ run_full_path_test(const ns_plugin_expandpath_test_params_t *test, REQUIRE(test->input != NULL); REQUIRE(test->result != ISC_R_SUCCESS || test->output != NULL); + if (test->result == ISC_R_SUCCESS) { + will_return(__wrap_isc_file_exists, test->exists); + } + /* * Prepare a target buffer of given size. Store it in 'state' so that * it can get cleaned up by _teardown() if the test fails. @@ -108,6 +121,7 @@ ISC_RUN_TEST_IMPL(ns_plugin_expandpath) { { NS_TEST_ID("correct use with an absolute path"), .input = "/usr/lib/named/foo.so", + .exists = true, .output_size = PATH_MAX, .result = ISC_R_SUCCESS, .output = "/usr/lib/named/foo.so", @@ -115,6 +129,7 @@ ISC_RUN_TEST_IMPL(ns_plugin_expandpath) { { NS_TEST_ID("correct use with a relative path"), .input = "../../foo.so", + .exists = true, .output_size = PATH_MAX, .result = ISC_R_SUCCESS, .output = "../../foo.so", @@ -122,31 +137,72 @@ ISC_RUN_TEST_IMPL(ns_plugin_expandpath) { { NS_TEST_ID("correct use with a filename"), .input = "foo.so", + .exists = true, .output_size = PATH_MAX, .result = ISC_R_SUCCESS, .output = NAMED_PLUGINDIR "/foo.so", }, + { + NS_TEST_ID("correct use with an absolute path and no " + "extension"), + .input = "/usr/lib/named/foo", + .exists = false, + .output_size = PATH_MAX, + .result = ISC_R_SUCCESS, + .output = "/usr/lib/named/foo.so", + }, + { + NS_TEST_ID("correct use with a relative path and no " + "extension"), + .input = "../../foo", + .exists = false, + .output_size = PATH_MAX, + .result = ISC_R_SUCCESS, + .output = "../../foo.so", + }, + { + NS_TEST_ID("correct use with a filename and no " + "extension"), + .input = "foo", + .exists = false, + .output_size = PATH_MAX, + .result = ISC_R_SUCCESS, + .output = NAMED_PLUGINDIR "/foo.so", + }, + { + NS_TEST_ID("correct use with a filename and no " + "extension but a name with dots"), + .input = "foo.bar", + .exists = false, + .output_size = PATH_MAX, + .result = ISC_R_SUCCESS, + .output = NAMED_PLUGINDIR "/foo.bar.so", + }, { NS_TEST_ID("no space at all in target buffer"), .input = "/usr/lib/named/foo.so", + .exists = true, .output_size = 0, .result = ISC_R_NOSPACE, }, { NS_TEST_ID("target buffer too small to fit input"), .input = "/usr/lib/named/foo.so", + .exists = true, .output_size = 1, .result = ISC_R_NOSPACE, }, { NS_TEST_ID("target buffer too small to fit NULL byte"), .input = "/foo.so", + .exists = true, .output_size = 7, .result = ISC_R_NOSPACE, }, { NS_TEST_ID("target buffer too small to fit full path"), .input = "foo.so", + .exists = true, .output_size = 7, .result = ISC_R_NOSPACE, },