From: Yi Chen Date: Sun, 22 Jun 2025 12:55:53 +0000 (+0800) Subject: test: shell: Add wait_local_port_listen() helper to lib.sh X-Git-Tag: v1.1.4~55 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=fe51e4e2dfece307f19ae88155d1ad836bc43317;p=thirdparty%2Fnftables.git test: shell: Add wait_local_port_listen() helper to lib.sh Introduce a new helper function wait_local_port_listen() in helpers/lib.sh. Update the flowtables and nat_ftp test cases to use this helper. Signed-off-by: Yi Chen Signed-off-by: Florian Westphal --- diff --git a/tests/shell/helpers/lib.sh b/tests/shell/helpers/lib.sh index d2d20984..ce27aac3 100755 --- a/tests/shell/helpers/lib.sh +++ b/tests/shell/helpers/lib.sh @@ -28,3 +28,24 @@ assert_fail() echo "PASS: ${@}" fi } + +wait_local_port_listen() +{ + local listener_ns="${1}" + local port="${2}" + local protocol="${3}" + local pattern + local i + + pattern=":$(printf "%04X" "${port}") " + + # for tcp protocol additionally check the socket state + [ ${protocol} = "tcp" ] && pattern="${pattern}0A" + for i in $(seq 10); do + if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \ + /proc/net/"${protocol}"* | grep -q "${pattern}"; then + break + fi + sleep 0.1 + done +} diff --git a/tests/shell/testcases/packetpath/flowtables b/tests/shell/testcases/packetpath/flowtables index f3580a5f..dbe470a8 100755 --- a/tests/shell/testcases/packetpath/flowtables +++ b/tests/shell/testcases/packetpath/flowtables @@ -83,7 +83,8 @@ assert_pass "set net.netfilter.nf_conntrack_tcp_timeout_established=86400" # A trick to control the timing to send a packet ip netns exec $S socat TCP6-LISTEN:10001 GOPEN:/tmp/socat-$rnd,ignoreeof & -sleep 1 +wait_local_port_listen $S 10001 tcp + ip netns exec $C socat -b 2048 PIPE:/tmp/pipefile-$rnd 'TCP:[2001:db8:ffff:22::1]:10001' & sleep 1 ip netns exec $C echo "send sth" >> /tmp/pipefile-$rnd ; assert_pass "send a packet" diff --git a/tests/shell/testcases/packetpath/nat_ftp b/tests/shell/testcases/packetpath/nat_ftp index 2a80c43f..c2fb3a1c 100755 --- a/tests/shell/testcases/packetpath/nat_ftp +++ b/tests/shell/testcases/packetpath/nat_ftp @@ -133,7 +133,8 @@ pam_service_name=vsftpd background=YES EOF ip netns exec $S vsftpd ${FTPCONF} -sleep 1 +wait_local_port_listen $S 21 tcp + ip netns exec $S ss -6ltnp | grep -q '*:21' assert_pass "start vsftpd server" @@ -142,7 +143,7 @@ assert_pass "start vsftpd server" reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! -sleep 1 +sleep 0.5 ip netns exec $C curl --no-progress-meter --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp passive mode " @@ -159,6 +160,7 @@ reload_ruleset ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$! +sleep 0.5 ip netns exec $C curl --no-progress-meter -P - --connect-timeout 5 ftp://[${ip_rc}]:2121/$(basename $INFILE) -o $OUTFILE assert_pass "curl ftp active mode "