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