| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | From: "Jason A. Donenfeld" <Jason@zx2c4.com> |
| 3 | Date: Tue, 4 Feb 2020 22:17:29 +0100 |
| 4 | Subject: [PATCH] wireguard: selftests: tie socket waiting to target pid |
| 5 | |
| 6 | commit 88f404a9b1d75388225b1c67b6dd327cb2182777 upstream. |
| 7 | |
| 8 | Without this, we wind up proceeding too early sometimes when the |
| 9 | previous process has just used the same listening port. So, we tie the |
| 10 | listening socket query to the specific pid we're interested in. |
| 11 | |
| 12 | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> |
| 13 | Signed-off-by: David S. Miller <davem@davemloft.net> |
| 14 | Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> |
| 15 | --- |
| 16 | tools/testing/selftests/wireguard/netns.sh | 17 ++++++++--------- |
| 17 | 1 file changed, 8 insertions(+), 9 deletions(-) |
| 18 | |
| 19 | --- a/tools/testing/selftests/wireguard/netns.sh |
| 20 | +++ b/tools/testing/selftests/wireguard/netns.sh |
| 21 | @@ -38,9 +38,8 @@ ip0() { pretty 0 "ip $*"; ip -n $netns0 |
| 22 | ip1() { pretty 1 "ip $*"; ip -n $netns1 "$@"; } |
| 23 | ip2() { pretty 2 "ip $*"; ip -n $netns2 "$@"; } |
| 24 | sleep() { read -t "$1" -N 1 || true; } |
| 25 | -waitiperf() { pretty "${1//*-}" "wait for iperf:5201"; while [[ $(ss -N "$1" -tlp 'sport = 5201') != *iperf3* ]]; do sleep 0.1; done; } |
| 26 | -waitncatudp() { pretty "${1//*-}" "wait for udp:1111"; while [[ $(ss -N "$1" -ulp 'sport = 1111') != *ncat* ]]; do sleep 0.1; done; } |
| 27 | -waitncattcp() { pretty "${1//*-}" "wait for tcp:1111"; while [[ $(ss -N "$1" -tlp 'sport = 1111') != *ncat* ]]; do sleep 0.1; done; } |
| 28 | +waitiperf() { pretty "${1//*-}" "wait for iperf:5201 pid $2"; while [[ $(ss -N "$1" -tlpH 'sport = 5201') != *\"iperf3\",pid=$2,fd=* ]]; do sleep 0.1; done; } |
| 29 | +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; } |
| 30 | 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;"; } |
| 31 | |
| 32 | cleanup() { |
| 33 | @@ -119,22 +118,22 @@ tests() { |
| 34 | |
| 35 | # TCP over IPv4 |
| 36 | n2 iperf3 -s -1 -B 192.168.241.2 & |
| 37 | - waitiperf $netns2 |
| 38 | + waitiperf $netns2 $! |
| 39 | n1 iperf3 -Z -t 3 -c 192.168.241.2 |
| 40 | |
| 41 | # TCP over IPv6 |
| 42 | n1 iperf3 -s -1 -B fd00::1 & |
| 43 | - waitiperf $netns1 |
| 44 | + waitiperf $netns1 $! |
| 45 | n2 iperf3 -Z -t 3 -c fd00::1 |
| 46 | |
| 47 | # UDP over IPv4 |
| 48 | n1 iperf3 -s -1 -B 192.168.241.1 & |
| 49 | - waitiperf $netns1 |
| 50 | + waitiperf $netns1 $! |
| 51 | n2 iperf3 -Z -t 3 -b 0 -u -c 192.168.241.1 |
| 52 | |
| 53 | # UDP over IPv6 |
| 54 | n2 iperf3 -s -1 -B fd00::2 & |
| 55 | - waitiperf $netns2 |
| 56 | + waitiperf $netns2 $! |
| 57 | n1 iperf3 -Z -t 3 -b 0 -u -c fd00::2 |
| 58 | } |
| 59 | |
| 60 | @@ -207,7 +206,7 @@ n1 ping -W 1 -c 1 192.168.241.2 |
| 61 | n1 wg set wg0 peer "$pub2" allowed-ips 192.168.241.0/24 |
| 62 | exec 4< <(n1 ncat -l -u -p 1111) |
| 63 | ncat_pid=$! |
| 64 | -waitncatudp $netns1 |
| 65 | +waitncatudp $netns1 $ncat_pid |
| 66 | n2 ncat -u 192.168.241.1 1111 <<<"X" |
| 67 | read -r -N 1 -t 1 out <&4 && [[ $out == "X" ]] |
| 68 | kill $ncat_pid |
| 69 | @@ -216,7 +215,7 @@ n1 wg set wg0 peer "$more_specific_key" |
| 70 | n2 wg set wg0 listen-port 9997 |
| 71 | exec 4< <(n1 ncat -l -u -p 1111) |
| 72 | ncat_pid=$! |
| 73 | -waitncatudp $netns1 |
| 74 | +waitncatudp $netns1 $ncat_pid |
| 75 | n2 ncat -u 192.168.241.1 1111 <<<"X" |
| 76 | ! read -r -N 1 -t 1 out <&4 || false |
| 77 | kill $ncat_pid |