Test: Be more paranoid about our log output.
[metze/wireshark/wip.git] / test / suite_wslua.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 '''Wireshark Lua scripting tests'''
11
12 import config
13 import filecmp
14 import io
15 import os.path
16 import subprocesstest
17 import unittest
18
19 dhcp_pcap = 'dhcp.pcap'
20 dns_port_pcap = 'dns_port.pcap'
21 empty_pcap = 'empty.pcap'
22 segmented_fpm_pcap = 'segmented_fpm.pcap'
23 sip_pcapng = 'sip.pcapng'
24 sipmsg_log = 'sipmsg.log'
25 wpa_induction_pcap_gz = 'wpa-Induction.pcap.gz'
26
27 def check_lua_script(self, lua_script, cap_file, check_passed, *args):
28     if not config.have_lua:
29         self.skipTest('Test requires Lua scripting support.')
30     tshark_cmd = [config.cmd_tshark,
31         '-r', os.path.join(config.capture_dir, cap_file),
32         '-X', 'lua_script:' + os.path.join(config.lua_dir, lua_script)
33     ]
34     tshark_cmd += args
35     tshark_proc = self.assertRun(tshark_cmd)
36
37     if check_passed:
38         self.assertTrue(self.grepOutput(r'All tests passed!'))
39
40     return tshark_proc
41
42 def check_lua_script_verify(self, lua_script, cap_file, check_stage_1=False, heur_regmode=None):
43     # First run tshark with the dissector script.
44     if heur_regmode is None:
45         tshark_proc = check_lua_script(self, lua_script, dns_port_pcap, check_stage_1,
46             '-V'
47         )
48     else:
49         tshark_proc = check_lua_script(self, lua_script, dns_port_pcap, check_stage_1,
50             '-V',
51             '-X', 'lua_script1:heur_regmode={}'.format(heur_regmode)
52         )
53
54     # then dump tshark's output to a verification file.
55     verify_file = self.filename_from_id('testin.txt')
56     with io.open(verify_file, 'w', newline='\n') as testin_fd:
57         testin_fd.write(tshark_proc.stdout_str)
58         testin_fd.close()
59
60     # finally run tshark again with the verification script and the verification file.
61     if heur_regmode is None:
62         check_lua_script(self, 'verify_dissector.lua', empty_pcap, True,
63             '-X', 'lua_script1:verify_file=' + verify_file,
64         )
65     else:
66         check_lua_script(self, 'verify_dissector.lua', empty_pcap, True,
67             '-X', 'lua_script1:verify_file=' + verify_file,
68             '-X', 'lua_script1:no_heur',
69         )
70
71 class case_wslua(subprocesstest.SubprocessTestCase):
72     def test_wslua_dir(self):
73         '''wslua directory functions'''
74         check_lua_script(self, 'dir.lua', empty_pcap, True)
75
76     # Mode_1, mode_2, and mode_3, and fpm were all under wslua_step_dissector_test
77     # in the Bash version.
78     def test_wslua_dissector_mode_1(self):
79         '''wslua dissector functions, mode 1'''
80         check_lua_script_verify(self, 'dissector.lua', dns_port_pcap)
81
82     def test_wslua_dissector_mode_2(self):
83         '''wslua dissector functions, mode 2'''
84         check_lua_script_verify(self, 'dissector.lua', dns_port_pcap, heur_regmode=2)
85
86     def test_wslua_dissector_mode_3(self):
87         '''wslua dissector functions, mode 3'''
88         check_lua_script_verify(self, 'dissector.lua', dns_port_pcap, heur_regmode=3)
89
90     def test_wslua_dissector_fpm(self):
91         '''wslua dissector functions, fpm'''
92         tshark_fpm_tcp_proc = check_lua_script(self, 'dissectFPM.lua', segmented_fpm_pcap, False,
93             '-T', 'fields',
94             '-e', 'frame.number',
95             '-e', 'fpm',
96             '-e', 'fpm.version',
97             '-e', 'fpm.type',
98             '-e', 'fpm.length',
99             '-o', 'fpm.dissect_tcp:true'
100         )
101
102         tshark_fpm_no_tcp_proc = check_lua_script(self, 'dissectFPM.lua', segmented_fpm_pcap, False,
103             '-T', 'fields',
104             '-e', 'frame.number',
105             '-e', 'fpm',
106             '-e', 'fpm.version',
107             '-e', 'fpm.type',
108             '-e', 'fpm.length',
109             '-o', 'fpm.dissect_tcp:false'
110         )
111
112         self.diffOutput(tshark_fpm_tcp_proc.stdout_str,
113             tshark_fpm_no_tcp_proc.stdout_str,
114             'fpm.dissect_tcp:true',
115             'fpm.dissect_tcp:false',
116         )
117
118     def test_wslua_field(self):
119         '''wslua fields'''
120         check_lua_script(self, 'field.lua', dhcp_pcap, True)
121
122     # reader, writer, and acme_reader were all under wslua_step_file_test
123     # in the Bash version.
124     def test_wslua_file_reader(self):
125         '''wslua file reader'''
126         cap_file_1 = os.path.join(config.capture_dir, dhcp_pcap)
127         cap_file_2 = os.path.join(config.capture_dir, wpa_induction_pcap_gz)
128
129         # First run tshark with the pcap_file_reader script.
130         lua_proc_1 = check_lua_script(self, 'pcap_file.lua', cap_file_1, False)
131         lua_proc_2 = check_lua_script(self, 'pcap_file.lua', cap_file_2, False)
132         lua_out = lua_proc_1.stdout_str + lua_proc_2.stdout_str
133
134         # then run tshark again without the script
135         tshark_proc_1 = self.assertRun((config.cmd_tshark, '-r', cap_file_1))
136         tshark_proc_2 = self.assertRun((config.cmd_tshark, '-r', cap_file_2))
137         tshark_out = tshark_proc_1.stdout_str + tshark_proc_2.stdout_str
138
139         self.diffOutput(lua_out, tshark_out, 'tshark + lua script', 'tshark only')
140
141     def test_wslua_file_writer(self):
142         '''wslua file writer'''
143         cap_file_1 = os.path.join(config.capture_dir, dhcp_pcap)
144         cap_file_2 = self.filename_from_id('lua_writer.pcap')
145
146         # Generate a new capture file using the Lua writer.
147         check_lua_script(self, 'pcap_file.lua', cap_file_1, False,
148             '-w', cap_file_2,
149             '-F', 'lua_pcap2',
150         )
151         self.assertTrue(filecmp.cmp(cap_file_1, cap_file_2), cap_file_1 + ' differs from ' + cap_file_2)
152
153     def test_wslua_file_acme_reader(self):
154         '''wslua acme file reader'''
155
156         cap_file = self.filename_from_id('lua_acme_reader.pcap')
157         # Read an acme sipmsg.log using the acme Lua reader, writing it out as pcapng.
158         check_lua_script(self, 'acme_file.lua', sipmsg_log, False,
159             '-w', cap_file,
160             '-F', 'pcapng',
161         )
162
163         # Read lua_acme_reader.pcap and sip.pcapng and compare their verbose outputs.
164         tshark_proc_1 = self.assertRun((config.cmd_tshark,
165             '-r', cap_file,
166             '-V'
167         ))
168         tshark_proc_2 = self.assertRun((config.cmd_tshark,
169             '-r', os.path.join(config.capture_dir, sip_pcapng),
170             '-V'
171         ))
172         self.diffOutput(tshark_proc_1.stdout_str, tshark_proc_2.stdout_str, 'sipmsg.log', 'sip.pcapng')
173
174     def test_wslua_listener(self):
175         '''wslua listener'''
176         check_lua_script(self, 'listener.lua', dhcp_pcap, True)
177
178     def test_wslua_nstime(self):
179         '''wslua nstime'''
180         check_lua_script(self, 'nstime.lua', dhcp_pcap, True)
181
182     def test_wslua_pinfo(self):
183         '''wslua pinfo'''
184         check_lua_script(self, 'pinfo.lua', dhcp_pcap, True)
185
186     def test_wslua_proto(self):
187         '''wslua proto'''
188         check_lua_script_verify(self, 'proto.lua', dns_port_pcap, check_stage_1=True)
189
190     def test_wslua_protofield_tree(self):
191         '''wslua protofield with a tree'''
192         check_lua_script(self, 'protofield.lua', dns_port_pcap, True,
193             '-V',
194             '-Y', 'test.filtered==1',
195         )
196
197     def test_wslua_protofield_no_tree(self):
198         '''wslua protofield without a tree'''
199         check_lua_script(self, 'protofield.lua', dns_port_pcap, True,
200             '-Y', 'test.filtered==1',
201         )
202
203     def test_wslua_int64(self):
204         '''wslua int64'''
205         check_lua_script(self, 'int64.lua', empty_pcap, True)
206
207     def test_wslua_args_1(self):
208         '''wslua args 1'''
209         check_lua_script(self, 'script_args.lua', empty_pcap, True,
210             '-X', 'lua_script1:1',
211         )
212
213     def test_wslua_args_2(self):
214         '''wslua args 2'''
215         check_lua_script(self, 'script_args.lua', empty_pcap, True,
216             '-X', 'lua_script1:3',
217             '-X', 'lua_script1:foo',
218             '-X', 'lua_script1:bar',
219         )
220
221     def test_wslua_args_3(self):
222         '''wslua args 3'''
223         check_lua_script(self, 'script_args.lua', empty_pcap, True,
224             '-X', 'lua_script:' + os.path.join(config.lua_dir, 'script_args.lua'),
225             '-X', 'lua_script1:3',
226             '-X', 'lua_script2:1',
227             '-X', 'lua_script1:foo',
228             '-X', 'lua_script1:bar',
229         )
230
231     def test_wslua_args_4(self):
232         '''wslua args 4'''
233         check_lua_script(self, 'script_args.lua', empty_pcap, False)
234         self.assertFalse(self.grepOutput(r'All tests passed!'))
235
236     def test_wslua_args_5(self):
237         '''wslua args 5'''
238         check_lua_script(self, 'script_args.lua', empty_pcap, False,
239             '-X', 'lua_script1:3',
240         )
241         self.assertFalse(self.grepOutput(r'All tests passed!'))
242
243     def test_wslua_globals(self):
244         '''wslua globals'''
245         check_lua_script(self, 'verify_globals.lua', empty_pcap, True,
246             '-X', 'lua_script1:' + os.path.join(config.lua_dir, ''),
247             '-X', 'lua_script1:' + os.path.join(config.lua_dir, 'globals_2.2.txt'),
248         )
249
250     @unittest.skip('GRegex tests are broken since PCRE 8.34, see bug 12997.')
251     def test_wslua_gregex(self):
252         '''wslua GRegex'''
253         check_lua_script(self, 'gregex.lua', empty_pcap, True,
254             '-X', 'lua_script1:' + os.path.join(config.lua_dir, ''),
255             '-X', 'lua_script1:glib',
256             '-X', 'lua_script1:-V',
257         )
258
259     def test_wslua_struct(self):
260         '''wslua struct'''
261         check_lua_script(self, 'struct.lua', empty_pcap, True)
262
263     def test_wslua_tvb_tree(self):
264         '''wslua tvb with a tree'''
265         check_lua_script(self, 'tvb.lua', dns_port_pcap, True, '-V')
266
267     def test_wslua_tvb_no_tree(self):
268         '''wslua tvb without a tree'''
269         check_lua_script(self, 'tvb.lua', dns_port_pcap, True)