Test two tinc daemons using network namespaces.
authorGuus Sliepen <guus@tinc-vpn.org>
Fri, 24 Jan 2014 15:09:32 +0000 (16:09 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Fri, 24 Jan 2014 15:17:09 +0000 (16:17 +0100)
Testing multiple daemons connecting to each other on the same computer is
usually difficult, because connections to local IP addresses will bypass most
of the network stack. However, recent versions of Linux support network
namespaces, which can isolate network interfaces. We use this to isolate the
virtual interface of the daemons from each other, so we get the behaviour as if
the daemons were each running on their own machine. This can also be used for
more complicated tests (including those with firewall rules) without disturbing
the real network setup of the host computer.

test/Makefile.am
test/ns-ping.test [new file with mode: 0755]

index a9c3895..5457b2f 100644 (file)
@@ -4,6 +4,7 @@ TESTS = \
        executables.test \
        import-export.test \
        invite-join.test \
+       ns-ping.test \
        ping.test \
        sptps-basic.test \
        variables.test
diff --git a/test/ns-ping.test b/test/ns-ping.test
new file mode 100755 (executable)
index 0000000..d5f9c7f
--- /dev/null
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+. ./testlib.sh
+
+# Skip this test if we aren't root or if "ip netns" does not exist
+
+test "`id -u`" = "0" || exit 77
+ip netns list || exit 77
+
+# Initialize two nodes
+
+$tinc $c1 <<EOF
+init foo
+set Mode switch
+set Interface ping.test1
+set Port 32573
+set Address localhost
+EOF
+
+cat >$d1/tinc-up <<EOF
+#!/bin/sh
+ip netns add ping.test1
+ip link set dev \$INTERFACE netns ping.test1
+ip netns exec ping.test1 ip addr add 192.168.1.1/24 dev \$INTERFACE
+ip netns exec ping.test1 ip link set \$INTERFACE up
+EOF
+
+$tinc $c2 <<EOF
+init bar
+set Mode switch
+set Interface ping.test2
+set Port 32574
+EOF
+
+cat >$d2/tinc-up <<EOF
+#!/bin/sh
+ip netns add ping.test2
+ip link set dev \$INTERFACE netns ping.test2
+ip netns exec ping.test2 ip addr add 192.168.1.2/24 dev \$INTERFACE
+ip netns exec ping.test2 ip link set \$INTERFACE up
+EOF
+
+# Exchange configuration files
+
+$tinc $c1 export | $tinc $c2 exchange | $tinc $c1 import
+
+# Start tinc
+
+$tinc $c1 start $r1
+$tinc $c2 start $r2
+
+sleep 1
+
+# The nodes should not be able to ping each other if there is no connection
+
+ip netns exec ping.test1 ping -W1 -c3 192.168.1.2 && exit 1
+
+# After connecting they should be
+
+$tinc $c2 add ConnectTo foo
+sleep 1
+
+ip netns exec ping.test1 ping -W1 -c3 192.168.1.2
+
+# Clean up
+
+$tinc $c2 stop
+$tinc $c1 stop
+ip netns del ping.test2
+ip netns del ping.test1