aboutsummaryrefslogtreecommitdiffstats
path: root/test/suite-mergecap.sh
blob: 2e7d9b581bc704919936c821bfc261364c86a7b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
#
# Run the mergecap unit tests
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

# common checking code:
# arg 1 = return value from mergecap command
# arg 2 = file type string
# arg 3 = file encap
# arg 4 = number of IDBs generated
# arg 5 = number of file packets merged
# arg 6 = number of some IDB packets merged
mergecap_common_check() {
	if [ ! $1 -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "exit status of mergecap: $1"
		return
	fi

	grep -q "merging complete" testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		test_step_failed "mergecap didn't complete"
	fi

	$CAPINFOS -tEIc ./testout.pcap > capinfo_testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "exit status of capinfos: $RETURNVALUE"
		return
	fi

	grep -Eiq "File type:[[:blank:]]+$2" capinfo_testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "mergecap output format was not '$2'"
	fi

	grep -Eiq "File encapsulation:[[:blank:]]+$3" capinfo_testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "mergecap output encap type was not '$3'"
	fi

	grep -Eiq "Number of interfaces in file:[[:blank:]]+$4" capinfo_testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "mergecap output did not generate only $4 IDB"
	fi

	# this checks the file's number of packets
	grep -Eiq "Number of packets:[[:blank:]]+$5" capinfo_testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "mergecap output did not generate $5 packets for file"
	fi

	# this checks the IDB number of packets
	grep -Eiq "Number of packets =[[:blank:]]+$6" capinfo_testout.txt
	if [ $? -ne 0 ]; then
		cat ./testout.txt
		cat ./capinfo_testout.txt
		test_step_failed "mergecap output did not generate $6 packets in IDB"
	fi
}



# this is a common one for legacy PCAP output
# for this, arg1=returnvalue, arg2=#of file and IDB packets
mergecap_common_pcap_pkt() {
	mergecap_common_check "$1" 'Wireshark/tcpdump/... - pcap' "Ethernet" "1" "$2" "$2"
}

# this is a common one for PCAPNG output
# for this, arg1=returnvalue, arg2=encap type, arg3=#of IDBs, arg4=# of file pkts, arg5=# of IDB pkts
mergecap_common_pcapng_pkt() {
	mergecap_common_check "$1" 'Wireshark/... - pcapng' "$2" "$3" "$4" "$5"
}



mergecap_step_basic_1_pcap_pcap_test() {
	$MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcap_pkt $RETURNVALUE 4
	test_step_ok
}

mergecap_step_basic_2_pcap_pcap_test() {
	$MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcap_pkt $RETURNVALUE 8
	test_step_ok
}

mergecap_step_basic_3_empty_pcap_pcap_test() {
	$MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}empty.pcap" "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}empty.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcap_pkt $RETURNVALUE 4
	test_step_ok
}

mergecap_step_basic_2_nano_pcap_pcap_test() {
	$MERGECAP -vF pcap -w testout.pcap "${CAPTURE_DIR}dhcp-nanosecond.pcap" "${CAPTURE_DIR}rsasnakeoil2.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcap_pkt $RETURNVALUE 62
	test_step_ok
}

mergecap_step_basic_1_pcap_pcapng_test() {
	$MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
	test_step_ok
}

mergecap_step_basic_2_pcap_pcapng_test() {
	$MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
	test_step_ok
}

mergecap_step_basic_2_pcap_none_pcapng_test() {
	$MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 2 8 4
	test_step_ok
}

mergecap_step_basic_2_pcap_all_pcapng_test() {
	$MERGECAP -vI 'all' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
	test_step_ok
}

mergecap_step_basic_2_pcap_any_pcapng_test() {
	$MERGECAP -vI 'any' -w testout.pcap "${CAPTURE_DIR}dhcp.pcap" "${CAPTURE_DIR}dhcp.pcap" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 8 8
	test_step_ok
}

mergecap_step_basic_1_pcapng_pcapng_test() {
	$MERGECAP -v -w testout.pcap "${CAPTURE_DIR}dhcp.pcapng" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Ethernet" 1 4 4
	test_step_ok
}

mergecap_step_1_pcapng_many_pcapng_test() {
	$MERGECAP -v -w testout.pcap "${CAPTURE_DIR}many_interfaces.pcapng.1" > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 64 62
	test_step_ok
}

mergecap_step_3_pcapng_pcapng_test() {
	$MERGECAP -v -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 88 86
	test_step_ok
}

mergecap_step_3_pcapng_none_pcapng_test() {
	$MERGECAP -vI 'none' -w testout.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
	RETURNVALUE=$?
	mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 88 62
	test_step_ok
}

mergecap_step_3_pcapng_all_pcapng_test() {
	# build a pcapng of all the interfaces repeated by using mode 'none'
	$MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
	# the above generated 33 IDBs, 88 total pkts, 62 in first IDB

	# and use that generated pcap for our test
	$MERGECAP -vI 'all' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
	RETURNVALUE=$?
	# check for 33 IDBs, 88*3=264 total pkts, 62*3=186 in first IDB
	mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 33 264 186
	test_step_ok
}

mergecap_step_3_pcapng_any_pcapng_test() {
	# build a pcapng of all the interfaces repeated by using mode 'none'
	$MERGECAP -vI 'none' -w testin.pcap "${CAPTURE_DIR}"many_interfaces.pcapng* > testout.txt 2>&1
	# the above generated 33 IDBs, 88 total pkts, 62 in first IDB

	# and use that generated pcap for our test
	$MERGECAP -vI 'any' -w testout.pcap ./testin.pcap ./testin.pcap ./testin.pcap > testout.txt 2>&1
	RETURNVALUE=$?
	# check for 11 IDBs, 88*3=264 total pkts, 86*3=258 in first IDB
	mergecap_common_pcapng_pkt $RETURNVALUE "Per packet" 11 264 258
	test_step_ok
}


mergecap_cleanup_step() {
	rm -f ./testout.txt
	rm -f ./capinfo_testout.txt
	rm -f ./testout.pcap
	rm -f ./testin.pcap
}

mergecap_suite() {
	test_step_set_pre mergecap_cleanup_step
	test_step_set_post mergecap_cleanup_step
	test_step_add "1 pcap in ----> pcap out" mergecap_step_basic_1_pcap_pcap_test
	test_step_add "2 pcaps in ---> pcap out" mergecap_step_basic_2_pcap_pcap_test
	test_step_add "3 pcaps in ---> pcap out; two are empty" mergecap_step_basic_3_empty_pcap_pcap_test
	test_step_add "2 pcaps in ---> pcap out; one is nanosecond pcap" mergecap_step_basic_2_nano_pcap_pcap_test

	test_step_add "1 pcap in ----> pcapng out" mergecap_step_basic_1_pcap_pcapng_test
	test_step_add "2 pcaps in ---> pcapng out" mergecap_step_basic_2_pcap_pcapng_test
	test_step_add "2 pcaps in ---> pcapng out; merge mode none" mergecap_step_basic_2_pcap_none_pcapng_test
	test_step_add "2 pcaps in ---> pcapng out; merge mode all" mergecap_step_basic_2_pcap_all_pcapng_test
	test_step_add "2 pcaps in ---> pcapng out; merge mode any" mergecap_step_basic_2_pcap_any_pcapng_test

	test_step_add "1 pcapng in --> pcapng out" mergecap_step_basic_1_pcapng_pcapng_test
	test_step_add "1 pcapng in --> pcapng out; many interfaces" mergecap_step_1_pcapng_many_pcapng_test
	test_step_add "3 pcapngs in -> pcapng out; wildcarded" mergecap_step_3_pcapng_pcapng_test
	test_step_add "3 pcapngs in -> pcapng out; merge mode none" mergecap_step_3_pcapng_none_pcapng_test
	test_step_add "3 pcapngs in -> pcapng out; merge mode all" mergecap_step_3_pcapng_all_pcapng_test
	test_step_add "3 pcapngs in -> pcapng out; merge mode any" mergecap_step_3_pcapng_any_pcapng_test
}

#
# Editor modelines  -  https://www.wireshark.org/tools/modelines.html
#
# Local variables:
# sh-basic-offset: 8
# tab-width: 8
# indent-tabs-mode: t
# End:
#
# vi: set shiftwidth=8 tabstop=8 noexpandtab:
# :indentSize=8:tabSize=8:noTabs=false:
#