Add cmocka packages to CI jobs
[tinc] / test / compression.test
1 #!/bin/sh
2
3 # shellcheck disable=SC1090
4 . "$TESTLIB_PATH"
5
6 require_root "$0" "$@"
7 test -e /dev/net/tun || exit "$EXIT_SKIP_TEST"
8 ip netns list || exit "$EXIT_SKIP_TEST"
9 command -v socat || exit "$EXIT_SKIP_TEST"
10
11 ip_foo=192.168.1.1
12 ip_bar=192.168.1.2
13 port_foo=30100
14 recv_port_foo=30101
15 mask=24
16
17 echo '[STEP] Determining supported compression levels'
18
19 features=$(tincd foo --version)
20 bogus_levels="-1 13"
21 levels=0
22
23 add_levels() {
24   algo=$1
25   shift
26
27   if echo "$features" | grep "comp_$algo"; then
28     levels="$levels $*"
29   else
30     bogus_levels="$bogus_levels $*"
31   fi
32 }
33
34 add_levels zlib 1 2 3 4 5 6 7 8 9
35 add_levels lzo 10 11
36 add_levels lz4 12
37
38 echo "Supported compression levels: $levels"
39 echo "Unsupported compression levels: $bogus_levels"
40
41 echo [STEP] Create network namespaces
42
43 ip netns add foo
44 ip netns add bar
45 tmp_file=$(mktemp)
46
47 cleanup_hook() {
48   ip netns del foo
49   ip netns del bar
50   rm -f "$tmp_file"
51 }
52
53 echo [STEP] Initialize two nodes
54
55 tinc foo <<EOF
56 init foo
57 set Subnet $ip_foo
58 set Interface foo
59 set Port $port_foo
60 set Address localhost
61 EOF
62
63 tinc bar <<EOF
64 init bar
65 set Subnet $ip_bar
66 set Interface bar
67 set ConnectTo foo
68 EOF
69
70 # shellcheck disable=SC2016
71 create_script foo tinc-up "
72   ip link set dev \$INTERFACE netns foo
73   ip netns exec foo ip addr add $ip_foo/$mask dev \$INTERFACE
74   ip netns exec foo ip link set \$INTERFACE up
75 "
76
77 # shellcheck disable=SC2016
78 create_script bar tinc-up "
79   ip link set dev \$INTERFACE netns bar
80   ip netns exec bar ip addr add $ip_bar/$mask dev \$INTERFACE
81   ip netns exec bar ip link set \$INTERFACE up
82 "
83
84 echo [STEP] Exchange configuration files
85
86 tinc foo export | tinc bar exchange | tinc foo import
87
88 echo [STEP] Test supported compression levels
89
90 ref_file=$0
91
92 create_script foo hosts/bar-up
93 create_script bar hosts/foo-up
94
95 for level in $levels; do
96   echo "[STEP] Testing compression level $level"
97
98   tinc foo set Compression "$level"
99   tinc bar set Compression "$level"
100
101   start_tinc foo
102   wait_script foo tinc-up
103
104   start_tinc bar
105   wait_script bar tinc-up
106
107   wait_script foo hosts/bar-up
108   wait_script bar hosts/foo-up
109
110   sh <<EOF
111     set -eu
112     ip netns exec foo socat -u TCP4-LISTEN:$recv_port_foo,reuseaddr OPEN:"$tmp_file",creat &
113     ip netns exec bar socat -u OPEN:"$ref_file" TCP4:$ip_foo:$recv_port_foo,retry=30 &
114     wait
115 EOF
116
117   diff -w "$ref_file" "$tmp_file"
118
119   tinc foo stop
120   tinc bar stop
121 done
122
123 echo '[STEP] Invalid compression levels should fail'
124
125 for level in $bogus_levels; do
126   echo "[STEP] Testing bogus compression level $level"
127   tinc foo set Compression "$level"
128
129   output=$(expect_code "$EXIT_FAILURE" start_tinc foo 2>&1)
130
131   if ! echo "$output" | grep -q 'Bogus compression level'; then
132     bail 'expected message about the wrong compression level'
133   fi
134 done