Skip to contentSkip to conversation

wireguard: selftests: refactor the test structure

PATCH In review Hangbin Liu 2 participants 3 messages Nov 16, 2021 – Nov 25, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wireguard: selftests: refactor the test structure

The current wireguard selftest mixed all tests together, which makes it very hard to read. Refactor the test and split it roughly to basic, nat, saddr and regression tests to make it more clear.

Move the common functions to a new file lib.sh and rename the main test to wg_test.sh

Also, fix some config issues during re-organize. e.g.
- update basic topo in lib.sh
- Fix the IPv6 prefix to 112 in all topology
- add 127.0.0.2 on lo for saddr test as ncat need an IP address to bind

---
 tools/testing/selftests/wireguard/lib.sh     | 149 +++++
 tools/testing/selftests/wireguard/netns.sh   | 662 -------------------
 tools/testing/selftests/wireguard/wg_test.sh | 621 +++++++++++++++++
 3 files changed, 770 insertions(+), 662 deletions(-)
 create mode 100644 tools/testing/selftests/wireguard/lib.sh
 delete mode 100755 tools/testing/selftests/wireguard/netns.sh
 create mode 100755 tools/testing/selftests/wireguard/wg_test.sh
tools/testing/selftests/wireguard/lib.sh new file +149 -0
new file mode 100644
index 000000000000..2621a16c821b
@@ -0,0 +1,149 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+
+exec 3>&1
+export LANG=C
+export WG_HIDE_KEYS=never
+has_basic_topo=false
+netns0="wg-test-$$-0"
+netns1="wg-test-$$-1"
+netns2="wg-test-$$-2"
+pretty() { echo -e "\x1b[32m\x1b[1m[+] ${1:+NS$1: }${2}\x1b[0m" >&3; }
+pp() { pretty "" "$*"; "$@"; }
+maybe_exec() { if [[ $BASHPID -eq $$ ]]; then "$@"; else exec "$@"; fi; }
+n0() { pretty 0 "$*"; maybe_exec ip netns exec $netns0 "$@"; }
+n1() { pretty 1 "$*"; maybe_exec ip netns exec $netns1 "$@"; }
+n2() { pretty 2 "$*"; maybe_exec ip netns exec $netns2 "$@"; }
+ip0() { pretty 0 "ip $*"; ip -n $netns0 "$@"; }
+ip1() { pretty 1 "ip $*"; ip -n $netns1 "$@"; }
+ip2() { pretty 2 "ip $*"; ip -n $netns2 "$@"; }
+sleep() { read -t "$1" -N 1 || true; }
+waitiperf() { pretty "${1//*-}" "wait for iperf:${3:-5201} pid $2"; while [[ $(ss -N "$1" -tlpH "sport = ${3:-5201}") != *\"iperf3\",pid=$2,fd=* ]]; do sleep 0.1; done; }
+waitncatudp() { pretty "${1//*-}" "wait for udp:1111 pid $2"; while [[ $(ss -N "$1" -ulpH 'sport = 1111') != *\"ncat\",pid=$2,fd=* ]]; do sleep 0.1; done; }
+waitiface() { pretty "${1//*-}" "wait for $2 to come up"; ip netns exec "$1" bash -c "while [[ \$(< \"/sys/class/net/$2/operstate\") != up ]]; do read -t .1 -N 0 || true; done;"; }
+orig_message_cost="$(< /proc/sys/net/core/message_cost)"
+
+cleanup() {
+	set +e
+	exec 2>/dev/null
+	printf "$orig_message_cost" > /proc/sys/net/core/message_cost
+	ip0 link del dev wg0
+	ip0 link del dev wg1
+	ip1 link del dev wg0
+	ip1 link del dev wg1
+	ip2 link del dev wg0
+	ip2 link del dev wg1
+	local to_kill="$(ip netns pids $netns0) $(ip netns pids $netns1) $(ip netns pids $netns2)"
+	[[ -n $to_kill ]] && kill $to_kill
+	pp ip netns del $netns1
+	pp ip netns del $netns2
+	pp ip netns del $netns0
+	exit
+}
+
+# The follow functions setup the below topology:
+#
+# ┌────────────────────┐   ┌────────────────────┐   ┌────────────────────┐
+# │   $ns1 namespace   │   │   $ns0 namespace   │   │   $ns2 namespace   │
+# │                    │   │                    │   │                    │
+# │     ┌────────┐     │   │     ┌────────┐     │   │     ┌────────┐     │
+# │     │  wg0   │     │   │     │   lo   │     │   │     │  wg0   │     │
+# │┌────┴────────┴────┐│   │     └────────┘     │   │┌────┴────────┴────┐│
+# ││192.168.241.1/24  ││   │                    │   ││192.168.241.2/24  ││
+# ││fd00::1/112       ││   │                    │   ││fd00::2/112       ││
+# │└──────────────────┘│   │                    │   │└──────────────────┘│
+# └────────────────────┘   └────────────────────┘   └────────────────────┘
+
+configure_peers() {
+	ip1 addr add 192.168.241.1/24 dev wg0
+	ip1 addr add fd00::1/112 dev wg0
+
+	ip2 addr add 192.168.241.2/24 dev wg0
+	ip2 addr add fd00::2/112 dev wg0
+
+	n1 wg set wg0 \
+		private-key <(echo "$key1") \
+		listen-port 1 \
+		peer "$pub2" \
+			preshared-key <(echo "$psk") \
+			allowed-ips 192.168.241.2/32,fd00::2/128
+	n2 wg set wg0 \
+		private-key <(echo "$key2") \
+		listen-port 2 \
+		peer "$pub1" \
+			preshared-key <(echo "$psk") \
+			allowed-ips 192.168.241.1/32,fd00::1/128
+
+	ip1 link set up dev wg0
+	ip2 link set up dev wg0
+}
+
+setup_basic_topo()
+{
+	printf 0 > /proc/sys/net/core/message_cost
+
+	ip netns del $netns0 2>/dev/null || true
+	ip netns del $netns1 2>/dev/null || true
+	ip netns del $netns2 2>/dev/null || true
+	pp ip netns add $netns0
+	pp ip netns add $netns1
+	pp ip netns add $netns2
+	ip0 link set up dev lo
+
+	ip1 link add dev wg0 type wireguard
+	ip2 link add dev wg0 type wireguard
+	key1="$(pp wg genkey)"
+	key2="$(pp wg genkey)"
+	key3="$(pp wg genkey)"
+	key4="$(pp wg genkey)"
+	pub1="$(pp wg pubkey <<<"$key1")"
+	pub2="$(pp wg pubkey <<<"$key2")"
+	pub3="$(pp wg pubkey <<<"$key3")"
+	pub4="$(pp wg pubkey <<<"$key4")"
+	psk="$(pp wg genpsk)"
+	[[ -n $key1 && -n $key2 && -n $psk ]]
+}
+
+do_tests() {
+	# Ping over IPv4
+	n2 ping -c 10 -f -W 1 192.168.241.1
+	n1 ping -c 10 -f -W 1 192.168.241.2
+
+	# Ping over IPv6
+	n2 ping6 -c 10 -f -W 1 fd00::1
+	n1 ping6 -c 10 -f -W 1 fd00::2
+
+	# TCP over IPv4
+	n2 iperf3 -s -1 -B 192.168.241.2 &
+	waitiperf $netns2 $!
+	n1 iperf3 -Z -t 3 -c 192.168.241.2
+
+	# TCP over IPv6
+	n1 iperf3 -s -1 -B fd00::1 &
+	waitiperf $netns1 $!
+	n2 iperf3 -Z -t 3 -c fd00::1
+
+	# UDP over IPv4
+	n1 iperf3 -s -1 -B 192.168.241.1 &
+	waitiperf $netns1 $!
+	n2 iperf3 -Z -t 3 -b 0 -u -c 192.168.241.1
+
+	# UDP over IPv6
+	n2 iperf3 -s -1 -B fd00::2 &
+	waitiperf $netns2 $!
+	n1 iperf3 -Z -t 3 -b 0 -u -c fd00::2
+
+	# TCP over IPv4, in parallel
+	for max in 4 5 50; do
+		local pids=( )
+		for ((i=0; i < max; ++i)) do
+			n2 iperf3 -p $(( 5200 + i )) -s -1 -B 192.168.241.2 &
+			pids+=( $! ); waitiperf $netns2 $! $(( 5200 + i ))
+		done
+		for ((i=0; i < max; ++i)) do
+			n1 iperf3 -Z -t 3 -p $(( 5200 + i )) -c 192.168.241.2 &
+		done
+		wait "${pids[@]}"
+	done
+}
tools/testing/selftests/wireguard/netns.sh deleted +0 -662

662 lines not on this page · raw message

tools/testing/selftests/wireguard/wg_test.sh new file +621 -0

621 lines not on this page · raw message

Signature · 1 line
2.31.1
#1 lore raw

Hi Hangbin,

I don't know how interested in this I am. Splitting things into two files means more confusing maintenance, and categorizing sections strictly into functions means there's more overhead when adding tests (e.g. "where do they fit?"), because the categories you've chosen are fairly broad, rather than being functions for each specific test. I'd be more amenable to something _entirely_ granular, because that'd be consistent, or what we have now, which is just linear. Full granularity, though, has its own downsides, of increased clutter. Alternatively, if you'd like to add some comments around the different areas to better document what's happening, perhaps that'd accomplish the same thing as this patch.

Jason

#2 lore raw
Quoted 14 lines · Jason A. Donenfeld

Hi Hangbin,

I don't know how interested in this I am. Splitting things into two files means more confusing maintenance, and categorizing sections strictly into functions means there's more overhead when adding tests (e.g. "where do they fit?"), because the categories you've chosen are fairly broad, rather than being functions for each specific test. I'd be more amenable to something _entirely_ granular, because that'd be consistent, or what we have now, which is just linear. Full granularity, though, has its own downsides, of increased clutter. Alternatively, if you'd like to add some comments around the different areas to better document what's happening, perhaps that'd accomplish the same thing as this patch.

Hi Jason,

May be my timezone is not very fit for yours. So I will copy my IRC replies in the mail to moving on our kselftest topic.

The reason I did this patch is because I want to make the test more clear and able to run each test case separately. My though is to make the wireguard test looks like tools/testing/selftests/net/fib_tests.sh.(Of course this could be discussed).

Because the linear structure makes reader hard to find out what test it does. The function name in my current patch is also a little broad to look, which could to be updated. After updating, I'd like to make the test has 2 parts, functional tests and regression test. Functional tests for big part of function tests and regression test for small specific issues.

BTW, one downside about current linear structure I think is that when someone want to add a new test, he need to read through the whole test to know that kind of topology at last. But with function structure, when we want to add a new test. We can just do like:
1. set up basic topology
2. configure to specific topo for testing, or just skip the first step and configure to specific topo directly.
3. Do test
4. Clean up environment or reset to basic topology

I think this would make adding new test case easier. What do you think?

Thanks
Hangbin