test: convert capture tests to use fixtures, fix tests without dumpcap
[metze/wireshark/wip.git] / test / suite_fileformats.py
1 #
2 # -*- coding: utf-8 -*-
3 # Wireshark tests
4 # By Gerald Combs <gerald@wireshark.org>
5 #
6 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 #
8 # SPDX-License-Identifier: GPL-2.0-or-later
9 #
10 '''File format conversion tests'''
11
12 import os.path
13 import subprocesstest
14 import unittest
15 import fixtures
16
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')
19
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'
24
25
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:
29         return f.read()
30
31
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,
38                 '-r', '-',
39                 '-Tfields',
40                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
41                 '<', capture_file('dhcp.pcap')
42                 )),
43             shell=True)
44         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
45
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,
49                 '-r', '-',
50                 '-Tfields',
51                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
52                 '<', capture_file('dhcp-nanosecond.pcap')
53                 )),
54             shell=True)
55         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
56
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'),
61                 '-Tfields',
62                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
63                 ),
64             )
65         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
66
67
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,
74                 '-r', '-',
75                 '-Tfields',
76                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
77                 '<', capture_file('dhcp.pcapng')
78                 )),
79             shell=True)
80         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
81
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'),
86                 '-Tfields',
87                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
88                 ),
89             )
90         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
91
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,
95                 '-r', '-',
96                 '-Tfields',
97                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
98                 '<', capture_file('dhcp-nanosecond.pcapng')
99                 )),
100             shell=True)
101         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
102
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'),
107                 '-Tfields',
108                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
109                 ),
110             )
111         self.assertTrue(self.diffOutput(capture_proc.stdout_str, fileformats_baseline_str, 'tshark', baseline_file))
112
113
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',
123             ))
124         self.assertEqual(proc.stdout_str.strip(), '480\t128,128,88,88,132,132,132,132')