1 # Copyright (c) 2013 by Gilbert Ramirez <gram@alumni.rice.edu>
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 from subprocesstest
import count_output
, grep_output
12 def dfilter_cmd(cmd_tshark
, capture_file
, request
):
13 def wrapped(dfilter
, frame_number
=None, prefs
=None, read_filter
=False):
16 "-n", # No name resolution
17 "-r", # Next arg is trace file to read
18 capture_file(request
.instance
.trace_file
),
23 "--selected-frame={}".format(frame_number
)
28 "-R", # read filter (requires two-pass mode)
33 "-Y", # packet display filter (used to be -R)
44 @pytest.fixture(scope
='session')
45 def cmd_dftest(program
):
46 return program('dftest')
49 def dftest_cmd(cmd_dftest
):
60 def checkDFilterCount(dfilter_cmd
, dfilter_env
):
61 def checkDFilterCount_real(dfilter
, expected_count
, prefs
=None):
62 """Run a display filter and expect a certain number of packets."""
63 proc
= subprocesstest
.check_run(dfilter_cmd(dfilter
, prefs
=prefs
),
65 universal_newlines
=True,
68 logging
.debug(proc
.stderr
)
69 assert count_output(proc
.stdout
) == expected_count
70 return checkDFilterCount_real
73 def checkDFilterCountWithSelectedFrame(dfilter_cmd
, dfilter_env
):
74 def checkDFilterCount_real(dfilter
, expected_count
, selected_frame
, prefs
=None):
75 """Run a display filter and expect a certain number of packets."""
76 proc
= subprocesstest
.check_run(dfilter_cmd(dfilter
, frame_number
=selected_frame
, prefs
=prefs
),
78 universal_newlines
=True,
81 logging
.debug(proc
.stderr
)
82 assert count_output(proc
.stdout
) == expected_count
83 return checkDFilterCount_real
86 def checkDFilterCountReadFilter(dfilter_cmd
, dfilter_env
):
87 def checkDFilterCount_real(dfilter
, expected_count
):
88 """Run a read filter in two pass mode and expect a certain number of packets."""
89 proc
= subprocesstest
.check_run(dfilter_cmd(dfilter
, read_filter
=True),
91 universal_newlines
=True,
94 logging
.debug(proc
.stderr
)
95 assert count_output(proc
.stdout
) == expected_count
96 return checkDFilterCount_real
99 def checkDFilterFail(dftest_cmd
, dfilter_env
):
100 def checkDFilterFail_real(dfilter
, error_message
):
101 """Run a display filter and expect dftest to fail."""
102 proc
= subprocesstest
.run(dftest_cmd(dfilter
),
104 universal_newlines
=True,
107 logging
.debug(proc
.stderr
)
108 assert proc
.returncode
== 4
109 assert error_message
in proc
.stderr
110 return checkDFilterFail_real
113 def checkDFilterSucceed(dftest_cmd
, dfilter_env
):
114 def checkDFilterSucceed_real(dfilter
, expect_stdout
=None):
115 """Run a display filter and expect dftest to succeed."""
116 proc
= subprocesstest
.run(dftest_cmd(dfilter
),
118 universal_newlines
=True,
121 logging
.debug(proc
.stderr
)
122 assert proc
.returncode
== 0
124 assert expect_stdout
in proc
.stdout
125 return checkDFilterSucceed_real