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