]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
ns_plugin_expandpath() auto-extension unit-tests
authorColin Vidal <colin@isc.org>
Mon, 21 Jul 2025 13:58:31 +0000 (15:58 +0200)
committerColin Vidal <colin@isc.org>
Mon, 28 Jul 2025 21:08:04 +0000 (23:08 +0200)
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
tests/ns/plugin_test.c

index 7d4e2e30fafb8b3653f737884c3c6d7a72e7886d..ca0270ca376129d0b6a2a1051e887d03f327d92d 100644 (file)
@@ -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(
index a7427c4c850220293fc0b1410fff69249a856074..1de3e7c989e2e5a8c4d0095ab3b79e19ce3b4193 100644 (file)
 
 #include <dns/lib.h>
 
-#include <ns/hooks.h>
+#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 <tests/ns.h>
 
@@ -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,
                },