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
10 '''File format conversion tests'''
17 # XXX Currently unused. It would be nice to be able to use this below.
18 time_output_args = ('-Tfields', '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta')
20 # Microsecond pcap, direct read was used to generate the baseline:
21 # tshark -Tfields -e frame.number -e frame.time_epoch -e frame.time_delta \
22 # -r captures/dhcp.pcap > baseline/ff-ts-usec-pcap-direct.txt
23 baseline_file = 'ff-ts-usec-pcap-direct.txt'
26 @fixtures.fixture(scope='session')
27 def fileformats_baseline_str(dirs):
28 with open(os.path.join(dirs.baseline_dir, baseline_file), 'r') as f:
32 @fixtures.mark_usefixtures('test_env')
33 @fixtures.uses_fixtures
34 class case_fileformat_pcap(subprocesstest.SubprocessTestCase):
35 def test_pcap_usec_stdin(self, cmd_tshark, capture_file, fileformats_baseline_str):
36 '''Microsecond pcap direct vs microsecond pcap stdin'''
37 capture_proc = self.runProcess(' '.join((cmd_tshark,
40 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
41 '<', capture_file('dhcp.pcap')
44 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
46 def test_pcap_nsec_stdin(self, cmd_tshark, capture_file, fileformats_baseline_str):
47 '''Microsecond pcap direct vs nanosecond pcap stdin'''
48 capture_proc = self.runProcess(' '.join((cmd_tshark,
51 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
52 '<', capture_file('dhcp-nanosecond.pcap')
55 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
57 def test_pcap_nsec_direct(self, cmd_tshark, capture_file, fileformats_baseline_str):
58 '''Microsecond pcap direct vs nanosecond pcap direct'''
59 capture_proc = self.runProcess((cmd_tshark,
60 '-r', capture_file('dhcp-nanosecond.pcap'),
62 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
65 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
68 @fixtures.mark_usefixtures('test_env')
69 @fixtures.uses_fixtures
70 class case_fileformat_pcapng(subprocesstest.SubprocessTestCase):
71 def test_pcapng_usec_stdin(self, cmd_tshark, capture_file, fileformats_baseline_str):
72 '''Microsecond pcap direct vs microsecond pcapng stdin'''
73 capture_proc = self.runProcess(' '.join((cmd_tshark,
76 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
77 '<', capture_file('dhcp.pcapng')
80 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
82 def test_pcapng_usec_direct(self, cmd_tshark, capture_file, fileformats_baseline_str):
83 '''Microsecond pcap direct vs microsecond pcapng direct'''
84 capture_proc = self.runProcess((cmd_tshark,
85 '-r', capture_file('dhcp.pcapng'),
87 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
90 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
92 def test_pcapng_nsec_stdin(self, cmd_tshark, capture_file, fileformats_baseline_str):
93 '''Microsecond pcap direct vs nanosecond pcapng stdin'''
94 capture_proc = self.runProcess(' '.join((cmd_tshark,
97 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
98 '<', capture_file('dhcp-nanosecond.pcapng')
101 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
103 def test_pcapng_nsec_direct(self, cmd_tshark, capture_file, fileformats_baseline_str):
104 '''Microsecond pcap direct vs nanosecond pcapng direct'''
105 capture_proc = self.runProcess((cmd_tshark,
106 '-r', capture_file('dhcp-nanosecond.pcapng'),
108 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
111 self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
114 @fixtures.mark_usefixtures('test_env')
115 @fixtures.uses_fixtures
116 class case_fileformat_mime(subprocesstest.SubprocessTestCase):
117 def test_mime_pcapng_gz(self, cmd_tshark, capture_file):
118 '''Test that the full uncompressed contents is shown.'''
119 proc = self.runProcess((cmd_tshark,
120 '-r', capture_file('icmp.pcapng.gz'),
121 '-Xread_format:MIME Files Format',
122 '-Tfields', '-e', 'frame.len', '-e', 'pcapng.block.length',
124 self.assertEqual(proc.stdout_str.strip(), '480\t128,128,88,88,132,132,132,132')