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

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