test: convert suite_nameres to use fixtures
[metze/wireshark/wip.git] / test / fixtures_ws.py
1 #
2 # -*- coding: utf-8 -*-
3 # Wireshark tests
4 #
5 # Copyright (c) 2018 Peter Wu <peter@lekensteyn.nl>
6 #
7 # SPDX-License-Identifier: GPL-2.0-or-later
8 #
9 '''Fixtures that are specific to Wireshark.'''
10
11 import logging
12 import os
13 import re
14 import subprocess
15 import sys
16 import tempfile
17 import types
18
19 import fixtures
20 import config
21 import subprocesstest
22
23
24 @fixtures.fixture(scope='session')
25 def program_path():
26     # XXX stop using config
27     return config.program_path
28
29
30 @fixtures.fixture(scope='session')
31 def program(program_path):
32     def resolver(name):
33         dotexe = ''
34         if sys.platform.startswith('win32'):
35             dotexe = '.exe'
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,))
39         return path
40     return resolver
41
42
43 @fixtures.fixture(scope='session')
44 def cmd_capinfos(program):
45     return program('capinfos')
46
47
48 @fixtures.fixture(scope='session')
49 def cmd_dumpcap(program):
50     return program('dumpcap')
51
52
53 @fixtures.fixture(scope='session')
54 def cmd_mergecap(program):
55     return program('mergecap')
56
57
58 @fixtures.fixture(scope='session')
59 def cmd_rawshark(program):
60     return program('rawshark')
61
62
63 @fixtures.fixture(scope='session')
64 def cmd_tshark(program):
65     return program('tshark')
66
67
68 @fixtures.fixture(scope='session')
69 def cmd_text2pcap(program):
70     return program('text2pcap')
71
72
73 @fixtures.fixture(scope='session')
74 def cmd_wireshark(program):
75     return program('wireshark')
76
77
78 @fixtures.fixture(scope='session')
79 def features(cmd_tshark):
80     '''Returns an object describing available features in tshark.'''
81     try:
82         # XXX stop using config
83         tshark_v = subprocess.check_output(
84             (cmd_tshark, '--version'),
85             stderr=subprocess.PIPE,
86             universal_newlines=True,
87             env=config.baseEnv()
88         )
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)
92         tshark_v = ''
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,
100     )
101
102
103 @fixtures.fixture(scope='session')
104 def dirs():
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'),
114     )
115
116
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)
122     return resolver
123
124
125 @fixtures.fixture
126 def home_path():
127     '''Per-test home directory, removed when finished.'''
128     with tempfile.TemporaryDirectory(prefix='wireshark-tests-home-') as dirname:
129         yield dirname
130
131
132 @fixtures.fixture
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')
137     else:
138         conf_path = os.path.join(home_path, '.config', 'wireshark')
139     os.makedirs(conf_path)
140     return conf_path
141
142
143 @fixtures.fixture
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()
148     env['TZ'] = 'UTC'
149     home_env = 'APPDATA' if sys.platform.startswith('win32') else 'HOME'
150     env[home_env] = home_path
151
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
156     return env
157
158
159 @fixtures.fixture
160 def test_env(base_env, conf_path, request):
161     '''A process environment with a populated configuration directory.'''
162     # Populate our UAT files
163     uat_files = [
164         '80211_keys',
165         'dtlsdecrypttablefile',
166         'esp_sa',
167         'ssl_keys',
168         'c1222_decryption_table',
169         'ikev1_decryption_table',
170         'ikev2_decryption_table',
171     ]
172     for uat in uat_files:
173         # XXX stop using config
174         config.setUpUatFile(conf_path, uat)
175
176     env = base_env
177     env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = '1'
178     env['WIRESHARK_QUIT_AFTER_CAPTURE'] = '1'
179
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
184     return env
185
186 # XXX capture: capture_interface