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
19 testout_pcap = 'testout.pcap'
20 baseline_file = 'io-rawshark-dhcp-pcap.txt'
21 baseline_fd = io.open(os.path.join(config.baseline_dir, baseline_file), 'r', encoding='UTF-8', errors='replace')
22 baseline_str = baseline_fd.read()
25 def check_io_4_packets(self, cmd=None, from_stdin=False, to_stdout=False):
26 # Test direct->direct, stdin->direct, and direct->stdout file I/O.
27 # Similar to suite_capture.check_capture_10_packets and
28 # suite_capture.check_capture_stdin.
29 if cmd == config.cmd_wireshark and not config.canDisplay():
30 self.skipTest('Test requires a display.')
31 self.assertIsNotNone(cmd)
32 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
33 testout_file = self.filename_from_id(testout_pcap)
34 if from_stdin and to_stdout:
35 # XXX If we support this, should we bother with separate stdin->direct
36 # and direct->stdout tests?
37 self.fail('Stdin and stdout not supported in the same test.')
39 # cat -B "${CAPTURE_DIR}dhcp.pcap" | $DUT -r - -w ./testout.pcap 2>./testout.txt
40 cat_dhcp_cmd = subprocesstest.cat_dhcp_command('cat')
41 stdin_cmd = '{0} | "{1}" -r - -w "{2}"'.format(cat_dhcp_cmd, cmd, testout_file)
42 io_proc = self.runProcess(stdin_cmd, shell=True)
44 # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w - > ./testout.pcap 2>./testout.txt
45 stdout_cmd = '"{0}" -r "{1}" -w - > "{2}"'.format(cmd, capture_file, testout_file)
46 io_proc = self.runProcess(stdout_cmd, shell=True)
47 else: # direct->direct
48 # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1
49 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
50 io_proc = self.runProcess(subprocesstest.capture_command(cmd,
54 io_returncode = io_proc.returncode
55 self.assertEqual(io_returncode, 0)
56 self.assertTrue(os.path.isfile(testout_file))
57 if (io_returncode == 0):
58 self.checkPacketCount(4)
60 class case_tshark_io(subprocesstest.SubprocessTestCase):
61 def test_tshark_io_stdin_direct(self):
62 '''Read from stdin and write direct using TShark'''
63 check_io_4_packets(self, cmd=config.cmd_tshark, from_stdin=True)
65 def test_tshark_io_direct_stdout(self):
66 '''Read direct and write to stdout using TShark'''
67 check_io_4_packets(self, cmd=config.cmd_tshark, to_stdout=True)
69 def test_tshark_io_direct_direct(self):
70 '''Read direct and write direct using TShark'''
71 check_io_4_packets(self, cmd=config.cmd_tshark)
73 # The Bash version didn't test Wireshark or dumpcap
75 class case_rawshark_io(subprocesstest.SubprocessTestCase):
76 @unittest.skipUnless(sys.byteorder == 'little', 'Requires a little endian system')
77 def test_rawshark_io_stdin(self):
78 '''Read from stdin using Rawshark'''
79 # tail -c +25 "${CAPTURE_DIR}dhcp.pcap" | $RAWSHARK -dencap:1 -R "udp.port==68" -nr - > $IO_RAWSHARK_DHCP_PCAP_TESTOUT 2> /dev/null
80 # diff -u --strip-trailing-cr $IO_RAWSHARK_DHCP_PCAP_BASELINE $IO_RAWSHARK_DHCP_PCAP_TESTOUT > $DIFF_OUT 2>&1
81 capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
82 testout_file = self.filename_from_id(testout_pcap)
83 raw_dhcp_cmd = subprocesstest.cat_dhcp_command('raw')
84 rawshark_cmd = '{0} | "{1}" -r - -n -dencap:1 -R "udp.port==68"'.format(raw_dhcp_cmd, config.cmd_rawshark)
85 rawshark_proc = self.runProcess(rawshark_cmd, shell=True)
86 rawshark_returncode = rawshark_proc.returncode
87 self.assertEqual(rawshark_returncode, 0)
88 if (rawshark_returncode == 0):
89 self.assertTrue(self.diffOutput(rawshark_proc.stdout_str, baseline_str, 'rawshark', baseline_file))