Test: Fix capinfos output and command paths.
[metze/wireshark/wip.git] / test / suite_clopts.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 '''Command line option tests'''
11
12 import config
13 import os.path
14 import subprocess
15 import subprocesstest
16 import unittest
17
18 #glossaries = ('fields', 'protocols', 'values', 'decodes', 'defaultprefs', 'currentprefs')
19
20 glossaries = ('decodes', 'values')
21 testout_pcap = 'testout.pcap'
22
23 class case_dumpcap_invalid_chars(subprocesstest.SubprocessTestCase):
24     # XXX Should we generate individual test functions instead of looping?
25     def test_dumpcap_invalid_chars(self):
26         '''Invalid dumpcap parameters'''
27         for char_arg in 'CEFGHJKNOQRTUVWXYejloxz':
28             self.assertRun((config.cmd_dumpcap, '-' + char_arg),
29                            expected_return=self.exit_command_line)
30
31
32 class case_dumpcap_valid_chars(subprocesstest.SubprocessTestCase):
33     # XXX Should we generate individual test functions instead of looping?
34     def test_dumpcap_valid_chars(self):
35         for char_arg in 'hv':
36             self.assertRun((config.cmd_dumpcap, '-' + char_arg))
37
38
39 class case_dumpcap_invalid_interface_chars(subprocesstest.SubprocessTestCase):
40     # XXX Should we generate individual test functions instead of looping?
41     def test_dumpcap_interface_chars(self):
42         '''Valid dumpcap parameters requiring capture permissions'''
43         valid_returns = [self.exit_ok, self.exit_error]
44         for char_arg in 'DL':
45             process = self.runProcess((config.cmd_dumpcap, '-' + char_arg))
46             self.assertIn(process.returncode, valid_returns)
47
48
49 class case_dumpcap_capture_clopts(subprocesstest.SubprocessTestCase):
50     def test_dumpcap_invalid_capfilter(self):
51         '''Invalid capture filter'''
52         if not config.canCapture():
53             self.skipTest('Test requires capture privileges and an interface.')
54         invalid_filter = '__invalid_protocol'
55         # $DUMPCAP -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>&1
56         testout_file = self.filename_from_id(testout_pcap)
57         self.runProcess((config.cmd_dumpcap, '-f', invalid_filter, '-w', testout_file ))
58         self.assertTrue(self.grepOutput('Invalid capture filter "' + invalid_filter + '" for interface'))
59
60     def test_dumpcap_invalid_interface_name(self):
61         '''Invalid capture interface name'''
62         if not config.canCapture():
63             self.skipTest('Test requires capture privileges and an interface.')
64         invalid_interface = '__invalid_interface'
65         # $DUMPCAP -i invalid_interface -w './testout.pcap' > ./testout.txt 2>&1
66         testout_file = self.filename_from_id(testout_pcap)
67         self.runProcess((config.cmd_dumpcap, '-i', invalid_interface, '-w', testout_file))
68         self.assertTrue(self.grepOutput('The capture session could not be initiated'))
69
70     def test_dumpcap_invalid_interface_index(self):
71         '''Invalid capture interface index'''
72         if not config.canCapture():
73             self.skipTest('Test requires capture privileges and an interface.')
74         invalid_index = '0'
75         # $DUMPCAP -i 0 -w './testout.pcap' > ./testout.txt 2>&1
76         testout_file = self.filename_from_id(testout_pcap)
77         self.runProcess((config.cmd_dumpcap, '-i', invalid_index, '-w', testout_file))
78         self.assertTrue(self.grepOutput('There is no interface with that adapter index'))
79
80
81 class case_basic_clopts(subprocesstest.SubprocessTestCase):
82     def test_existing_file(self):
83         # $TSHARK -r "${CAPTURE_DIR}dhcp.pcap" > ./testout.txt 2>&1
84         cap_file = os.path.join(config.capture_dir, 'dhcp.pcap')
85         self.assertRun((config.cmd_tshark, '-r', cap_file))
86
87     def test_nonexistent_file(self):
88         # $TSHARK - r ThisFileDontExist.pcap > ./testout.txt 2 > &1
89         cap_file = os.path.join(config.capture_dir, '__ceci_nest_pas_une.pcap')
90         self.assertRun((config.cmd_tshark, '-r', cap_file),
91                        expected_return=self.exit_error)
92
93
94 class case_tshark_invalid_chars(subprocesstest.SubprocessTestCase):
95     # XXX Should we generate individual test functions instead of looping?
96     def test_tshark_invalid_chars(self):
97         '''Invalid tshark parameters'''
98         for char_arg in 'ABCEFHJKMNORTUWXYZabcdefijkmorstuwyz':
99             self.assertRun((config.cmd_tshark, '-' + char_arg),
100                            expected_return=self.exit_command_line)
101
102
103 class case_tshark_valid_chars(subprocesstest.SubprocessTestCase):
104     # XXX Should we generate individual test functions instead of looping?
105     def test_tshark_valid_chars(self):
106         for char_arg in 'Ghv':
107             self.assertRun((config.cmd_tshark, '-' + char_arg))
108
109
110 class case_tshark_invalid_interface_chars(subprocesstest.SubprocessTestCase):
111     # XXX Should we generate individual test functions instead of looping?
112     def test_tshark_interface_chars(self):
113         '''Valid tshark parameters requiring capture permissions'''
114         valid_returns = [self.exit_ok, self.exit_error]
115         for char_arg in 'DL':
116             process = self.runProcess((config.cmd_tshark, '-' + char_arg))
117             self.assertIn(process.returncode, valid_returns)
118
119
120 class case_tshark_capture_clopts(subprocesstest.SubprocessTestCase):
121     def test_tshark_invalid_capfilter(self):
122         '''Invalid capture filter'''
123         if not config.canCapture():
124             self.skipTest('Test requires capture privileges and an interface.')
125         invalid_filter = '__invalid_protocol'
126         # $TSHARK -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>&1
127         testout_file = self.filename_from_id(testout_pcap)
128         self.runProcess((config.cmd_tshark, '-f', invalid_filter, '-w', testout_file ))
129         self.assertTrue(self.grepOutput('Invalid capture filter "' + invalid_filter + '" for interface'))
130
131     def test_tshark_invalid_interface_name(self):
132         '''Invalid capture interface name'''
133         if not config.canCapture():
134             self.skipTest('Test requires capture privileges and an interface.')
135         invalid_interface = '__invalid_interface'
136         # $TSHARK -i invalid_interface -w './testout.pcap' > ./testout.txt 2>&1
137         testout_file = self.filename_from_id(testout_pcap)
138         self.runProcess((config.cmd_tshark, '-i', invalid_interface, '-w', testout_file))
139         self.assertTrue(self.grepOutput('The capture session could not be initiated'))
140
141     def test_tshark_invalid_interface_index(self):
142         '''Invalid capture interface index'''
143         if not config.canCapture():
144             self.skipTest('Test requires capture privileges and an interface.')
145         invalid_index = '0'
146         # $TSHARK -i 0 -w './testout.pcap' > ./testout.txt 2>&1
147         testout_file = self.filename_from_id(testout_pcap)
148         self.runProcess((config.cmd_tshark, '-i', invalid_index, '-w', testout_file))
149         self.assertTrue(self.grepOutput('There is no interface with that adapter index'))
150
151
152 class case_tshark_name_resolution_clopts(subprocesstest.SubprocessTestCase):
153     def test_tshark_valid_name_resolution(self):
154         if not config.canCapture():
155             self.skipTest('Test requires capture privileges and an interface.')
156         # $TSHARK -N mntC -a duration:1 > ./testout.txt 2>&1
157         self.assertRun((config.cmd_tshark, '-N', 'mntC', '-a', 'duration: 1'))
158
159     # XXX Add invalid name resolution.
160
161 class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase):
162     def test_tshark_dump_glossary(self):
163         for glossary in glossaries:
164             try:
165                 self.log_fd.truncate()
166             except:
167                 pass
168             self.assertRun((config.cmd_tshark, '-G', glossary))
169
170     def test_tshark_glossary_valid_utf8(self):
171         for glossary in glossaries:
172             env = os.environ.copy()
173             env['LANG'] = 'en_US.UTF-8'
174             g_contents = subprocess.check_output((config.cmd_tshark, '-G', glossary), env=env, stderr=subprocess.PIPE)
175             decoded = True
176             try:
177                 g_contents.decode('UTF-8')
178             except UnicodeDecodeError:
179                 decoded = False
180             self.assertTrue(decoded, '{} is not valid UTF-8'.format(glossary))
181
182     def test_tshark_glossary_plugin_count(self):
183         self.runProcess((config.cmd_tshark, '-G', 'plugins'), env=os.environ.copy())
184         self.assertGreaterEqual(self.countOutput('dissector'), 10, 'Fewer than 10 dissector plugins found')
185
186
187 # Purposefully fail a test. Used for testing the test framework.
188 # class case_fail_on_purpose(subprocesstest.SubprocessTestCase):
189 #     def test_fail_on_purpose(self):
190 #         self.runProcess(('echo', 'hello, world'))
191 #         self.fail('Not implemented')