[libvirt] [PATCH 1/4] tests: Split off the mock part of the port allocator test

Andrea Bolognani abologna at redhat.com
Thu Feb 11 13:36:28 UTC 2016


Instead of compiling either the mock or the non-mock part of the
file based on a compiler flag, split the mock part off to its
own file.
---
 tests/Makefile.am            |   4 +-
 tests/virportallocatormock.c | 108 ++++++++++++++++++++++++++++++++++++++++++
 tests/virportallocatortest.c | 110 +++----------------------------------------
 3 files changed, 117 insertions(+), 105 deletions(-)
 create mode 100644 tests/virportallocatormock.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5f5a561..1fe7d6b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1028,8 +1028,8 @@ virportallocatortest_SOURCES = \
 virportallocatortest_LDADD = $(LDADDS)
 
 libvirportallocatormock_la_SOURCES = \
-	virportallocatortest.c
-libvirportallocatormock_la_CFLAGS = $(AM_CFLAGS) -DMOCK_HELPER=1
+	virportallocatormock.c
+libvirportallocatormock_la_CFLAGS = $(AM_CFLAGS)
 libvirportallocatormock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
 libvirportallocatormock_la_LIBADD = $(MOCKLIBS_LIBS)
 
diff --git a/tests/virportallocatormock.c b/tests/virportallocatormock.c
new file mode 100644
index 0000000..e44191e
--- /dev/null
+++ b/tests/virportallocatormock.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2013-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: Daniel P. Berrange <berrange at redhat.com>
+ */
+
+#include <config.h>
+#include <stdlib.h>
+
+#if HAVE_DLFCN_H
+# include <dlfcn.h>
+#endif
+
+#if defined(RTLD_NEXT)
+# include "internal.h"
+# include <sys/socket.h>
+# include <errno.h>
+# include <arpa/inet.h>
+# include <netinet/in.h>
+# include <stdio.h>
+# include <unistd.h>
+
+static bool host_has_ipv6;
+static int (*realsocket)(int domain, int type, int protocol);
+
+static void init_syms(void)
+{
+    int fd;
+
+    if (realsocket)
+        return;
+
+    realsocket = dlsym(RTLD_NEXT, "socket");
+
+    if (!realsocket) {
+        fprintf(stderr, "Unable to find 'socket' symbol\n");
+        abort();
+    }
+
+    fd = realsocket(AF_INET6, SOCK_STREAM, 0);
+    if (fd < 0)
+        return;
+
+    host_has_ipv6 = true;
+    close(fd);
+}
+
+int socket(int domain,
+           int type,
+           int protocol)
+{
+    init_syms();
+
+    if (getenv("LIBVIRT_TEST_IPV4ONLY") && domain == AF_INET6) {
+        errno = EAFNOSUPPORT;
+        return -1;
+    }
+
+    return realsocket(domain, type, protocol);
+}
+
+int bind(int sockfd ATTRIBUTE_UNUSED,
+         const struct sockaddr *addr,
+         socklen_t addrlen ATTRIBUTE_UNUSED)
+{
+    struct sockaddr_in saddr;
+
+    memcpy(&saddr, addr, sizeof(saddr));
+
+    if (host_has_ipv6 && !getenv("LIBVIRT_TEST_IPV4ONLY")) {
+        if (saddr.sin_port == htons(5900) ||
+            (saddr.sin_family == AF_INET &&
+             saddr.sin_port == htons(5904)) ||
+            (saddr.sin_family == AF_INET6 &&
+             (saddr.sin_port == htons(5905) ||
+              saddr.sin_port == htons(5906)))) {
+            errno = EADDRINUSE;
+            return -1;
+        }
+        return 0;
+    }
+
+    if (saddr.sin_port == htons(5900) ||
+        saddr.sin_port == htons(5904) ||
+        saddr.sin_port == htons(5905) ||
+        saddr.sin_port == htons(5906)) {
+        errno = EADDRINUSE;
+        return -1;
+    }
+
+    return 0;
+}
+
+#endif /* ! defined(RTLD_NEXT) */
diff --git a/tests/virportallocatortest.c b/tests/virportallocatortest.c
index 077aad8..8a8f413 100644
--- a/tests/virportallocatortest.c
+++ b/tests/virportallocatortest.c
@@ -22,101 +22,14 @@
 #include <stdlib.h>
 #include "virfile.h"
 #include "testutils.h"
+#include "virutil.h"
+#include "virerror.h"
+#include "viralloc.h"
+#include "virlog.h"
+#include "virportallocator.h"
+#include "virstring.h"
 
-#if HAVE_DLFCN_H
-# include <dlfcn.h>
-#endif
-
-#if defined(RTLD_NEXT)
-# ifdef MOCK_HELPER
-#  include "internal.h"
-#  include <sys/socket.h>
-#  include <errno.h>
-#  include <arpa/inet.h>
-#  include <netinet/in.h>
-#  include <stdio.h>
-
-static bool host_has_ipv6;
-static int (*realsocket)(int domain, int type, int protocol);
-
-static void init_syms(void)
-{
-    int fd;
-
-    if (realsocket)
-        return;
-
-    realsocket = dlsym(RTLD_NEXT, "socket");
-
-    if (!realsocket) {
-        fprintf(stderr, "Unable to find 'socket' symbol\n");
-        abort();
-    }
-
-    fd = realsocket(AF_INET6, SOCK_STREAM, 0);
-    if (fd < 0)
-        return;
-
-    host_has_ipv6 = true;
-    close(fd);
-}
-
-int socket(int domain,
-           int type,
-           int protocol)
-{
-    init_syms();
-
-    if (getenv("LIBVIRT_TEST_IPV4ONLY") && domain == AF_INET6) {
-        errno = EAFNOSUPPORT;
-        return -1;
-    }
-
-    return realsocket(domain, type, protocol);
-}
-
-int bind(int sockfd ATTRIBUTE_UNUSED,
-         const struct sockaddr *addr,
-         socklen_t addrlen ATTRIBUTE_UNUSED)
-{
-    struct sockaddr_in saddr;
-
-    memcpy(&saddr, addr, sizeof(saddr));
-
-    if (host_has_ipv6 && !getenv("LIBVIRT_TEST_IPV4ONLY")) {
-        if (saddr.sin_port == htons(5900) ||
-            (saddr.sin_family == AF_INET &&
-             saddr.sin_port == htons(5904)) ||
-            (saddr.sin_family == AF_INET6 &&
-             (saddr.sin_port == htons(5905) ||
-              saddr.sin_port == htons(5906)))) {
-            errno = EADDRINUSE;
-            return -1;
-        }
-        return 0;
-    }
-
-    if (saddr.sin_port == htons(5900) ||
-        saddr.sin_port == htons(5904) ||
-        saddr.sin_port == htons(5905) ||
-        saddr.sin_port == htons(5906)) {
-        errno = EADDRINUSE;
-        return -1;
-    }
-
-    return 0;
-}
-
-# else
-
-#  include "virutil.h"
-#  include "virerror.h"
-#  include "viralloc.h"
-#  include "virlog.h"
-#  include "virportallocator.h"
-#  include "virstring.h"
-
-#  define VIR_FROM_THIS VIR_FROM_RPC
+#define VIR_FROM_THIS VIR_FROM_RPC
 
 VIR_LOG_INIT("tests.portallocatortest");
 
@@ -255,12 +168,3 @@ mymain(void)
 }
 
 VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/libvirportallocatormock.so")
-# endif
-
-#else /* ! defined(RTLD_NEXT) */
-int
-main(void)
-{
-    return EXIT_AM_SKIP;
-}
-#endif
-- 
2.5.0




More information about the libvir-list mailing list