2 # -*- coding: utf-8 -*-
4 # By Gerald Combs <gerald@wireshark.org>
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
8 # SPDX-License-Identifier: GPL-2.0-or-later
31 capture_interface = None
49 have_libgcrypt17 = False
55 custom_profile_path = None
56 custom_profile_name = 'Custom Profile'
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')
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
71 def setCanCapture(new_cc):
74 def setCaptureInterface(iface):
75 global capture_interface
77 capture_interface = iface
80 return not sys.platform.startswith('win32')
83 if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
85 # Qt requires XKEYBOARD and Xrender, which Xvnc doesn't provide.
92 global have_libgcrypt17
96 have_libgcrypt17 = False
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):
102 if re.search('with +nghttp2', tshark_v):
104 if re.search('(with +MIT +Kerberos|with +Heimdal +Kerberos)', tshark_v):
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
111 def getDefaultCaptureInterface():
112 '''Choose a default capture interface for our platform. Currently Windows only.'''
113 global capture_interface
114 if capture_interface:
116 if cmd_dumpcap is None:
118 if not sys.platform.startswith('win32'):
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')
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)
129 capture_interface = iface_m.group(1)
134 def getPingCommand():
135 '''Return an argument list required to ping www.wireshark.org for 60 seconds.'''
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
147 def setProgramPath(path):
152 if sys.platform.startswith('win32'):
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):
161 globals()[cmd_var] = cmd_path
163 getDefaultCaptureInterface()
167 def testEnvironment():
170 def setUpTestEnvironment():
173 global custom_profile_path
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'):
181 conf_path = os.path.join(home_path, 'Wireshark')
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)
190 # Populate our UAT files
193 'dtlsdecrypttablefile',
196 'c1222_decryption_table',
197 'ikev1_decryption_table',
198 'ikev2_decryption_table',
200 for uat in uat_files:
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
208 def setUpUatFile(conf_file):
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()
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)
226 def setUpHostFiles():
229 global custom_profile_path
230 if program_path is None:
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
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'))
244 if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
247 # Initialize ourself.
249 setProgramPath(os.path.curdir)