TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / test / suite_dfilter / dfiltertest.py
blobdd59690aeb3f4577f3b1cedf0c954b5c301f18fc
1 # Copyright (c) 2013 by Gilbert Ramirez <gram@alumni.rice.edu>
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import subprocesstest
6 from subprocesstest import count_output, grep_output
7 import pytest
8 import logging
11 @pytest.fixture
12 def dfilter_cmd(cmd_tshark, capture_file, request):
13 def wrapped(dfilter, frame_number=None, prefs=None, read_filter=False):
14 cmd = [
15 cmd_tshark,
16 "-n", # No name resolution
17 "-r", # Next arg is trace file to read
18 capture_file(request.instance.trace_file),
20 if frame_number:
21 cmd.extend([
22 "-2", # two-pass mode
23 "--selected-frame={}".format(frame_number)
25 if read_filter:
26 cmd.extend([
27 "-2", # two-pass mode
28 "-R", # read filter (requires two-pass mode)
29 dfilter
31 else:
32 cmd.extend([
33 "-Y", # packet display filter (used to be -R)
34 dfilter
36 if prefs:
37 cmd.extend([
38 "-o",
39 prefs
41 return cmd
42 return wrapped
44 @pytest.fixture(scope='session')
45 def cmd_dftest(program):
46 return program('dftest')
48 @pytest.fixture
49 def dftest_cmd(cmd_dftest):
50 def wrapped(dfilter):
51 cmd = [
52 cmd_dftest,
53 "--",
54 dfilter
56 return cmd
57 return wrapped
59 @pytest.fixture
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),
64 capture_output=True,
65 universal_newlines=True,
66 env=dfilter_env)
67 if proc.stderr:
68 logging.debug(proc.stderr)
69 assert count_output(proc.stdout) == expected_count
70 return checkDFilterCount_real
72 @pytest.fixture
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),
77 capture_output=True,
78 universal_newlines=True,
79 env=dfilter_env)
80 if proc.stderr:
81 logging.debug(proc.stderr)
82 assert count_output(proc.stdout) == expected_count
83 return checkDFilterCount_real
85 @pytest.fixture
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),
90 capture_output=True,
91 universal_newlines=True,
92 env=dfilter_env)
93 if proc.stderr:
94 logging.debug(proc.stderr)
95 assert count_output(proc.stdout) == expected_count
96 return checkDFilterCount_real
98 @pytest.fixture
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),
103 capture_output=True,
104 universal_newlines=True,
105 env=dfilter_env)
106 if proc.stderr:
107 logging.debug(proc.stderr)
108 assert proc.returncode == 4
109 assert error_message in proc.stderr
110 return checkDFilterFail_real
112 @pytest.fixture
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),
117 capture_output=True,
118 universal_newlines=True,
119 env=dfilter_env)
120 if proc.stderr:
121 logging.debug(proc.stderr)
122 assert proc.returncode == 0
123 if expect_stdout:
124 assert expect_stdout in proc.stdout
125 return checkDFilterSucceed_real