1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
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
|