Browse code

unit tests: implement test for sitnl

This patch introduces a new unit test that is not executed
by the cmocka framework, but rather used by a new t_net.sh
bash script.

The idea behind this test is to ensure that invoking sitnl
functions or running iproute commands leads to the same
networking (interface and routing table) state.

To achieve this, the t_net.sh script first runs a binary
implemented invoking sitnl functions and then takes a
"screenshot" of the state. Subsequently a series of
iproute commands, expected to mimic exactly the same behaviour
as the sitnl functions invoked before, are executed.
The final state is then compared with the screenshot
previously taken.

If no mismatching is found, the test is passed.

The current unit_test, however, does not cover all the
sitnl functionalities and it is expected to be extended
in the future.

Signed-off-by: Antonio Quartulli <a@unstable.cc>
Acked-by: Arne Schwabe <arne@rfc2549.org>
Message-Id: <20181219050118.6568-7-a@unstable.cc>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg18027.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Antonio Quartulli authored on 2018/12/19 14:01:17
Showing 5 changed files
... ...
@@ -294,9 +294,11 @@ else
294 294
 fi
295 295
 
296 296
 AC_DEFINE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A string representing our host])
297
+AM_CONDITIONAL([TARGET_LINUX], [false])
297 298
 case "$host" in
298 299
 	*-*-linux*)
299 300
 		AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?])
301
+		AM_CONDITIONAL([TARGET_LINUX], [true])
300 302
 		AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L"], [Target prefix])
301 303
 		have_sitnl="yes"
302 304
 		;;
... ...
@@ -14,7 +14,8 @@ MAINTAINERCLEANFILES = \
14 14
 
15 15
 SUBDIRS = unit_tests
16 16
 
17
-test_scripts = t_client.sh
17
+test_scripts = t_net.sh
18
+test_scripts += t_client.sh
18 19
 test_scripts += t_lpback.sh t_cltsrv.sh
19 20
 
20 21
 TESTS_ENVIRONMENT = top_srcdir="$(top_srcdir)"
21 22
new file mode 100755
... ...
@@ -0,0 +1,180 @@
0
+#!/bin/sh
1
+
2
+IFACE="dummy0"
3
+UNIT_TEST="./unit_tests/openvpn/networking_testdriver"
4
+MAX_TEST=${1:-7}
5
+
6
+KILL_EXEC=`which kill`
7
+CC=${CC:-gcc}
8
+
9
+srcdir="${srcdir:-.}"
10
+top_builddir="${top_builddir:-..}"
11
+openvpn="${top_builddir}/src/openvpn/openvpn"
12
+
13
+
14
+# bail out right away on non-linux. NetLink (the object of this test) is only
15
+# used on Linux, therefore testing other platform is not needed.
16
+#
17
+# Note: statements in the rest of the script may not even pass syntax check on
18
+# solaris/bsd. It uses /bin/bash
19
+if [ "$(uname -s)" != "Linux" ]; then
20
+    echo "$0: this test runs only on Linux. SKIPPING TEST."
21
+    exit 77
22
+fi
23
+
24
+# Commands used to retrieve the network state.
25
+# State is retrieved after running sitnl and after running
26
+# iproute commands. The two are then compared and expected to be equal.
27
+typeset -a GET_STATE
28
+GET_STATE[0]="ip link show dev $IFACE | sed 's/^[0-9]\+: //'"
29
+GET_STATE[1]="ip addr show dev $IFACE | sed 's/^[0-9]\+: //'"
30
+GET_STATE[2]="ip route show dev $IFACE"
31
+GET_STATE[3]="ip -6 route show dev $IFACE"
32
+
33
+LAST_STATE=$((${#GET_STATE[@]} - 1))
34
+
35
+reload_dummy()
36
+{
37
+    $RUN_SUDO $openvpn --dev $IFACE --dev-type tun --rmtun >/dev/null
38
+    $RUN_SUDO $openvpn --dev $IFACE --dev-type tun --mktun >/dev/null
39
+    if [ $? -ne 0 ]; then
40
+        echo "can't create interface $IFACE"
41
+        exit 1
42
+    fi
43
+
44
+    #ip link set dev $IFACE address 00:11:22:33:44:55
45
+}
46
+
47
+run_test()
48
+{
49
+    # run all test cases from 0 to $1 in sequence
50
+    CMD=
51
+    for k in $(seq 0 $1); do
52
+        # the unit-test prints to stdout the iproute command corresponding
53
+        # to the sitnl operation being executed.
54
+        # Format is "CMD: <commandhere>"
55
+        OUT=$($RUN_SUDO $UNIT_TEST $k $IFACE)
56
+        # ensure unit test worked properly
57
+        if [ $? -ne 0 ]; then
58
+            echo "unit-test $k errored out:"
59
+            echo "$OUT"
60
+            exit 1
61
+        fi
62
+
63
+        NEW=$(echo "$OUT" | sed -n 's/CMD: //p')
64
+        CMD="$CMD $RUN_SUDO $NEW ;"
65
+    done
66
+
67
+    # collect state for later comparison
68
+    for k in $(seq 0 $LAST_STATE); do
69
+        STATE_TEST[$k]="$(eval ${GET_STATE[$k]})"
70
+    done
71
+}
72
+
73
+
74
+## execution starts here
75
+
76
+if [ -r "${top_builddir}"/t_client.rc ]; then
77
+    . "${top_builddir}"/t_client.rc
78
+elif [ -r "${srcdir}"/t_client.rc ]; then
79
+    . "${srcdir}"/t_client.rc
80
+else
81
+    echo "$0: cannot find 't_client.rc' in build dir ('${top_builddir}')" >&2
82
+    echo "$0: or source directory ('${srcdir}'). SKIPPING TEST." >&2
83
+    exit 77
84
+fi
85
+
86
+if [ ! -x "$openvpn" ]; then
87
+    echo "no (executable) openvpn binary in current build tree. FAIL." >&2
88
+    exit 1
89
+fi
90
+
91
+if [ ! -x "$UNIT_TEST" ]; then
92
+    echo "no test_networking driver available. SKIPPING TEST." >&2
93
+    exit 77
94
+fi
95
+
96
+
97
+# Ensure PREFER_KSU is in a known state
98
+PREFER_KSU="${PREFER_KSU:-0}"
99
+
100
+# make sure we have permissions to run ifconfig/route from OpenVPN
101
+# can't use "id -u" here - doesn't work on Solaris
102
+ID=`id`
103
+if expr "$ID" : "uid=0" >/dev/null
104
+then :
105
+else
106
+    if [ "${PREFER_KSU}" -eq 1 ];
107
+    then
108
+        # Check if we have a valid kerberos ticket
109
+        klist -l 1>/dev/null 2>/dev/null
110
+        if [ $? -ne 0 ];
111
+        then
112
+            # No kerberos ticket found, skip ksu and fallback to RUN_SUDO
113
+            PREFER_KSU=0
114
+            echo "$0: No Kerberos ticket available.  Will not use ksu."
115
+        else
116
+            RUN_SUDO="ksu -q -e"
117
+        fi
118
+    fi
119
+
120
+    if [ -z "$RUN_SUDO" ]
121
+    then
122
+        echo "$0: this test must run be as root, or RUN_SUDO=... " >&2
123
+        echo "      must be set correctly in 't_client.rc'. SKIP." >&2
124
+        exit 77
125
+    else
126
+        # We have to use sudo. Make sure that we (hopefully) do not have
127
+        # to ask the users password during the test. This is done to
128
+        # prevent timing issues, e.g. when the waits for openvpn to start
129
+	    if $RUN_SUDO $KILL_EXEC -0 $$
130
+        then
131
+	        echo "$0: $RUN_SUDO $KILL_EXEC -0 succeeded, good."
132
+        else
133
+	        echo "$0: $RUN_SUDO $KILL_EXEC -0 failed, cannot go on. SKIP." >&2
134
+	        exit 77
135
+        fi
136
+    fi
137
+fi
138
+
139
+for i in $(seq 0 $MAX_TEST); do
140
+    # reload dummy module to cleanup state
141
+    reload_dummy
142
+    typeset -a STATE_TEST
143
+    run_test $i
144
+
145
+    # reload dummy module to cleanup state before running iproute commands
146
+    reload_dummy
147
+
148
+    # CMD has been set by the unit test
149
+    eval $CMD
150
+    if [ $? -ne 0 ]; then
151
+        echo "error while executing:"
152
+        echo "$CMD"
153
+        exit 1
154
+    fi
155
+
156
+    # collect state after running manual ip command
157
+    for k in $(seq 0 $LAST_STATE); do
158
+        STATE_IP[$k]="$(eval ${GET_STATE[$k]})"
159
+    done
160
+
161
+    # ensure states after running unit test matches the one after running
162
+    # manual iproute commands
163
+    for j in $(seq 0 $LAST_STATE); do
164
+        if [ "${STATE_TEST[$j]}" != "${STATE_IP[$j]}" ]; then
165
+            echo "state $j mismatching after '$CMD'"
166
+            echo "after unit-test:"
167
+            echo "${STATE_TEST[$j]}"
168
+            echo "after iproute command:"
169
+            echo "${STATE_IP[$j]}"
170
+            exit 1
171
+        fi
172
+    done
173
+
174
+done
175
+
176
+# remove interface for good
177
+$RUN_SUDO $openvpn --dev $IFACE --dev-type tun --rmtun >/dev/null
178
+
179
+exit 0
... ...
@@ -1,17 +1,22 @@
1 1
 AUTOMAKE_OPTIONS = foreign
2 2
 
3
-check_PROGRAMS=
3
+test_binaries=
4 4
 
5 5
 if HAVE_LD_WRAP_SUPPORT
6
-check_PROGRAMS += argv_testdriver buffer_testdriver
6
+test_binaries += argv_testdriver buffer_testdriver
7 7
 endif
8 8
 
9
-check_PROGRAMS += crypto_testdriver packet_id_testdriver
9
+test_binaries += crypto_testdriver packet_id_testdriver
10 10
 if HAVE_LD_WRAP_SUPPORT
11
-check_PROGRAMS += tls_crypt_testdriver
11
+test_binaries += tls_crypt_testdriver
12 12
 endif
13 13
 
14
-TESTS = $(check_PROGRAMS)
14
+TESTS = $(test_binaries)
15
+check_PROGRAMS = $(test_binaries)
16
+
17
+if TARGET_LINUX
18
+check_PROGRAMS += networking_testdriver
19
+endif
15 20
 
16 21
 openvpn_includedir = $(top_srcdir)/include
17 22
 openvpn_srcdir = $(top_srcdir)/src/openvpn
... ...
@@ -72,3 +77,18 @@ tls_crypt_testdriver_SOURCES = test_tls_crypt.c mock_msg.c mock_msg.h \
72 72
 	$(openvpn_srcdir)/packet_id.c \
73 73
 	$(openvpn_srcdir)/platform.c \
74 74
 	$(openvpn_srcdir)/run_command.c
75
+
76
+networking_testdriver_CFLAGS = @TEST_CFLAGS@ \
77
+	-I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) \
78
+	$(OPTIONAL_CRYPTO_CFLAGS)
79
+networking_testdriver_LDFLAGS = @TEST_LDFLAGS@ -L$(openvpn_srcdir) \
80
+	$(OPTIONAL_CRYPTO_LIBS)
81
+networking_testdriver_SOURCES = test_networking.c mock_msg.c \
82
+	$(openvpn_srcdir)/networking_sitnl.c \
83
+	$(openvpn_srcdir)/buffer.c \
84
+	$(openvpn_srcdir)/crypto.c \
85
+	$(openvpn_srcdir)/crypto_mbedtls.c \
86
+	$(openvpn_srcdir)/crypto_openssl.c \
87
+	$(openvpn_srcdir)/otime.c \
88
+	$(openvpn_srcdir)/packet_id.c \
89
+	$(openvpn_srcdir)/platform.c
75 90
new file mode 100644
... ...
@@ -0,0 +1,229 @@
0
+#include "config.h"
1
+#include "syshead.h"
2
+#include "networking.h"
3
+
4
+#include "mock_msg.h"
5
+
6
+
7
+static char *iface = "dummy0";
8
+
9
+#ifdef ENABLE_SITNL
10
+
11
+static int
12
+net__iface_up(bool up)
13
+{
14
+    printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");
15
+
16
+    return net_iface_up(NULL, iface, up);
17
+}
18
+
19
+static int
20
+net__iface_mtu_set(int mtu)
21
+{
22
+    printf("CMD: ip link set %s mtu %d\n", iface, mtu);
23
+
24
+    return net_iface_mtu_set(NULL, iface, mtu);
25
+}
26
+
27
+static int
28
+net__addr_v4_add(const char *addr_str, int prefixlen, const char *brd_str)
29
+{
30
+    in_addr_t addr, brd;
31
+    int ret;
32
+
33
+    ret = inet_pton(AF_INET, addr_str, &addr);
34
+    if (ret != 1)
35
+        return -1;
36
+
37
+    ret = inet_pton(AF_INET, brd_str, &brd);
38
+    if (ret != 1)
39
+        return -1;
40
+
41
+    addr = ntohl(addr);
42
+    brd = ntohl(brd);
43
+
44
+    printf("CMD: ip addr add %s/%d brd %s dev %s\n", addr_str, prefixlen,
45
+           brd_str, iface);
46
+
47
+    return net_addr_v4_add(NULL, iface, &addr, prefixlen, &brd);
48
+}
49
+
50
+static int
51
+net__addr_v6_add(const char *addr_str, int prefixlen)
52
+{
53
+    struct in6_addr addr;
54
+    int ret;
55
+
56
+    ret = inet_pton(AF_INET6, addr_str, &addr);
57
+    if (ret != 1)
58
+        return -1;
59
+
60
+    printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
61
+
62
+    return net_addr_v6_add(NULL, iface, &addr, prefixlen);
63
+}
64
+
65
+static int
66
+net__route_v4_add(const char *dst_str, int prefixlen, int metric)
67
+{
68
+    in_addr_t dst;
69
+    int ret;
70
+
71
+    if (!dst_str)
72
+        return -1;
73
+
74
+    ret = inet_pton(AF_INET, dst_str, &dst);
75
+    if (ret != 1)
76
+        return -1;
77
+
78
+    dst = ntohl(dst);
79
+
80
+    printf("CMD: ip route add %s/%d dev %s", dst_str, prefixlen, iface);
81
+    if (metric > 0)
82
+        printf(" metric %d", metric);
83
+    printf("\n");
84
+
85
+    return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
86
+
87
+}
88
+
89
+static int
90
+net__route_v4_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
91
+                     int metric)
92
+{
93
+    in_addr_t dst, gw;
94
+    int ret;
95
+
96
+    if (!dst_str || !gw_str)
97
+        return -1;
98
+
99
+    ret = inet_pton(AF_INET, dst_str, &dst);
100
+    if (ret != 1)
101
+        return -1;
102
+
103
+    ret = inet_pton(AF_INET, gw_str, &gw);
104
+    if (ret != 1)
105
+        return -1;
106
+
107
+    dst = ntohl(dst);
108
+    gw = ntohl(gw);
109
+
110
+    printf("CMD: ip route add %s/%d dev %s via %s", dst_str, prefixlen, iface,
111
+           gw_str);
112
+    if (metric > 0)
113
+        printf(" metric %d", metric);
114
+    printf("\n");
115
+
116
+    return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
117
+}
118
+
119
+static int
120
+net__route_v6_add(const char *dst_str, int prefixlen, int metric)
121
+{
122
+    struct in6_addr dst;
123
+    int ret;
124
+
125
+    if (!dst_str)
126
+        return -1;
127
+
128
+    ret = inet_pton(AF_INET6, dst_str, &dst);
129
+    if (ret != 1)
130
+        return -1;
131
+
132
+    printf("CMD: ip -6 route add %s/%d dev %s", dst_str, prefixlen, iface);
133
+    if (metric > 0)
134
+        printf(" metric %d", metric);
135
+    printf("\n");
136
+
137
+    return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
138
+
139
+}
140
+
141
+static int
142
+net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str,
143
+                     int metric)
144
+{
145
+    struct in6_addr dst, gw;
146
+    int ret;
147
+
148
+    if (!dst_str || !gw_str)
149
+        return -1;
150
+
151
+    ret = inet_pton(AF_INET6, dst_str, &dst);
152
+    if (ret != 1)
153
+        return -1;
154
+
155
+    ret = inet_pton(AF_INET6, gw_str, &gw);
156
+    if (ret != 1)
157
+        return -1;
158
+
159
+    printf("CMD: ip -6 route add %s/%d dev %s via %s", dst_str, prefixlen,
160
+           iface, gw_str);
161
+    if (metric > 0)
162
+        printf(" metric %d", metric);
163
+    printf("\n");
164
+
165
+    return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
166
+}
167
+
168
+static void
169
+usage(char *name)
170
+{
171
+    printf("Usage: %s <0-7>\n", name);
172
+}
173
+
174
+int
175
+main(int argc, char *argv[])
176
+{
177
+    int test;
178
+
179
+    mock_set_debug_level(10);
180
+
181
+    if (argc < 2)
182
+    {
183
+        usage(argv[0]);
184
+        return -1;
185
+    }
186
+
187
+    if (argc > 3)
188
+    {
189
+        iface = argv[2];
190
+    }
191
+
192
+    test = atoi(argv[1]);
193
+    switch (test)
194
+    {
195
+        case 0:
196
+            return net__iface_up(true);
197
+        case 1:
198
+            return net__iface_mtu_set(1281);
199
+        case 2:
200
+            return net__addr_v4_add("10.255.255.1", 24, "10.255.255.255");
201
+        case 3:
202
+            return net__addr_v6_add("2001::1", 64);
203
+        case 4:
204
+            return net__route_v4_add("11.11.11.0", 24, 0);
205
+        case 5:
206
+            return net__route_v4_add_gw("11.11.12.0", 24, "10.255.255.2", 0);
207
+        case 6:
208
+            return net__route_v6_add("2001:babe:cafe:babe::", 64, 600);
209
+        case 7:
210
+            return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600);
211
+        default:
212
+            printf("invalid test: %d\n", test);
213
+            break;
214
+    }
215
+
216
+    usage(argv[0]);
217
+    return -1;
218
+}
219
+
220
+#else
221
+
222
+int
223
+main(int argc, char *argv[])
224
+{
225
+    return 0;
226
+}
227
+
228
+#endif /* ENABLE_SITNL */