2 # -*- coding: utf-8 -*-
5 # By Gerald Combs <gerald@wireshark.org>
7 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
9 # SPDX-License-Identifier: GPL-2.0-or-later
11 '''Main test script'''
14 # - Avoid printing Python tracebacks when we assert? It looks like we'd need
15 # to override unittest.TextTestResult.addFailure().
16 # - Switch to Python 3 only? [Windows, Linux, macOS] x [Python 2, Python 3]
18 # - Remove BIN_PATH/hosts via config.tearDownHostFiles + case_name_resolution.tearDownClass?
27 def find_test_ids(suite, all_ids):
28 if hasattr(suite, '__iter__'):
30 find_test_ids(s, all_ids)
32 all_ids.append(suite.id())
34 def dump_failed_output(suite):
35 if hasattr(suite, '__iter__'):
37 dump_failures = getattr(s, 'dump_failures', None)
44 parser = argparse.ArgumentParser(description='Wireshark unit tests')
45 cap_group = parser.add_mutually_exclusive_group()
46 cap_group.add_argument('-e', '--enable-capture', action='store_true', help='Enable capture tests')
47 cap_group.add_argument('-E', '--disable-capture', action='store_true', help='Disable capture tests')
48 cap_group.add_argument('-i', '--capture-interface', nargs=1, default=None, help='Capture interface index or name')
49 parser.add_argument('-p', '--program-path', nargs=1, default=os.path.curdir, help='Path to Wireshark executables.')
50 list_group = parser.add_mutually_exclusive_group()
51 list_group.add_argument('-l', '--list', action='store_true', help='List tests. One of "all" or a full or partial test name.')
52 list_group.add_argument('--list-suites', action='store_true', help='List all suites.')
53 list_group.add_argument('--list-groups', action='store_true', help='List all suites and groups.')
54 list_group.add_argument('--list-cases', action='store_true', help='List all suites, groups, and cases.')
55 parser.add_argument('-v', '--verbose', action='store_const', const=2, default=1, help='Verbose tests.')
56 parser.add_argument('tests_to_run', nargs='*', metavar='test', default=['all'], help='Tests to run. One of "all" or a full or partial test name. Default is "all".')
57 args = parser.parse_args()
59 if args.enable_capture:
60 config.setCanCapture(True)
61 elif args.disable_capture:
62 config.setCanCapture(False)
64 if args.capture_interface:
65 config.setCaptureInterface(args.capture_interface[0])
67 all_tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='suite_*')
70 find_test_ids(all_tests, all_ids)
74 for ttr in args.tests_to_run:
79 if ttrl in tid.lower():
83 print('No tests found. You asked for:\n ' + '\n '.join(args.tests_to_run))
88 print('\n'.join(run_ids))
93 aparts = aid.split('.')
94 all_suites |= {aparts[0]}
95 config.all_suites = list(all_suites)
96 config.all_suites.sort()
100 aparts = aid.split('.')
101 if aparts[1].startswith('group_'):
102 all_groups |= {'.'.join(aparts[:2])}
104 all_groups |= {aparts[0]}
105 config.all_groups = list(all_groups)
106 config.all_groups.sort()
109 print('\n'.join(config.all_suites))
113 print('\n'.join(config.all_groups))
119 rparts = rid.split('.')
120 cases |= {'.'.join(rparts[:2])}
121 print('\n'.join(list(cases)))
124 program_path = args.program_path[0]
125 if not config.setProgramPath(program_path):
126 print('One or more required executables not found at {}\n'.format(program_path))
131 if sys.stdout.encoding != 'UTF-8':
134 sys.stderr.write('Warning: Output encoding is {0} and not UTF-8.\n'.format(sys.stdout.encoding))
135 if sys.version_info[0] >= 3:
136 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout.buffer, 'backslashreplace')
137 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr.buffer, 'backslashreplace')
139 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout, 'backslashreplace')
140 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr, 'backslashreplace')
142 run_suite = unittest.defaultTestLoader.loadTestsFromNames(run_ids)
143 runner = unittest.TextTestRunner(verbosity=args.verbose)
144 test_result = runner.run(run_suite)
146 dump_failed_output(run_suite)
148 if test_result.errors:
151 if test_result.failures:
154 if __name__ == '__main__':