test: do not silently ignore feature detection failures
[metze/wireshark/wip.git] / test / config.py
index 5b34d13f40846656189e6bfcb55a865acc33a67c..240875140c0622f88a501f7e12d68c7c8a5d0de1 100644 (file)
@@ -9,9 +9,11 @@
 #
 '''Configuration'''
 
+import logging
 import os
 import os.path
 import re
+import shutil
 import subprocess
 import sys
 import tempfile
@@ -19,7 +21,10 @@ import tempfile
 commands = (
     'capinfos',
     'dumpcap',
+    'mergecap',
     'rawshark',
+    'sharkd',
+    'text2pcap',
     'tshark',
     'wireshark',
 )
@@ -28,11 +33,14 @@ can_capture = False
 capture_interface = None
 
 # Our executables
+program_path = None
 # Strings
 cmd_capinfos = None
 cmd_dumpcap = None
+cmd_mergecap = None
 cmd_rawshark = None
 cmd_tshark = None
+cmd_text2pcap = None
 cmd_wireshark = None
 # Arrays
 args_ping = None
@@ -40,17 +48,25 @@ args_ping = None
 have_lua = False
 have_nghttp2 = False
 have_kerberos = False
+have_libgcrypt16 = False
 have_libgcrypt17 = False
 
 test_env = None
+program_path = None
 home_path = None
 conf_path = None
+custom_profile_path = None
+custom_profile_name = 'Custom Profile'
+
 this_dir = os.path.dirname(__file__)
 baseline_dir = os.path.join(this_dir, 'baseline')
 capture_dir = os.path.join(this_dir, 'captures')
 config_dir = os.path.join(this_dir, 'config')
 key_dir = os.path.join(this_dir, 'keys')
 lua_dir = os.path.join(this_dir, 'lua')
+tools_dir = os.path.join(this_dir, '..', 'tools')
+
+all_groups = []
 
 def canCapture():
     # XXX This appears to be evaluated at the wrong time when called
@@ -78,24 +94,27 @@ def getTsharkInfo():
     global have_lua
     global have_nghttp2
     global have_kerberos
+    global have_libgcrypt16
     global have_libgcrypt17
-    have_lua = False
-    have_nghttp2 = False
-    have_kerberos = False
-    have_libgcrypt17 = False
+    if not cmd_tshark:
+        logging.warning("tshark binary is not yet set")
+        return
     try:
-        tshark_v_blob = str(subprocess.check_output((cmd_tshark, '--version'), stderr=subprocess.PIPE))
-        tshark_v = ' '.join(tshark_v_blob.splitlines())
-        if re.search('with +Lua', tshark_v):
-            have_lua = True
-        if re.search('with +nghttp2', tshark_v):
-            have_nghttp2 = True
-        if re.search('(with +MIT +Kerberos|with +Heimdal +Kerberos)', tshark_v):
-            have_kerberos = True
-        gcry_m = re.search('with +Gcrypt +([0-9]+\.[0-9]+)', tshark_v)
-        have_libgcrypt17 = gcry_m and float(gcry_m.group(1)) >= 1.7
-    except:
-        pass
+        tshark_v = subprocess.check_output(
+            (cmd_tshark, '--version'),
+            stderr=subprocess.PIPE,
+            universal_newlines=True,
+            env={'WIRESHARK_CONFIG_DIR': '/dummy/non/existing'}
+        ).replace('\n', ' ')
+    except subprocess.CalledProcessError as e:
+        logging.warning("Failed to detect tshark features: %s", e)
+        tshark_v = ''
+    have_lua = bool(re.search('with +Lua', tshark_v))
+    have_nghttp2 = bool(re.search('with +nghttp2', tshark_v))
+    have_kerberos = bool(re.search('(with +MIT +Kerberos|with +Heimdal +Kerberos)', tshark_v))
+    gcry_m = re.search('with +Gcrypt +([0-9]+\.[0-9]+)', tshark_v)
+    have_libgcrypt16 = gcry_m and float(gcry_m.group(1)) >= 1.6
+    have_libgcrypt17 = gcry_m and float(gcry_m.group(1)) >= 1.7
 
 def getDefaultCaptureInterface():
     '''Choose a default capture interface for our platform. Currently Windows only.'''
@@ -107,8 +126,12 @@ def getDefaultCaptureInterface():
     if not sys.platform.startswith('win32'):
         return
     try:
-        dumpcap_d_blob = str(subprocess.check_output((cmd_dumpcap, '-D'), stderr=subprocess.PIPE))
-        for d_line in dumpcap_d_blob.splitlines():
+        dumpcap_d_data = subprocess.check_output((cmd_dumpcap, '-D'), stderr=subprocess.PIPE)
+        if sys.version_info[0] >= 3:
+            dumpcap_d_stdout = dumpcap_d_data.decode('UTF-8', 'replace')
+        else:
+            dumpcap_d_stdout = unicode(dumpcap_d_data, 'UTF-8', 'replace')
+        for d_line in dumpcap_d_stdout.splitlines():
             iface_m = re.search('(\d+)\..*(Ethernet|Network Connection|VMware|Intel)', d_line)
             if iface_m:
                 capture_interface = iface_m.group(1)
@@ -138,13 +161,15 @@ def setProgramPath(path):
         dotexe = '.exe'
     for cmd in commands:
         cmd_var = 'cmd_' + cmd
-        cmd_path = os.path.join(path, cmd + dotexe)
+        cmd_path = os.path.normpath(os.path.join(path, cmd + dotexe))
         if not os.path.exists(cmd_path) or not os.access(cmd_path, os.X_OK):
             cmd_path = None
+            program_path = None
             retval = False
         globals()[cmd_var] = cmd_path
     getTsharkInfo()
     getDefaultCaptureInterface()
+    setUpHostFiles()
     return retval
 
 def testEnvironment():
@@ -153,7 +178,10 @@ def testEnvironment():
 def setUpTestEnvironment():
     global home_path
     global conf_path
+    global custom_profile_path
     global test_env
+
+    # Create our directories
     test_confdir = tempfile.mkdtemp(prefix='wireshark-tests.')
     home_path = os.path.join(test_confdir, 'home')
     if sys.platform.startswith('win32'):
@@ -163,10 +191,31 @@ def setUpTestEnvironment():
         home_env = 'HOME'
         conf_path = os.path.join(home_path, '.config', 'wireshark')
     os.makedirs(conf_path)
+    # Test spaces while we're here.
+    custom_profile_path = os.path.join(conf_path, 'profiles', custom_profile_name)
+    os.makedirs(custom_profile_path)
+
+    # Populate our UAT files
+    uat_files = [
+        '80211_keys',
+        'dtlsdecrypttablefile',
+        'esp_sa',
+        'ssl_keys',
+        'c1222_decryption_table',
+        'ikev1_decryption_table',
+        'ikev2_decryption_table',
+    ]
+    for uat in uat_files:
+        setUpUatFile(uat)
+
+    # Set up our environment
     test_env = os.environ.copy()
+    test_env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = 'True'
+    test_env['WIRESHARK_QUIT_AFTER_CAPTURE'] = 'True'
+    test_env['TZ'] = 'UTC'
     test_env[home_env] = home_path
 
-def setUpConfigFile(conf_file):
+def setUpUatFile(conf_file):
     global home_path
     global conf_path
     if home_path is None or conf_path is None:
@@ -184,9 +233,26 @@ def setUpConfigFile(conf_file):
         cf_fd.write(cf_contents)
         cf_fd.close()
 
+def setUpHostFiles():
+    global program_path
+    global conf_path
+    global custom_profile_path
+    if program_path is None:
+        return
+    if conf_path is None or custom_profile_path is None:
+        setUpTestEnvironment()
+    bundle_path = os.path.join(program_path, 'Wireshark.app', 'Contents', 'MacOS')
+    if os.path.isdir(bundle_path):
+        global_path = bundle_path
+    else:
+        global_path = program_path
+    hosts_path_pfx = os.path.join(this_dir, 'hosts.')
+    shutil.copyfile(hosts_path_pfx + 'global', os.path.join(global_path, 'hosts'))
+    shutil.copyfile(hosts_path_pfx + 'personal', os.path.join(conf_path, 'hosts'))
+    shutil.copyfile(hosts_path_pfx + 'custom', os.path.join(custom_profile_path, 'hosts'))
+
 if sys.platform.startswith('win32') or sys.platform.startswith('darwin'):
     can_capture = True
 
 # Initialize ourself.
 getPingCommand()
-setProgramPath(os.path.curdir)