]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: Add a simple test suite for --echo option
authorPhil Sutter <phil@nwl.cc>
Wed, 9 Aug 2017 11:16:43 +0000 (13:16 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 14 Aug 2017 09:32:20 +0000 (11:32 +0200)
The fancy thing about this is that it uses the actual echo output to
undo the changes to the rule set.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tests/echo/run-tests.sh [new file with mode: 0755]
tests/echo/testcases/simple.t [new file with mode: 0644]

diff --git a/tests/echo/run-tests.sh b/tests/echo/run-tests.sh
new file mode 100755 (executable)
index 0000000..da7934d
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+cd $(dirname $0)
+nft=../../src/nft
+nft_opts="-nn -a --echo"
+debug=false
+
+debug_echo() {
+       $debug || return
+
+       echo "$@"
+}
+
+trap "$nft flush ruleset" EXIT
+
+for testcase in testcases/*.t; do
+       echo "running tests from file $(basename $testcase)"
+       # files are like this:
+       #
+       # <input command>[;;<output regexp>]
+
+       $nft flush ruleset
+
+       while read line; do
+               [[ -z "$line" || "$line" == "#"* ]] && continue
+
+               # XXX: this only works if there is no semicolon in output
+               input="${line%;;*}"
+               output="${line##*;;}"
+
+               [[ -z $output ]] && output="$input"
+
+               debug_echo "calling '$nft $nft_opts $input'"
+               cmd_out=$($nft $nft_opts $input)
+               # strip trailing whitespace (happens when adding a named set)
+               cmd_out="${cmd_out% }"
+               debug_echo "got output '$cmd_out'"
+               [[ $cmd_out == $output ]] || {
+                       echo "Warning: Output differs:"
+                       echo "# nft $nft_opts $input"
+                       echo "- $output"
+                       echo "+ $cmd_out"
+               }
+       done <$testcase
+done
diff --git a/tests/echo/testcases/simple.t b/tests/echo/testcases/simple.t
new file mode 100644 (file)
index 0000000..566fd7e
--- /dev/null
@@ -0,0 +1,12 @@
+add table ip t
+add chain ip t c
+
+# note the added handle output
+add rule ip t c accept;;add rule ip t c accept # handle *
+add rule ip t c tcp dport { 22, 80, 443 } accept;;add rule ip t c tcp dport { 22, 80, 443 } accept # handle *
+
+add set ip t ipset { type ipv4_addr; }
+add element ip t ipset { 192.168.0.1 }
+
+# counter output comes with statistics
+add counter ip t cnt;;add counter ip t cnt *