3 # By Gerald Combs <gerald@wireshark.org>
5 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 # SPDX-License-Identifier: GPL-2.0-or-later
14 from subprocesstest
import cat_dhcp_command
, check_packet_count
18 testout_pcap
= 'testout.pcap'
19 baseline_file
= 'io-rawshark-dhcp-pcap.txt'
22 @pytest.fixture(scope
='session')
23 def io_baseline_str(dirs
):
24 with
open(os
.path
.join(dirs
.baseline_dir
, baseline_file
), 'r') as f
:
28 def check_io_4_packets(capture_file
, result_file
, cmd_tshark
, cmd_capinfos
, from_stdin
=False, to_stdout
=False, env
=None):
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.
33 testout_file
= result_file(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 pytest
.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
= cat_dhcp_command('cat')
41 stdin_cmd
= '{0} | "{1}" -r - -w "{2}"'.format(cat_dhcp_cmd
, cmd_tshark
, testout_file
)
42 subprocess
.check_call(stdin_cmd
, shell
=True, env
=env
)
44 # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w - > ./testout.pcap 2>./testout.txt
45 stdout_cmd
= '"{0}" -r "{1}" -w - > "{2}"'.format(cmd_tshark
, capture_file('dhcp.pcap'), testout_file
)
46 subprocess
.check_call(stdout_cmd
, shell
=True, env
=env
)
47 else: # direct->direct
48 # $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1
49 subprocess
.check_call((cmd_tshark
,
50 '-r', capture_file('dhcp.pcap'),
53 assert os
.path
.isfile(testout_file
)
54 check_packet_count(cmd_capinfos
, 4, testout_file
)
58 def test_tshark_io_stdin_direct(self
, cmd_tshark
, cmd_capinfos
, capture_file
, result_file
, test_env
):
59 '''Read from stdin and write direct using TShark'''
60 check_io_4_packets(capture_file
, result_file
, cmd_tshark
, cmd_capinfos
, from_stdin
=True, env
=test_env
)
62 def test_tshark_io_direct_stdout(self
, cmd_tshark
, cmd_capinfos
, capture_file
, result_file
, test_env
):
63 '''Read direct and write to stdout using TShark'''
64 check_io_4_packets(capture_file
, result_file
, cmd_tshark
, cmd_capinfos
, to_stdout
=True, env
=test_env
)
66 def test_tshark_io_direct_direct(self
, cmd_tshark
, cmd_capinfos
, capture_file
, result_file
, test_env
):
67 '''Read direct and write direct using TShark'''
68 check_io_4_packets(capture_file
, result_file
, cmd_tshark
, cmd_capinfos
, env
=test_env
)
72 if sys
.byteorder
!= 'little':
73 pytest
.skip('Requires a little endian system')
74 def test_rawshark_io_stdin(self
, cmd_rawshark
, capture_file
, result_file
, io_baseline_str
, test_env
):
75 '''Read from stdin using Rawshark'''
76 # tail -c +25 "${CAPTURE_DIR}dhcp.pcap" | $RAWSHARK -dencap:1 -R "udp.port==68" -nr - > $IO_RAWSHARK_DHCP_PCAP_TESTOUT 2> /dev/null
77 # diff -u --strip-trailing-cr $IO_RAWSHARK_DHCP_PCAP_BASELINE $IO_RAWSHARK_DHCP_PCAP_TESTOUT > $DIFF_OUT 2>&1
78 capture_file
= capture_file('dhcp.pcap')
79 testout_file
= result_file(testout_pcap
)
80 raw_dhcp_cmd
= cat_dhcp_command('raw')
81 rawshark_cmd
= '{0} | "{1}" -r - -n -dencap:1 -R "udp.port==68"'.format(raw_dhcp_cmd
, cmd_rawshark
)
82 rawshark_stdout
= subprocess
.check_output(rawshark_cmd
, shell
=True, encoding
='utf-8', env
=test_env
)
83 assert rawshark_stdout
== io_baseline_str