]>
git.ipfire.org Git - thirdparty/iptables.git/blob - xlate-test.py
8 from subprocess
import Popen
, PIPE
10 keywords
= ("iptables-translate", "ip6tables-translate", "ebtables-translate")
11 xtables_nft_multi
= 'xtables-nft-multi'
13 if sys
.stdout
.isatty():
14 colors
= {"magenta": "\033[95m", "green": "\033[92m", "yellow": "\033[93m",
15 "red": "\033[91m", "end": "\033[0m"}
17 colors
= {"magenta": "", "green": "", "yellow": "", "red": "", "end": ""}
21 return colors
["magenta"] + string
+ colors
["end"]
25 return colors
["red"] + string
+ colors
["end"]
29 return colors
["yellow"] + string
+ colors
["end"]
33 return colors
["green"] + string
+ colors
["end"]
36 def run_test(name
, payload
):
37 global xtables_nft_multi
39 tests
= passed
= failed
= errors
= 0
43 if line
.startswith(keywords
):
45 process
= Popen([ xtables_nft_multi
] + shlex
.split(line
), stdout
=PIPE
, stderr
=PIPE
)
46 (output
, error
) = process
.communicate()
47 if process
.returncode
== 0:
48 translation
= output
.decode("utf-8").rstrip(" \n")
49 expected
= next(payload
).rstrip(" \n")
50 if translation
!= expected
:
53 result
.append(name
+ ": " + red("Fail"))
54 result
.append(magenta("src: ") + line
.rstrip(" \n"))
55 result
.append(magenta("exp: ") + expected
)
56 result
.append(magenta("res: ") + translation
+ "\n")
63 result
.append(name
+ ": " + red("Error: ") + "iptables-translate failure")
64 result
.append(error
.decode("utf-8"))
65 if (passed
== tests
) and not args
.test
:
66 print(name
+ ": " + green("OK"))
68 print("\n".join(result
))
70 print("1 test file, %d tests, %d tests passed, %d tests failed, %d errors" % (tests
, passed
, failed
, errors
))
72 return tests
, passed
, failed
, errors
75 def load_test_files():
76 test_files
= total_tests
= total_passed
= total_error
= total_failed
= 0
77 for test
in sorted(os
.listdir("extensions")):
78 if test
.endswith(".txlate"):
79 with
open("extensions/" + test
, "r") as payload
:
80 tests
, passed
, failed
, errors
= run_test(test
, payload
)
83 total_passed
+= passed
84 total_failed
+= failed
88 print("%d test files, %d tests, %d tests passed, %d tests failed, %d errors" % (test_files
, total_tests
, total_passed
, total_failed
, total_error
))
91 global xtables_nft_multi
93 os
.putenv("XTABLES_LIBDIR", os
.path
.abspath("extensions"))
94 xtables_nft_multi
= os
.path
.abspath(os
.path
.curdir
) \
95 + '/iptables/' + xtables_nft_multi
98 if not args
.test
.endswith(".txlate"):
99 args
.test
+= ".txlate"
101 with
open(args
.test
, "r") as payload
:
102 run_test(args
.test
, payload
)
104 print(red("Error: ") + "test file does not exist")
109 parser
= argparse
.ArgumentParser()
110 parser
.add_argument('-H', '--host', action
='store_true',
111 help='Run tests against installed binaries')
112 parser
.add_argument("test", nargs
="?", help="run only the specified test file")
113 args
= parser
.parse_args()