[Libguestfs] [nbdkit PATCH v4 2/4] tests: Add coverage of new nbdkit_set_dlopen_prefix

Eric Blake eblake at redhat.com
Sun Feb 16 04:22:11 UTC 2020


Although the real reason for adding the new interface is the vddk
plugin, that one is harder to test (not everyone configures it to be
built); so adding a standalone plugin that does the bare minimum to
validate our code is worthwhile.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 tests/Makefile.am          |  25 +++++++++
 tests/test-dlopen-plugin.c | 107 +++++++++++++++++++++++++++++++++++++
 tests/test-dlopen.sh       |  52 ++++++++++++++++++
 3 files changed, 184 insertions(+)
 create mode 100644 tests/test-dlopen-plugin.c
 create mode 100755 tests/test-dlopen.sh

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8b1638ef..369c0e6f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -100,6 +100,7 @@ EXTRA_DIST = \
 	test-data-extents.sh \
 	test-data-file.sh \
 	test-data-raw.sh \
+	test-dlopen.sh \
 	test-debug-flags.sh \
 	test-dump-config.sh \
 	test-dump-plugin.sh \
@@ -374,6 +375,30 @@ test_cxx_filter_la_LDFLAGS = \
 	$(NULL)
 endif HAVE_CXX

+# This builds a plugin that calls dlopen, to test our dlopen override
+# that supplies a prefix.
+TESTS += test-dlopen.sh
+# check_LTLIBRARIES won't build a shared library (see automake manual).
+# So we have to do this and add a dependency.
+noinst_LTLIBRARIES += test-dlopen-plugin.la
+test-dlopen.sh: test-dlopen-plugin.la
+
+test_dlopen_plugin_la_SOURCES = \
+	test-dlopen-plugin.c \
+	$(top_srcdir)/include/nbdkit-plugin.h \
+	$(NULL)
+test_dlopen_plugin_la_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	$(NULL)
+test_dlopen_plugin_la_CFLAGS = $(WARNINGS_CFLAGS)
+test_dlopen_plugin_la_LIBADD = $(DL_LIBS)
+# For use of the -rpath option, see:
+# https://lists.gnu.org/archive/html/libtool/2007-07/msg00067.html
+test_dlopen_plugin_la_LDFLAGS = \
+	-module -avoid-version -shared -rpath /nowhere \
+	$(DL_LDFLAGS) \
+	$(NULL)
+
 # Exit with parent test.
 check_PROGRAMS += test-exit-with-parent
 TESTS += test-exit-with-parent
diff --git a/tests/test-dlopen-plugin.c b/tests/test-dlopen-plugin.c
new file mode 100644
index 00000000..86f09e12
--- /dev/null
+++ b/tests/test-dlopen-plugin.c
@@ -0,0 +1,107 @@
+/* nbdkit
+ * Copyright (C) 2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <nbdkit-plugin.h>
+
+#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
+
+static const char *libdir;
+
+static int
+dlopen_config (const char *key, const char *value)
+{
+  if (strcmp (key, "libdir") == 0)
+    libdir = value;
+  return 0;
+}
+
+static int
+dlopen_config_complete (void)
+{
+  char *msg;
+
+  if (libdir) {
+    if (nbdkit_set_dlopen_prefix (libdir) == -1)
+      return -1;
+  }
+
+  /* The main goal of this plugin is to show that we can hook dlopen.
+   * Test this by requesting a library that doesn't exist, where the
+   * difference in the error message shows if we were successful.
+   */
+  dlopen ("no_one_has_a_library_by_this_name", RTLD_NOW);
+  msg = dlerror ();
+  assert (msg);
+  nbdkit_error ("dlopen failed as expected: %s\n", msg);
+
+  return 0;
+}
+
+/* These must be provided, but we don't really need to use them. */
+static void *
+dlopen_open (int readonly)
+{
+  return NBDKIT_HANDLE_NOT_NEEDED;
+}
+
+static int64_t
+dlopen_get_size (void *handle)
+{
+  return 0;
+}
+
+static int
+dlopen_pread (void *handle, void *buf, uint32_t count, uint64_t offset)
+{
+  assert (false);
+}
+
+static struct nbdkit_plugin plugin = {
+  .name              = "dlopenplugin",
+  .version           = PACKAGE_VERSION,
+  .config            = dlopen_config,
+  .config_complete   = dlopen_config_complete,
+  .open              = dlopen_open,
+  .get_size          = dlopen_get_size,
+  .pread             = dlopen_pread,
+};
+
+NBDKIT_REGISTER_PLUGIN(plugin)
diff --git a/tests/test-dlopen.sh b/tests/test-dlopen.sh
new file mode 100755
index 00000000..85db3172
--- /dev/null
+++ b/tests/test-dlopen.sh
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+# nbdkit
+# Copyright (C) 2020 Red Hat Inc.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+source ./functions.sh
+set -e
+set -x
+
+files="test-dlopen.err"
+rm -f $files
+cleanup_fn rm -f $files
+
+libs=./.libs
+
+# First: show that without using the new function, dlopen complains
+# about a relative request.
+nbdkit $libs/test-dlopen-plugin.so --run : 2> test-dlopen.err
+cat test-dlopen.err
+grep "as expected: no_one_has" test-dlopen.err
+
+# Now, show that we can alter the message based on our config parameter.
+nbdkit $libs/test-dlopen-plugin.so libdir="$PWD" --run : 2> test-dlopen.err
+cat test-dlopen.err
+grep -F "as expected: $PWD/no_one_has" test-dlopen.err
-- 
2.24.1




More information about the Libguestfs mailing list