2018-10-13 16:08:43 +00:00
#!/usr/bin/env python3
2018-04-03 00:12:23 +00:00
#
# Wireshark tests
# By Gerald Combs <gerald@wireshark.org>
#
# Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
''' Main test script '''
# To do:
# - Avoid printing Python tracebacks when we assert? It looks like we'd need
# to override unittest.TextTestResult.addFailure().
2018-04-27 23:03:24 +00:00
2018-04-03 00:12:23 +00:00
import argparse
2018-11-26 18:01:42 +00:00
import codecs
2018-04-03 00:12:23 +00:00
import os . path
2021-04-27 18:06:55 +00:00
import suite_external
2018-04-03 00:12:23 +00:00
import sys
import unittest
2018-11-15 17:44:59 +00:00
import fixtures
2018-10-15 14:07:30 +00:00
# Required to make fixtures available to tests!
import fixtures_ws
2018-04-03 00:12:23 +00:00
2018-11-15 17:44:59 +00:00
_all_test_groups = None
@fixtures.fixture ( scope = ' session ' )
def all_test_groups ( ) :
return _all_test_groups
2018-04-03 00:12:23 +00:00
def find_test_ids ( suite , all_ids ) :
if hasattr ( suite , ' __iter__ ' ) :
for s in suite :
find_test_ids ( s , all_ids )
else :
all_ids . append ( suite . id ( ) )
def main ( ) :
parser = argparse . ArgumentParser ( description = ' Wireshark unit tests ' )
cap_group = parser . add_mutually_exclusive_group ( )
cap_group . add_argument ( ' -E ' , ' --disable-capture ' , action = ' store_true ' , help = ' Disable capture tests ' )
2020-02-13 23:18:14 +00:00
release_group = parser . add_mutually_exclusive_group ( )
release_group . add_argument ( ' --enable-release ' , action = ' store_true ' , help = ' Enable release tests ' )
2018-11-15 17:44:59 +00:00
parser . add_argument ( ' -p ' , ' --program-path ' , default = os . path . curdir , help = ' Path to Wireshark executables. ' )
2021-04-27 18:06:55 +00:00
parser . add_argument ( ' -x ' , ' --add-external-tests ' , action = ' append ' , help = ' Path to an external test definition (.json) file. ' )
2019-01-21 14:05:37 +00:00
parser . add_argument ( ' --skip-missing-programs ' ,
help = ' Skip tests that lack programs from this list instead of failing '
' them. Use " all " to ignore all missing programs. ' )
2018-04-03 00:12:23 +00:00
list_group = parser . add_mutually_exclusive_group ( )
list_group . add_argument ( ' -l ' , ' --list ' , action = ' store_true ' , help = ' List tests. One of " all " or a full or partial test name. ' )
list_group . add_argument ( ' --list-suites ' , action = ' store_true ' , help = ' List all suites. ' )
2018-06-18 19:01:01 +00:00
list_group . add_argument ( ' --list-groups ' , action = ' store_true ' , help = ' List all suites and groups. ' )
list_group . add_argument ( ' --list-cases ' , action = ' store_true ' , help = ' List all suites, groups, and cases. ' )
2018-04-03 00:12:23 +00:00
parser . add_argument ( ' -v ' , ' --verbose ' , action = ' store_const ' , const = 2 , default = 1 , help = ' Verbose tests. ' )
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 " . ' )
args = parser . parse_args ( )
2021-04-27 18:06:55 +00:00
# XXX This should be a fixture.
suite_external . add_external_configs ( args . add_external_tests )
2018-05-03 19:05:12 +00:00
all_tests = unittest . defaultTestLoader . discover ( os . path . dirname ( __file__ ) , pattern = ' suite_* ' )
2018-04-03 00:12:23 +00:00
all_ids = [ ]
find_test_ids ( all_tests , all_ids )
run_ids = [ ]
for tid in all_ids :
for ttr in args . tests_to_run :
ttrl = ttr . lower ( )
if ttrl == ' all ' :
run_ids = all_ids
break
if ttrl in tid . lower ( ) :
run_ids . append ( tid )
if not run_ids :
print ( ' No tests found. You asked for: \n ' + ' \n ' . join ( args . tests_to_run ) )
parser . print_usage ( )
sys . exit ( 1 )
if args . list :
print ( ' \n ' . join ( run_ids ) )
sys . exit ( 0 )
2018-05-05 22:53:54 +00:00
all_suites = set ( )
for aid in all_ids :
aparts = aid . split ( ' . ' )
all_suites | = { aparts [ 0 ] }
2018-11-15 17:44:59 +00:00
all_suites = sorted ( all_suites )
2018-05-05 22:53:54 +00:00
2018-06-18 19:01:01 +00:00
all_groups = set ( )
for aid in all_ids :
aparts = aid . split ( ' . ' )
if aparts [ 1 ] . startswith ( ' group_ ' ) :
all_groups | = { ' . ' . join ( aparts [ : 2 ] ) }
else :
all_groups | = { aparts [ 0 ] }
2018-11-15 17:44:59 +00:00
all_groups = sorted ( all_groups )
global _all_test_groups
_all_test_groups = all_groups
2018-06-18 19:01:01 +00:00
2018-04-03 00:12:23 +00:00
if args . list_suites :
2018-11-15 17:44:59 +00:00
print ( ' \n ' . join ( all_suites ) )
2018-04-03 00:12:23 +00:00
sys . exit ( 0 )
2018-06-18 19:01:01 +00:00
if args . list_groups :
2018-11-15 17:44:59 +00:00
print ( ' \n ' . join ( all_groups ) )
2018-06-18 19:01:01 +00:00
sys . exit ( 0 )
2018-04-03 00:12:23 +00:00
if args . list_cases :
cases = set ( )
for rid in run_ids :
rparts = rid . split ( ' . ' )
cases | = { ' . ' . join ( rparts [ : 2 ] ) }
print ( ' \n ' . join ( list ( cases ) ) )
sys . exit ( 0 )
2018-11-26 18:01:42 +00:00
if codecs . lookup ( sys . stdout . encoding ) . name != ' utf-8 ' :
2018-05-21 16:02:26 +00:00
import locale
2018-11-26 18:01:42 +00:00
sys . stderr . write ( ' Warning: Output encoding is {0} and not utf-8. \n ' . format ( sys . stdout . encoding ) )
2018-10-13 16:08:43 +00:00
sys . stdout = codecs . getwriter ( locale . getpreferredencoding ( ) ) ( sys . stdout . buffer , ' backslashreplace ' )
sys . stderr = codecs . getwriter ( locale . getpreferredencoding ( ) ) ( sys . stderr . buffer , ' backslashreplace ' )
2018-05-21 16:02:26 +00:00
2018-04-03 00:12:23 +00:00
run_suite = unittest . defaultTestLoader . loadTestsFromNames ( run_ids )
runner = unittest . TextTestRunner ( verbosity = args . verbose )
2018-10-15 14:07:30 +00:00
# for unittest compatibility (not needed with pytest)
2018-11-15 17:44:59 +00:00
fixtures_ws . fixtures . create_session ( args )
2018-10-15 14:07:30 +00:00
try :
test_result = runner . run ( run_suite )
finally :
# for unittest compatibility (not needed with pytest)
fixtures_ws . fixtures . destroy_session ( )
2018-04-03 00:12:23 +00:00
if test_result . errors :
sys . exit ( 2 )
if test_result . failures :
sys . exit ( 1 )
if __name__ == ' __main__ ' :
main ( )