Test: Add wslua.
[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_blob = str(subprocess.check_output((cmd_dumpcap, '-D'), stderr=subprocess.PIPE))
122         for d_line in dumpcap_d_blob.splitlines():
123             iface_m = re.search('(\d+)\..*(Ethernet|Network Connection|VMware|Intel)', d_line)
124             if iface_m:
125                 capture_interface = iface_m.group(1)
126                 break
127     except:
128         pass
129
130 def getPingCommand():
131     '''Return an argument list required to ping www.wireshark.org for 60 seconds.'''
132     global args_ping
133     # XXX The shell script tests swept over packet sizes from 1 to 240 every 0.25 seconds.
134     if sys.platform.startswith('win32'):
135         # XXX Check for psping? https://docs.microsoft.com/en-us/sysinternals/downloads/psping
136         args_ping = ('ping', '-n', '60', '-l', '100', 'www.wireshark.org')
137     elif sys.platform.startswith('linux') or sys.platform.startswith('freebsd'):
138         args_ping = ('ping', '-c', '240', '-s', '100', '-i', '0.25', 'www.wireshark.org')
139     elif sys.platform.startswith('darwin'):
140         args_ping = ('ping', '-c', '1', '-g', '1', '-G', '240', '-i', '0.25', 'www.wireshark.org')
141     # XXX Other BSDs, Solaris, etc
142
143 def setProgramPath(path):
144     global program_path
145     program_path = path
146     retval = True
147     dotexe = ''
148     if sys.platform.startswith('win32'):
149         dotexe = '.exe'
150     for cmd in commands:
151         cmd_var = 'cmd_' + cmd
152         cmd_path = os.path.normpath(os.path.join(path, cmd + dotexe))
153         if not os.path.exists(cmd_path) or not os.access(cmd_path, os.X_OK):
154             cmd_path = None
155             program_path = None
156             retval = False
157         globals()[cmd_var] = cmd_path
158     getTsharkInfo()
159     getDefaultCaptureInterface()
160     setUpHostFiles()
161     return retval
162
163 def testEnvironment():
164     return test_env
165
166 def setUpTestEnvironment():
167     global home_path
168     global conf_path
169     global custom_profile_path
170     global test_env
171
172     # Create our directories
173     test_confdir = tempfile.mkdtemp(prefix='wireshark-tests.')
174     home_path = os.path.join(test_confdir, 'home')
175     if sys.platform.startswith('win32'):
176         home_env = 'APPDATA'
177         conf_path = os.path.join(home_path, 'Wireshark')
178     else:
179         home_env = 'HOME'
180         conf_path = os.path.join(home_path, '.config', 'wireshark')
181     os.makedirs(conf_path)
182     # Test spaces while we're here.
183     custom_profile_path = os.path.join(conf_path, 'profiles', custom_profile_name)
184     os.makedirs(custom_profile_path)
185
186     # Populate our UAT files
187     uat_files = [
188         '80211_keys',
189         'dtlsdecrypttablefile',
190         'esp_sa',
191         'ssl_keys',
192         'c1222_decryption_table',
193         'ikev1_decryption_table',
194         'ikev2_decryption_table',
195     ]
196     for uat in uat_files:
197         setUpUatFile(uat)
198
199     # Set up our environment
200     test_env = os.environ.copy()
201     test_env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = '1'
202     test_env[home_env] = home_path
203
204 def setUpUatFile(conf_file):
205     global home_path
206     global conf_path
207     if home_path is None or conf_path is None:
208         setUpTestEnvironment()
209     template = os.path.join(os.path.dirname(__file__), 'config', conf_file) + '.tmpl'
210     with open(template, 'r') as tplt_fd:
211         tplt_contents = tplt_fd.read()
212         tplt_fd.close()
213         key_dir_path = os.path.join(key_dir, '')
214         # uat.c replaces backslashes...
215         key_dir_path = key_dir_path.replace('\\', '\\x5c')
216         cf_contents = tplt_contents.replace('TEST_KEYS_DIR', key_dir_path)
217     out_file = os.path.join(conf_path, conf_file)
218     with open(out_file, 'w') as cf_fd:
219         cf_fd.write(cf_contents)
220         cf_fd.close()
221
222 def setUpHostFiles():
223     global program_path
224     global conf_path
225     global custom_profile_path
226     if program_path is None:
227         return
228     if conf_path is None or custom_profile_path is None:
229         setUpTestEnvironment()
230     bundle_path = os.path.join(program_path, 'Wireshark.app', 'Contents', 'MacOS')
231     if os.path.isdir(bundle_path):
232         global_path = bundle_path
233     else:
234         global_path = program_path
235     hosts_path_pfx = os.path.join(this_dir, 'hosts.')
236     shutil.copyfile(hosts_path_pfx + 'global', os.path.join(global_path, 'hosts'))
237     shutil.copyfile(hosts_path_pfx + 'personal', os.path.join(conf_path, 'hosts'))
238     shutil.copyfile(hosts_path_pfx + 'custom', os.path.join(custom_profile_path, 'hosts'))
239
240 if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
241     can_capture = True
242
243 # Initialize ourself.
244 getPingCommand()
245 setProgramPath(os.path.curdir)