3 # Run the mergecap unit tests
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
8 # SPDX-License-Identifier: GPL-2.0-or-later
11 # common checking code:
12 # arg 1 = return value from mergecap command
13 # arg 2 = file type string
15 # arg 4 = number of IDBs generated
16 # arg 5 = number of file packets merged
17 # arg 6 = number of some IDB packets merged
18 mergecap_common_check() {
19 if [ ! $1 -eq $EXIT_OK ]; then
22 test_step_failed "exit status of mergecap: $1"
26 grep -q "merging complete" testout.txt
29 test_step_failed "mergecap didn't complete"
32 $CAPINFOS -tEIc ./testout.pcap > capinfo_testout.txt 2>&1
34 if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
37 cat ./capinfo_testout.txt
38 test_step_failed "exit status of capinfos: $RETURNVALUE"
42 grep -Eiq "File type:[[:blank:]]+$2" capinfo_testout.txt
45 cat ./capinfo_testout.txt
46 test_step_failed "mergecap output format was not '$2'"
49 grep -Eiq "File encapsulation:[[:blank:]]+$3" capinfo_testout.txt
52 cat ./capinfo_testout.txt
53 test_step_failed "mergecap output encap type was not '$3'"
56 grep -Eiq "Number of interfaces in file:[[:blank:]]+$4" capinfo_testout.txt
59 cat ./capinfo_testout.txt
60 test_step_failed "mergecap output did not generate only $4 IDB"
63 # this checks the file's number of packets
64 grep -Eiq "Number of packets:[[:blank:]]+$5" capinfo_testout.txt
67 cat ./capinfo_testout.txt
68 test_step_failed "mergecap output did not generate $5 packets for file"
71 # this checks the IDB number of packets
72 grep -Eiq "Number of packets =[[:blank:]]+$6" capinfo_testout.txt
75 cat ./capinfo_testout.txt
76 test_step_failed "mergecap output did not generate $6 packets in IDB"
82 # this is a common one for legacy PCAP output
83 # for this, arg1=returnvalue, arg2=#of file and IDB packets
84 mergecap_common_pcap_pkt() {
85 mergecap_common_check "$1" 'Wireshark/tcpdump/... - pcap' "Ethernet" "1" "$2" "$2"
88 # this is a common one for PCAPNG output
89 # for this, arg1=returnvalue, arg2=encap type, arg3=#of IDBs, arg4=# of file pkts, arg5=# of IDB pkts
90 mergecap_common_pcapng_pkt() {
91 mergecap_common_check "$1" 'Wireshark/... - pcapng' "$2" "$3" "$4" "$5"
96 mergecap_step_basic_1_pcap_pcap_test() {
97 $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
99 mergecap_common_pcap_pkt $RETURNVALUE 4
103 mergecap_step_basic_2_pcap_pcap_test() {
104 $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
106 mergecap_common_pcap_pkt $RETURNVALUE 8
110 mergecap_step_basic_3_empty_pcap_pcap_test() {
111 $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
113 mergecap_common_pcap_pkt $RETURNVALUE 4
117 mergecap_step_basic_2_nano_pcap_pcap_test() {
118 $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
120 mergecap_common_pcap_pkt $RETURNVALUE 62
124 mergecap_step_basic_1_pcap_pcapng_test() {
125 $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
127 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
131 mergecap_step_basic_2_pcap_pcapng_test() {
132 $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
134 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
138 mergecap_step_basic_2_pcap_none_pcapng_test() {
139 $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
141 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 2 8 4
145 mergecap_step_basic_2_pcap_all_pcapng_test() {
146 $MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
148 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
152 mergecap_step_basic_2_pcap_any_pcapng_test() {
153 $MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
155 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
159 mergecap_step_basic_1_pcapng_pcapng_test() {
160 $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
162 mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
166 mergecap_step_1_pcapng_many_pcapng_test() {
167 $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
169 mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 64 62
173 mergecap_step_3_pcapng_pcapng_test() {
174 $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
176 mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 88 86
180 mergecap_step_3_pcapng_none_pcapng_test() {
181 $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
183 mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 88 62
187 mergecap_step_3_pcapng_all_pcapng_test() {
188 # build a pcapng of all the interfaces repeated by using mode 'none'
189 $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
190 # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
192 # and use that generated pcap for our test
193 $MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
195 # check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
196 mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 264 186
200 mergecap_step_3_pcapng_any_pcapng_test() {
201 # build a pcapng of all the interfaces repeated by using mode 'none'
202 $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
203 # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
205 # and use that generated pcap for our test
206 $MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
208 # check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
209 mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 264 258
214 mergecap_cleanup_step() {
216 rm -f ./capinfo_testout.txt
222 test_step_set_pre mergecap_cleanup_step
223 test_step_set_post mergecap_cleanup_step
224 test_step_add "1 pcap in ----> pcap out" mergecap_step_basic_1_pcap_pcap_test
225 test_step_add "2 pcaps in ---> pcap out" mergecap_step_basic_2_pcap_pcap_test
226 test_step_add "3 pcaps in ---> pcap out; two are empty" mergecap_step_basic_3_empty_pcap_pcap_test
227 test_step_add "2 pcaps in ---> pcap out; one is nanosecond pcap" mergecap_step_basic_2_nano_pcap_pcap_test
229 test_step_add "1 pcap in ----> pcapng out" mergecap_step_basic_1_pcap_pcapng_test
230 test_step_add "2 pcaps in ---> pcapng out" mergecap_step_basic_2_pcap_pcapng_test
231 test_step_add "2 pcaps in ---> pcapng out; merge mode none" mergecap_step_basic_2_pcap_none_pcapng_test
232 test_step_add "2 pcaps in ---> pcapng out; merge mode all" mergecap_step_basic_2_pcap_all_pcapng_test
233 test_step_add "2 pcaps in ---> pcapng out; merge mode any" mergecap_step_basic_2_pcap_any_pcapng_test
235 test_step_add "1 pcapng in --> pcapng out" mergecap_step_basic_1_pcapng_pcapng_test
236 test_step_add "1 pcapng in --> pcapng out; many interfaces" mergecap_step_1_pcapng_many_pcapng_test
237 test_step_add "3 pcapngs in -> pcapng out; wildcarded" mergecap_step_3_pcapng_pcapng_test
238 test_step_add "3 pcapngs in -> pcapng out; merge mode none" mergecap_step_3_pcapng_none_pcapng_test
239 test_step_add "3 pcapngs in -> pcapng out; merge mode all" mergecap_step_3_pcapng_all_pcapng_test
240 test_step_add "3 pcapngs in -> pcapng out; merge mode any" mergecap_step_3_pcapng_any_pcapng_test
244 # Editor modelines - https://www.wireshark.org/tools/modelines.html
249 # indent-tabs-mode: t
252 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
253 # :indentSize=8:tabSize=8:noTabs=false: