selftests: netfilter: Fix and review rpath.sh
authorPhil Sutter <phil@nwl.cc>
Wed, 9 Nov 2022 09:06:04 +0000 (10:06 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 9 Nov 2022 09:29:57 +0000 (10:29 +0100)
Address a few problems with the initial test script version:

* On systems with ip6tables but no ip6tables-legacy, testing for
  ip6tables was disabled by accident.
* Firewall setup phase did not respect possibly unavailable tools.
* Consistently call nft via '$nft'.

Fixes: 6e31ce831c63b ("selftests: netfilter: Test reverse path filtering")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tools/testing/selftests/netfilter/rpath.sh

index 2d8da7b..f7311e6 100755 (executable)
@@ -15,7 +15,7 @@ fi
 
 if ip6tables-legacy --version >/dev/null 2>&1; then
        ip6tables='ip6tables-legacy'
-elif ip6tables --version >/dev/null 2>&1; then
+elif ip6tables --version >/dev/null 2>&1; then
        ip6tables='ip6tables'
 else
        ip6tables=''
@@ -62,9 +62,11 @@ ip -net "$ns1" a a fec0:42::2/64 dev v0 nodad
 ip -net "$ns2" a a fec0:42::1/64 dev d0 nodad
 
 # firewall matches to test
-ip netns exec "$ns2" "$iptables" -t raw -A PREROUTING -s 192.168.0.0/16 -m rpfilter
-ip netns exec "$ns2" "$ip6tables" -t raw -A PREROUTING -s fec0::/16 -m rpfilter
-ip netns exec "$ns2" nft -f - <<EOF
+[ -n "$iptables" ] && ip netns exec "$ns2" \
+       "$iptables" -t raw -A PREROUTING -s 192.168.0.0/16 -m rpfilter
+[ -n "$ip6tables" ] && ip netns exec "$ns2" \
+       "$ip6tables" -t raw -A PREROUTING -s fec0::/16 -m rpfilter
+[ -n "$nft" ] && ip netns exec "$ns2" $nft -f - <<EOF
 table inet t {
        chain c {
                type filter hook prerouting priority raw;
@@ -106,8 +108,8 @@ testrun() {
        if [ -n "$nft" ]; then
                (
                        echo "delete table inet t";
-                       ip netns exec "$ns2" nft -s list table inet t;
-               ) | ip netns exec "$ns2" nft -f -
+                       ip netns exec "$ns2" $nft -s list table inet t;
+               ) | ip netns exec "$ns2" $nft -f -
        fi
 
        # test 1: martian traffic should fail rpfilter matches