Test: More fixes and updates.
[metze/wireshark/wip.git] / test / suite-mergecap.sh
1 #!/bin/bash
2 #
3 # Run the mergecap unit tests
4 #
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 #
8 # SPDX-License-Identifier: GPL-2.0-or-later
9 #
10
11 # common checking code:
12 # arg 1 = return value from mergecap command
13 # arg 2 = file type string
14 # arg 3 = file encap
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
20                 echo
21                 cat ./testout.txt
22                 test_step_failed "exit status of mergecap: $1"
23                 return
24         fi
25
26         grep -q "merging complete" testout.txt
27         if [ $? -ne 0 ]; then
28                 cat ./testout.txt
29                 test_step_failed "mergecap didn't complete"
30         fi
31
32         $CAPINFOS -tEIc ./testout.pcap > capinfo_testout.txt 2>&1
33         RETURNVALUE=$?
34         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
35                 echo
36                 cat ./testout.txt
37                 cat ./capinfo_testout.txt
38                 test_step_failed "exit status of capinfos: $RETURNVALUE"
39                 return
40         fi
41
42         grep -Eiq "File type:[[:blank:]]+$2" capinfo_testout.txt
43         if [ $? -ne 0 ]; then
44                 cat ./testout.txt
45                 cat ./capinfo_testout.txt
46                 test_step_failed "mergecap output format was not '$2'"
47         fi
48
49         grep -Eiq "File encapsulation:[[:blank:]]+$3" capinfo_testout.txt
50         if [ $? -ne 0 ]; then
51                 cat ./testout.txt
52                 cat ./capinfo_testout.txt
53                 test_step_failed "mergecap output encap type was not '$3'"
54         fi
55
56         grep -Eiq "Number of interfaces in file:[[:blank:]]+$4" capinfo_testout.txt
57         if [ $? -ne 0 ]; then
58                 cat ./testout.txt
59                 cat ./capinfo_testout.txt
60                 test_step_failed "mergecap output did not generate only $4 IDB"
61         fi
62
63         # this checks the file's number of packets
64         grep -Eiq "Number of packets:[[:blank:]]+$5" capinfo_testout.txt
65         if [ $? -ne 0 ]; then
66                 cat ./testout.txt
67                 cat ./capinfo_testout.txt
68                 test_step_failed "mergecap output did not generate $5 packets for file"
69         fi
70
71         # this checks the IDB number of packets
72         grep -Eiq "Number of packets =[[:blank:]]+$6" capinfo_testout.txt
73         if [ $? -ne 0 ]; then
74                 cat ./testout.txt
75                 cat ./capinfo_testout.txt
76                 test_step_failed "mergecap output did not generate $6 packets in IDB"
77         fi
78 }
79
80
81
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"
86 }
87
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"
92 }
93
94
95
96 mergecap_step_basic_1_pcap_pcap_test() {
97         $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
98         RETURNVALUE=$?
99         mergecap_common_pcap_pkt $RETURNVALUE 4
100         test_step_ok
101 }
102
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
105         RETURNVALUE=$?
106         mergecap_common_pcap_pkt $RETURNVALUE 8
107         test_step_ok
108 }
109
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
112         RETURNVALUE=$?
113         mergecap_common_pcap_pkt $RETURNVALUE 4
114         test_step_ok
115 }
116
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
119         RETURNVALUE=$?
120         mergecap_common_pcap_pkt $RETURNVALUE 62
121         test_step_ok
122 }
123
124 mergecap_step_basic_1_pcap_pcapng_test() {
125         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
126         RETURNVALUE=$?
127         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
128         test_step_ok
129 }
130
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
133         RETURNVALUE=$?
134         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
135         test_step_ok
136 }
137
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
140         RETURNVALUE=$?
141         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 2 8 4
142         test_step_ok
143 }
144
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
147         RETURNVALUE=$?
148         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
149         test_step_ok
150 }
151
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
154         RETURNVALUE=$?
155         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
156         test_step_ok
157 }
158
159 mergecap_step_basic_1_pcapng_pcapng_test() {
160         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
161         RETURNVALUE=$?
162         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
163         test_step_ok
164 }
165
166 mergecap_step_1_pcapng_many_pcapng_test() {
167         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
168         RETURNVALUE=$?
169         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 64 62
170         test_step_ok
171 }
172
173 mergecap_step_3_pcapng_pcapng_test() {
174         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
175         RETURNVALUE=$?
176         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 88 86
177         test_step_ok
178 }
179
180 mergecap_step_3_pcapng_none_pcapng_test() {
181         $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
182         RETURNVALUE=$?
183         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 88 62
184         test_step_ok
185 }
186
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
191
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
194         RETURNVALUE=$?
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
197         test_step_ok
198 }
199
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
204
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
207         RETURNVALUE=$?
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
210         test_step_ok
211 }
212
213
214 mergecap_cleanup_step() {
215         rm -f ./testout.txt
216         rm -f ./capinfo_testout.txt
217         rm -f ./testout.pcap
218         rm -f ./testin.pcap
219 }
220
221 mergecap_suite() {
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
228
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
234
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
241 }
242
243 #
244 # Editor modelines  -  https://www.wireshark.org/tools/modelines.html
245 #
246 # Local variables:
247 # sh-basic-offset: 8
248 # tab-width: 8
249 # indent-tabs-mode: t
250 # End:
251 #
252 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
253 # :indentSize=8:tabSize=8:noTabs=false:
254 #