Blame 0003-test-Implement-self-test-functionality.patch

04bd563
From c5e5b8415e9c91d132678dcfccde8df848ee70c8 Mon Sep 17 00:00:00 2001
04bd563
From: Phil Sutter <psutter@redhat.com>
04bd563
Date: Fri, 25 Nov 2016 11:38:02 +0100
04bd563
Subject: [PATCH] test: Implement self-test functionality
04bd563
04bd563
This allows to run 'test' unattended by setting environment variable
04bd563
SELFTEST=on. Instead of printing the settings libkeepalive should have
04bd563
changed, it uses the input values still present in environment to assert
04bd563
them being set correctly.
04bd563
04bd563
Signed-off-by: Phil Sutter <psutter@redhat.com>
04bd563
---
04bd563
 test/test.c | 42 ++++++++++++++++++++++++++++++++++++++----
04bd563
 1 file changed, 38 insertions(+), 4 deletions(-)
04bd563
04bd563
diff --git a/test/test.c b/test/test.c
04bd563
index 224c1b4944fc5..7eaaaed2e9840 100644
04bd563
--- a/test/test.c
04bd563
+++ b/test/test.c
04bd563
@@ -32,14 +32,23 @@
04bd563
 */
04bd563
 
04bd563
 #define _GNU_SOURCE
04bd563
+#include <stdbool.h>
04bd563
 #include <stdio.h>
04bd563
 #include <stdlib.h>
04bd563
+#include <strings.h>
04bd563
 #include <unistd.h>
04bd563
 #include <sys/types.h>
04bd563
 #include <sys/socket.h>
04bd563
 #include <netinet/in.h>
04bd563
 #include <netinet/tcp.h>
04bd563
 
04bd563
+#define assert(x) do { \
04bd563
+  if (!(x)) { \
04bd563
+    printf("%s:%d: assertion '" #x "' failed!\n", __FILE__, __LINE__); \
04bd563
+    exit(EXIT_FAILURE); \
04bd563
+  } \
04bd563
+} while (0)
04bd563
+
04bd563
 int main(void);
04bd563
 
04bd563
 int main()
04bd563
@@ -47,6 +56,11 @@ int main()
04bd563
   int s;
04bd563
   int optval;
04bd563
   socklen_t optlen = sizeof(optval);
04bd563
+  const char *env;
04bd563
+  bool selftest = false;
04bd563
+
04bd563
+  env = getenv("SELFTEST");
04bd563
+	selftest = env && !strcasecmp(env, "on");
04bd563
 
04bd563
   if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
04bd563
     perror("socket()");
04bd563
@@ -58,7 +72,12 @@ int main()
04bd563
     close(s);
04bd563
     exit(EXIT_FAILURE);
04bd563
   }
04bd563
-  printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
04bd563
+  if (selftest) {
04bd563
+    env = getenv("KEEPALIVE");
04bd563
+    assert((env && !strcasecmp(env, "off")) ^ optval);
04bd563
+  } else {
04bd563
+    printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
04bd563
+  }
04bd563
 
04bd563
   if(optval) {
04bd563
     #ifdef TCP_KEEPCNT
04bd563
@@ -67,7 +86,12 @@ int main()
04bd563
       close(s);
04bd563
       exit(EXIT_FAILURE);
04bd563
     }
04bd563
-    printf("TCP_KEEPCNT   = %d\n", optval);
04bd563
+    if (selftest) {
04bd563
+      env = getenv("KEEPCNT");
04bd563
+      assert(!env || atoi(env) == optval);
04bd563
+    } else {
04bd563
+      printf("TCP_KEEPCNT   = %d\n", optval);
04bd563
+    }
04bd563
     #endif
04bd563
 
04bd563
     #ifdef TCP_KEEPIDLE
04bd563
@@ -76,7 +100,12 @@ int main()
04bd563
       close(s);
04bd563
       exit(EXIT_FAILURE);
04bd563
     }
04bd563
-    printf("TCP_KEEPIDLE  = %d\n", optval);
04bd563
+    if (selftest) {
04bd563
+      env = getenv("KEEPIDLE");
04bd563
+      assert(!env || atoi(env) == optval);
04bd563
+    } else {
04bd563
+      printf("TCP_KEEPIDLE  = %d\n", optval);
04bd563
+    }
04bd563
     #endif
04bd563
 
04bd563
     #ifdef TCP_KEEPINTVL
04bd563
@@ -85,7 +114,12 @@ int main()
04bd563
       close(s);
04bd563
       exit(EXIT_FAILURE);
04bd563
     }
04bd563
-    printf("TCP_KEEPINTVL = %d\n", optval);
04bd563
+    if (selftest) {
04bd563
+      env = getenv("KEEPINTVL");
04bd563
+      assert(!env || atoi(env) == optval);
04bd563
+    } else {
04bd563
+      printf("TCP_KEEPINTVL = %d\n", optval);
04bd563
+    }
04bd563
     #endif
04bd563
   }
04bd563
 
04bd563
-- 
04bd563
2.10.0
04bd563