Skip to contentSkip to conversation

test: shell: Don't use system nft binary

PATCH Applied Yi Chen 2 participants 6 messages Jun 22, 2025 netfilter

lore
Share & tools
lore

Yi Chen

author
#0 lore raw

PATCH v2 1/4 test: shell: Don't use system nft binary

Use the defined $NFT variable instead of calling the system nft binary directly.
Add a nat_ftp.nodump file to avoid the following check-tree.sh error:
ERR: "tests/shell/testcases/packetpath/nat_ftp" has no "tests/shell/testcases/packetpath/dumps/nat_ftp.{nft,nodump}" file.

---
 tests/shell/testcases/packetpath/dumps/nat_ftp.nodump | 0
 tests/shell/testcases/packetpath/flowtables           | 4 ++--
 tests/shell/testcases/packetpath/nat_ftp              | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
 create mode 100644 tests/shell/testcases/packetpath/dumps/nat_ftp.nodump
tests/shell/testcases/packetpath/dumps/nat_ftp.nodump new file +0 -0
new file mode 100644
index 00000000..e69de29b
tests/shell/testcases/packetpath/flowtables +2 -2
index b68c5dd4..ab11431f 100755
@@ -67,7 +67,7 @@ sleep 3
 ip netns exec $C ping -q -6 2001:db8:ffff:22::1 -c1
 assert_pass "topo initialization"
 
-ip netns exec $R nft -f - <<EOF
+ip netns exec $R $NFT -f - <<EOF
 table ip6 filter {
         flowtable f1 {
                 hook ingress priority -100
@@ -88,7 +88,7 @@ assert_pass "apply nft ruleset"
 
 if [ ! -r /proc/net/nf_conntrack ]
 then
-	echo "E: nf_conntrack unreadable, skipping" >&2	
+	echo "E: nf_conntrack unreadable, skipping" >&2
 	exit 77
 fi
 
tests/shell/testcases/packetpath/nat_ftp +2 -2
index 327047b8..738bcb98 100755
@@ -20,7 +20,7 @@ assert_pass()
 	if [ $ret != 0 ]
 	then
 		echo "FAIL: ${@}"
-		ip netns exec $R nft list ruleset
+		ip netns exec $R $NFT list ruleset
 		tcpdump -nnr ${PCAP}
 		test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack
 		ip netns exec $R conntrack -S
@@ -82,7 +82,7 @@ assert_pass "topo initialization"
 reload_ruleset()
 {
 	ip netns exec $R conntrack -F 2> /dev/null
-	ip netns exec $R nft -f - <<-EOF
+	ip netns exec $R $NFT -f - <<-EOF
 	flush ruleset
 	table ip6 ftp_helper_nat_test {
 		ct helper ftp-standard {
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH v2 2/4 test: shell: Introduce $NFT_TEST_LIBRARY_FILE, helper/lib.sh

Consolidate frequently used functions in helper/lib.sh switch nat_ftp and flowtables to use it.

---
 tests/shell/helpers/lib.sh                  | 30 +++++++++++++++++++++
 tests/shell/helpers/test-wrapper.sh         |  1 +
 tests/shell/testcases/packetpath/flowtables | 27 ++++---------------
 tests/shell/testcases/packetpath/nat_ftp    | 24 +++++++----------
 4 files changed, 45 insertions(+), 37 deletions(-)
 create mode 100755 tests/shell/helpers/lib.sh
tests/shell/helpers/lib.sh new file +30 -0
new file mode 100755
index 00000000..d2d20984
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# contains utility functions commonly used in tests.
+
+assert_pass()
+{
+	local ret=$?
+	if [ $ret != 0 ]; then
+		echo "FAIL: ${@}"
+		if type -t assert_failout; then
+			assert_failout
+		fi
+		exit 1
+	else
+		echo "PASS: ${@}"
+	fi
+}
+assert_fail()
+{
+	local ret=$?
+	if [ $ret == 0 ]; then
+		echo "FAIL: ${@}"
+		if type -t assert_failout; then
+			assert_failout
+		fi
+		exit 1
+	else
+		echo "PASS: ${@}"
+	fi
+}
tests/shell/helpers/test-wrapper.sh +1 -0
index 4a7e8b7b..6ec4e030 100755
@@ -36,6 +36,7 @@ TESTDIR="$(dirname "$TEST")"
 START_TIME="$(cut -d ' ' -f1 /proc/uptime)"
 
 export TMPDIR="$NFT_TEST_TESTTMPDIR"
+export NFT_TEST_LIBRARY_FILE="$NFT_TEST_BASEDIR/helpers/lib.sh"
 
 CLEANUP_UMOUNT_VAR_RUN=n
 
tests/shell/testcases/packetpath/flowtables +5 -22
index ab11431f..f3580a5f 100755
@@ -3,6 +3,8 @@
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_socat)
 # NFT_TEST_SKIP(NFT_TEST_SKIP_slow)
 
+. $NFT_TEST_LIBRARY_FILE
+
 rnd=$(mktemp -u XXXXXXXX)
 R="flowtable-router-$rnd"
 C="flowtable-client-$rnd"
@@ -17,29 +19,10 @@ cleanup()
 }
 trap cleanup EXIT
 
-assert_pass()
-{
-	local ret=$?
-	if [ $ret != 0 ]
-	then
-		echo "FAIL: ${@}"
-		ip netns exec $R cat /proc/net/nf_conntrack
-		exit 1
-	else
-		echo "PASS: ${@}"
-	fi
-}
-assert_fail()
+# Call back when assertion fails.
+assert_failout()
 {
-	local ret=$?
-	if [ $ret == 0 ]
-	then
-		echo "FAIL: ${@}"
-		ip netns exec $R cat /proc/net/nf_conntrack
-		exit 1
-	else
-		echo "PASS: ${@}"
-	fi
+	ip netns exec $R cat /proc/net/nf_conntrack
 }
 
 ip netns add $R
tests/shell/testcases/packetpath/nat_ftp +9 -15
index 738bcb98..37ef14a3 100755
@@ -4,6 +4,8 @@
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_curl)
 # NFT_TEST_REQUIRES(NFT_TEST_HAVE_vsftpd)
 
+. $NFT_TEST_LIBRARY_FILE
+
 cleanup()
 {
 	for i in $R $C $S;do
@@ -14,22 +16,14 @@ cleanup()
 }
 trap cleanup EXIT
 
-assert_pass()
+assert_failout()
 {
-	local ret=$?
-	if [ $ret != 0 ]
-	then
-		echo "FAIL: ${@}"
-		ip netns exec $R $NFT list ruleset
-		tcpdump -nnr ${PCAP}
-		test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack
-		ip netns exec $R conntrack -S
-		ip netns exec $R conntrack -L
-		ip netns exec $S ss -nitepal
-		exit 1
-	else
-		echo "PASS: ${@}"
-	fi
+	ip netns exec $R $NFT list ruleset
+	tcpdump -nnr ${PCAP}
+	test -r /proc/net/nf_conntrack && ip netns exec $R cat /proc/net/nf_conntrack
+	ip netns exec $R conntrack -S
+	ip netns exec $R conntrack -L
+	ip netns exec $S ss -nitepal
 }
 
 rnd=$(mktemp -u XXXXXXXX)
Signature · 1 line
2.49.0
author
#2 lore raw

PATCH v2 3/4 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.

---
 tests/shell/helpers/lib.sh                  | 21 +++++++++++++++++++++
 tests/shell/testcases/packetpath/flowtables |  3 ++-
 tests/shell/testcases/packetpath/nat_ftp    |  6 ++++--
 3 files changed, 27 insertions(+), 3 deletions(-)
tests/shell/helpers/lib.sh +21 -0
index d2d20984..ce27aac3 100755
@@ -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
+}
tests/shell/testcases/packetpath/flowtables +2 -1
index f3580a5f..dbe470a8 100755
@@ -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"
tests/shell/testcases/packetpath/nat_ftp +4 -2
index 37ef14a3..3ba4029d 100755
@@ -132,7 +132,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"
 
@@ -141,7 +142,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 "
 
@@ -158,6 +159,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 "
 
Signature · 1 line
2.49.0
author
#3 lore raw

PATCH v2 4/4 test: shell: Add rate_limit test case for 'limit statement'.

---
 .../packetpath/dumps/rate_limit.nodump        |   0
 tests/shell/testcases/packetpath/rate_limit   | 136 ++++++++++++++++++
 2 files changed, 136 insertions(+)
 create mode 100644 tests/shell/testcases/packetpath/dumps/rate_limit.nodump
 create mode 100755 tests/shell/testcases/packetpath/rate_limit
tests/shell/testcases/packetpath/dumps/rate_limit.nodump new file +0 -0
new file mode 100644
index 00000000..e69de29b
tests/shell/testcases/packetpath/rate_limit new file +136 -0

136 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer Applied
#4 lore raw

Re: [PATCH v2 3/4] test: shell: Add wait_local_port_listen() helper to lib.sh

In reply to Yi Chen
Quoted 3 lines · Yi Chen

ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$!
+sleep 0.5

You might want to add a wait_for_tcpdump_listen helper, that checks /proc/net/packet for tcpdump appearance, or /porc/$pid/fd/ for appearance of the packet socket.

No need to resend and no need to work on this, just something to keep in mind of the future.

I've applied all 4 patches and pushed them out, thanks for addressing all review comments.

author
#5 lore raw

Re: [PATCH v2 3/4] test: shell: Add wait_local_port_listen() helper to lib.sh

Thank you very much for your review and thoughtful guidance!

Quoted 16 lines · Florian Westphal
Quoted 3 lines · Yi Chen

ip netns exec $S tcpdump -q --immediate-mode -Ui s_r -w ${PCAP} 2> /dev/null & pid=$!
+sleep 0.5

You might want to add a wait_for_tcpdump_listen helper, that checks /proc/net/packet for tcpdump appearance, or /porc/$pid/fd/ for appearance of the packet socket.

No need to resend and no need to work on this, just something to keep in mind of the future.

I've applied all 4 patches and pushed them out, thanks for addressing all review comments.