]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: Don't assume IPv4 connectivity is available
authorAndrea Bolognani <abologna@redhat.com>
Tue, 14 Jul 2020 19:39:50 +0000 (21:39 +0200)
committerAndrea Bolognani <abologna@redhat.com>
Wed, 15 Jul 2020 10:52:20 +0000 (12:52 +0200)
If the host doesn't have a single IPv4 address assigned to any of
its interfaces, not even the loopback one, then virnetsockettest
will fail with

  Cannot identify IPv4/6 availability

because, while the IPv6 bind attempt is conditional, the IPv4 one
is not, and in this case it will always fail.

This commit is better viewed with 'git show -w'.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tests/virnetsockettest.c

index 78fb9cbffd360ad9dcb7649fdc5d2cbebbe84062..f56e623cb3d61819ddb1f2dcfc68c7b93860f382 100644 (file)
@@ -56,8 +56,10 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
 
     for (i = 0; i < 50; i++) {
         int only = 1;
-        if ((s4 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-            goto cleanup;
+        if (*hasIPv4) {
+            if ((s4 = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+                goto cleanup;
+        }
 
         if (*hasIPv6) {
             if ((s6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0)
@@ -77,13 +79,15 @@ checkProtocols(bool *hasIPv4, bool *hasIPv6,
         in6.sin6_port = htons(BASE_PORT + i);
         in6.sin6_addr = in6addr_loopback;
 
-        if (bind(s4, (struct sockaddr *)&in4, sizeof(in4)) < 0) {
-            if (errno == EADDRINUSE) {
-                VIR_FORCE_CLOSE(s4);
-                VIR_FORCE_CLOSE(s6);
-                continue;
+        if (*hasIPv4) {
+            if (bind(s4, (struct sockaddr *)&in4, sizeof(in4)) < 0) {
+                if (errno == EADDRINUSE) {
+                    VIR_FORCE_CLOSE(s4);
+                    VIR_FORCE_CLOSE(s6);
+                    continue;
+                }
+                goto cleanup;
             }
-            goto cleanup;
         }
 
         if (*hasIPv6) {