aboutsummaryrefslogtreecommitdiffstats
path: root/test/suite-wslua.sh
blob: f523f47a58a861c546c2fca0240a98a8683469df (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/bin/bash
#
# Run the Lua API unit tests
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

wslua_step_dir_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/dir.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_dissector_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# First run tshark with the dissector script.
	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua > testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-1 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# then run tshark again with the verification script. (it internally reads in testin.txt)
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "subtest-1 didn't find pass marker"
	fi

	# run tshark with the dissector script again, but in mode 2.
	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua -X lua_script1:heur_regmode=2 > testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-2 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# then run tshark again with the verification script. (it internally reads in testin.txt)
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua -X lua_script1:no_heur > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "subtest-2 didn't find pass marker"
	fi

	# run tshark with the dissector script again, but in mode 3.
	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua -X lua_script1:heur_regmode=3 > testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-3 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# then run tshark again with the verification script. (it internally reads in testin.txt)
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua -X lua_script1:no_heur > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "subtest-3 didn't find pass marker"
		return
	fi

	FPM_ARGS="
		-r $CAPTURE_DIR/segmented_fpm.pcap
		-X lua_script:$TESTS_DIR/lua/dissectFPM.lua
		-T fields
		-e frame.number
		-e fpm
		-e fpm.version
		-e fpm.type
		-e fpm.length
		"
	# run tshark with the FPM dissector script, enabling dissect_tcp_pdus().
	$TSHARK $FPM_ARGS -o fpm.dissect_tcp:true > testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-4a exit status of $DUT: $RETURNVALUE"
		return
	fi

	# run tshark with the FPM dissector script, disabling dissect_tcp_pdus().
	$TSHARK $FPM_ARGS -o fpm.dissect_tcp:false > testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "subtest-4b exit status of $DUT: $RETURNVALUE"
		return
	fi

	# now compare the two files - they should be identical
	if diff -q ./testin.txt ./testout.txt; then
		rm ./testin.txt
		rm ./testout.txt
		test_step_ok
	else
		echo
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "subtest-4 the new and old tcp dissection methods differed"
	fi
}

wslua_step_field_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/field.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_file_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# First run tshark with the pcap_file_reader script.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/pcap_file.lua > testin.txt 2>&1
	$TSHARK -r $CAPTURE_DIR/wpa-Induction.pcap.gz -X lua_script:$TESTS_DIR/lua/pcap_file.lua >> testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-1 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# then run tshark again without the script
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap > testout.txt 2>&1
	$TSHARK -r $CAPTURE_DIR/wpa-Induction.pcap.gz >> testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "subtest-2 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# now compare the two files - they should be identical
	if diff -q ./testin.txt ./testout.txt; then
		rm ./testin.txt
	else
		echo
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "subtest-3 reading the pcap file with Lua did not match internal"
		return
	fi

	# Now generate a new capture file using the Lua writer.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/pcap_file.lua -w testin.txt -F lua_pcap2 > testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "subtest-4 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# now compare the two files - they should be identical
	if diff -q $CAPTURE_DIR/dhcp.pcap ./testin.txt; then
		rm ./testin.txt
	else
		echo
		cat ./testout.txt
		test_step_failed "subtest-5 creating a new pcap file using Lua did not match dhcp.cap"
		return
	fi

	# Now read an acme sipmsg.log using the acme Lua reader, writing it out as pcapng.
	$TSHARK -r $CAPTURE_DIR/sipmsg.log -X lua_script:$TESTS_DIR/lua/acme_file.lua -w testin.txt -F pcapng > testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "subtest-6 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# testin.txt is now a pcapng, read it out using -V verbose into testout.txt
	$TSHARK -r ./testin.txt -V > testout.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testout.txt
		test_step_failed "subtest-7 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# now readout sip.pcapng into testin.txt using -V verbose
	$TSHARK -r $CAPTURE_DIR/sip.pcapng -V > testin.txt 2>&1
	RETURNVALUE=$?
	if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
		echo
		cat ./testin.txt
		test_step_failed "subtest-8 exit status of $DUT: $RETURNVALUE"
		return
	fi

	# now compare testin and testout - they should be identical
	if diff -q ./testout.txt ./testin.txt; then
		test_step_ok
	else
		echo
		cat ./testout.txt
		diff ./testout.txt ./testin.txt
		test_step_failed "subtest-9 writing the acme sipmsg.log out as pcapng did not match sip.pcapng"
	fi
}

wslua_step_listener_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/listener.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_nstime_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/nstime.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_pinfo_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/pinfo.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}


wslua_step_proto_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# First run tshark with the proto script.
	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/proto.lua > testin.txt 2>&1
	grep -q "All tests passed!" testin.txt
	if [ $? -ne 0 ]; then
		cat ./testin.txt
		test_step_failed "didn't find pass marker"
	fi

	# then run tshark again with the verification script. (it internally reads in testin.txt)
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		echo
		cat ./testin.txt
		cat ./testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_int64_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/int64.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		echo
		cat ./testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_args_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:1 > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_args_test test 1 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_args_test test 2 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script2:1 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_args_test test 3 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		cat testout.txt
		test_step_failed "lua_args_test negative test 4 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		cat testout.txt
		test_step_failed "lua_args_test negative test 5 failed"
	fi
	test_step_ok
}

wslua_step_globals_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_globals.lua -X lua_script1:$TESTS_DIR/lua/ -X lua_script1:$TESTS_DIR/lua/globals_1.8.txt > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_globals_test test 1 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_globals.lua -X lua_script1:$TESTS_DIR/lua/ -X lua_script1:$TESTS_DIR/lua/globals_1.10.txt > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_globals_test test 2 failed"
	fi
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_globals.lua -X lua_script1:$TESTS_DIR/lua/ -X lua_script1:$TESTS_DIR/lua/globals_1.12.txt > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_globals_test test 2 failed"
	fi
	test_step_ok
}

wslua_step_gregex_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/gregex.lua -X lua_script1:-d$TESTS_DIR/lua/ -X lua_script1:glib -X lua_script1:-V > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_struct_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	$TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/struct.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_step_tvb_test() {
	if [ $HAVE_LUA -ne 0 ]; then
		test_step_skipped
		return
	fi

	# Tshark catches lua script failures, so we have to parse the output.
	# perform this twice: once with a tree, once without
	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua -V > testout.txt 2>&1
	grep -q "All tests passed!" testout.txt
	if [ $? -ne 0 ]; then
		cat testout.txt
		test_step_failed "lua_args_test test 1 failed"
	fi

	$TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua > testout.txt 2>&1
	if grep -q "All tests passed!" testout.txt; then
		test_step_ok
	else
		cat testout.txt
		test_step_failed "didn't find pass marker"
	fi
}

wslua_cleanup_step() {
	rm -f ./testout.txt
	rm -f ./testin.txt
}

wslua_suite() {
	test_step_set_pre wslua_cleanup_step
	test_step_set_post wslua_cleanup_step
	test_step_add "wslua dir" wslua_step_dir_test
	test_step_add "wslua dissector" wslua_step_dissector_test
	test_step_add "wslua field/fieldinfo" wslua_step_field_test
	test_step_add "wslua file" wslua_step_file_test
	test_step_add "wslua globals" wslua_step_globals_test
	test_step_add "wslua gregex" wslua_step_gregex_test
	test_step_add "wslua int64" wslua_step_int64_test
	test_step_add "wslua listener" wslua_step_listener_test
	test_step_add "wslua nstime" wslua_step_nstime_test
	test_step_add "wslua pinfo" wslua_step_pinfo_test
	test_step_add "wslua proto/protofield" wslua_step_proto_test
	test_step_add "wslua script arguments" wslua_step_args_test
	test_step_add "wslua struct" wslua_step_struct_test
	test_step_add "wslua tvb" wslua_step_tvb_test
}
#
# Editor modelines  -  https://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 8
# tab-width: 8
# indent-tabs-mode: t
# End:
#
# vi: set shiftwidth=8 tabstop=8 noexpandtab:
# :indentSize=8:tabSize=8:noTabs=false:
#