]> xenbits.xensource.com Git - libvirt.git/commitdiff
virusbmock: Switch to VIR_MOCK_REAL_INIT()
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 13 Mar 2024 16:35:15 +0000 (17:35 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 14 Mar 2024 12:03:21 +0000 (13:03 +0100)
Since virusbmock was written 10 years ago, back when we didn't
have virmock.h and its helpers, it open codes symbol resolution
(VIR_MOCK_REAL_INIT). With a bit of cleanup (e.g. renaming
realopen to real_open and so on) it can use virmock.h provided
macros.

And while at it, drop include of virusb.h - there is no
compelling reason for it include the file. The mock just
redirects paths passed to open()/opendir().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
tests/virusbmock.c

index 971d98aa9a682aa67efdc164ab66a70df5a07693..570f7c28eb1e551007eb83e4a365bb08de919926 100644 (file)
 #include <dlfcn.h>
 #include <fcntl.h>
 
-#include "virusb.h"
+#include "virmock.h"
 
 #define USB_SYSFS "/sys/bus/usb"
 #define FAKE_USB_SYSFS "virusbtestdata/sys_bus_usb"
 
-static int (*realopen)(const char *pathname, int flags, ...);
-static DIR *(*realopendir)(const char *name);
+static int (*real_open)(const char *pathname, int flags, ...);
+static DIR *(*real_opendir)(const char *name);
 
 static void init_syms(void)
 {
-    if (realopen)
+    if (real_open)
         return;
 
-    realopen = dlsym(RTLD_NEXT, "open");
-    realopendir = dlsym(RTLD_NEXT, "opendir");
-    if (!realopen || !realopendir) {
-        fprintf(stderr, "Error getting symbols");
-        abort();
-    }
+    VIR_MOCK_REAL_INIT(open);
+    VIR_MOCK_REAL_INIT(opendir);
 }
 
 static char *get_fake_path(const char *real_path)
@@ -66,7 +62,7 @@ DIR *opendir(const char *name)
 
     path = get_fake_path(name);
 
-    return realopendir(path);
+    return real_opendir(path);
 }
 
 int open(const char *pathname, int flags, ...)
@@ -91,6 +87,6 @@ int open(const char *pathname, int flags, ...)
         va_end(ap);
     }
 
-    ret = realopen(path, flags, mode);
+    ret = real_open(path, flags, mode);
     return ret;
 }