diff --git a/tools/dftestfiles/arp.pcap b/test/captures/arp.pcap similarity index 100% rename from tools/dftestfiles/arp.pcap rename to test/captures/arp.pcap diff --git a/tools/dftestfiles/http.pcap b/test/captures/http.pcap similarity index 100% rename from tools/dftestfiles/http.pcap rename to test/captures/http.pcap diff --git a/tools/dftestfiles/ipv6.pcap b/test/captures/ipv6.pcap similarity index 100% rename from tools/dftestfiles/ipv6.pcap rename to test/captures/ipv6.pcap diff --git a/tools/dftestfiles/ipx_rip.pcap b/test/captures/ipx_rip.pcap similarity index 100% rename from tools/dftestfiles/ipx_rip.pcap rename to test/captures/ipx_rip.pcap diff --git a/tools/dftestfiles/nfs.pcap b/test/captures/nfs.pcap similarity index 100% rename from tools/dftestfiles/nfs.pcap rename to test/captures/nfs.pcap diff --git a/tools/dftestfiles/ntp.pcap b/test/captures/ntp.pcap similarity index 100% rename from tools/dftestfiles/ntp.pcap rename to test/captures/ntp.pcap diff --git a/tools/dftestfiles/tftp.pcap b/test/captures/tftp.pcap similarity index 100% rename from tools/dftestfiles/tftp.pcap rename to test/captures/tftp.pcap diff --git a/test/config.py b/test/config.py index 0ff2698d06..cffa59fb69 100644 --- a/test/config.py +++ b/test/config.py @@ -203,6 +203,7 @@ def setUpTestEnvironment(): # Set up our environment test_env = os.environ.copy() test_env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = '1' + test_env['TZ'] = 'UTC' test_env[home_env] = home_path def setUpUatFile(conf_file): diff --git a/test/subprocesstest.py b/test/subprocesstest.py index cfef797280..61bab5f970 100644 --- a/test/subprocesstest.py +++ b/test/subprocesstest.py @@ -221,21 +221,32 @@ class SubprocessTestCase(unittest.TestCase): got_num_packets = True self.assertTrue(got_num_packets, 'Failed to capture exactly {} packets'.format(num_packets)) - def countOutput(self, search_pat, proc=None): + def countOutput(self, search_pat=None, count_stdout=True, count_stderr=False, proc=None): '''Returns the number of output lines (search_pat=None), otherwise returns a match count.''' match_count = 0 + self.assertTrue(count_stdout or count_stderr, 'No output to count.') + if proc is None: proc = self.processes[-1] - # We might want to let the caller decide what we're searching. - out_data = proc.stdout_str + proc.stderr_str + + out_data = u'' + if count_stdout: + out_data = proc.stdout_str + if count_stderr: + out_data += proc.stderr_str + + if search_pat is None: + return len(out_data.splitlines()) + search_re = re.compile(search_pat) for line in out_data.splitlines(): if search_re.search(line): match_count += 1 + return match_count def grepOutput(self, search_pat, proc=None): - return self.countOutput(search_pat, proc) > 0 + return self.countOutput(search_pat, count_stderr=True, proc=proc) > 0 def diffOutput(self, blob_a, blob_b, *args, **kwargs): '''Check for differences between blob_a and blob_b. Return False and log a unified diff if they differ. diff --git a/test/suite_clopts.py b/test/suite_clopts.py index 894a3ceef7..51fee661e7 100644 --- a/test/suite_clopts.py +++ b/test/suite_clopts.py @@ -166,6 +166,7 @@ class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase): except: pass self.assertRun((config.cmd_tshark, '-G', glossary)) + self.assertEqual(self.countOutput(count_stdout=False, count_stderr=True), 0, 'Found error output while printing glossary ' + glossary) def test_tshark_glossary_valid_utf8(self): for glossary in glossaries: diff --git a/test/suite_dfilter/__init__.py b/test/suite_dfilter/__init__.py new file mode 100644 index 0000000000..6fb259ec50 --- /dev/null +++ b/test/suite_dfilter/__init__.py @@ -0,0 +1,14 @@ +# +# Copyright (C) 2013 by Gilbert Ramirez +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import os.path +import unittest + +# Run by unittest.defaultTestLoader.discover in test.py +def load_tests(loader, standard_tests, pattern): + this_dir = os.path.dirname(__file__) + package_tests = loader.discover(start_dir=this_dir, pattern='group_*.py') + standard_tests.addTests(package_tests) + return standard_tests diff --git a/test/suite_dfilter/dfiltertest.py b/test/suite_dfilter/dfiltertest.py new file mode 100644 index 0000000000..8ac1786ec8 --- /dev/null +++ b/test/suite_dfilter/dfiltertest.py @@ -0,0 +1,36 @@ +# Copyright (c) 2013 by Gilbert Ramirez +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import config +import os.path +import subprocesstest + +class DFTestCase(subprocesstest.SubprocessTestCase): + """Base class for all tests in this dfilter-test collection.""" + + + def runDFilter(self, dfilter, expected_return=0): + # Create the tshark command + return self.assertRun((config.cmd_tshark, + "-n", # No name resolution + "-r", # Next arg is trace file to read + os.path.join(config.capture_dir, self.trace_file), + "-Y", # packet display filter (used to be -R) + dfilter + ), expected_return=expected_return) + + + def assertDFilterCount(self, dfilter, expected_count): + """Run a display filter and expect a certain number of packets.""" + + dfilter_proc = self.runDFilter(dfilter) + + dfp_count = self.countOutput() + msg = "Expected %d, got: %s" % (expected_count, dfp_count) + self.assertEqual(dfp_count, expected_count, msg) + + def assertDFilterFail(self, dfilter): + """Run a display filter and expect tshark to fail""" + + dfilter_proc = self.runDFilter(dfilter, expected_return=self.exit_error) diff --git a/tools/dftestlib/bytes_ether.py b/test/suite_dfilter/group_bytes_ether.py similarity index 97% rename from tools/dftestlib/bytes_ether.py rename to test/suite_dfilter/group_bytes_ether.py index cf479d2dff..71f8d484e0 100644 --- a/tools/dftestlib/bytes_ether.py +++ b/test/suite_dfilter/group_bytes_ether.py @@ -2,9 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -from dftestlib import dftest +import dfiltertest -class testBytesEther(dftest.DFTest): +class case_bytes_ether(dfiltertest.DFTestCase): trace_file = "ipx_rip.pcap" ### Note: Bytes test does not yet test FT_INT64. diff --git a/tools/dftestlib/bytes_ipv6.py b/test/suite_dfilter/group_bytes_ipv6.py similarity index 97% rename from tools/dftestlib/bytes_ipv6.py rename to test/suite_dfilter/group_bytes_ipv6.py index 8dd88994bd..94237aa26c 100644 --- a/tools/dftestlib/bytes_ipv6.py +++ b/test/suite_dfilter/group_bytes_ipv6.py @@ -2,9 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -from dftestlib import dftest +import dfiltertest -class testBytesIPv6(dftest.DFTest): +class case_bytes_ipv6(dfiltertest.DFTestCase): trace_file = "ipv6.pcap" def test_eq_1(self): diff --git a/tools/dftestlib/bytes_type.py b/test/suite_dfilter/group_bytes_type.py similarity index 84% rename from tools/dftestlib/bytes_type.py rename to test/suite_dfilter/group_bytes_type.py index 3c0b648b85..4031d27956 100644 --- a/tools/dftestlib/bytes_type.py +++ b/test/suite_dfilter/group_bytes_type.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testBytes(dftest.DFTest): +class case_bytes_type(dfiltertest.DFTestCase): trace_file = "arp.pcap" def test_bytes_1(self): diff --git a/tools/dftestlib/double.py b/test/suite_dfilter/group_double.py similarity index 96% rename from tools/dftestlib/double.py rename to test/suite_dfilter/group_double.py index 123e0b576f..3bf7cb6083 100644 --- a/tools/dftestlib/double.py +++ b/test/suite_dfilter/group_double.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testDouble(dftest.DFTest): +class case_double(dfiltertest.DFTestCase): trace_file = "ntp.pcap" diff --git a/tools/dftestlib/integer.py b/test/suite_dfilter/group_integer.py similarity index 98% rename from tools/dftestlib/integer.py rename to test/suite_dfilter/group_integer.py index d47e837851..42901fb1ea 100644 --- a/tools/dftestlib/integer.py +++ b/test/suite_dfilter/group_integer.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testInteger(dftest.DFTest): +class case_integer(dfiltertest.DFTestCase): trace_file = "ntp.pcap" def test_eq_1(self): diff --git a/tools/dftestlib/integer_1byte.py b/test/suite_dfilter/group_integer_1byte.py similarity index 83% rename from tools/dftestlib/integer_1byte.py rename to test/suite_dfilter/group_integer_1byte.py index 0dae75698b..be8ba198e8 100644 --- a/tools/dftestlib/integer_1byte.py +++ b/test/suite_dfilter/group_integer_1byte.py @@ -3,9 +3,9 @@ # SPDX-License-Identifier: GPL-2.0-or-later -from dftestlib import dftest +import dfiltertest -class testInteger1Byte(dftest.DFTest): +class case_integer_1_byte(dfiltertest.DFTestCase): trace_file = "ipx_rip.pcap" diff --git a/tools/dftestlib/ipv4.py b/test/suite_dfilter/group_ipv4.py similarity index 98% rename from tools/dftestlib/ipv4.py rename to test/suite_dfilter/group_ipv4.py index c32e7fbf94..33a996f259 100644 --- a/tools/dftestlib/ipv4.py +++ b/test/suite_dfilter/group_ipv4.py @@ -3,9 +3,9 @@ # SPDX-License-Identifier: GPL-2.0-or-later -from dftestlib import dftest +import dfiltertest -class testIPv4(dftest.DFTest): +class case_ipv4(dfiltertest.DFTestCase): trace_file = "nfs.pcap" def test_uint64_1(self): diff --git a/tools/dftestlib/membership.py b/test/suite_dfilter/group_membership.py similarity index 95% rename from tools/dftestlib/membership.py rename to test/suite_dfilter/group_membership.py index a52ac9fe03..6cfc9a207b 100644 --- a/tools/dftestlib/membership.py +++ b/test/suite_dfilter/group_membership.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testMembership(dftest.DFTest): +class case_membership(dfiltertest.DFTestCase): trace_file = "http.pcap" def test_membership_1_match(self): diff --git a/tools/dftestlib/range_method.py b/test/suite_dfilter/group_range_method.py similarity index 93% rename from tools/dftestlib/range_method.py rename to test/suite_dfilter/group_range_method.py index 260d6ba315..520b8f261a 100644 --- a/tools/dftestlib/range_method.py +++ b/test/suite_dfilter/group_range_method.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testRange(dftest.DFTest): +class case_range(dfiltertest.DFTestCase): trace_file = "ipx_rip.pcap" def test_slice_1_pos(self): diff --git a/tools/dftestlib/scanner.py b/test/suite_dfilter/group_scanner.py similarity index 93% rename from tools/dftestlib/scanner.py rename to test/suite_dfilter/group_scanner.py index 56fed6d2cf..4637f506fc 100644 --- a/tools/dftestlib/scanner.py +++ b/test/suite_dfilter/group_scanner.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testScanner(dftest.DFTest): +class case_scanner(dfiltertest.DFTestCase): trace_file = "http.pcap" def test_dquote_1(self): diff --git a/tools/dftestlib/string_type.py b/test/suite_dfilter/group_string_type.py similarity index 98% rename from tools/dftestlib/string_type.py rename to test/suite_dfilter/group_string_type.py index 8428469a43..9bebfc94ab 100644 --- a/tools/dftestlib/string_type.py +++ b/test/suite_dfilter/group_string_type.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testString(dftest.DFTest): +class case_string(dfiltertest.DFTestCase): trace_file = "http.pcap" def test_eq_1(self): diff --git a/tools/dftestlib/stringz.py b/test/suite_dfilter/group_stringz.py similarity index 88% rename from tools/dftestlib/stringz.py rename to test/suite_dfilter/group_stringz.py index 100875c234..3e21a40fae 100644 --- a/tools/dftestlib/stringz.py +++ b/test/suite_dfilter/group_stringz.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testStringz(dftest.DFTest): +class case_stringz(dfiltertest.DFTestCase): trace_file = "tftp.pcap" def test_stringz_1(self): diff --git a/tools/dftestlib/time_relative.py b/test/suite_dfilter/group_time_relative.py similarity index 88% rename from tools/dftestlib/time_relative.py rename to test/suite_dfilter/group_time_relative.py index 9d66ac6084..24cd79ecc6 100644 --- a/tools/dftestlib/time_relative.py +++ b/test/suite_dfilter/group_time_relative.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testTimeRelative(dftest.DFTest): +class case_time_relative(dfiltertest.DFTestCase): trace_file = "nfs.pcap" def test_relative_time_1(self): diff --git a/tools/dftestlib/time_type.py b/test/suite_dfilter/group_time_type.py similarity index 92% rename from tools/dftestlib/time_type.py rename to test/suite_dfilter/group_time_type.py index 49e9fc1d92..3c77c0cc67 100644 --- a/tools/dftestlib/time_type.py +++ b/test/suite_dfilter/group_time_type.py @@ -2,15 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later -import os +import dfiltertest -from dftestlib import dftest - -# Force the timezone to UTC so the checks below work regardless of what time -# zone we're in. -os.environ['TZ'] = "UTC" - -class testTime(dftest.DFTest): +class case_time(dfiltertest.DFTestCase): trace_file = "http.pcap" def test_eq_1(self): diff --git a/tools/dftestlib/tvb.py b/test/suite_dfilter/group_tvb.py similarity index 96% rename from tools/dftestlib/tvb.py rename to test/suite_dfilter/group_tvb.py index db6e95998a..e00047b9e3 100644 --- a/tools/dftestlib/tvb.py +++ b/test/suite_dfilter/group_tvb.py @@ -2,11 +2,10 @@ # # SPDX-License-Identifier: GPL-2.0-or-later - +import dfiltertest import unittest -from dftestlib import dftest -class testTVB(dftest.DFTest): +class case_tvb(dfiltertest.DFTestCase): trace_file = "http.pcap" def test_eq_1(self): diff --git a/tools/dftestlib/uint64.py b/test/suite_dfilter/group_uint64.py similarity index 85% rename from tools/dftestlib/uint64.py rename to test/suite_dfilter/group_uint64.py index fcb3e65dbf..a4e50d7346 100644 --- a/tools/dftestlib/uint64.py +++ b/test/suite_dfilter/group_uint64.py @@ -2,10 +2,9 @@ # # SPDX-License-Identifier: GPL-2.0-or-later +import dfiltertest -from dftestlib import dftest - -class testUINT64(dftest.DFTest): +class case_uint64(dfiltertest.DFTestCase): trace_file = "nfs.pcap" def test_uint64_1(self): diff --git a/test/test.py b/test/test.py index 156da53a90..78421bd911 100755 --- a/test/test.py +++ b/test/test.py @@ -63,7 +63,7 @@ def main(): if args.capture_interface: config.setCaptureInterface(args.capture_interface[0]) - all_tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='suite_*.py') + all_tests = unittest.defaultTestLoader.discover(os.path.dirname(__file__), pattern='suite_*') all_ids = [] find_test_ids(all_tests, all_ids) diff --git a/tools/dfilter-test.py b/tools/dfilter-test.py deleted file mode 100755 index 61cae97b58..0000000000 --- a/tools/dfilter-test.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -""" -Test-suite to test wireshark's dfilter mechanism. -""" - -# -# Copyright (C) 2003-2013 by Gilbert Ramirez -# -# SPDX-License-Identifier: GPL-2.0-or-later - -import os -import sys -import types -import unittest - -# Import each test class so unittest.main() can find them -from dftestlib.bytes_type import testBytes -from dftestlib.bytes_ether import testBytesEther -from dftestlib.bytes_ipv6 import testBytesIPv6 -from dftestlib.double import testDouble -from dftestlib.integer import testInteger -from dftestlib.integer_1byte import testInteger1Byte -from dftestlib.ipv4 import testIPv4 -from dftestlib.membership import testMembership -from dftestlib.range_method import testRange -from dftestlib.scanner import testScanner -from dftestlib.string_type import testString -from dftestlib.stringz import testStringz -from dftestlib.time_type import testTime -from dftestlib.time_relative import testTimeRelative -from dftestlib.tvb import testTVB -from dftestlib.uint64 import testUINT64 - -if __name__ == "__main__": - unittest.main(verbosity=2) diff --git a/tools/dftestlib/__init__.py b/tools/dftestlib/__init__.py deleted file mode 100644 index 500d94c1b7..0000000000 --- a/tools/dftestlib/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# -# Copyright (C) 2013 by Gilbert Ramirez -# -# SPDX-License-Identifier: GPL-2.0-or-later diff --git a/tools/dftestlib/dftest.py b/tools/dftestlib/dftest.py deleted file mode 100644 index ba7ceabe3b..0000000000 --- a/tools/dftestlib/dftest.py +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright (c) 2013 by Gilbert Ramirez -# -# SPDX-License-Identifier: GPL-2.0-or-later - - -import os -import tempfile -import unittest - -from dftestlib import util - -# The binaries to use. We assume we are running -# from the top of the wireshark distro -TSHARK = os.path.join(os.getenv("WS_BIN_PATH", "."), "tshark") - -class DFTest(unittest.TestCase): - """Base class for all tests in this dfilter-test collection.""" - - # Remove these file when finished (in tearDownClass) - files_to_remove = [] - - @classmethod - def setUpClass(cls): - """Create the trace file to be used in the tests.""" - assert cls.trace_file - - # if the class sets the 'trace_file' field, then it - # names the trace file to use for the tests. It *should* - # reside in dftestfiles - assert not os.path.isabs(cls.trace_file) - toolsdir = os.path.dirname(os.path.dirname(__file__)) - cls.trace_file = os.path.join(toolsdir, "dftestfiles", cls.trace_file) - - @classmethod - def tearDownClass(cls): - """Remove the trace file used in the tests.""" - for filename in cls.files_to_remove: - if os.path.exists(filename): - try: - os.remove(filename) - except OSError: - pass - - - def runDFilter(self, dfilter): - # Create the tshark command - cmdv = [TSHARK, - "-n", # No name resolution - "-r", # Next arg is trace file to read - self.trace_file, - "-Y", # packet display filter (used to be -R) - dfilter] - - (status, output) = util.exec_cmdv(cmdv) - return status, output - - - def assertDFilterCount(self, dfilter, expected_count): - """Run a display filter and expect a certain number of packets.""" - - (status, output) = self.runDFilter(dfilter) - - # tshark must succeed - self.assertEqual(status, util.SUCCESS, output) - - # Split the output (one big string) into lines, removing - # empty lines (extra newline at end of output) - lines = [L for L in output.split("\n") if L != ""] - - msg = "Expected %d, got: %s" % (expected_count, output) - self.assertEqual(len(lines), expected_count, msg) - - def assertDFilterFail(self, dfilter): - """Run a display filter and expect tshark to fail""" - - (status, output) = self.runDFilter(dfilter) - - # tshark must succeed - self.assertNotEqual(status, util.SUCCESS, output) diff --git a/tools/dftestlib/util.py b/tools/dftestlib/util.py deleted file mode 100644 index fef4ec3a36..0000000000 --- a/tools/dftestlib/util.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (c) 2013 by Gilbert Ramirez -# -# SPDX-License-Identifier: GPL-2.0-or-later - - -import subprocess, sys - -SUCCESS = 0 -def exec_cmdv(cmdv, cwd=None, stdin=None): - """Run the commands in cmdv, returning (retval, output), - where output is stdout and stderr combined. - If cwd is given, the child process runs in that directory. - If a filehandle is passed as stdin, it is used as stdin. - If there is an OS-level error, None is the retval.""" - - try: - output = subprocess.check_output(cmdv, stderr=subprocess.STDOUT, - cwd=cwd, stdin=stdin) - retval = SUCCESS - - # If file isn't executable - except OSError as e: - return (None, str(e)) - - # If process returns non-zero - except subprocess.CalledProcessError as e: - output = e.output - retval = e.returncode - - if sys.version_info[0] >= 3: - output = output.decode('utf-8') - - return (retval, output) -