Test: Add fileformats and I/O.
[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 config
13 import io
14 import os.path
15 import subprocesstest
16 import sys
17 import unittest
18
19 # XXX Currently unused. It would be nice to be able to use this below.
20 time_output_args = ('-Tfields', '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta')
21
22 # Microsecond pcap, direct read was used to generate the baseline:
23 # tshark -Tfields -e frame.number -e frame.time_epoch -e frame.time_delta \
24 #   -r captures/dhcp.pcap > baseline/ff-ts-usec-pcap-direct.txt
25 baseline_file = 'ff-ts-usec-pcap-direct.txt'
26 baseline_fd = io.open(os.path.join(config.baseline_dir, baseline_file), 'r', encoding='UTF-8', errors='replace')
27 baseline_str = baseline_fd.read()
28 baseline_fd.close()
29
30 class case_fileformat_pcap(subprocesstest.SubprocessTestCase):
31     def test_pcap_usec_stdin(self):
32         '''Microsecond pcap direct vs microsecond pcap stdin'''
33         capture_file = os.path.join(config.capture_dir, 'dhcp.pcap')
34         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
35                 '-r', '-',
36                 '-Tfields',
37                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
38                 '<', capture_file
39                 , shell=True),
40             shell=True)
41         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
42
43     def test_pcap_nsec_stdin(self):
44         '''Microsecond pcap direct vs nanosecond pcap stdin'''
45         capture_file = os.path.join(config.capture_dir, 'dhcp-nanosecond.pcap')
46         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
47                 '-r', '-',
48                 '-Tfields',
49                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
50                 '<', capture_file
51                 , shell=True),
52             shell=True)
53         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
54
55     def test_pcap_nsec_direct(self):
56         '''Microsecond pcap direct vs nanosecond pcap direct'''
57         capture_file = os.path.join(config.capture_dir, 'dhcp-nanosecond.pcap')
58         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
59                 '-r', capture_file,
60                 '-Tfields',
61                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
62                 ),
63             )
64         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
65
66 class case_fileformat_pcapng(subprocesstest.SubprocessTestCase):
67     def test_pcapng_usec_stdin(self):
68         '''Microsecond pcap direct vs microsecond pcapng stdin'''
69         capture_file = os.path.join(config.capture_dir, 'dhcp.pcapng')
70         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
71                 '-r', '-',
72                 '-Tfields',
73                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
74                 '<', capture_file
75                 , shell=True),
76             shell=True)
77         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
78
79     def test_pcapng_usec_direct(self):
80         '''Microsecond pcap direct vs microsecond pcapng direct'''
81         capture_file = os.path.join(config.capture_dir, 'dhcp.pcapng')
82         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
83                 '-r', capture_file,
84                 '-Tfields',
85                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta',
86                 ),
87             )
88         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
89
90     def test_pcapng_nsec_stdin(self):
91         '''Microsecond pcap direct vs nanosecond pcapng stdin'''
92         capture_file = os.path.join(config.capture_dir, 'dhcp-nanosecond.pcapng')
93         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
94                 '-r', '-',
95                 '-Tfields',
96                 '-e', 'frame.number', '-e', 'frame.time_epoch', '-e', 'frame.time_delta'
97                 '<', capture_file
98                 , shell=True),
99             shell=True)
100         self.assertTrue(self.diffOutput(capture_proc.stdout_str, baseline_str, 'tshark', baseline_file))
101
102     def test_pcapng_nsec_direct(self):
103         '''Microsecond pcap direct vs nanosecond pcapng direct'''
104         capture_file = os.path.join(config.capture_dir, 'dhcp-nanosecond.pcapng')
105         capture_proc = self.runProcess(subprocesstest.capture_command(config.cmd_tshark,
106                 '-r', capture_file,
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, baseline_str, 'tshark', baseline_file))
112