[skinny] Update message definitions
[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 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #
22
23 # common checking code:
24 # arg 1 = return value from mergecap command
25 # arg 2 = file type string
26 # arg 3 = file encap
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 mergecap_common_check() {
31         if [ ! $1 -eq $EXIT_OK ]; then
32                 echo
33                 cat ./testout.txt
34                 test_step_failed "exit status of mergecap: $1"
35                 return
36         fi
37
38         grep -q "merging complete" testout.txt
39         if [ $? -ne 0 ]; then
40                 cat ./testout.txt
41                 test_step_failed "mergecap didn't complete"
42         fi
43
44         $CAPINFOS -tEIc ./testout.pcap > capinfo_testout.txt 2>&1
45         RETURNVALUE=$?
46         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
47                 echo
48                 cat ./testout.txt
49                 cat ./capinfo_testout.txt
50                 test_step_failed "exit status of capinfos: $RETURNVALUE"
51                 return
52         fi
53
54         grep -Eiq "File type:[[:blank:]]+$2" capinfo_testout.txt
55         if [ $? -ne 0 ]; then
56                 cat ./testout.txt
57                 cat ./capinfo_testout.txt
58                 test_step_failed "mergecap output format was not '$2'"
59         fi
60
61         grep -Eiq "File encapsulation:[[:blank:]]+$3" capinfo_testout.txt
62         if [ $? -ne 0 ]; then
63                 cat ./testout.txt
64                 cat ./capinfo_testout.txt
65                 test_step_failed "mergecap output encap type was not '$3'"
66         fi
67
68         grep -Eiq "Number of interfaces in file:[[:blank:]]+$4" capinfo_testout.txt
69         if [ $? -ne 0 ]; then
70                 cat ./testout.txt
71                 cat ./capinfo_testout.txt
72                 test_step_failed "mergecap output did not generate only $4 IDB"
73         fi
74
75         # this checks the file's number of packets
76         grep -Eiq "Number of packets:[[:blank:]]+$5" capinfo_testout.txt
77         if [ $? -ne 0 ]; then
78                 cat ./testout.txt
79                 cat ./capinfo_testout.txt
80                 test_step_failed "mergecap output did not generate $5 packets for file"
81         fi
82
83         # this checks the IDB number of packets
84         grep -Eiq "Number of packets =[[:blank:]]+$6" capinfo_testout.txt
85         if [ $? -ne 0 ]; then
86                 cat ./testout.txt
87                 cat ./capinfo_testout.txt
88                 test_step_failed "mergecap output did not generate $6 packets in IDB"
89         fi
90 }
91
92
93
94 # this is a common one for legacy PCAP output
95 # for this, arg1=returnvalue, arg2=#of file and IDB packets
96 mergecap_common_pcap_pkt() {
97         mergecap_common_check "$1" 'Wireshark/tcpdump/... - pcap' "Ethernet" "1" "$2" "$2"
98 }
99
100 # this is a common one for PCAPNG output
101 # for this, arg1=returnvalue, arg2=encap type, arg3=#of IDBs, arg4=# of file pkts, arg5=# of IDB pkts
102 mergecap_common_pcapng_pkt() {
103         mergecap_common_check "$1" 'Wireshark/... - pcapng' "$2" "$3" "$4" "$5"
104 }
105
106
107
108 mergecap_step_basic_1_pcap_pcap_test() {
109         $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
110         RETURNVALUE=$?
111         mergecap_common_pcap_pkt $RETURNVALUE 4
112         test_step_ok
113 }
114
115 mergecap_step_basic_2_pcap_pcap_test() {
116         $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
117         RETURNVALUE=$?
118         mergecap_common_pcap_pkt $RETURNVALUE 8
119         test_step_ok
120 }
121
122 mergecap_step_basic_3_empty_pcap_pcap_test() {
123         $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
124         RETURNVALUE=$?
125         mergecap_common_pcap_pkt $RETURNVALUE 4
126         test_step_ok
127 }
128
129 mergecap_step_basic_2_nano_pcap_pcap_test() {
130         $MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
131         RETURNVALUE=$?
132         mergecap_common_pcap_pkt $RETURNVALUE 62
133         test_step_ok
134 }
135
136 mergecap_step_basic_1_pcap_pcapng_test() {
137         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
138         RETURNVALUE=$?
139         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
140         test_step_ok
141 }
142
143 mergecap_step_basic_2_pcap_pcapng_test() {
144         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
145         RETURNVALUE=$?
146         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
147         test_step_ok
148 }
149
150 mergecap_step_basic_2_pcap_none_pcapng_test() {
151         $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
152         RETURNVALUE=$?
153         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 2 8 4
154         test_step_ok
155 }
156
157 mergecap_step_basic_2_pcap_all_pcapng_test() {
158         $MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
159         RETURNVALUE=$?
160         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
161         test_step_ok
162 }
163
164 mergecap_step_basic_2_pcap_any_pcapng_test() {
165         $MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
166         RETURNVALUE=$?
167         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
168         test_step_ok
169 }
170
171 mergecap_step_basic_1_pcapng_pcapng_test() {
172         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
173         RETURNVALUE=$?
174         mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
175         test_step_ok
176 }
177
178 mergecap_step_1_pcapng_many_pcapng_test() {
179         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
180         RETURNVALUE=$?
181         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 64 62
182         test_step_ok
183 }
184
185 mergecap_step_3_pcapng_pcapng_test() {
186         $MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
187         RETURNVALUE=$?
188         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 88 86
189         test_step_ok
190 }
191
192 mergecap_step_3_pcapng_none_pcapng_test() {
193         $MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
194         RETURNVALUE=$?
195         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 88 62
196         test_step_ok
197 }
198
199 mergecap_step_3_pcapng_all_pcapng_test() {
200         # build a pcapng of all the interfaces repeated by using mode 'none'
201         $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
202         # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
203
204         # and use that generated pcap for our test
205         $MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
206         RETURNVALUE=$?
207         # check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
208         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 264 186
209         test_step_ok
210 }
211
212 mergecap_step_3_pcapng_any_pcapng_test() {
213         # build a pcapng of all the interfaces repeated by using mode 'none'
214         $MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
215         # the above generated 33 IDBs, 88 total pkts, 62 in first IDB
216
217         # and use that generated pcap for our test
218         $MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
219         RETURNVALUE=$?
220         # check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
221         mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 264 258
222         test_step_ok
223 }
224
225
226 mergecap_cleanup_step() {
227         rm -f ./testout.txt
228         rm -f ./capinfo_testout.txt
229         rm -f ./testout.pcap
230         rm -f ./testin.pcap
231 }
232
233 mergecap_suite() {
234         test_step_set_pre mergecap_cleanup_step
235         test_step_set_post mergecap_cleanup_step
236         test_step_add "1 pcap in ----> pcap out" mergecap_step_basic_1_pcap_pcap_test
237         test_step_add "2 pcaps in ---> pcap out" mergecap_step_basic_2_pcap_pcap_test
238         test_step_add "3 pcaps in ---> pcap out; two are empty" mergecap_step_basic_3_empty_pcap_pcap_test
239         test_step_add "2 pcaps in ---> pcap out; one is nanosecond pcap" mergecap_step_basic_2_nano_pcap_pcap_test
240
241         test_step_add "1 pcap in ----> pcapng out" mergecap_step_basic_1_pcap_pcapng_test
242         test_step_add "2 pcaps in ---> pcapng out" mergecap_step_basic_2_pcap_pcapng_test
243         test_step_add "2 pcaps in ---> pcapng out; merge mode none" mergecap_step_basic_2_pcap_none_pcapng_test
244         test_step_add "2 pcaps in ---> pcapng out; merge mode all" mergecap_step_basic_2_pcap_all_pcapng_test
245         test_step_add "2 pcaps in ---> pcapng out; merge mode any" mergecap_step_basic_2_pcap_any_pcapng_test
246
247         test_step_add "1 pcapng in --> pcapng out" mergecap_step_basic_1_pcapng_pcapng_test
248         test_step_add "1 pcapng in --> pcapng out; many interfaces" mergecap_step_1_pcapng_many_pcapng_test
249         test_step_add "3 pcapngs in -> pcapng out; wildcarded" mergecap_step_3_pcapng_pcapng_test
250         test_step_add "3 pcapngs in -> pcapng out; merge mode none" mergecap_step_3_pcapng_none_pcapng_test
251         test_step_add "3 pcapngs in -> pcapng out; merge mode all" mergecap_step_3_pcapng_all_pcapng_test
252         test_step_add "3 pcapngs in -> pcapng out; merge mode any" mergecap_step_3_pcapng_any_pcapng_test
253 }
254
255 #
256 # Editor modelines  -  https://www.wireshark.org/tools/modelines.html
257 #
258 # Local variables:
259 # sh-basic-offset: 8
260 # tab-width: 8
261 # indent-tabs-mode: t
262 # End:
263 #
264 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
265 # :indentSize=8:tabSize=8:noTabs=false:
266 #