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