Fix running the test suite in out-of-tree builds.
[tinc] / test / ns-ping.test
1 #!/bin/sh
2
3 # shellcheck source=testlib.sh
4 . ./testlib.sh
5
6 echo "[STEP] Skip this test if we aren't root or if 'ip netns' does not exist"
7
8 test "$(id -u)" = "0" || exit $EXIT_SKIP_TEST
9 test -e /dev/net/tun || exit $EXIT_SKIP_TEST
10 ip netns list || exit $EXIT_SKIP_TEST
11
12 ip_foo=192.168.1.1
13 ip_bar=192.168.1.2
14 mask=24
15
16 echo [STEP] Initialize two nodes
17
18 tinc foo <<EOF
19 init foo
20 set Subnet $ip_foo
21 set Interface ping.test1
22 set Port 30090
23 set Address localhost
24 set AutoConnect no
25 EOF
26
27 # shellcheck disable=SC2016
28 create_script foo tinc-up "
29   ip netns add ping.test1
30   ip link set dev \$INTERFACE netns ping.test1
31   ip netns exec ping.test1 ip addr add $ip_foo/$mask dev \$INTERFACE
32   ip netns exec ping.test1 ip link set \$INTERFACE up
33 "
34
35 tinc bar <<EOF
36 init bar
37 set Subnet $ip_bar
38 set Interface ping.test2
39 set Port 30091
40 set AutoConnect no
41 EOF
42
43 # shellcheck disable=SC2016
44 create_script bar tinc-up "
45   ip netns add ping.test2
46   ip link set dev \$INTERFACE netns ping.test2
47   ip netns exec ping.test2 ip addr add $ip_bar/$mask dev \$INTERFACE
48   ip netns exec ping.test2 ip link set \$INTERFACE up
49 "
50
51 echo [STEP] Exchange configuration files
52
53 tinc foo export | tinc bar exchange | tinc foo import
54
55 echo [STEP] Start tinc
56
57 start_tinc foo
58 start_tinc bar
59
60 wait_script foo tinc-up
61 wait_script bar tinc-up
62
63 echo [STEP] The nodes should not be able to ping each other if there is no connection
64
65 must_fail ip netns exec ping.test1 ping -W1 -c3 $ip_bar
66
67 echo [STEP] After connecting they should be
68
69 create_script bar hosts/foo-up
70
71 tinc bar add ConnectTo foo
72 wait_script bar hosts/foo-up
73
74 ip netns exec ping.test1 ping -W1 -c3 $ip_bar
75
76 echo [STEP] Clean up
77
78 ip netns del ping.test2
79 ip netns del ping.test1