[PATCH v4 6/8] tests: Introduce global mock library

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 cfg.mk              |   2 +-
 tests/Makefile.am   |  13 ++++-
 tests/virtestmock.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 tests/virtestmock.c

diff --git a/cfg.mk b/cfg.mk
index b277db1..c0aba57 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1164,7 +1164,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 75efb90..da68f2e 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) \
@@ -443,6 +447,7 @@ endif WITH_DBUS
 if WITH_LINUX
 test_libraries += virusbmock.la \
 		virnetdevbandwidthmock.la \
+		virtestmock.la
 		$(NULL)
 endif WITH_LINUX
 
@@ -1142,9 +1147,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/virtestmock.c b/tests/virtestmock.c
new file mode 100644
index 0000000..0877956
--- /dev/null
+++ b/tests/virtestmock.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2016 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@xxxxxxxxxx>
+ */
+
+#include <config.h>
+
+#include "virmock.h"
+#include <unistd.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include "internal.h"
+#include "configmake.h"
+
+static int (*real_open)(const char *path, int flags, ...);
+static FILE *(*real_fopen)(const char *path, const char *mode);
+static int (*real_access)(const char *path, int mode);
+static int (*real_stat)(const char *path, struct stat *sb);
+static int (*real___xstat)(int ver, const char *path, struct stat *sb);
+static int (*real_lstat)(const char *path, struct stat *sb);
+static int (*real___lxstat)(int ver, const char *path, struct stat *sb);
+
+static void init_syms(void)
+{
+    if (real_open)
+        return;
+
+    VIR_MOCK_REAL_INIT(open);
+    VIR_MOCK_REAL_INIT(fopen);
+    VIR_MOCK_REAL_INIT(access);
+    VIR_MOCK_REAL_INIT_ALT(stat, __xstat);
+    VIR_MOCK_REAL_INIT_ALT(lstat, __lxstat);
+}
+
+static void
+checkPath(const char *path ATTRIBUTE_UNUSED)
+{
+    /* Nada */
+}
+
+
+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 = real_open(path, flags, mode);
+    } else {
+        ret = real_open(path, flags);
+    }
+    return ret;
+}
+
+FILE *fopen(const char *path, const char *mode)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return real_fopen(path, mode);
+}
+
+
+int access(const char *path, int mode)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return real_access(path, mode);
+}
+
+int stat(const char *path, struct stat *sb)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return VIR_MOCK_CALL_STAT(_STAT_VER, path, sb);
+}
+
+int
+__xstat(int ver, const char *path, struct stat *sb)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return VIR_MOCK_CALL_STAT(ver, path, sb);
+}
+
+int
+lstat(const char *path, struct stat *sb)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return VIR_MOCK_CALL_LSTAT(_STAT_VER, path, sb);
+}
+
+int
+__lxstat(int ver, const char *path, struct stat *sb)
+{
+    init_syms();
+
+    checkPath(path);
+
+    return VIR_MOCK_CALL_LSTAT(ver, path, sb);
+}
-- 
2.8.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]