Test+CMake: Make our test grouping more fine-grained.
[metze/wireshark/wip.git] / test / config.py
1 #
2 # -*- coding: utf-8 -*-
3 # Wireshark tests
4 # By Gerald Combs <gerald@wireshark.org>
5 #
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 #
8 # SPDX-License-Identifier: GPL-2.0-or-later
9 #
10 '''Configuration'''
11
12 import os
13 import os.path
14 import re
15 import shutil
16 import subprocess
17 import sys
18 import tempfile
19
20 commands = (
21     'capinfos',
22     'dumpcap',
23     'mergecap',
24     'rawshark',
25     'text2pcap',
26     'tshark',
27     'wireshark',
28 )
29
30 can_capture = False
31 capture_interface = None
32
33 # Our executables
34 program_path = None
35 # Strings
36 cmd_capinfos = None
37 cmd_dumpcap = None
38 cmd_mergecap = None
39 cmd_rawshark = None
40 cmd_tshark = None
41 cmd_text2pcap = None
42 cmd_wireshark = None
43 # Arrays
44 args_ping = None
45
46 have_lua = False
47 have_nghttp2 = False
48 have_kerberos = False
49 have_libgcrypt16 = False
50 have_libgcrypt17 = False
51
52 test_env = None
53 program_path = None
54 home_path = None
55 conf_path = None
56 custom_profile_path = None
57 custom_profile_name = 'Custom Profile'
58
59 this_dir = os.path.dirname(__file__)
60 baseline_dir = os.path.join(this_dir, 'baseline')
61 capture_dir = os.path.join(this_dir, 'captures')
62 config_dir = os.path.join(this_dir, 'config')
63 key_dir = os.path.join(this_dir, 'keys')
64 lua_dir = os.path.join(this_dir, 'lua')
65 tools_dir = os.path.join(this_dir, '..', 'tools')
66
67 all_groups = []
68
69 def canCapture():
70     # XXX This appears to be evaluated at the wrong time when called
71     # from a unittest.skipXXX decorator.
72     return can_capture and capture_interface is not None
73
74 def setCanCapture(new_cc):
75     can_capture = new_cc
76
77 def setCaptureInterface(iface):
78     global capture_interface
79     setCanCapture(True)
80     capture_interface = iface
81
82 def canMkfifo():
83     return not sys.platform.startswith('win32')
84
85 def canDisplay():
86     if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
87         return True
88     # Qt requires XKEYBOARD and Xrender, which Xvnc doesn't provide.
89     return False
90
91 def getTsharkInfo():
92     global have_lua
93     global have_nghttp2
94     global have_kerberos
95     global have_libgcrypt16
96     global have_libgcrypt17
97     have_lua = False
98     have_nghttp2 = False
99     have_kerberos = False
100     have_libgcrypt16 = False
101     have_libgcrypt17 = False
102     try:
103         tshark_v_blob = str(subprocess.check_output((cmd_tshark, '--version'), stderr=subprocess.PIPE))
104         tshark_v = ' '.join(tshark_v_blob.splitlines())
105         if re.search('with +Lua', tshark_v):
106             have_lua = True
107         if re.search('with +nghttp2', tshark_v):
108             have_nghttp2 = True
109         if re.search('(with +MIT +Kerberos|with +Heimdal +Kerberos)', tshark_v):
110             have_kerberos = True
111         gcry_m = re.search('with +Gcrypt +([0-9]+\.[0-9]+)', tshark_v)
112         have_libgcrypt16 = gcry_m and float(gcry_m.group(1)) >= 1.6
113         have_libgcrypt17 = gcry_m and float(gcry_m.group(1)) >= 1.7
114     except:
115         pass
116
117 def getDefaultCaptureInterface():
118     '''Choose a default capture interface for our platform. Currently Windows only.'''
119     global capture_interface
120     if capture_interface:
121         return
122     if cmd_dumpcap is None:
123         return
124     if not sys.platform.startswith('win32'):
125         return
126     try:
127         dumpcap_d_data = subprocess.check_output((cmd_dumpcap, '-D'), stderr=subprocess.PIPE)
128         if sys.version_info[0] >= 3:
129             dumpcap_d_stdout = dumpcap_d_data.decode('UTF-8', 'replace')
130         else:
131             dumpcap_d_stdout = unicode(dumpcap_d_data, 'UTF-8', 'replace')
132         for d_line in dumpcap_d_stdout.splitlines():
133             iface_m = re.search('(\d+)\..*(Ethernet|Network Connection|VMware|Intel)', d_line)
134             if iface_m:
135                 capture_interface = iface_m.group(1)
136                 break
137     except:
138         pass
139
140 def getPingCommand():
141     '''Return an argument list required to ping www.wireshark.org for 60 seconds.'''
142     global args_ping
143     # XXX The shell script tests swept over packet sizes from 1 to 240 every 0.25 seconds.
144     if sys.platform.startswith('win32'):
145         # XXX Check for psping? https://docs.microsoft.com/en-us/sysinternals/downloads/psping
146         args_ping = ('ping', '-n', '60', '-l', '100', 'www.wireshark.org')
147     elif sys.platform.startswith('linux') or sys.platform.startswith('freebsd'):
148         args_ping = ('ping', '-c', '240', '-s', '100', '-i', '0.25', 'www.wireshark.org')
149     elif sys.platform.startswith('darwin'):
150         args_ping = ('ping', '-c', '1', '-g', '1', '-G', '240', '-i', '0.25', 'www.wireshark.org')
151     # XXX Other BSDs, Solaris, etc
152
153 def setProgramPath(path):
154     global program_path
155     program_path = path
156     retval = True
157     dotexe = ''
158     if sys.platform.startswith('win32'):
159         dotexe = '.exe'
160     for cmd in commands:
161         cmd_var = 'cmd_' + cmd
162         cmd_path = os.path.normpath(os.path.join(path, cmd + dotexe))
163         if not os.path.exists(cmd_path) or not os.access(cmd_path, os.X_OK):
164             cmd_path = None
165             program_path = None
166             retval = False
167         globals()[cmd_var] = cmd_path
168     getTsharkInfo()
169     getDefaultCaptureInterface()
170     setUpHostFiles()
171     return retval
172
173 def testEnvironment():
174     return test_env
175
176 def setUpTestEnvironment():
177     global home_path
178     global conf_path
179     global custom_profile_path
180     global test_env
181
182     # Create our directories
183     test_confdir = tempfile.mkdtemp(prefix='wireshark-tests.')
184     home_path = os.path.join(test_confdir, 'home')
185     if sys.platform.startswith('win32'):
186         home_env = 'APPDATA'
187         conf_path = os.path.join(home_path, 'Wireshark')
188     else:
189         home_env = 'HOME'
190         conf_path = os.path.join(home_path, '.config', 'wireshark')
191     os.makedirs(conf_path)
192     # Test spaces while we're here.
193     custom_profile_path = os.path.join(conf_path, 'profiles', custom_profile_name)
194     os.makedirs(custom_profile_path)
195
196     # Populate our UAT files
197     uat_files = [
198         '80211_keys',
199         'dtlsdecrypttablefile',
200         'esp_sa',
201         'ssl_keys',
202         'c1222_decryption_table',
203         'ikev1_decryption_table',
204         'ikev2_decryption_table',
205     ]
206     for uat in uat_files:
207         setUpUatFile(uat)
208
209     # Set up our environment
210     test_env = os.environ.copy()
211     test_env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = 'True'
212     test_env['WIRESHARK_QUIT_AFTER_CAPTURE'] = 'True'
213     test_env['TZ'] = 'UTC'
214     test_env[home_env] = home_path
215
216 def setUpUatFile(conf_file):
217     global home_path
218     global conf_path
219     if home_path is None or conf_path is None:
220         setUpTestEnvironment()
221     template = os.path.join(os.path.dirname(__file__), 'config', conf_file) + '.tmpl'
222     with open(template, 'r') as tplt_fd:
223         tplt_contents = tplt_fd.read()
224         tplt_fd.close()
225         key_dir_path = os.path.join(key_dir, '')
226         # uat.c replaces backslashes...
227         key_dir_path = key_dir_path.replace('\\', '\\x5c')
228         cf_contents = tplt_contents.replace('TEST_KEYS_DIR', key_dir_path)
229     out_file = os.path.join(conf_path, conf_file)
230     with open(out_file, 'w') as cf_fd:
231         cf_fd.write(cf_contents)
232         cf_fd.close()
233
234 def setUpHostFiles():
235     global program_path
236     global conf_path
237     global custom_profile_path
238     if program_path is None:
239         return
240     if conf_path is None or custom_profile_path is None:
241         setUpTestEnvironment()
242     bundle_path = os.path.join(program_path, 'Wireshark.app', 'Contents', 'MacOS')
243     if os.path.isdir(bundle_path):
244         global_path = bundle_path
245     else:
246         global_path = program_path
247     hosts_path_pfx = os.path.join(this_dir, 'hosts.')
248     shutil.copyfile(hosts_path_pfx + 'global', os.path.join(global_path, 'hosts'))
249     shutil.copyfile(hosts_path_pfx + 'personal', os.path.join(conf_path, 'hosts'))
250     shutil.copyfile(hosts_path_pfx + 'custom', os.path.join(custom_profile_path, 'hosts'))
251
252 if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
253     can_capture = True
254
255 # Initialize ourself.
256 getPingCommand()
257 setProgramPath(os.path.curdir)