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 '''Command line option tests'''
18 #glossaries = ('fields', 'protocols', 'values', 'decodes', 'defaultprefs', 'currentprefs')
20 glossaries = ('decodes', 'values')
21 testout_pcap = 'testout.pcap'
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)
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):
36 self.assertRun((config.cmd_dumpcap, '-' + char_arg))
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]
45 process = self.runProcess((config.cmd_dumpcap, '-' + char_arg))
46 self.assertIn(process.returncode, valid_returns)
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'))
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'))
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.')
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'))
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))
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)
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)
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))
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)
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'))
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'))
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.')
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'))
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'))
159 # XXX Add invalid name resolution.
161 class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase):
162 def test_tshark_dump_glossary(self):
163 for glossary in glossaries:
165 self.log_fd.truncate()
168 self.assertRun((config.cmd_tshark, '-G', glossary))
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)
177 g_contents.decode('UTF-8')
178 except UnicodeDecodeError:
180 self.assertTrue(decoded, '{} is not valid UTF-8'.format(glossary))
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')
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')