The intent is that this library is going to be called every time
to check if we are not touching anything outside srcdir or
builddir.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
cfg.mk | 2 +-
tests/Makefile.am | 13 +++-
tests/testutils.c | 33 +++++++++
tests/testutils.h | 29 ++------
tests/vircgroupmock.c | 6 +-
tests/virpcimock.c | 6 +-
tests/virtestmock.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 241 insertions(+), 28 deletions(-)
create mode 100644 tests/virtestmock.c
diff --git a/cfg.mk b/cfg.mk
index 16da2b3..afee8e9 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1153,7 +1153,7 @@ exclude_file_name_regexp--sc_copyright_usage = \
^COPYING(|\.LESSER)$$
exclude_file_name_regexp--sc_flags_usage = \
-
^(cfg\.mk|docs/|src/util/virnetdevtap\.c$$|tests/(vir(cgroup|pci|usb)|nss|qemuxml2argv)mock\.c$$)
+
^(cfg\.mk|docs/|src/util/virnetdevtap\.c$$|tests/(vir(cgroup|pci|test|usb)|nss|qemuxml2argv)mock\.c$$)
exclude_file_name_regexp--sc_libvirt_unmarked_diagnostics = \
^(src/rpc/gendispatch\.pl$$|tests/)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 67f597a..6412337 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -18,7 +18,9 @@
# old automake does not provide abs_{src,build}dir variables
abs_builddir = $(shell pwd)
+abs_topbuilddir = $(shell cd .. && pwd)
abs_srcdir = $(shell cd $(srcdir) && pwd)
+abs_topsrcdir = $(shell cd $(top_srcdir) && pwd)
SHELL = $(PREFERABLY_POSIX_SHELL)
@@ -33,7 +35,9 @@ INCLUDES = \
AM_CFLAGS = \
-Dabs_builddir="\"$(abs_builddir)\"" \
+ -Dabs_topbuilddir="\"$(abs_topbuilddir)\"" \
-Dabs_srcdir="\"$(abs_srcdir)\"" \
+ -Dabs_topsrcdir="\"$(abs_topsrcdir)\"" \
$(LIBXML_CFLAGS) \
$(LIBNL_CFLAGS) \
$(GNUTLS_CFLAGS) \
@@ -448,6 +452,7 @@ endif WITH_DBUS
if WITH_LINUX
test_libraries += virusbmock.la \
virnetdevbandwidthmock.la \
+ virtestmock.la
$(NULL)
endif WITH_LINUX
@@ -1134,9 +1139,15 @@ virnetdevbandwidthmock_la_CFLAGS = $(AM_CFLAGS)
virnetdevbandwidthmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
virnetdevbandwidthmock_la_LIBADD = $(MOCKLIBS_LIBS)
+virtestmock_la_SOURCES = \
+ virtestmock.c
+virtestmock_la_CFLAGS = $(AM_CFLAGS)
+virtestmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
+virtestmock_la_LIBADD = $(MOCKLIBS_LIBS)
else ! WITH_LINUX
EXTRA_DIST += virusbtest.c virusbmock.c \
- virnetdevbandwidthtest.c virnetdevbandwidthmock.c
+ virnetdevbandwidthtest.c virnetdevbandwidthmock.c \
+ virtestmock.c
endif ! WITH_LINUX
if WITH_DBUS
diff --git a/tests/testutils.c b/tests/testutils.c
index 79d0763..403ab37 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -832,8 +832,35 @@ virTestSetEnvPath(void)
return ret;
}
+static void
+virTestLDPreload(char *const argv[],
+ const char *lib)
+{
+ const char *preload = getenv("LD_PRELOAD");
+
+ if (preload == NULL || strstr(preload, lib) == NULL) {
+ char *newenv;
+
+ if (!virFileIsExecutable(lib)) {
+ fprintf(stderr, "%s is not executable\n", lib);
+ _exit(EXIT_FAILURE);
+ }
+
+ if ((!preload && VIR_STRDUP(newenv, lib) < 0) ||
+ (preload && virAsprintf(&newenv, "%s:%s", lib, preload)
< 0)) {
+ fprintf(stderr, "Out of memory\n");
+ _exit(EXIT_FAILURE);
+ }
+ setenv("LD_PRELOAD", newenv, 1);
+ execv(argv[0], argv);
+ }
+}
+
+#define TEST_MOCK (abs_builddir "/.libs/virtestmock.so")
+
int virtTestMain(int argc,
char **argv,
+ const char *lib,
int (*func)(void))
{
int ret;
@@ -842,6 +869,12 @@ int virtTestMain(int argc,
char *oomstr;
#endif
+#ifdef __linux__
+ virTestLDPreload(argv, TEST_MOCK);
+#endif
+ if (lib)
+ virTestLDPreload(argv, lib);
+
virFileActivateDirOverride(argv[0]);
if (virTestSetEnvPath() < 0)
diff --git a/tests/testutils.h b/tests/testutils.h
index 0417a0b..215f349 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -102,33 +102,18 @@ const char *virtTestCounterNext(void);
int virtTestMain(int argc,
char **argv,
+ const char *lib,
int (*func)(void));
/* Setup, then call func() */
-# define VIRT_TEST_MAIN(func) \
- int main(int argc, char **argv) { \
- return virtTestMain(argc, argv, func); \
+# define VIRT_TEST_MAIN(func) \
+ int main(int argc, char **argv) { \
+ return virtTestMain(argc, argv, NULL, func); \
}
-# define VIRT_TEST_MAIN_PRELOAD(func, lib) \
- int main(int argc, char **argv) { \
- const char *preload = getenv("LD_PRELOAD"); \
- if (preload == NULL || strstr(preload, lib) == NULL) { \
- char *newenv; \
- if (!virFileIsExecutable(lib)) { \
- perror(lib); \
- return EXIT_FAILURE; \
- } \
- if (!preload) { \
- newenv = (char *) lib; \
- } else if (virAsprintf(&newenv, "%s:%s", lib, preload) < 0)
{ \
- perror("virAsprintf"); \
- return EXIT_FAILURE; \
- } \
- setenv("LD_PRELOAD", newenv, 1); \
- execv(argv[0], argv); \
- } \
- return virtTestMain(argc, argv, func); \
+# define VIRT_TEST_MAIN_PRELOAD(func, lib) \
+ int main(int argc, char **argv) { \
+ return virtTestMain(argc, argv, lib, func); \
}
virCapsPtr virTestGenericCapsInit(void);
diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c
index cfc51e8..395254b 100644
--- a/tests/vircgroupmock.c
+++ b/tests/vircgroupmock.c
@@ -416,8 +416,10 @@ static void init_syms(void)
LOAD_SYM(fopen);
LOAD_SYM(access);
- LOAD_SYM_ALT(lstat, __lxstat);
- LOAD_SYM_ALT(stat, __xstat);
+ LOAD_SYM(lstat);
+ LOAD_SYM(__lxstat);
+ LOAD_SYM(stat);
+ LOAD_SYM(__xstat);
LOAD_SYM(mkdir);
LOAD_SYM(open);
}
diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index cfe42a6..ac8a665 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -790,8 +790,10 @@ init_syms(void)
} while (0)
LOAD_SYM(access);
- LOAD_SYM_ALT(lstat, __lxstat);
- LOAD_SYM_ALT(stat, __xstat);
+ LOAD_SYM(lstat);
+ LOAD_SYM(__lxstat);
+ LOAD_SYM(stat);
+ LOAD_SYM(__xstat);
LOAD_SYM(canonicalize_file_name);
LOAD_SYM(open);
LOAD_SYM(close);
diff --git a/tests/virtestmock.c b/tests/virtestmock.c
new file mode 100644
index 0000000..f138e98
--- /dev/null
+++ b/tests/virtestmock.c
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <
http://www.gnu.org/licenses/>.
+ *
+ * Author: Michal Privoznik <mprivozn(a)redhat.com>
+ */
+
+#include <config.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <dlfcn.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "internal.h"
+#include "configmake.h"
+
+static int (*realopen)(const char *path, int flags, ...);
+static FILE *(*realfopen)(const char *path, const char *mode);
+static int (*realaccess)(const char *path, int mode);
+static int (*realstat)(const char *path, struct stat *sb);
+static int (*real__xstat)(int ver, const char *path, struct stat *sb);
+static int (*reallstat)(const char *path, struct stat *sb);
+static int (*real__lxstat)(int ver, const char *path, struct stat *sb);
+
+static void init_syms(void)
+{
+ if (realopen)
+ return;
+
+#define LOAD_SYM(name) \
+ do { \
+ if (!(real ## name = dlsym(RTLD_NEXT, #name))) { \
+ fprintf(stderr, "Cannot find real '%s' symbol\n", #name);
\
+ abort(); \
+ } \
+ } while (0)
+
+#define LOAD_SYM_ALT(name1, name2) \
+ do { \
+ if (!(real ## name1 = dlsym(RTLD_NEXT, #name1)) && \
+ !(real ## name2 = dlsym(RTLD_NEXT, #name2))) { \
+ fprintf(stderr, "Cannot find real '%s' or '%s'
symbol\n", #name1, #name2); \
+ abort(); \
+ } \
+ } while (0)
+
+ LOAD_SYM(open);
+ LOAD_SYM(fopen);
+ LOAD_SYM(access);
+ LOAD_SYM_ALT(stat, __xstat);
+ LOAD_SYM_ALT(lstat, __lxstat);
+}
+
+static void
+checkPath(const char *path)
+{
+ if (!STRPREFIX(path, abs_topsrcdir) &&
+ !STRPREFIX(path, abs_topbuilddir)) {
+ /* Okay, this is a dummy check and spurious print.
+ * But this is going to be replaced soon. */
+ fprintf(stderr, "*** %s ***\n", path);
+ }
+}
+
+
+int open(const char *path, int flags, ...)
+{
+ int ret;
+
+ init_syms();
+
+ checkPath(path);
+
+ if (flags & O_CREAT) {
+ va_list ap;
+ mode_t mode;
+ va_start(ap, flags);
+ mode = va_arg(ap, mode_t);
+ va_end(ap);
+ ret = realopen(path, flags, mode);
+ } else {
+ ret = realopen(path, flags);
+ }
+ return ret;
+}
+
+FILE *fopen(const char *path, const char *mode)
+{
+ init_syms();
+
+ checkPath(path);
+
+ return realfopen(path, mode);
+}
+
+
+int access(const char *path, int mode)
+{
+ init_syms();
+
+ checkPath(path);
+
+ return realaccess(path, mode);
+}
+
+int stat(const char *path, struct stat *sb)
+{
+ init_syms();
+
+ checkPath(path);
+
+ if (!realstat)
+ return real__xstat(1, path, sb);
+
+ return realstat(path, sb);
+}
+
+int
+__xstat(int ver, const char *path, struct stat *sb)
+{
+ init_syms();
+
+ checkPath(path);
+ if (!real__xstat) {
+ if (ver == 1) {
+ return realstat(path, sb);
+ } else {
+ fprintf(stderr, "Not handled __xstat(ver=%d)", ver);
+ abort();
+ }
+ }
+
+ return real__xstat(ver, path, sb);
+}
+
+int
+lstat(const char *path, struct stat *sb)
+{
+ init_syms();
+
+ checkPath(path);
+
+ if (!reallstat)
+ return real__lxstat(1, path, sb);
+
+ return reallstat(path, sb);
+}
+
+int
+__lxstat(int ver, const char *path, struct stat *sb)
+{
+ init_syms();
+
+ checkPath(path);
+ if (!real__lxstat) {
+ if (ver == 1) {
+ return reallstat(path, sb);
+ } else {
+ fprintf(stderr, "Not handled __lxstat(ver=%d)", ver);
+ abort();
+ }
+ }
+
+ return real__lxstat(ver, path, sb);
+}
--
2.7.3