3 # By Gerald Combs <gerald@wireshark.org>
5 # Ported from a set of Bash scripts which were copyright 2005 Ulf Lamping
7 # SPDX-License-Identifier: GPL-2.0-or-later
13 from subprocesstest
import grep_output
15 testout_pcap
= 'testout.pcap'
16 testout_pcapng
= 'testout.pcapng'
18 file_type_to_descr
= {
19 'pcap': 'Wireshark/tcpdump/... - pcap',
20 'pcapng': 'Wireshark/... - pcapng',
23 # common checking code:
24 # arg 1 = return value from mergecap command
25 # arg 2 = file type string
27 # arg 4 = number of IDBs generated
28 # arg 5 = number of file packets merged
29 # arg 6 = number of some IDB packets merged
30 def check_mergecap(mergecap_proc
, file_type
, encapsulation
, tot_packets
, generated_idbs
, idb_packets
, cmd_capinfos
, testout_file
, env
=None):
32 assert mergecap_proc
.returncode
== 0
33 assert grep_output(mergecap_proc
.stderr
, 'merging complete')
34 assert file_type
in file_type_to_descr
, 'Invalid file type'
36 capinfos_stdout
= subprocess
.check_output([cmd_capinfos
, '-t', '-E', '-I', '-c', testout_file
], encoding
='utf-8', env
=env
)
38 file_descr
= file_type_to_descr
[file_type
]
39 type_pat
= r
'File type:\s+{}'.format(file_descr
)
40 assert re
.search(type_pat
, capinfos_stdout
), \
41 'Failed to generate a {} file'.format(file_type
)
43 encap_pat
= r
'File encapsulation:\s+{}'.format(encapsulation
)
44 assert re
.search(encap_pat
, capinfos_stdout
), \
45 'Failed to generate an {} encapsulation'.format(encapsulation
)
47 pkt_pat
= r
'Number of packets:\s+{}'.format(tot_packets
)
48 assert re
.search(pkt_pat
, capinfos_stdout
), \
49 'Failed to generate {} packets'.format(tot_packets
)
51 gidb_pat
= r
'Number of interfaces in file:\s+{}'.format(generated_idbs
)
52 assert re
.search(gidb_pat
, capinfos_stdout
), \
53 'Failed to generate {} IDBs'.format(generated_idbs
)
55 midb_pat
= r
'\s+Number of packets\s+=\s+{}'.format(idb_packets
)
56 assert re
.search(midb_pat
, capinfos_stdout
), \
57 'Failed to merge {} IDB packets'.format(idb_packets
)
60 class TestMergecapPcap
:
61 def test_mergecap_basic_1_pcap_pcap(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
62 '''Merge a single pcap file to pcap'''
63 # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
64 testout_file
= result_file(testout_pcap
)
65 mergecap_proc
= subprocess
.run((cmd_mergecap
,
69 capture_file('dhcp.pcap'),
70 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
71 check_mergecap(mergecap_proc
, 'pcap', 'Ethernet', 4, 1, 4, cmd_capinfos
, testout_file
, test_env
)
73 def test_mergecap_basic_2_pcap_pcap(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
74 '''Merge two pcap files to pcap'''
75 # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
76 testout_file
= result_file(testout_pcap
)
77 mergecap_proc
= subprocess
.run((cmd_mergecap
,
81 capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
82 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
83 check_mergecap(mergecap_proc
, 'pcap', 'Ethernet', 8, 1, 8, cmd_capinfos
, testout_file
, test_env
)
85 def test_mergecap_basic_3_empty_pcap_pcap(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
86 '''Merge three pcap files to pcap, two empty'''
87 # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
88 testout_file
= result_file(testout_pcap
)
89 mergecap_proc
= subprocess
.run((cmd_mergecap
,
93 capture_file('empty.pcap'), capture_file('dhcp.pcap'), capture_file('empty.pcap'),
94 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
95 check_mergecap(mergecap_proc
, 'pcap', 'Ethernet', 4, 1, 4, cmd_capinfos
, testout_file
, test_env
)
97 def test_mergecap_basic_2_nano_pcap_pcap(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
98 '''Merge two pcap files to pcap, one with nanosecond timestamps'''
99 # $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
100 testout_file
= result_file(testout_pcap
)
101 mergecap_proc
= subprocess
.run((cmd_mergecap
,
105 capture_file('dhcp-nanosecond.pcap'), capture_file('rsasnakeoil2.pcap'),
106 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
107 check_mergecap(mergecap_proc
, 'pcap', 'Ethernet', 62, 1, 62, cmd_capinfos
, testout_file
, test_env
)
110 class TestMergecapPcapng
:
111 def test_mergecap_basic_1_pcap_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
112 '''Merge a single pcap file to pcapng'''
113 # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
114 testout_file
= result_file(testout_pcapng
)
115 mergecap_proc
= subprocess
.run((cmd_mergecap
,
118 capture_file('dhcp.pcap'),
119 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
120 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 4, 1, 4, cmd_capinfos
, testout_file
, test_env
)
122 def test_mergecap_basic_2_pcap_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
123 '''Merge two pcap files to pcapng'''
124 # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
125 testout_file
= result_file(testout_pcapng
)
126 mergecap_proc
= subprocess
.run((cmd_mergecap
,
129 capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
130 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
131 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos
, testout_file
, test_env
)
133 def test_mergecap_basic_2_pcap_none_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
134 '''Merge two pcap files to pcapng, "none" merge mode'''
135 # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
136 testout_file
= result_file(testout_pcapng
)
137 mergecap_proc
= subprocess
.run((cmd_mergecap
,
141 capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
142 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
143 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 8, 2, 4, cmd_capinfos
, testout_file
, test_env
)
145 def test_mergecap_basic_2_pcap_all_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
146 '''Merge two pcap files to pcapng, "all" merge mode'''
147 # $MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
148 testout_file
= result_file(testout_pcapng
)
149 mergecap_proc
= subprocess
.run((cmd_mergecap
,
153 capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
154 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
155 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos
, testout_file
, test_env
)
157 def test_mergecap_basic_2_pcap_any_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
158 '''Merge two pcap files to pcapng, "any" merge mode'''
159 # $MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
160 testout_file
= result_file(testout_pcapng
)
161 mergecap_proc
= subprocess
.run((cmd_mergecap
,
165 capture_file('dhcp.pcap'), capture_file('dhcp.pcap'),
166 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
167 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 8, 1, 8, cmd_capinfos
, testout_file
, test_env
)
169 def test_mergecap_basic_1_pcapng_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
170 '''Merge a single pcapng file to pcapng'''
171 # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
172 testout_file
= result_file(testout_pcapng
)
173 mergecap_proc
= subprocess
.run((cmd_mergecap
,
176 capture_file('dhcp.pcapng'),
177 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
178 check_mergecap(mergecap_proc
, 'pcapng', 'Ethernet', 4, 1, 4, cmd_capinfos
, testout_file
, test_env
)
180 def test_mergecap_1_pcapng_many_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
181 '''Merge one pcapng file with many interfaces to pcapng'''
182 # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
183 testout_file
= result_file(testout_pcapng
)
184 mergecap_proc
= subprocess
.run((cmd_mergecap
,
187 capture_file('many_interfaces.pcapng.1'),
188 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
189 check_mergecap(mergecap_proc
, 'pcapng', 'Per packet', 64, 11, 62, cmd_capinfos
, testout_file
, test_env
)
191 def test_mergecap_3_pcapng_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
192 '''Merge multiple pcapng files with many interfaces to pcapng'''
193 # $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
194 testout_file
= result_file(testout_pcapng
)
195 mergecap_proc
= subprocess
.run((cmd_mergecap
,
198 capture_file('many_interfaces.pcapng.1'),
199 capture_file('many_interfaces.pcapng.2'),
200 capture_file('many_interfaces.pcapng.3'),
201 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
202 check_mergecap( mergecap_proc
, 'pcapng', 'Per packet', 88, 11, 86, cmd_capinfos
, testout_file
, test_env
)
204 def test_mergecap_3_pcapng_none_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
205 '''Merge multiple pcapng files with many interfaces to pcapng, "none" merge mode'''
206 # $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
207 testout_file
= result_file(testout_pcapng
)
208 mergecap_proc
= subprocess
.run((cmd_mergecap
,
212 capture_file('many_interfaces.pcapng.1'),
213 capture_file('many_interfaces.pcapng.2'),
214 capture_file('many_interfaces.pcapng.3'),
215 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
216 check_mergecap(mergecap_proc
, 'pcapng', 'Per packet', 88, 33, 62, cmd_capinfos
, testout_file
, test_env
)
218 def test_mergecap_3_pcapng_all_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
219 '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
220 # build a pcapng of all the interfaces repeated by using mode 'none'
221 # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
222 testin_file
= result_file('testin.pcapng')
223 subprocess
.check_call((cmd_mergecap
,
227 capture_file('many_interfaces.pcapng.1'),
228 capture_file('many_interfaces.pcapng.2'),
229 capture_file('many_interfaces.pcapng.3'),
231 # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
233 # and use that generated pcap for our test
234 # $MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
235 testout_file
= result_file(testout_pcapng
)
236 mergecap_proc
= subprocess
.run((cmd_mergecap
,
240 testin_file
, testin_file
, testin_file
,
241 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
242 # check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
243 check_mergecap(mergecap_proc
, 'pcapng', 'Per packet', 264, 33, 186, cmd_capinfos
, testout_file
, test_env
)
245 def test_mergecap_3_pcapng_any_pcapng(self
, cmd_mergecap
, capture_file
, result_file
, cmd_capinfos
, test_env
):
246 '''Merge multiple pcapng files to pcapng in "none" mode, then merge that to "all" mode.'''
247 # build a pcapng of all the interfaces repeated by using mode 'none'
248 # $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
249 testin_file
= result_file('testin.pcapng')
250 subprocess
.check_call((cmd_mergecap
,
254 capture_file('many_interfaces.pcapng.1'),
255 capture_file('many_interfaces.pcapng.2'),
256 capture_file('many_interfaces.pcapng.3'),
258 # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
260 # and use that generated pcap for our test
261 # $MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
262 testout_file
= result_file(testout_pcapng
)
263 mergecap_proc
= subprocess
.run((cmd_mergecap
,
267 testin_file
, testin_file
, testin_file
,
268 ), capture_output
=True, encoding
='utf-8', env
=test_env
)
269 # check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
270 check_mergecap(mergecap_proc
, 'pcapng', 'Per packet', 264, 11, 258, cmd_capinfos
, testout_file
, test_env
)