Suite test: fix modelines for bash script (emac)
[metze/wireshark/wip.git] / test / suite-wslua.sh
1 #!/bin/bash
2 #
3 # Run the Lua API 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 wslua_step_dir_test() {
24         if [ $HAVE_LUA -ne 0 ]; then
25                 test_step_skipped
26                 return
27         fi
28
29         # Tshark catches lua script failures, so we have to parse the output.
30         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/dir.lua > testout.txt 2>&1
31         if grep -q "All tests passed!" testout.txt; then
32                 test_step_ok
33         else
34                 cat testout.txt
35                 test_step_failed "didn't find pass marker"
36         fi
37 }
38
39 wslua_step_dissector_test() {
40         if [ $HAVE_LUA -ne 0 ]; then
41                 test_step_skipped
42                 return
43         fi
44
45         # First run tshark with the dissector script.
46         $TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua > testin.txt 2>&1
47         RETURNVALUE=$?
48         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
49                 echo
50                 cat ./testin.txt
51                 test_step_failed "subtest-1 exit status of $DUT: $RETURNVALUE"
52                 return
53         fi
54
55         # then run tshark again with the verification script. (it internally reads in testin.txt)
56         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
57         grep -q "All tests passed!" testout.txt
58         if [ $? -ne 0 ]; then
59                 cat ./testin.txt
60                 cat ./testout.txt
61                 test_step_failed "subtest-1 didn't find pass marker"
62         fi
63
64         # run tshark with the dissector script again, but in mode 2.
65         $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
66         RETURNVALUE=$?
67         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
68                 echo
69                 cat ./testin.txt
70                 test_step_failed "subtest-2 exit status of $DUT: $RETURNVALUE"
71                 return
72         fi
73
74         # then run tshark again with the verification script. (it internally reads in testin.txt)
75         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua -X lua_script1:no_heur > testout.txt 2>&1
76         grep -q "All tests passed!" testout.txt
77         if [ $? -ne 0 ]; then
78                 cat ./testin.txt
79                 cat ./testout.txt
80                 test_step_failed "subtest-2 didn't find pass marker"
81         fi
82
83         # run tshark with the dissector script again, but in mode 3.
84         $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
85         RETURNVALUE=$?
86         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
87                 echo
88                 cat ./testin.txt
89                 test_step_failed "subtest-3 exit status of $DUT: $RETURNVALUE"
90                 return
91         fi
92
93         # then run tshark again with the verification script. (it internally reads in testin.txt)
94         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua -X lua_script1:no_heur > testout.txt 2>&1
95         grep -q "All tests passed!" testout.txt
96         if [ $? -ne 0 ]; then
97                 cat ./testin.txt
98                 cat ./testout.txt
99                 test_step_failed "subtest-3 didn't find pass marker"
100                 return
101         fi
102
103         FPM_ARGS="
104                 -r $CAPTURE_DIR/segmented_fpm.pcap
105                 -X lua_script:$TESTS_DIR/lua/dissectFPM.lua
106                 -T fields
107                 -e frame.number
108                 -e fpm
109                 -e fpm.version
110                 -e fpm.type
111                 -e fpm.length
112                 "
113         # run tshark with the FPM dissector script, enabling dissect_tcp_pdus().
114         $TSHARK $FPM_ARGS -o fpm.dissect_tcp:true > testin.txt 2>&1
115         RETURNVALUE=$?
116         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
117                 echo
118                 cat ./testin.txt
119                 test_step_failed "subtest-4a exit status of $DUT: $RETURNVALUE"
120                 return
121         fi
122
123         # run tshark with the FPM dissector script, disabling dissect_tcp_pdus().
124         $TSHARK $FPM_ARGS -o fpm.dissect_tcp:false > testout.txt 2>&1
125         RETURNVALUE=$?
126         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
127                 echo
128                 cat ./testout.txt
129                 test_step_failed "subtest-4b exit status of $DUT: $RETURNVALUE"
130                 return
131         fi
132
133         # now compare the two files - they should be identical
134         if diff -q ./testin.txt ./testout.txt; then
135                 rm ./testin.txt
136                 rm ./testout.txt
137                 test_step_ok
138         else
139                 echo
140                 cat ./testin.txt
141                 cat ./testout.txt
142                 test_step_failed "subtest-4 the new and old tcp dissection methods differed"
143         fi
144 }
145
146 wslua_step_field_test() {
147         if [ $HAVE_LUA -ne 0 ]; then
148                 test_step_skipped
149                 return
150         fi
151
152         # Tshark catches lua script failures, so we have to parse the output.
153         $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/field.lua > testout.txt 2>&1
154         if grep -q "All tests passed!" testout.txt; then
155                 test_step_ok
156         else
157                 cat testout.txt
158                 test_step_failed "didn't find pass marker"
159         fi
160 }
161
162 wslua_step_file_test() {
163         if [ $HAVE_LUA -ne 0 ]; then
164                 test_step_skipped
165                 return
166         fi
167
168         # First run tshark with the pcap_file_reader script.
169         $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/pcap_file.lua > testin.txt 2>&1
170         $TSHARK -r $CAPTURE_DIR/wpa-Induction.pcap.gz -X lua_script:$TESTS_DIR/lua/pcap_file.lua >> testin.txt 2>&1
171         RETURNVALUE=$?
172         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
173                 echo
174                 cat ./testin.txt
175                 test_step_failed "subtest-1 exit status of $DUT: $RETURNVALUE"
176                 return
177         fi
178
179         # then run tshark again without the script
180         $TSHARK -r $CAPTURE_DIR/dhcp.pcap > testout.txt 2>&1
181         $TSHARK -r $CAPTURE_DIR/wpa-Induction.pcap.gz >> testout.txt 2>&1
182         RETURNVALUE=$?
183         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
184                 echo
185                 cat ./testout.txt
186                 test_step_failed "subtest-2 exit status of $DUT: $RETURNVALUE"
187                 return
188         fi
189
190         # now compare the two files - they should be identical
191         if diff -q ./testin.txt ./testout.txt; then
192                 rm ./testin.txt
193         else
194                 echo
195                 cat ./testin.txt
196                 cat ./testout.txt
197                 test_step_failed "subtest-3 reading the pcap file with Lua did not match internal"
198                 return
199         fi
200
201         # Now generate a new capture file using the Lua writer.
202         $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
203         RETURNVALUE=$?
204         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
205                 echo
206                 cat ./testout.txt
207                 test_step_failed "subtest-4 exit status of $DUT: $RETURNVALUE"
208                 return
209         fi
210
211         # now compare the two files - they should be identical
212         if diff -q $CAPTURE_DIR/dhcp.pcap ./testin.txt; then
213                 rm ./testin.txt
214         else
215                 echo
216                 cat ./testout.txt
217                 test_step_failed "subtest-5 creating a new pcap file using Lua did not match dhcp.cap"
218                 return
219         fi
220
221         # Now read an acme sipmsg.log using the acme Lua reader, writing it out as pcapng.
222         $TSHARK -r $CAPTURE_DIR/sipmsg.log -X lua_script:$TESTS_DIR/lua/acme_file.lua -w testin.txt -F pcapng > testout.txt 2>&1
223         RETURNVALUE=$?
224         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
225                 echo
226                 cat ./testout.txt
227                 test_step_failed "subtest-6 exit status of $DUT: $RETURNVALUE"
228                 return
229         fi
230
231         # testin.txt is now a pcapng, read it out using -V verbose into testout.txt
232         $TSHARK -r ./testin.txt -V > testout.txt 2>&1
233         RETURNVALUE=$?
234         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
235                 echo
236                 cat ./testout.txt
237                 test_step_failed "subtest-7 exit status of $DUT: $RETURNVALUE"
238                 return
239         fi
240
241         # now readout sip.pcapng into testin.txt using -V verbose
242         $TSHARK -r $CAPTURE_DIR/sip.pcapng -V > testin.txt 2>&1
243         RETURNVALUE=$?
244         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
245                 echo
246                 cat ./testin.txt
247                 test_step_failed "subtest-8 exit status of $DUT: $RETURNVALUE"
248                 return
249         fi
250
251         # now compare testin and testout - they should be identical
252         if diff -q ./testout.txt ./testin.txt; then
253                 test_step_ok
254         else
255                 echo
256                 cat ./testout.txt
257                 diff ./testout.txt ./testin.txt
258                 test_step_failed "subtest-9 writing the acme sipmsg.log out as pcapng did not match sip.pcapng"
259         fi
260 }
261
262 wslua_step_listener_test() {
263         if [ $HAVE_LUA -ne 0 ]; then
264                 test_step_skipped
265                 return
266         fi
267
268         # Tshark catches lua script failures, so we have to parse the output.
269         $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/listener.lua > testout.txt 2>&1
270         if grep -q "All tests passed!" testout.txt; then
271                 test_step_ok
272         else
273                 cat testout.txt
274                 test_step_failed "didn't find pass marker"
275         fi
276 }
277
278 wslua_step_nstime_test() {
279         if [ $HAVE_LUA -ne 0 ]; then
280                 test_step_skipped
281                 return
282         fi
283
284         # Tshark catches lua script failures, so we have to parse the output.
285         $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/nstime.lua > testout.txt 2>&1
286         if grep -q "All tests passed!" testout.txt; then
287                 test_step_ok
288         else
289                 cat testout.txt
290                 test_step_failed "didn't find pass marker"
291         fi
292 }
293
294 wslua_step_pinfo_test() {
295         if [ $HAVE_LUA -ne 0 ]; then
296                 test_step_skipped
297                 return
298         fi
299
300         # Tshark catches lua script failures, so we have to parse the output.
301         $TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/pinfo.lua > testout.txt 2>&1
302         if grep -q "All tests passed!" testout.txt; then
303                 test_step_ok
304         else
305                 cat testout.txt
306                 test_step_failed "didn't find pass marker"
307         fi
308 }
309
310
311 wslua_step_proto_test() {
312         if [ $HAVE_LUA -ne 0 ]; then
313                 test_step_skipped
314                 return
315         fi
316
317         # First run tshark with the proto script.
318         $TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/proto.lua > testin.txt 2>&1
319         grep -q "All tests passed!" testin.txt
320         if [ $? -ne 0 ]; then
321                 cat ./testin.txt
322                 test_step_failed "didn't find pass marker"
323         fi
324
325         # then run tshark again with the verification script. (it internally reads in testin.txt)
326         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
327         if grep -q "All tests passed!" testout.txt; then
328                 test_step_ok
329         else
330                 echo
331                 cat ./testin.txt
332                 cat ./testout.txt
333                 test_step_failed "didn't find pass marker"
334         fi
335 }
336
337 wslua_step_int64_test() {
338         if [ $HAVE_LUA -ne 0 ]; then
339                 test_step_skipped
340                 return
341         fi
342
343         # Tshark catches lua script failures, so we have to parse the output.
344         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/int64.lua > testout.txt 2>&1
345         if grep -q "All tests passed!" testout.txt; then
346                 test_step_ok
347         else
348                 echo
349                 cat ./testout.txt
350                 test_step_failed "didn't find pass marker"
351         fi
352 }
353
354 wslua_step_args_test() {
355         if [ $HAVE_LUA -ne 0 ]; then
356                 test_step_skipped
357                 return
358         fi
359
360         # Tshark catches lua script failures, so we have to parse the output.
361         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:1 > testout.txt 2>&1
362         grep -q "All tests passed!" testout.txt
363         if [ $? -ne 0 ]; then
364                 cat testout.txt
365                 test_step_failed "lua_args_test test 1 failed"
366         fi
367         $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
368         grep -q "All tests passed!" testout.txt
369         if [ $? -ne 0 ]; then
370                 cat testout.txt
371                 test_step_failed "lua_args_test test 2 failed"
372         fi
373         $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
374         grep -q "All tests passed!" testout.txt
375         if [ $? -ne 0 ]; then
376                 cat testout.txt
377                 test_step_failed "lua_args_test test 3 failed"
378         fi
379         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua > testout.txt 2>&1
380         if grep -q "All tests passed!" testout.txt; then
381                 cat testout.txt
382                 test_step_failed "lua_args_test negative test 4 failed"
383         fi
384         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 > testout.txt 2>&1
385         if grep -q "All tests passed!" testout.txt; then
386                 cat testout.txt
387                 test_step_failed "lua_args_test negative test 5 failed"
388         fi
389         test_step_ok
390 }
391
392 wslua_step_globals_test() {
393         if [ $HAVE_LUA -ne 0 ]; then
394                 test_step_skipped
395                 return
396         fi
397
398         # Tshark catches lua script failures, so we have to parse the output.
399         $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_2.2.txt > testout.txt 2>&1
400         grep -q "All tests passed!" testout.txt
401         if [ $? -ne 0 ]; then
402                 cat testout.txt
403                 test_step_failed "lua_globals_test test 1 failed"
404         fi
405         test_step_ok
406 }
407
408 wslua_step_gregex_test() {
409         if [ $HAVE_LUA -ne 0 ]; then
410                 test_step_skipped
411                 return
412         fi
413
414         # Tshark catches lua script failures, so we have to parse the output.
415         $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
416         if grep -q "All tests passed!" testout.txt; then
417                 test_step_ok
418         else
419                 cat testout.txt
420                 test_step_failed "didn't find pass marker"
421         fi
422 }
423
424 wslua_step_struct_test() {
425         if [ $HAVE_LUA -ne 0 ]; then
426                 test_step_skipped
427                 return
428         fi
429
430         # Tshark catches lua script failures, so we have to parse the output.
431         $TSHARK -r $CAPTURE_DIR/empty.pcap -X lua_script:$TESTS_DIR/lua/struct.lua > testout.txt 2>&1
432         if grep -q "All tests passed!" testout.txt; then
433                 test_step_ok
434         else
435                 cat testout.txt
436                 test_step_failed "didn't find pass marker"
437         fi
438 }
439
440 wslua_step_tvb_test() {
441         if [ $HAVE_LUA -ne 0 ]; then
442                 test_step_skipped
443                 return
444         fi
445
446         # Tshark catches lua script failures, so we have to parse the output.
447         # perform this twice: once with a tree, once without
448         $TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua -V > testout.txt 2>&1
449         grep -q "All tests passed!" testout.txt
450         if [ $? -ne 0 ]; then
451                 cat testout.txt
452                 test_step_failed "lua_args_test test 1 failed"
453         fi
454
455         $TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua > testout.txt 2>&1
456         if grep -q "All tests passed!" testout.txt; then
457                 test_step_ok
458         else
459                 cat testout.txt
460                 test_step_failed "didn't find pass marker"
461         fi
462 }
463
464 wslua_cleanup_step() {
465         rm -f ./testout.txt
466         rm -f ./testin.txt
467 }
468
469 wslua_suite() {
470         test_step_set_pre wslua_cleanup_step
471         test_step_set_post wslua_cleanup_step
472         test_step_add "wslua dir" wslua_step_dir_test
473         test_step_add "wslua dissector" wslua_step_dissector_test
474         test_step_add "wslua field/fieldinfo" wslua_step_field_test
475         test_step_add "wslua file" wslua_step_file_test
476         test_step_add "wslua globals" wslua_step_globals_test
477         test_step_add "wslua gregex" wslua_step_gregex_test
478         test_step_add "wslua int64" wslua_step_int64_test
479         test_step_add "wslua listener" wslua_step_listener_test
480         test_step_add "wslua nstime" wslua_step_nstime_test
481         test_step_add "wslua pinfo" wslua_step_pinfo_test
482         test_step_add "wslua proto/protofield" wslua_step_proto_test
483         test_step_add "wslua script arguments" wslua_step_args_test
484         test_step_add "wslua struct" wslua_step_struct_test
485         test_step_add "wslua tvb" wslua_step_tvb_test
486 }
487 #
488 # Editor modelines  -  https://www.wireshark.org/tools/modelines.html
489 #
490 # Local variables:
491 # sh-basic-offset: 8
492 # tab-width: 8
493 # indent-tabs-mode: t
494 # End:
495 #
496 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
497 # :indentSize=8:tabSize=8:noTabs=false:
498 #