Lua: Switch from disable_lua to enable_lua.
[metze/wireshark/wip.git] / test / suite_mergecap.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 '''Mergecap tests'''
11
12 import config
13 import os.path
14 import re
15 import subprocesstest
16 import unittest
17
18 dhcp_pcap = os.path.join(config.capture_dir, 'dhcp.pcap')
19 dhcp_pcapng = os.path.join(config.capture_dir, 'dhcp.pcapng')
20 dhcp_nanosecond_pcap = os.path.join(config.capture_dir, 'dhcp-nanosecond.pcap')
21 empty_pcap = os.path.join(config.capture_dir, 'empty.pcap')
22 many_interfaces_pcapng_1 = os.path.join(config.capture_dir, 'many_interfaces.pcapng.1')
23 many_interfaces_pcapng_2 = os.path.join(config.capture_dir, 'many_interfaces.pcapng.2')
24 many_interfaces_pcapng_3 = os.path.join(config.capture_dir, 'many_interfaces.pcapng.3')
25 rsasnakeoil2_pcap = os.path.join(config.capture_dir, 'rsasnakeoil2.pcap')
26 testout_pcap = 'testout.pcap'
27 testout_pcapng = 'testout.pcapng'
28
29 file_type_to_descr = {
30     'pcap': 'Wireshark/tcpdump/... - pcap',
31     'pcapng': 'Wireshark/... - pcapng',
32 }
33
34 file_type_to_testout = {
35     'pcap': testout_pcap,
36     'pcapng': testout_pcapng,
37 }
38
39 # common checking code:
40 # arg 1 = return value from mergecap command
41 # arg 2 = file type string
42 # arg 3 = file encap
43 # arg 4 = number of IDBs generated
44 # arg 5 = number of file packets merged
45 # arg 6 = number of some IDB packets merged
46 def check_mergecap(self, mergecap_proc, file_type, encapsulation, tot_packets, generated_idbs, idb_packets):
47     mergecap_returncode = mergecap_proc.returncode
48     self.assertEqual(mergecap_returncode, 0)
49     if mergecap_returncode != 0:
50         return
51
52     mergecap_success = self.grepOutput('merging complete')
53     self.assertTrue(mergecap_success)
54     if not mergecap_success:
55         return
56
57     self.assertTrue(file_type in file_type_to_descr, 'Invalid file type')
58
59     testout_file = self.filename_from_id(file_type_to_testout[file_type])
60     capinfos_testout = self.getCaptureInfo(capinfos_args=('-t', '-E', '-I', '-c'), cap_file=testout_file)
61
62     file_descr = file_type_to_descr[file_type]
63     type_pat = 'File type:\s+{}'.format(file_descr)
64     self.assertTrue(re.search(type_pat, capinfos_testout) is not None,
65         'Failed to generate a {} file'.format(file_type))
66
67     encap_pat = 'File encapsulation:\s+{}'.format(encapsulation)
68     self.assertTrue(re.search(encap_pat, capinfos_testout) is not None,
69         'Failed to generate an {} encapsulation'.format(encapsulation))
70
71     pkt_pat = 'Number of packets:\s+{}'.format(tot_packets)
72     self.assertTrue(re.search(pkt_pat, capinfos_testout) is not None,
73         'Failed to generate {} packets'.format(tot_packets))
74
75     gidb_pat = 'Number of interfaces in file:\s+{}'.format(generated_idbs)
76     self.assertTrue(re.search(gidb_pat, capinfos_testout) is not None,
77         'Failed to generate {} IDBs'.format(generated_idbs))
78
79     midb_pat = '\s+Number of packets\s+=\s+{}'.format(idb_packets)
80     self.assertTrue(re.search(midb_pat, capinfos_testout) is not None,
81         'Failed to merge {} IDB packets'.format(idb_packets))
82
83 class case_mergecap_pcap(subprocesstest.SubprocessTestCase):
84     def test_mergecap_basic_1_pcap_pcap(self):
85         '''Merge a single pcap file to pcap'''
86         # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
87         testout_file = self.filename_from_id(testout_pcap)
88         mergecap_proc = self.runProcess((config.cmd_mergecap,
89             '-v',
90             '-F', 'pcap',
91             '-w', testout_file,
92             dhcp_pcap,
93         ))
94         check_mergecap(self, mergecap_proc, 'pcap', 'Ethernet', 4, 1, 4)
95
96     def test_mergecap_basic_2_pcap_pcap(self):
97         '''Merge two pcap files to pcap'''
98         # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
99         testout_file = self.filename_from_id(testout_pcap)
100         mergecap_proc = self.runProcess((config.cmd_mergecap,
101             '-v',
102             '-F', 'pcap',
103             '-w', testout_file,
104             dhcp_pcap, dhcp_pcap,
105         ))
106         check_mergecap(self, mergecap_proc, 'pcap', 'Ethernet', 8, 1, 8)
107
108     def test_mergecap_basic_3_empty_pcap_pcap(self):
109         '''Merge three pcap files to pcap, two empty'''
110         # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
111         testout_file = self.filename_from_id(testout_pcap)
112         mergecap_proc = self.runProcess((config.cmd_mergecap,
113             '-v',
114             '-F', 'pcap',
115             '-w', testout_file,
116             empty_pcap, dhcp_pcap, empty_pcap,
117         ))
118         check_mergecap(self, mergecap_proc, 'pcap', 'Ethernet', 4, 1, 4)
119
120     def test_mergecap_basic_2_nano_pcap_pcap(self):
121         '''Merge two pcap files to pcap, one with nanosecond timestamps'''
122         # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
123         testout_file = self.filename_from_id(testout_pcap)
124         mergecap_proc = self.runProcess((config.cmd_mergecap,
125             '-v',
126             '-F', 'pcap',
127             '-w', testout_file,
128             dhcp_nanosecond_pcap, rsasnakeoil2_pcap,
129         ))
130         check_mergecap(self, mergecap_proc, 'pcap', 'Ethernet', 62, 1, 62)
131
132 class case_mergecap_pcapng(subprocesstest.SubprocessTestCase):
133     def test_mergecap_basic_1_pcap_pcapng(self):
134         '''Merge a single pcap file to pcapng'''
135         # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
136         testout_file = self.filename_from_id(testout_pcapng)
137         mergecap_proc = self.runProcess((config.cmd_mergecap,
138             '-v',
139             '-w', testout_file,
140             dhcp_pcap,
141         ))
142         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 4, 1, 4)
143
144     def test_mergecap_basic_2_pcap_pcapng(self):
145         '''Merge two pcap files to pcapng'''
146         # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
147         testout_file = self.filename_from_id(testout_pcapng)
148         mergecap_proc = self.runProcess((config.cmd_mergecap,
149             '-v',
150             '-w', testout_file,
151             dhcp_pcap, dhcp_pcap,
152         ))
153         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8)
154
155     def test_mergecap_basic_2_pcap_none_pcapng(self):
156         '''Merge two pcap files to pcapng, "none" merge mode'''
157         # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
158         testout_file = self.filename_from_id(testout_pcapng)
159         mergecap_proc = self.runProcess((config.cmd_mergecap,
160             '-v',
161             '-I', 'none',
162             '-w', testout_file,
163             dhcp_pcap, dhcp_pcap,
164         ))
165         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 8, 2, 4)
166
167     def test_mergecap_basic_2_pcap_all_pcapng(self):
168         '''Merge two pcap files to pcapng, "all" merge mode'''
169         # $MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
170         testout_file = self.filename_from_id(testout_pcapng)
171         mergecap_proc = self.runProcess((config.cmd_mergecap,
172             '-v',
173             '-I', 'all',
174             '-w', testout_file,
175             dhcp_pcap, dhcp_pcap,
176         ))
177         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8)
178
179     def test_mergecap_basic_2_pcap_any_pcapng(self):
180         '''Merge two pcap files to pcapng, "any" merge mode'''
181         # $MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
182         testout_file = self.filename_from_id(testout_pcapng)
183         mergecap_proc = self.runProcess((config.cmd_mergecap,
184             '-v',
185             '-I', 'any',
186             '-w', testout_file,
187             dhcp_pcap, dhcp_pcap,
188         ))
189         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 8, 1, 8)
190
191     def test_mergecap_basic_1_pcapng_pcapng(self):
192         '''Merge a single pcapng file to pcapng'''
193         # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
194         testout_file = self.filename_from_id(testout_pcapng)
195         mergecap_proc = self.runProcess((config.cmd_mergecap,
196             '-v',
197             '-w', testout_file,
198             dhcp_pcapng,
199         ))
200         check_mergecap(self, mergecap_proc, 'pcapng', 'Ethernet', 4, 1, 4)
201
202     def test_mergecap_1_pcapng_many_pcapng(self):
203         '''Merge one pcapng file with many interfaces to pcapng'''
204         # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
205         testout_file = self.filename_from_id(testout_pcapng)
206         mergecap_proc = self.runProcess((config.cmd_mergecap,
207             '-v',
208             '-w', testout_file,
209             many_interfaces_pcapng_1,
210         ))
211         check_mergecap(self, mergecap_proc, 'pcapng', 'Per packet', 64, 11, 62)
212
213     def test_mergecap_3_pcapng_pcapng(self):
214         '''Merge multiple pcapng files with many interfaces to pcapng'''
215         # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
216         testout_file = self.filename_from_id(testout_pcapng)
217         mergecap_proc = self.runProcess((config.cmd_mergecap,
218             '-v',
219             '-w', testout_file,
220             many_interfaces_pcapng_1,
221             many_interfaces_pcapng_2,
222             many_interfaces_pcapng_3,
223         ))
224         check_mergecap(self, mergecap_proc, 'pcapng', 'Per packet', 88, 11, 86)
225
226     def test_mergecap_3_pcapng_none_pcapng(self):
227         '''Merge multiple pcapng files with many interfaces to pcapng, "none" merge mode'''
228         # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
229         testout_file = self.filename_from_id(testout_pcapng)
230         mergecap_proc = self.runProcess((config.cmd_mergecap,
231             '-v',
232             '-I', 'none',
233             '-w', testout_file,
234             many_interfaces_pcapng_1,
235             many_interfaces_pcapng_2,
236             many_interfaces_pcapng_3,
237         ))
238         check_mergecap(self, mergecap_proc, 'pcapng', 'Per packet', 88, 33, 62)
239
240     def test_mergecap_3_pcapng_all_pcapng(self):
241         '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
242         # build a pcapng of all the interfaces repeated by using mode 'none'
243         # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
244         testin_file = self.filename_from_id('testin.pcapng')
245         self.assertRun((config.cmd_mergecap,
246             '-v',
247             '-I', 'none',
248             '-w', testin_file,
249             many_interfaces_pcapng_1,
250             many_interfaces_pcapng_2,
251             many_interfaces_pcapng_3,
252         ))
253         # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
254
255         # and use that generated pcap for our test
256         # $MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
257         testout_file = self.filename_from_id(testout_pcapng)
258         mergecap_proc = self.runProcess((config.cmd_mergecap,
259             '-v',
260             '-I', 'all',
261             '-w', testout_file,
262             testin_file, testin_file, testin_file,
263         ))
264         # check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
265         check_mergecap(self, mergecap_proc, 'pcapng', 'Per packet', 264, 33, 186)
266
267     def test_mergecap_3_pcapng_any_pcapng(self):
268         '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
269         # build a pcapng of all the interfaces repeated by using mode 'none'
270         # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
271         testin_file = self.filename_from_id('testin.pcapng')
272         self.assertRun((config.cmd_mergecap,
273             '-v',
274             '-I', 'none',
275             '-w', testin_file,
276             many_interfaces_pcapng_1,
277             many_interfaces_pcapng_2,
278             many_interfaces_pcapng_3,
279         ))
280         # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
281
282         # and use that generated pcap for our test
283         # $MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
284         testout_file = self.filename_from_id(testout_pcapng)
285         mergecap_proc = self.runProcess((config.cmd_mergecap,
286             '-v',
287             '-I', 'any',
288             '-w', testout_file,
289             testin_file, testin_file, testin_file,
290         ))
291         # check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
292         check_mergecap(self, mergecap_proc, 'pcapng', 'Per packet', 264, 11, 258)
293