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