]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests/py: Make nft-test.py a little more robust
authorPhil Sutter <phil@nwl.cc>
Wed, 29 Aug 2018 14:33:39 +0000 (16:33 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 3 Sep 2018 11:56:23 +0000 (13:56 +0200)
When adding a new test, missing payload file causes nft-test.py to choke
due to accessing undeclared variables. Fix this by making sure relevant
variables are declared outside of try-catch blocks.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tests/py/nft-test.py

index 23f73d50563e468440f9900e62bc283edfebfd3f..4e69cd2782232ca8dbd0980f7971a2dfc3919391 100755 (executable)
@@ -653,6 +653,9 @@ def payload_check(payload_buffer, file, cmd):
     file.seek(0, 0)
     i = 0
 
+    if not payload_buffer:
+        return False
+
     for lineno, want_line in enumerate(payload_buffer):
         line = file.readline()
 
@@ -698,11 +701,12 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
         return [-1, warning, error, unit_tests]
 
     if rule[1].strip() == "ok":
+        payload_expected = None
         try:
             payload_log = open("%s.payload" % filename_path)
             payload_expected = payload_find_expected(payload_log, rule[0])
         except:
-            payload_expected = None
+            payload_log = None
 
         if enable_json_option:
             try:
@@ -744,7 +748,10 @@ def rule_add(rule, filename, lineno, force_all_family_option, filename_path):
                 payload_log = open("%s.payload.%s" % (filename_path, table.family))
                 table_payload_expected = payload_find_expected(payload_log, rule[0])
             except:
-                if not payload_expected:
+                if not payload_log:
+                    print_error("did not find any payload information",
+                                filename_path)
+                elif not payload_expected:
                     print_error("did not find payload information for "
                                 "rule '%s'" % rule[0], payload_log.name, 1)
             if not table_payload_expected: