Blame SOURCES/0042-tests-shell-Merge-and-extend-return-codes-test.patch

b144b7
From c5f07a7d718f812f916686926567adbac6c1b125 Mon Sep 17 00:00:00 2001
b144b7
From: Phil Sutter <phil@nwl.cc>
b144b7
Date: Thu, 6 Aug 2020 18:52:34 +0200
b144b7
Subject: [PATCH] tests: shell: Merge and extend return codes test
b144b7
b144b7
Merge scripts for iptables and ip6tables, they were widely identical.
b144b7
Also extend the test by one check (removing a non-existent rule with
b144b7
valid chain and target) and quote the error messages where differences
b144b7
are deliberately ignored.
b144b7
b144b7
Signed-off-by: Phil Sutter <phil@nwl.cc>
b144b7
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
b144b7
(cherry picked from commit cd3e83d1b04fd2683f0fb06e496ee5be08a96b4f)
b144b7
b144b7
Conflicts:
b144b7
	iptables/tests/shell/testcases/ip6tables/0004-return-codes_0
b144b7
	iptables/tests/shell/testcases/iptables/0004-return-codes_0
b144b7
-> Missing upstream commit a7f1e208cdf9c ("nft: split parsing from
b144b7
   netlink commands") which added a few tests to both files.
b144b7
b144b7
Signed-off-by: Phil Sutter <psutter@redhat.com>
b144b7
---
b144b7
 .../testcases/ip6tables/0004-return-codes_0   |  38 -------
b144b7
 .../testcases/iptables/0004-return-codes_0    | 104 ++++++++++--------
b144b7
 2 files changed, 58 insertions(+), 84 deletions(-)
b144b7
 delete mode 100755 iptables/tests/shell/testcases/ip6tables/0004-return-codes_0
b144b7
b144b7
diff --git a/iptables/tests/shell/testcases/ip6tables/0004-return-codes_0 b/iptables/tests/shell/testcases/ip6tables/0004-return-codes_0
b144b7
deleted file mode 100755
b144b7
index f023b7915498e..0000000000000
b144b7
--- a/iptables/tests/shell/testcases/ip6tables/0004-return-codes_0
b144b7
+++ /dev/null
b144b7
@@ -1,38 +0,0 @@
b144b7
-#!/bin/sh
b144b7
-
b144b7
-# make sure error return codes are as expected useful cases
b144b7
-# (e.g. commands to check ruleset state)
b144b7
-
b144b7
-global_rc=0
b144b7
-
b144b7
-cmd() { # (rc, cmd, [args ...])
b144b7
-	rc_exp=$1; shift
b144b7
-
b144b7
-	$XT_MULTI "$@"
b144b7
-	rc=$?
b144b7
-
b144b7
-	[ $rc -eq $rc_exp ] || {
b144b7
-		echo "---> expected $rc_exp, got $rc for command '$@'"
b144b7
-		global_rc=1
b144b7
-	}
b144b7
-}
b144b7
-
b144b7
-# test chain creation
b144b7
-cmd 0 ip6tables -N foo
b144b7
-cmd 1 ip6tables -N foo
b144b7
-# iptables-nft allows this - bug or feature?
b144b7
-#cmd 2 ip6tables -N "invalid name"
b144b7
-
b144b7
-# test rule adding
b144b7
-cmd 0 ip6tables -A INPUT -j ACCEPT
b144b7
-cmd 1 ip6tables -A noexist -j ACCEPT
b144b7
-
b144b7
-# test rule checking
b144b7
-cmd 0 ip6tables -C INPUT -j ACCEPT
b144b7
-cmd 1 ip6tables -C FORWARD -j ACCEPT
b144b7
-cmd 1 ip6tables -C nonexist -j ACCEPT
b144b7
-cmd 2 ip6tables -C INPUT -j foobar
b144b7
-cmd 2 ip6tables -C INPUT -m foobar -j ACCEPT
b144b7
-cmd 3 ip6tables -t foobar -C INPUT -j ACCEPT
b144b7
-
b144b7
-exit $global_rc
b144b7
diff --git a/iptables/tests/shell/testcases/iptables/0004-return-codes_0 b/iptables/tests/shell/testcases/iptables/0004-return-codes_0
b144b7
index ce02e0bcb128b..67f1698945753 100755
b144b7
--- a/iptables/tests/shell/testcases/iptables/0004-return-codes_0
b144b7
+++ b/iptables/tests/shell/testcases/iptables/0004-return-codes_0
b144b7
@@ -13,69 +13,81 @@ cmd() { # (rc, msg, cmd, [args ...])
b144b7
 		msg_exp="$1"; shift
b144b7
 	}
b144b7
 
b144b7
-	msg="$($XT_MULTI "$@" 2>&1 >/dev/null)"
b144b7
-	rc=$?
b144b7
+	for ipt in iptables ip6tables; do
b144b7
+		msg="$($XT_MULTI $ipt "$@" 2>&1 >/dev/null)"
b144b7
+		rc=$?
b144b7
 
b144b7
-	[ $rc -eq $rc_exp ] || {
b144b7
-		echo "---> expected return code $rc_exp, got $rc for command '$@'"
b144b7
-		global_rc=1
b144b7
-	}
b144b7
+		[ $rc -eq $rc_exp ] || {
b144b7
+			echo "---> expected return code $rc_exp, got $rc for command '$ipt $@'"
b144b7
+			global_rc=1
b144b7
+		}
b144b7
 
b144b7
-	[ -n "$msg_exp" ] || return
b144b7
-	grep -q "$msg_exp" <<< $msg || {
b144b7
-		echo "---> expected error message '$msg_exp', got '$msg' for command '$@'"
b144b7
-		global_rc=1
b144b7
-	}
b144b7
+		[ -n "$msg_exp" ] || continue
b144b7
+		msg_exp_full="${ipt}$msg_exp"
b144b7
+		grep -q "$msg_exp_full" <<< $msg || {
b144b7
+			echo "---> expected error message '$msg_exp_full', got '$msg' for command '$ipt $@'"
b144b7
+			global_rc=1
b144b7
+		}
b144b7
+	done
b144b7
 }
b144b7
 
b144b7
-EEXIST_F="File exists."
b144b7
-EEXIST="Chain already exists."
b144b7
-ENOENT="No chain/target/match by that name."
b144b7
-E2BIG_I="Index of insertion too big."
b144b7
-E2BIG_D="Index of deletion too big."
b144b7
-E2BIG_R="Index of replacement too big."
b144b7
-EBADRULE="Bad rule (does a matching rule exist in that chain?)."
b144b7
-ENOTGT="Couldn't load target \`foobar':No such file or directory"
b144b7
-ENOMTH="Couldn't load match \`foobar':No such file or directory"
b144b7
-ENOTBL="can't initialize iptables table \`foobar': Table does not exist"
b144b7
+EEXIST_F=": File exists."
b144b7
+EEXIST=": Chain already exists."
b144b7
+ENOENT=": No chain/target/match by that name."
b144b7
+E2BIG_I=": Index of insertion too big."
b144b7
+E2BIG_D=": Index of deletion too big."
b144b7
+E2BIG_R=": Index of replacement too big."
b144b7
+EBADRULE=": Bad rule (does a matching rule exist in that chain?)."
b144b7
+#ENOTGT=" v[0-9\.]* [^ ]*: Couldn't load target \`foobar':No such file or directory"
b144b7
+ENOMTH=" v[0-9\.]* [^ ]*: Couldn't load match \`foobar':No such file or directory"
b144b7
+ENOTBL=": can't initialize iptables table \`foobar': Table does not exist"
b144b7
 
b144b7
 # test chain creation
b144b7
-cmd 0 iptables -N foo
b144b7
-cmd 1 "$EEXIST" iptables -N foo
b144b7
+cmd 0 -N foo
b144b7
+cmd 1 "$EEXIST" -N foo
b144b7
 # iptables-nft allows this - bug or feature?
b144b7
-#cmd 2 iptables -N "invalid name"
b144b7
+#cmd 2 -N "invalid name"
b144b7
 
b144b7
 # test chain flushing/zeroing
b144b7
-cmd 0 iptables -F foo
b144b7
-cmd 0 iptables -Z foo
b144b7
-cmd 1 "$ENOENT" iptables -F bar
b144b7
-cmd 1 "$ENOENT" iptables -Z bar
b144b7
+cmd 0 -F foo
b144b7
+cmd 0 -Z foo
b144b7
+cmd 1 "$ENOENT" -F bar
b144b7
+cmd 1 "$ENOENT" -Z bar
b144b7
 
b144b7
 # test chain rename
b144b7
-cmd 0 iptables -E foo bar
b144b7
-cmd 1 "$EEXIST_F" iptables -E foo bar
b144b7
+cmd 0 -E foo bar
b144b7
+cmd 1 "$EEXIST_F" -E foo bar
b144b7
 
b144b7
 # test rule adding
b144b7
-cmd 0 iptables -A INPUT -j ACCEPT
b144b7
-cmd 1 "$ENOENT" iptables -A noexist -j ACCEPT
b144b7
+cmd 0 -A INPUT -j ACCEPT
b144b7
+cmd 1 "$ENOENT" -A noexist -j ACCEPT
b144b7
+# next three differ:
b144b7
+# legacy: Couldn't load target `foobar':No such file or directory
b144b7
+# nft:    Chain 'foobar' does not exist
b144b7
+cmd 2 "" -I INPUT -j foobar
b144b7
+cmd 2 "" -R INPUT 1 -j foobar
b144b7
+cmd 2 "" -D INPUT -j foobar
b144b7
+cmd 1 "$EBADRULE" -D INPUT -p tcp --dport 22 -j ACCEPT
b144b7
 
b144b7
 # test rulenum commands
b144b7
-cmd 1 "$E2BIG_I" iptables -I INPUT 23 -j ACCEPT
b144b7
-cmd 1 "$E2BIG_D" iptables -D INPUT 23
b144b7
-cmd 1 "$E2BIG_R" iptables -R INPUT 23 -j ACCEPT
b144b7
-cmd 1 "$ENOENT" iptables -I nonexist 23 -j ACCEPT
b144b7
-cmd 1 "$ENOENT" iptables -D nonexist 23
b144b7
-cmd 1 "$ENOENT" iptables -R nonexist 23 -j ACCEPT
b144b7
+cmd 1 "$E2BIG_I" -I INPUT 23 -j ACCEPT
b144b7
+cmd 1 "$E2BIG_D" -D INPUT 23
b144b7
+cmd 1 "$E2BIG_R" -R INPUT 23 -j ACCEPT
b144b7
+cmd 1 "$ENOENT" -I nonexist 23 -j ACCEPT
b144b7
+cmd 1 "$ENOENT" -D nonexist 23
b144b7
+cmd 1 "$ENOENT" -R nonexist 23 -j ACCEPT
b144b7
 
b144b7
 # test rule checking
b144b7
-cmd 0 iptables -C INPUT -j ACCEPT
b144b7
-cmd 1 "$EBADRULE" iptables -C FORWARD -j ACCEPT
b144b7
-cmd 1 "$BADRULE" iptables -C nonexist -j ACCEPT
b144b7
-cmd 2 "$ENOMTH" iptables -C INPUT -m foobar -j ACCEPT
b144b7
+cmd 0 -C INPUT -j ACCEPT
b144b7
+cmd 1 "$EBADRULE" -C FORWARD -j ACCEPT
b144b7
+cmd 1 "$BADRULE" -C nonexist -j ACCEPT
b144b7
+cmd 2 "$ENOMTH" -C INPUT -m foobar -j ACCEPT
b144b7
 # messages of those don't match, but iptables-nft ones are actually nicer.
b144b7
-#cmd 2 "$ENOTGT" iptables -C INPUT -j foobar
b144b7
-#cmd 3 "$ENOTBL" iptables -t foobar -C INPUT -j ACCEPT
b144b7
-cmd 2 "" iptables -C INPUT -j foobar
b144b7
-cmd 3 "" iptables -t foobar -C INPUT -j ACCEPT
b144b7
+# legacy: Couldn't load target `foobar':No such file or directory
b144b7
+# nft:    Chain 'foobar' does not exist
b144b7
+cmd 2 "" -C INPUT -j foobar
b144b7
+# legacy: can't initialize ip6tables table `foobar': Table does not exist (do you need to insmod?)
b144b7
+# nft:    table 'foobar' does not exist
b144b7
+cmd 3 "" -t foobar -C INPUT -j ACCEPT
b144b7
 
b144b7
 exit $global_rc
b144b7
-- 
b144b7
2.28.0
b144b7