2 # -*- coding: utf-8 -*-
4 # By Gerald Combs <gerald@wireshark.org>
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
8 # SPDX-License-Identifier: GPL-2.0-or-later
23 testout_pcap = 'testout.pcap'
26 def start_pinging(self):
28 if sys.platform.startswith('win32'):
29 # Fake '-i' with a subsecond interval.
30 for st in (0.1, 0.1, 0):
31 ping_procs.append(self.startProcess(config.args_ping))
34 ping_procs.append(self.startProcess(config.args_ping))
37 def stop_pinging(ping_procs):
38 for proc in ping_procs:
41 def check_capture_10_packets(self, cmd=None, to_stdout=False):
42 # Similar to suite_io.check_io_4_packets.
43 if not config.canCapture():
44 self.skipTest('Test requires capture privileges and an interface.')
45 if cmd == config.cmd_wireshark and not config.canDisplay():
46 self.skipTest('Test requires a display.')
47 if not config.args_ping:
48 self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform))
49 self.assertIsNotNone(cmd)
50 testout_file = self.filename_from_id(testout_pcap)
51 ping_procs = start_pinging(self)
53 capture_proc = self.runProcess(subprocesstest.capture_command(cmd,
54 '-i', '"{}"'.format(config.capture_interface),
58 '-a', 'duration:{}'.format(capture_duration),
59 '-f', '"icmp || icmp6"',
66 capture_proc = self.runProcess(subprocesstest.capture_command(cmd,
67 '-i', config.capture_interface,
71 '-a', 'duration:{}'.format(capture_duration),
72 '-f', 'icmp || icmp6',
74 capture_returncode = capture_proc.returncode
75 stop_pinging(ping_procs)
76 if capture_returncode != 0:
77 self.log_fd.write('{} -D output:\n'.format(cmd))
78 self.runProcess((cmd, '-D'))
79 self.assertEqual(capture_returncode, 0)
80 if (capture_returncode == 0):
81 self.checkPacketCount(10)
83 def check_capture_fifo(self, cmd=None):
84 if not config.canMkfifo():
85 self.skipTest('Test requires OS fifo support.')
86 if cmd == config.cmd_wireshark and not config.canDisplay():
87 self.skipTest('Test requires a display.')
88 self.assertIsNotNone(cmd)
89 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
90 testout_file = self.filename_from_id(testout_pcap)
91 fifo_file = self.filename_from_id('testout.fifo')
93 # If a previous test left its fifo laying around, e.g. from a failure, remove it.
98 slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow')
99 fifo_proc = self.startProcess(
100 ('{0} > {1}'.format(slow_dhcp_cmd, fifo_file)),
102 capture_proc = self.runProcess(subprocesstest.capture_command(cmd,
106 '-a', 'duration:{}'.format(capture_duration),
109 self.assertTrue(os.path.isfile(testout_file))
110 capture_returncode = capture_proc.returncode
111 self.assertEqual(capture_returncode, 0)
112 if (capture_returncode == 0):
113 self.checkPacketCount(8)
115 def check_capture_stdin(self, cmd=None):
116 # Similar to suite_io.check_io_4_packets.
117 if cmd == config.cmd_wireshark and not config.canDisplay():
118 self.skipTest('Test requires a display.')
119 self.assertIsNotNone(cmd)
120 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
121 testout_file = self.filename_from_id(testout_pcap)
122 slow_dhcp_cmd = subprocesstest.cat_dhcp_command('slow')
123 capture_cmd = subprocesstest.capture_command(cmd,
126 '-a', 'duration:{}'.format(capture_duration),
129 if cmd == config.cmd_wireshark:
130 capture_cmd += ' -o console.log.level:127'
131 pipe_proc = self.runProcess(slow_dhcp_cmd + ' | ' + capture_cmd, shell=True)
132 pipe_returncode = pipe_proc.returncode
133 self.assertEqual(pipe_returncode, 0)
134 if cmd == config.cmd_wireshark:
135 self.assertTrue(self.grepOutput('Wireshark is up and ready to go'), 'No startup message.')
136 self.assertTrue(self.grepOutput('Capture started'), 'No capture start message.')
137 self.assertTrue(self.grepOutput('Capture stopped'), 'No capture stop message.')
138 self.assertTrue(os.path.isfile(testout_file))
139 if (pipe_returncode == 0):
140 self.checkPacketCount(8)
142 def check_capture_2multi_10packets(self, cmd=None):
143 # This was present in the Bash version but was incorrect and not part of any suite.
144 # It's apparently intended to test file rotation.
145 self.skipTest('Not yet implemented')
147 def check_capture_read_filter(self, cmd=None):
148 if not config.canCapture():
149 self.skipTest('Test requires capture privileges and an interface.')
150 if cmd == config.cmd_wireshark and not config.canDisplay():
151 self.skipTest('Test requires a display.')
152 if not config.args_ping:
153 self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform))
154 self.assertIsNotNone(cmd)
155 ping_procs = start_pinging(self)
156 testout_file = self.filename_from_id(testout_pcap)
157 capture_proc = self.runProcess(subprocesstest.capture_command(cmd,
158 '-i', config.capture_interface,
162 '-R', 'dcerpc.cn_call_id==123456', # Something unlikely.
164 '-a', 'duration:{}'.format(capture_duration),
165 '-f', 'icmp || icmp6',
167 capture_returncode = capture_proc.returncode
168 stop_pinging(ping_procs)
169 self.assertEqual(capture_returncode, 0)
171 if (capture_returncode == 0):
172 self.checkPacketCount(0)
174 def check_capture_snapshot_len(self, cmd=None):
175 if not config.canCapture():
176 self.skipTest('Test requires capture privileges and an interface.')
177 if cmd == config.cmd_wireshark and not config.canDisplay():
178 self.skipTest('Test requires a display.')
179 if not config.args_ping:
180 self.skipTest('Your platform ({}) does not have a defined ping command.'.format(sys.platform))
181 self.assertIsNotNone(cmd)
182 ping_procs = start_pinging(self)
183 testout_file = self.filename_from_id(testout_pcap)
184 capture_proc = self.runProcess(subprocesstest.capture_command(cmd,
185 '-i', config.capture_interface,
188 '-s', str(snapshot_len),
189 '-a', 'duration:{}'.format(capture_duration),
190 '-f', 'icmp || icmp6',
192 capture_returncode = capture_proc.returncode
193 stop_pinging(ping_procs)
194 self.assertEqual(capture_returncode, 0)
195 self.assertTrue(os.path.isfile(testout_file))
197 # Use tshark to filter out all packets larger than 68 bytes.
198 testout2_file = self.filename_from_id('testout2.pcap')
200 filter_proc = self.runProcess((config.cmd_tshark,
203 '-Y', 'frame.cap_len>{}'.format(snapshot_len),
205 filter_returncode = filter_proc.returncode
206 self.assertEqual(capture_returncode, 0)
207 if (capture_returncode == 0):
208 self.checkPacketCount(0, cap_file=testout2_file)
210 class case_wireshark_capture(subprocesstest.SubprocessTestCase):
211 def test_wireshark_capture_10_packets_to_file(self):
212 '''Capture 10 packets from the network to a file using Wireshark'''
213 check_capture_10_packets(self, cmd=config.cmd_wireshark)
215 # Wireshark doesn't currently support writing to stdout while capturing.
216 # def test_wireshark_capture_10_packets_to_stdout(self):
217 # '''Capture 10 packets from the network to stdout using Wireshark'''
218 # check_capture_10_packets(self, cmd=config.cmd_wireshark, to_stdout=True)
220 def test_wireshark_capture_from_fifo(self):
221 '''Capture from a fifo using Wireshark'''
222 check_capture_fifo(self, cmd=config.cmd_wireshark)
224 def test_wireshark_capture_from_stdin(self):
225 '''Capture from stdin using Wireshark'''
226 check_capture_stdin(self, cmd=config.cmd_wireshark)
228 def test_wireshark_capture_snapshot_len(self):
229 '''Capture truncated packets using Wireshark'''
230 check_capture_snapshot_len(self, cmd=config.cmd_wireshark)
232 class case_tshark_capture(subprocesstest.SubprocessTestCase):
233 def test_tshark_capture_10_packets_to_file(self):
234 '''Capture 10 packets from the network to a file using TShark'''
235 check_capture_10_packets(self, cmd=config.cmd_tshark)
237 def test_tshark_capture_10_packets_to_stdout(self):
238 '''Capture 10 packets from the network to stdout using TShark'''
239 check_capture_10_packets(self, cmd=config.cmd_tshark, to_stdout=True)
241 def test_tshark_capture_from_fifo(self):
242 '''Capture from a fifo using TShark'''
243 check_capture_fifo(self, cmd=config.cmd_tshark)
245 def test_tshark_capture_from_stdin(self):
246 '''Capture from stdin using TShark'''
247 check_capture_stdin(self, cmd=config.cmd_tshark)
249 def test_tshark_capture_snapshot_len(self):
250 '''Capture truncated packets using TShark'''
251 check_capture_snapshot_len(self, cmd=config.cmd_tshark)
253 class case_dumpcap_capture(subprocesstest.SubprocessTestCase):
254 def test_dumpcap_capture_10_packets_to_file(self):
255 '''Capture 10 packets from the network to a file using Dumpcap'''
256 check_capture_10_packets(self, cmd=config.cmd_dumpcap)
258 def test_dumpcap_capture_10_packets_to_stdout(self):
259 '''Capture 10 packets from the network to stdout using Dumpcap'''
260 check_capture_10_packets(self, cmd=config.cmd_dumpcap, to_stdout=True)
262 def test_dumpcap_capture_from_fifo(self):
263 '''Capture from a fifo using Dumpcap'''
264 check_capture_fifo(self, cmd=config.cmd_dumpcap)
266 def test_dumpcap_capture_from_stdin(self):
267 '''Capture from stdin using Dumpcap'''
268 check_capture_stdin(self, cmd=config.cmd_dumpcap)
270 def test_dumpcap_capture_snapshot_len(self):
271 '''Capture truncated packets using Dumpcap'''
272 check_capture_snapshot_len(self, cmd=config.cmd_dumpcap)