]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
test: shell: Add wait_local_port_listen() helper to lib.sh
authorYi Chen <yiche@redhat.com>
Sun, 22 Jun 2025 12:55:53 +0000 (20:55 +0800)
committerFlorian Westphal <fw@strlen.de>
Sun, 22 Jun 2025 13:28:28 +0000 (15:28 +0200)
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 <yiche@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
tests/shell/helpers/lib.sh
tests/shell/testcases/packetpath/flowtables
tests/shell/testcases/packetpath/nat_ftp

index d2d209848e877737ff109d9606fde430e829a328..ce27aac35a22668ceffbcfcb20587d87aad7d97f 100755 (executable)
@@ -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
+}
index f3580a5fb9f0bcd65bc5dcdc4417ff480bcf7f32..dbe470a80d78c6e1d258fec3483f8e69b6ececb1 100755 (executable)
@@ -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"
index 2a80c43f6fc53abbb25703e2c904a5e7afdc8cfe..c2fb3a1c8ebcd00ba76b87b223b264d01bf2500a 100755 (executable)
@@ -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 "