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