Test: Add sharkd tests.
authorGerald Combs <gerald@wireshark.org>
Mon, 18 Jun 2018 18:01:31 +0000 (11:01 -0700)
committerAnders Broman <a.broman58@gmail.com>
Thu, 21 Jun 2018 03:30:30 +0000 (03:30 +0000)
Change-Id: I0e5049700ab9285196ce6b4567bd2d034529e763
Reviewed-on: https://code.wireshark.org/review/28327
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
CMakeLists.txt
test/config.py
test/subprocesstest.py
test/suite_sharkd.py [new file with mode: 0644]

index 9ccfeede26d3b9fefaec81fb5403a68a14093410..a6a59543fb07eb5ce6a8018ddba87679448e633d 100644 (file)
@@ -2947,6 +2947,7 @@ set(_test_group_list
        suite_mergecap
        suite_nameres
        suite_text2pcap
+       suite_sharkd
        suite_unittests
        suite_wslua
 )
index e927f650956388dc7cf21d0073167bd9ec4a1c54..21cc90fd11c36f36410b7dffae62d8dc2cd9fb8a 100644 (file)
@@ -22,6 +22,7 @@ commands = (
     'dumpcap',
     'mergecap',
     'rawshark',
+    'sharkd',
     'text2pcap',
     'tshark',
     'wireshark',
index 8fae5da038f64d62dfc4cde63a65caf4a5e67931..9815f230e802a96c8e39874f05f76764058ecbbf 100644 (file)
@@ -265,14 +265,14 @@ class SubprocessTestCase(unittest.TestCase):
             return False
         return True
 
-    def startProcess(self, proc_args, env=None, shell=False):
+    def startProcess(self, proc_args, stdin=None, env=None, shell=False):
         '''Start a process in the background. Returns a subprocess.Popen object.
 
         You typically wait for it using waitProcess() or assertWaitProcess().'''
         if env is None:
             # Avoid using the test user's real environment by default.
             env = config.test_env
-        proc = LoggingPopen(proc_args, env=env, shell=shell, log_fd=self.log_fd)
+        proc = LoggingPopen(proc_args, stdin=stdin, env=env, shell=shell, log_fd=self.log_fd)
         self.processes.append(proc)
         return proc
 
diff --git a/test/suite_sharkd.py b/test/suite_sharkd.py
new file mode 100644 (file)
index 0000000..27eb9d5
--- /dev/null
@@ -0,0 +1,72 @@
+#
+# -*- coding: utf-8 -*-
+# Wireshark tests
+# By Gerald Combs <gerald@wireshark.org>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+'''sharkd tests'''
+
+import config
+import json
+import os.path
+import subprocess
+import subprocesstest
+import sys
+import unittest
+
+dhcp_pcap = os.path.join(config.capture_dir, 'dhcp.pcap')
+
+class case_sharkd(subprocesstest.SubprocessTestCase):
+    def test_sharkd_hello_no_pcap(self):
+        '''sharkd hello message, no capture file'''
+        sharkd_proc = self.startProcess((config.cmd_sharkd, '-'),
+            stdin=subprocess.PIPE
+        )
+
+        sharkd_commands = '{"req":"status"}\n'
+        if sys.version_info[0] >= 3:
+            sharkd_commands = sharkd_commands.encode('UTF-8')
+        sharkd_proc.stdin.write(sharkd_commands)
+        self.waitProcess(sharkd_proc)
+
+        self.assertEqual(self.countOutput('Hello in child.', count_stdout=False, count_stderr=True), 1, 'No hello message.')
+
+        try:
+            jdata = json.loads(sharkd_proc.stdout_str)
+            self.assertEqual(jdata['duration'], 0.0, 'Missing duration.')
+        except:
+            self.fail('Invalid JSON: "{}"'.format(sharkd_proc.stdout_str))
+
+    def test_sharkd_hello_dhcp_pcap(self):
+        '''sharkd hello message, simple capture file'''
+        sharkd_proc = self.startProcess((config.cmd_sharkd, '-'),
+            stdin=subprocess.PIPE
+        )
+
+        sharkd_commands = ''
+        sharkd_commands = '{"req":"load","file":' + json.JSONEncoder().encode(dhcp_pcap) + '}\n'
+        sharkd_commands += '{"req":"status"}\n'
+        sharkd_commands += '{"req":"frames"}\n'
+        if sys.version_info[0] >= 3:
+            sharkd_commands = sharkd_commands.encode('UTF-8')
+
+        sharkd_proc.stdin.write(sharkd_commands)
+        self.waitProcess(sharkd_proc)
+
+        has_dhcp = False
+        for line in sharkd_proc.stdout_str.splitlines():
+            line = line.strip()
+            if not line: continue
+            try:
+                jdata = json.loads(line)
+            except:
+                self.fail('Invalid JSON for "{}"'.format(line))
+
+            try:
+                if 'DHCP' in jdata[0]['c']:
+                    has_dhcp = True
+            except:
+                pass
+
+        self.assertTrue(has_dhcp, 'Failed to find DHCP in JSON output')