2 # -*- coding: utf-8 -*-
5 # Copyright (c) 2018 Peter Wu <peter@lekensteyn.nl>
7 # SPDX-License-Identifier: GPL-2.0-or-later
9 '''Fixtures that are specific to Wireshark.'''
24 @fixtures.fixture(scope='session')
26 # XXX stop using config
27 return config.program_path
30 @fixtures.fixture(scope='session')
31 def program(program_path):
34 if sys.platform.startswith('win32'):
36 path = os.path.normpath(os.path.join(program_path, name + dotexe))
37 if not os.access(path, os.X_OK):
38 fixtures.skip('Program %s is not available' % (name,))
43 @fixtures.fixture(scope='session')
44 def cmd_capinfos(program):
45 return program('capinfos')
48 @fixtures.fixture(scope='session')
49 def cmd_dumpcap(program):
50 return program('dumpcap')
53 @fixtures.fixture(scope='session')
54 def cmd_mergecap(program):
55 return program('mergecap')
58 @fixtures.fixture(scope='session')
59 def cmd_rawshark(program):
60 return program('rawshark')
63 @fixtures.fixture(scope='session')
64 def cmd_tshark(program):
65 return program('tshark')
68 @fixtures.fixture(scope='session')
69 def cmd_text2pcap(program):
70 return program('text2pcap')
73 @fixtures.fixture(scope='session')
74 def cmd_wireshark(program):
75 return program('wireshark')
78 @fixtures.fixture(scope='session')
79 def features(cmd_tshark):
80 '''Returns an object describing available features in tshark.'''
82 # XXX stop using config
83 tshark_v = subprocess.check_output(
84 (cmd_tshark, '--version'),
85 stderr=subprocess.PIPE,
86 universal_newlines=True,
89 tshark_v = re.sub(r'\s+', ' ', tshark_v)
90 except subprocess.CalledProcessError as ex:
91 logging.warning('Failed to detect tshark features: %s', ex)
93 gcry_m = re.search(r'with +Gcrypt +([0-9]+\.[0-9]+)', tshark_v)
94 return types.SimpleNamespace(
95 have_lua='with Lua' in tshark_v,
96 have_nghttp2='with nghttp2' in tshark_v,
97 have_kerberos='with MIT Kerberos' in tshark_v or 'with Heimdal Kerberos' in tshark_v,
98 have_libgcrypt16=gcry_m and float(gcry_m.group(1)) >= 1.6,
99 have_libgcrypt17=gcry_m and float(gcry_m.group(1)) >= 1.7,
103 @fixtures.fixture(scope='session')
105 '''Returns fixed directories containing test input.'''
106 this_dir = os.path.dirname(__file__)
107 return types.SimpleNamespace(
108 baseline_dir=os.path.join(this_dir, 'baseline'),
109 capture_dir=os.path.join(this_dir, 'captures'),
110 config_dir=os.path.join(this_dir, 'config'),
111 key_dir=os.path.join(this_dir, 'keys'),
112 lua_dir=os.path.join(this_dir, 'lua'),
113 tools_dir=os.path.join(this_dir, '..', 'tools'),
117 @fixtures.fixture(scope='session')
118 def capture_file(dirs):
119 '''Returns the path to a capture file.'''
120 def resolver(filename):
121 return os.path.join(dirs.capture_dir, filename)
127 '''Per-test home directory, removed when finished.'''
128 with tempfile.TemporaryDirectory(prefix='wireshark-tests-home-') as dirname:
133 def conf_path(home_path):
134 '''Path to the Wireshark configuration directory.'''
135 if sys.platform.startswith('win32'):
136 conf_path = os.path.join(home_path, 'Wireshark')
138 conf_path = os.path.join(home_path, '.config', 'wireshark')
139 os.makedirs(conf_path)
144 def base_env(home_path, request):
145 """A modified environment to ensure reproducible tests. Tests can modify
146 this environment as they see fit."""
147 env = os.environ.copy()
149 home_env = 'APPDATA' if sys.platform.startswith('win32') else 'HOME'
150 env[home_env] = home_path
152 # Remove this if test instances no longer inherit from SubprocessTestCase?
153 assert isinstance(request.instance, subprocesstest.SubprocessTestCase)
154 # Inject the test environment as default if it was not overridden.
155 request.instance.injected_test_env = env
160 def test_env(base_env, conf_path, request):
161 '''A process environment with a populated configuration directory.'''
162 # Populate our UAT files
165 'dtlsdecrypttablefile',
168 'c1222_decryption_table',
169 'ikev1_decryption_table',
170 'ikev2_decryption_table',
172 for uat in uat_files:
173 # XXX stop using config
174 config.setUpUatFile(conf_path, uat)
177 env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = '1'
178 env['WIRESHARK_QUIT_AFTER_CAPTURE'] = '1'
180 # Remove this if test instances no longer inherit from SubprocessTestCase?
181 assert isinstance(request.instance, subprocesstest.SubprocessTestCase)
182 # Inject the test environment as default if it was not overridden.
183 request.instance.injected_test_env = env
186 # XXX capture: capture_interface