Skip to contentSkip to conversation

Re-enable NFLOG tests

PATCH In review Jeremy Sowden 3 participants 9 messages Feb 12, 2022 – Feb 22, 2022 netfilter

lore
Share & tools
lore
#0 lore raw

cover Re-enable NFLOG tests

Some of the NFLOG tests were disabled when iptables-nft was changed to use nft's nflog implementation, because nft doesn't support --nflog-range. This patch-set builds on Phil's recent work to support different test results for -legacy and -nft in order to re-enable those tests.

* Patch 1 renames a variable.
* Patches 2 & 3 add support for a new test result where the iptables command succeeds, but dumping the rule does not give the expected output.
* Patch 4 re-enables the tests.

Jeremy Sowden (4):
tests: iptables-test: rename variable
tests: add NOMATCH test result
tests: support explicit variant test result
tests: NFLOG: enable --nflog-range tests

 extensions/libxt_NFLOG.t | 12 +++----
 iptables-test.py         | 72 ++++++++++++++++++++++++++--------------
 2 files changed, 53 insertions(+), 31 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 tests: iptables-test: rename variable

"Splitted" hasn't been current since the seventeenth century. Replace it with "tokens".

---
 iptables-test.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
iptables-test.py +10 -10
index b49afcab3c40..91c77e3dc0e0 100755
@@ -107,20 +107,20 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns):
             return -1
 
     matching = 0
-    splitted = iptables.split(" ")
-    if len(splitted) == 2:
-        if splitted[1] == '-4':
+    tokens = iptables.split(" ")
+    if len(tokens) == 2:
+        if tokens[1] == '-4':
             command = IPTABLES_SAVE
-        elif splitted[1] == '-6':
+        elif tokens[1] == '-6':
             command = IP6TABLES_SAVE
-    elif len(splitted) == 1:
-        if splitted[0] == IPTABLES:
+    elif len(tokens) == 1:
+        if tokens[0] == IPTABLES:
             command = IPTABLES_SAVE
-        elif splitted[0] == IP6TABLES:
+        elif tokens[0] == IP6TABLES:
             command = IP6TABLES_SAVE
-        elif splitted[0] == ARPTABLES:
+        elif tokens[0] == ARPTABLES:
             command = ARPTABLES_SAVE
-        elif splitted[0] == EBTABLES:
+        elif tokens[0] == EBTABLES:
             command = EBTABLES_SAVE
 
     command = EXECUTABLE + " " + command
@@ -128,7 +128,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns):
     if netns:
             command = "ip netns exec ____iptables-container-test " + command
 
-    args = splitted[1:]
+    args = tokens[1:]
     proc = subprocess.Popen(command, shell=True,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/4 tests: add `NOMATCH` test result

In reply to Jeremy Sowden

Currently, there are two supported test results: OK and FAIL. It is expected that either the iptables command fails, or it succeeds and dumping the rule has the correct output. However, it is possible that the command may succeed but the output may not be correct. Add a NOMATCH result to cover this outcome.

Make a few white-space improvements at the same time.

---
 iptables-test.py | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)
iptables-test.py +25 -11
index 91c77e3dc0e0..4a587a29c823 100755
@@ -73,9 +73,9 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns):
     Executes an unit test. Returns the output of delete_rule().
 
     Parameters:
-    :param  iptables: string with the iptables command to execute
+    :param iptables: string with the iptables command to execute
     :param rule: string with iptables arguments for the rule to test
-    :param rule_save: string to find the rule in the output of iptables -save
+    :param rule_save: string to find the rule in the output of iptables-save
     :param res: expected result of the rule. Valid values: "OK", "FAIL"
     :param filename: name of the file tested (used for print_error purposes)
     :param lineno: line number being tested (used for print_error purposes)
@@ -92,7 +92,7 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns):
     # report failed test
     #
     if ret:
-        if res == "OK":
+        if res != "FAIL":
             reason = "cannot load: " + cmd
             print_error(reason, filename, lineno)
             return -1
@@ -146,10 +146,20 @@ def run_test(iptables, rule, rule_save, res, filename, lineno, netns):
     # find the rule
     matching = out.find(rule_save.encode('utf-8'))
     if matching < 0:
-        reason = "cannot find: " + iptables + " -I " + rule
-        print_error(reason, filename, lineno)
-        delete_rule(iptables, rule, filename, lineno)
-        return -1
+        if res == "OK":
+            reason = "cannot find: " + iptables + " -I " + rule
+            print_error(reason, filename, lineno)
+            delete_rule(iptables, rule, filename, lineno)
+            return -1
+        else:
+            # do not report this error
+            return 0
+    else:
+        if res != "OK":
+            reason = "should not match: " + cmd
+            print_error(reason, filename, lineno)
+            delete_rule(iptables, rule, filename, lineno)
+            return -1
 
     # Test "ip netns del NETNS" path with rules in place
     if netns:
@@ -190,14 +200,18 @@ def variant_res(res, variant):
     result. Therefore map @res to itself if given variant is current, invert it
     otherwise.
 
-    :param res: expected result from test spec ("OK" or "FAIL")
+    :param res: expected result from test spec ("OK", "FAIL" or "NOMATCH")
     :param variant: variant @res is scoped to by test spec ("NFT" or "LEGACY")
     '''
     variant_executable = {
-            "NFT": "xtables-nft-multi",
-            "LEGACY": "xtables-legacy-multi"
+        "NFT": "xtables-nft-multi",
+        "LEGACY": "xtables-legacy-multi"
+    }
+    res_inverse = {
+        "OK": "FAIL",
+        "FAIL": "OK",
+        "NOMATCH": "OK"
     }
-    res_inverse = { "OK": "FAIL", "FAIL": "OK" }
 
     if variant_executable[variant] == EXECUTABLE:
         return res
Signature · 1 line
2.34.1
reviewer
#3 lore raw

Re: [iptables PATCH 2/4] tests: add `NOMATCH` test result

Hi Jeremy,

Quoted 5 lines · Jeremy Sowden

Currently, there are two supported test results: OK and FAIL. It is expected that either the iptables command fails, or it succeeds and dumping the rule has the correct output. However, it is possible that the command may succeed but the output may not be correct. Add a NOMATCH result to cover this outcome.

Hmm. Wouldn't it make sense to extend the scope of LEGACY/NFT keywords to output checks as well instead of introducing a new one? I think we could cover expected output that way by duplicating the test case with different expected output instead of marking it as unspecific "may produce garbage".

Cheers, Phil

#4 lore raw

Re: [iptables PATCH 2/4] tests: add `NOMATCH` test result

Quoted 7 lines · Phil Sutter
Quoted 5 lines · Jeremy Sowden

Currently, there are two supported test results: OK and FAIL. It is expected that either the iptables command fails, or it succeeds and dumping the rule has the correct output. However, it is possible that the command may succeed but the output may not be correct. Add a NOMATCH result to cover this outcome.

Quoted 5 lines

Hmm. Wouldn't it make sense to extend the scope of LEGACY/NFT keywords to output checks as well instead of introducing a new one? I think we could cover expected output that way by duplicating the test case with different expected output instead of marking it as unspecific "may produce garbage".

Something like the following? One reason why I went with the NOMATCH result is that in the two divergent test-cases, there is no -nft output to match. We can make that work by just using the empty string as the alternative output since that will match anything. I don't think it's ideal, but it's simpler than overhauling the matching code for what is a rare corner case.

J.

From a18fa07425f82d71a46846e4f3656ec65155fd0c Mon Sep 17 00:00:00 2001
From: Jeremy Sowden <[email protected]>
Date: Sun, 20 Feb 2022 12:49:23 +0000
Subject: [iptables PATCH v2] tests: specify non-matching output instead of
NOMATCH test result

Recently, we introduced a new test result NOMATCH to cover the case where the test succeeds for both -legacy and -nft, but the two variants do not have the same output. This patch does away with the new result in favour of specifying the alternative output. For example, instead of

  -j EXAMPLE-TARGET --example-option;=;OK;LEGACY;NOMATCH

which specifies that the test passes for -legacy with the output matching the rule, but for -nft it passes with output which does not match the rule, we specify the test as:

  -j EXAMPLE-TARGET --example-option;=;OK;LEGACY;-j EXAMPLE-TARGET --nft-option

which specifies that the test passes for -legacy with the output matching the rule, but for -nft it passes with the different output given in the last field.

In the case of tests which have no output to match, we leave the last field empty:

  -j EXAMPLE-TARGET --example-option;=;OK;LEGACY;
---
 extensions/libxt_NFLOG.t |  4 ++--
 iptables-test.py         | 50 ++++++++++++++++++++++++++++++----------
 2 files changed, 40 insertions(+), 14 deletions(-)
extensions/libxt_NFLOG.t +2 -2
index 25f332ae16b6..b3241f51b153 100644
@@ -5,8 +5,8 @@
 -j NFLOG --nflog-group 0;-j NFLOG;OK
 # `--nflog-range` is broken and only supported by xtables-legacy.
 # It has been superseded by `--nflog--group`.
--j NFLOG --nflog-range 1;=;OK;LEGACY;NOMATCH
--j NFLOG --nflog-range 4294967295;=;OK;LEGACY;NOMATCH
+-j NFLOG --nflog-range 1;=;OK;LEGACY;
+-j NFLOG --nflog-range 4294967295;=;OK;LEGACY;
 -j NFLOG --nflog-range 4294967296;;FAIL
 -j NFLOG --nflog-range -1;;FAIL
 -j NFLOG --nflog-size 0;=;OK
iptables-test.py +38 -12

82 lines not on this page · raw message

Signature · 1 line
2.34.1
reviewer
#5 lore raw

Re: [iptables PATCH 2/4] tests: add `NOMATCH` test result

Hi Jeremy,

Quoted 14 lines · Jeremy Sowden
Quoted 7 lines · Phil Sutter
Quoted 5 lines · Jeremy Sowden

Currently, there are two supported test results: OK and FAIL. It is expected that either the iptables command fails, or it succeeds and dumping the rule has the correct output. However, it is possible that the command may succeed but the output may not be correct. Add a NOMATCH result to cover this outcome.

Quoted 5 lines

Hmm. Wouldn't it make sense to extend the scope of LEGACY/NFT keywords to output checks as well instead of introducing a new one? I think we could cover expected output that way by duplicating the test case with different expected output instead of marking it as unspecific "may produce garbage".

Quoted 6 lines

Something like the following? One reason why I went with the NOMATCH result is that in the two divergent test-cases, there is no -nft output to match. We can make that work by just using the empty string as the alternative output since that will match anything. I don't think it's ideal, but it's simpler than overhauling the matching code for what is a rare corner case.

Thanks for compiling the patch. What I had in mind was to merge result checks of failing rule with output mismatch, but I realize this would likely turn into a mess.

[...]

Quoted 4 lines

In the case of tests which have no output to match, we leave the last field empty:

  -j EXAMPLE-TARGET --example-option;=;OK;LEGACY;

A non-empty rule leading to empty output is a bug, IMHO.

[...]

extensions/libxt_NFLOG.t line 9

Quoted 10 lines
extensions/libxt_NFLOG.t +2 -2
@@ -5,8 +5,8 @@
 -j NFLOG --nflog-group 0;-j NFLOG;OK
 # `--nflog-range` is broken and only supported by xtables-legacy.
 # It has been superseded by `--nflog--group`.
--j NFLOG --nflog-range 1;=;OK;LEGACY;NOMATCH
--j NFLOG --nflog-range 4294967295;=;OK;LEGACY;NOMATCH
+-j NFLOG --nflog-range 1;=;OK;LEGACY;
+-j NFLOG --nflog-range 4294967295;=;OK;LEGACY;

The crucial detail here is that an expected output of "-j NFLOG" is trivial and str::find() won't complain about extra output.

Given that we're discussing corner cases and what I had in mind has its own downsides, I guess the status quo is fine at least for now. Sorry for the fuss!

Cheers, Phil

#6 lore raw

PATCH 3/4 tests: support explicit variant test result

In reply to Jeremy Sowden

Now that there are more than two test results, add support for explicitly indicating which result to expect if the variants differ.

---
 iptables-test.py | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
iptables-test.py +13 -5
index 4a587a29c823..6acaa82228fa 100755
@@ -186,22 +186,23 @@ def execute_cmd(cmd, filename, lineno):
     log_file.flush()
 
     # generic check for segfaults
-    if ret  == -11:
+    if ret == -11:
         reason = "command segfaults: " + cmd
         print_error(reason, filename, lineno)
     return ret
 
 
-def variant_res(res, variant):
+def variant_res(res, variant, alt_res=None):
     '''
     Adjust expected result with given variant
 
     If expected result is scoped to a variant, the other one yields a different
-    result. Therefore map @res to itself if given variant is current, invert it
-    otherwise.
+    result. Therefore map @res to itself if given variant is current, use the
+    alternate result, @alt_res, if specified, invert @res otherwise.
 
     :param res: expected result from test spec ("OK", "FAIL" or "NOMATCH")
     :param variant: variant @res is scoped to by test spec ("NFT" or "LEGACY")
+    :param alt_res: optional expected result for the alternate variant.
     '''
     variant_executable = {
         "NFT": "xtables-nft-multi",
@@ -215,6 +216,8 @@ def variant_res(res, variant):
 
     if variant_executable[variant] == EXECUTABLE:
         return res
+    if alt_res is not None:
+        return alt_res
     return res_inverse[res]
 
 
@@ -312,7 +315,12 @@ def run_test_file(filename, netns):
 
             res = item[2].rstrip()
             if len(item) > 3:
-                res = variant_res(res, item[3].rstrip())
+                variant = item[3].rstrip()
+                if len(item) > 4:
+                    alt_res = item[4].rstrip()
+                else:
+                    alt_res = None
+                res = variant_res(res, variant, alt_res)
 
             ret = run_test(iptables, rule, rule_save,
                            res, filename, lineno + 1, netns)
Signature · 1 line
2.34.1
#7 lore raw

PATCH 4/4 tests: NFLOG: enable `--nflog-range` tests

In reply to Jeremy Sowden

iptables-legacy and iptable-nft have different results for these tests. Now that it is possible to specify the expected results correctly, we can enable the tests.

---
 extensions/libxt_NFLOG.t | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
extensions/libxt_NFLOG.t +6 -6
index 561ec8c77650..25f332ae16b6 100644
@@ -3,12 +3,12 @@
 -j NFLOG --nflog-group 65535;=;OK
 -j NFLOG --nflog-group 65536;;FAIL
 -j NFLOG --nflog-group 0;-j NFLOG;OK
-# `--nflog-range` is broken and only supported by xtables-legacy.  It
-# has been superseded by `--nflog--group`.
-# -j NFLOG --nflog-range 1;=;OK
-# -j NFLOG --nflog-range 4294967295;=;OK
-# -j NFLOG --nflog-range 4294967296;;FAIL
-# -j NFLOG --nflog-range -1;;FAIL
+# `--nflog-range` is broken and only supported by xtables-legacy.
+# It has been superseded by `--nflog--group`.
+-j NFLOG --nflog-range 1;=;OK;LEGACY;NOMATCH
+-j NFLOG --nflog-range 4294967295;=;OK;LEGACY;NOMATCH
+-j NFLOG --nflog-range 4294967296;;FAIL
+-j NFLOG --nflog-range -1;;FAIL
 -j NFLOG --nflog-size 0;=;OK
 -j NFLOG --nflog-size 1;=;OK
 -j NFLOG --nflog-size 4294967295;=;OK
Signature · 1 line
2.34.1
maintainer
#8 lore raw
In reply to Jeremy Sowden
Quoted 5 lines · Jeremy Sowden

Some of the NFLOG tests were disabled when iptables-nft was changed to use nft's nflog implementation, because nft doesn't support --nflog-range. This patch-set builds on Phil's recent work to support different test results for -legacy and -nft in order to re-enable those tests.

Series applied, thank you.