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