blob: 4c014f9bd269a6a6052cd81da51790212ff8e3da [file] [log] [blame]
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02001#!/usr/bin/env python3
2# encoding: utf-8
3
4import os
5import sys
6import shlex
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02007import argparse
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +02008from subprocess import Popen, PIPE
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +02009
Florian Westphale67c0882018-04-13 17:40:56 +020010keywords = ("iptables-translate", "ip6tables-translate", "ebtables-translate")
Phil Sutter59372f02019-02-19 20:39:49 +010011xtables_nft_multi = 'xtables-nft-multi'
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020012
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020013if sys.stdout.isatty():
14 colors = {"magenta": "\033[95m", "green": "\033[92m", "yellow": "\033[93m",
15 "red": "\033[91m", "end": "\033[0m"}
16else:
17 colors = {"magenta": "", "green": "", "yellow": "", "red": "", "end": ""}
18
19
20def magenta(string):
21 return colors["magenta"] + string + colors["end"]
22
23
24def red(string):
25 return colors["red"] + string + colors["end"]
26
27
28def yellow(string):
29 return colors["yellow"] + string + colors["end"]
30
31
32def green(string):
33 return colors["green"] + string + colors["end"]
34
35
36def run_test(name, payload):
Phil Sutter59372f02019-02-19 20:39:49 +010037 global xtables_nft_multi
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020038 test_passed = True
Harsha Sharma4bd51772017-10-22 18:19:09 +053039 tests = passed = failed = errors = 0
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020040 result = []
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020041
42 for line in payload:
43 if line.startswith(keywords):
Harsha Sharma4bd51772017-10-22 18:19:09 +053044 tests += 1
Phil Sutter59372f02019-02-19 20:39:49 +010045 process = Popen([ xtables_nft_multi ] + shlex.split(line), stdout=PIPE, stderr=PIPE)
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020046 (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:
Harsha Sharma4bd51772017-10-22 18:19:09 +053051 test_passed = False
52 failed += 1
53 result.append(name + ": " + red("Fail"))
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020054 result.append(magenta("src: ") + line.rstrip(" \n"))
55 result.append(magenta("exp: ") + expected)
56 result.append(magenta("res: ") + translation + "\n")
57 test_passed = False
Harsha Sharma4bd51772017-10-22 18:19:09 +053058 else:
59 passed += 1
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020060 else:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020061 test_passed = False
Harsha Sharma4bd51772017-10-22 18:19:09 +053062 errors += 1
63 result.append(name + ": " + red("Error: ") + "iptables-translate failure")
Pablo M. Bermudo Garay3f92b252017-04-19 01:19:08 +020064 result.append(error.decode("utf-8"))
Harsha Sharma4bd51772017-10-22 18:19:09 +053065 if (passed == tests) and not args.test:
66 print(name + ": " + green("OK"))
67 if not test_passed:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020068 print("\n".join(result))
Harsha Sharma4bd51772017-10-22 18:19:09 +053069 if args.test:
70 print("1 test file, %d tests, %d tests passed, %d tests failed, %d errors" % (tests, passed, failed, errors))
71 else:
72 return tests, passed, failed, errors
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020073
74
75def load_test_files():
Harsha Sharma4bd51772017-10-22 18:19:09 +053076 test_files = total_tests = total_passed = total_error = total_failed = 0
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020077 for test in sorted(os.listdir("extensions")):
78 if test.endswith(".txlate"):
79 with open("extensions/" + test, "r") as payload:
Harsha Sharma4bd51772017-10-22 18:19:09 +053080 tests, passed, failed, errors = run_test(test, payload)
81 test_files += 1
82 total_tests += tests
83 total_passed += passed
84 total_failed += failed
85 total_error += errors
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020086
87
Harsha Sharma4bd51772017-10-22 18:19:09 +053088 print("%d test files, %d tests, %d tests passed, %d tests failed, %d errors" % (test_files, total_tests, total_passed, total_failed, total_error))
89
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020090def main():
Phil Sutter59372f02019-02-19 20:39:49 +010091 global xtables_nft_multi
92 if not args.host:
93 os.putenv("XTABLES_LIBDIR", os.path.abspath("extensions"))
94 xtables_nft_multi = os.path.abspath(os.path.curdir) \
95 + '/iptables/' + xtables_nft_multi
96
Florian Westphal255e55b2018-01-26 23:31:48 +010097 if args.test:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +020098 if not args.test.endswith(".txlate"):
99 args.test += ".txlate"
100 try:
Harsha Sharma4bd51772017-10-22 18:19:09 +0530101 with open(args.test, "r") as payload:
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +0200102 run_test(args.test, payload)
103 except IOError:
104 print(red("Error: ") + "test file does not exist")
105 else:
106 load_test_files()
107
108
109parser = argparse.ArgumentParser()
Phil Sutter59372f02019-02-19 20:39:49 +0100110parser.add_argument('-H', '--host', action='store_true',
111 help='Run tests against installed binaries')
Pablo M. Bermudo Garay340105f2017-03-31 14:34:38 +0200112parser.add_argument("test", nargs="?", help="run only the specified test file")
113args = parser.parse_args()
114main()