FIXUP: give names to sec_vt_command's
[wireshark-wip.git] / tools / dftestlib / dftest.py
blob05f2bd6bddc1faca3603784cb4d74ee1d9ef1d29
1 # Copyright (c) 2013 by Gilbert Ramirez <gram@alumni.rice.edu>
3 # $Id$
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 import os
21 import tempfile
22 import unittest
24 from dftestlib import util
26 # The binaries to use. We assume we are running
27 # from the top of the wireshark distro
28 TSHARK = os.path.join(".", "tshark")
30 class DFTest(unittest.TestCase):
31 """Base class for all tests in this dfilter-test collection."""
33 # Remove these file when finished (in tearDownClass)
34 files_to_remove = []
36 @classmethod
37 def setUpClass(cls):
38 """Create the trace file to be used in the tests."""
39 assert cls.trace_file
41 # if the class sets the 'trace_file' field, then it
42 # names the trace file to use for the tests. It *should*
43 # reside in dftestfiles
44 assert not os.path.isabs(cls.trace_file)
45 cls.trace_file = os.path.join(".", "tools", "dftestfiles",
46 cls.trace_file)
48 @classmethod
49 def tearDownClass(cls):
50 """Remove the trace file used in the tests."""
51 for filename in cls.files_to_remove:
52 if os.path.exists(filename):
53 try:
54 os.remove(filename)
55 except OSError:
56 pass
59 def runDFilter(self, dfilter):
60 # Create the tshark command
61 cmdv = [TSHARK,
62 "-n", # No name resolution
63 "-r", # Next arg is trace file to read
64 self.trace_file,
65 "-Y", # packet display filter (used to be -R)
66 dfilter]
68 (status, output) = util.exec_cmdv(cmdv)
69 return status, output
72 def assertDFilterCount(self, dfilter, expected_count):
73 """Run a display filter and expect a certain number of packets."""
75 (status, output) = self.runDFilter(dfilter)
77 # tshark must succeed
78 self.assertEqual(status, util.SUCCESS, output)
80 # Split the output (one big string) into lines, removing
81 # empty lines (extra newline at end of output)
82 lines = [L for L in output.split("\n") if L != ""]
84 msg = "Expected %d, got: %s" % (expected_count, output)
85 self.assertEqual(len(lines), expected_count, msg)
87 def assertDFilterFail(self, dfilter):
88 """Run a display filter and expect tshark to fail"""
90 (status, output) = self.runDFilter(dfilter)
92 # tshark must succeed
93 self.assertNotEqual(status, util.SUCCESS, output)