1#
2# Wireshark tests
3# By Gerald Combs <gerald@wireshark.org>
4#
5# Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
6#
7# SPDX-License-Identifier: GPL-2.0-or-later
8#
9'''File I/O tests'''
10
11import io
12import os.path
13import subprocesstest
14import sys
15import unittest
16import fixtures
17
18testout_pcap = 'testout.pcap'
19baseline_file = 'io-rawshark-dhcp-pcap.txt'
20
21
22@fixtures.fixture(scope='session')
23def io_baseline_str(dirs):
24    with open(os.path.join(dirs.baseline_dir, baseline_file), 'r') as f:
25        return f.read()
26
27
28def check_io_4_packets(self, capture_file, cmd=None, from_stdin=False, to_stdout=False):
29    # Test direct->direct, stdin->direct, and direct->stdout file I/O.
30    # Similar to suite_capture.check_capture_10_packets and
31    # suite_capture.check_capture_stdin.
32    self.assertIsNotNone(cmd)
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.')
38    elif from_stdin:
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.assertRun(stdin_cmd, shell=True)
43    elif to_stdout:
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('dhcp.pcap'), testout_file)
46        io_proc = self.assertRun(stdout_cmd, shell=True)
47    else: # direct->direct
48        # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1
49        io_proc = self.assertRun((cmd,
50            '-r', capture_file('dhcp.pcap'),
51            '-w', testout_file,
52        ))
53    self.assertTrue(os.path.isfile(testout_file))
54    self.checkPacketCount(4)
55
56
57@fixtures.mark_usefixtures('test_env')
58@fixtures.uses_fixtures
59class case_tshark_io(subprocesstest.SubprocessTestCase):
60    def test_tshark_io_stdin_direct(self, cmd_tshark, capture_file):
61        '''Read from stdin and write direct using TShark'''
62        check_io_4_packets(self, capture_file, cmd=cmd_tshark, from_stdin=True)
63
64    def test_tshark_io_direct_stdout(self, cmd_tshark, capture_file):
65        '''Read direct and write to stdout using TShark'''
66        check_io_4_packets(self, capture_file, cmd=cmd_tshark, to_stdout=True)
67
68    def test_tshark_io_direct_direct(self, cmd_tshark, capture_file):
69        '''Read direct and write direct using TShark'''
70        check_io_4_packets(self, capture_file, cmd=cmd_tshark)
71
72
73@fixtures.mark_usefixtures('test_env')
74@fixtures.uses_fixtures
75class case_rawshark_io(subprocesstest.SubprocessTestCase):
76    @unittest.skipUnless(sys.byteorder == 'little', 'Requires a little endian system')
77    def test_rawshark_io_stdin(self, cmd_rawshark, capture_file, io_baseline_str):
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 = capture_file('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, cmd_rawshark)
85        rawshark_proc = self.assertRun(rawshark_cmd, shell=True)
86        self.assertTrue(self.diffOutput(rawshark_proc.stdout_str, io_baseline_str, 'rawshark', baseline_file))
87