Blob Blame History Raw
From c5e5b8415e9c91d132678dcfccde8df848ee70c8 Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Fri, 25 Nov 2016 11:38:02 +0100
Subject: [PATCH] test: Implement self-test functionality

This allows to run 'test' unattended by setting environment variable
SELFTEST=on. Instead of printing the settings libkeepalive should have
changed, it uses the input values still present in environment to assert
them being set correctly.

Signed-off-by: Phil Sutter <psutter@redhat.com>
---
 test/test.c | 42 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/test/test.c b/test/test.c
index 224c1b4944fc5..7eaaaed2e9840 100644
--- a/test/test.c
+++ b/test/test.c
@@ -32,14 +32,23 @@
 */
 
 #define _GNU_SOURCE
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <strings.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 
+#define assert(x) do { \
+  if (!(x)) { \
+    printf("%s:%d: assertion '" #x "' failed!\n", __FILE__, __LINE__); \
+    exit(EXIT_FAILURE); \
+  } \
+} while (0)
+
 int main(void);
 
 int main()
@@ -47,6 +56,11 @@ int main()
   int s;
   int optval;
   socklen_t optlen = sizeof(optval);
+  const char *env;
+  bool selftest = false;
+
+  env = getenv("SELFTEST");
+	selftest = env && !strcasecmp(env, "on");
 
   if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
     perror("socket()");
@@ -58,7 +72,12 @@ int main()
     close(s);
     exit(EXIT_FAILURE);
   }
-  printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
+  if (selftest) {
+    env = getenv("KEEPALIVE");
+    assert((env && !strcasecmp(env, "off")) ^ optval);
+  } else {
+    printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
+  }
 
   if(optval) {
     #ifdef TCP_KEEPCNT
@@ -67,7 +86,12 @@ int main()
       close(s);
       exit(EXIT_FAILURE);
     }
-    printf("TCP_KEEPCNT   = %d\n", optval);
+    if (selftest) {
+      env = getenv("KEEPCNT");
+      assert(!env || atoi(env) == optval);
+    } else {
+      printf("TCP_KEEPCNT   = %d\n", optval);
+    }
     #endif
 
     #ifdef TCP_KEEPIDLE
@@ -76,7 +100,12 @@ int main()
       close(s);
       exit(EXIT_FAILURE);
     }
-    printf("TCP_KEEPIDLE  = %d\n", optval);
+    if (selftest) {
+      env = getenv("KEEPIDLE");
+      assert(!env || atoi(env) == optval);
+    } else {
+      printf("TCP_KEEPIDLE  = %d\n", optval);
+    }
     #endif
 
     #ifdef TCP_KEEPINTVL
@@ -85,7 +114,12 @@ int main()
       close(s);
       exit(EXIT_FAILURE);
     }
-    printf("TCP_KEEPINTVL = %d\n", optval);
+    if (selftest) {
+      env = getenv("KEEPINTVL");
+      assert(!env || atoi(env) == optval);
+    } else {
+      printf("TCP_KEEPINTVL = %d\n", optval);
+    }
     #endif
   }
 
-- 
2.10.0