1 # Copyright (c) 2013 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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)
38 """Create the trace file to be used in the tests."""
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",
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
):
59 def runDFilter(self
, dfilter
):
60 # Create the tshark command
62 "-n", # No name resolution
63 "-r", # Next arg is trace file to read
65 "-Y", # packet display filter (used to be -R)
68 (status
, output
) = util
.exec_cmdv(cmdv
)
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
)
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
)
93 self
.assertNotEqual(status
, util
.SUCCESS
, output
)