aboutsummaryrefslogtreecommitdiffstats
path: root/tools/dftestlib/dftest.py
diff options
context:
space:
mode:
authorGilbert Ramirez <gram@alumni.rice.edu>2013-09-18 05:07:46 +0000
committerGilbert Ramirez <gram@alumni.rice.edu>2013-09-18 05:07:46 +0000
commit4797eafddb99fd85d98944c6ccce04ea391951a3 (patch)
treebb3e38489329a99f43c10ea4aa29aa80e313d0fc /tools/dftestlib/dftest.py
parentab7d8cc87dbac5cfdd23d93b7b336fb60ac790b3 (diff)
Update dfilter-test.py to use a much more modern test harness,
the "unittest" module that comes with Python. Specifically, this takes advantage of a couple of features in the "unittest" in Python 2.7. The tests are all the same as before, but much better managed. This is in preparation for some work on the display filter code. svn path=/trunk/; revision=52136
Diffstat (limited to 'tools/dftestlib/dftest.py')
-rw-r--r--tools/dftestlib/dftest.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/dftestlib/dftest.py b/tools/dftestlib/dftest.py
new file mode 100644
index 0000000000..2fa44eec8c
--- /dev/null
+++ b/tools/dftestlib/dftest.py
@@ -0,0 +1,76 @@
+# Copyright (c) 2013 by Gilbert Ramirez <gram@alumni.rice.edu>
+
+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(".", "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)
+ cls.trace_file = os.path.join(".", "tools", "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)