Test: Add wslua.
[metze/wireshark/wip.git] / test / suite-io.sh
1 #!/bin/bash
2 #
3 # Test the file I/O of the Wireshark tools
4 #
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 2005 Ulf Lamping
8 #
9 # SPDX-License-Identifier: GPL-2.0-or-later
10 #
11
12
13 # common exit status values
14 EXIT_OK=0
15 EXIT_COMMAND_LINE=1
16 EXIT_ERROR=2
17
18 IO_RAWSHARK_DHCP_PCAP_BASELINE="$TESTS_DIR/baseline/io-rawshark-dhcp-pcap.txt"
19 IO_RAWSHARK_DHCP_PCAP_TESTOUT=./io-rawshark-dhcp-pcap-testout.txt
20
21 # input of file
22 io_step_input_file() {
23         $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w ./testout.pcap > ./testout.txt 2>&1
24         RETURNVALUE=$?
25         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
26                 test_step_failed "exit status of $DUT: $RETURNVALUE"
27                 # part of the Prerequisite checks
28                 # probably wrong interface, output the possible interfaces
29                 $TSHARK -D
30                 return
31         fi
32
33         # we should have an output file now
34         if [ ! -f "./testout.pcap" ]; then
35                 test_step_failed "No output file!"
36                 return
37         fi
38
39         # ok, we got a capture file, does it contain exactly 10 packets?
40         $CAPINFOS ./testout.pcap > ./testout.txt
41         grep -Ei 'Number of packets:[[:blank:]]+4' ./testout.txt > /dev/null
42         if [ $? -eq 0 ]; then
43                 test_step_ok
44         else
45                 echo
46                 cat ./testout.txt
47                 # part of the Prerequisite checks
48                 # probably wrong interface, output the possible interfaces
49                 $TSHARK -D
50                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
51         fi
52 }
53
54 # piping input file to stdout using "-w -"
55 io_step_output_piping() {
56         $DUT -r "${CAPTURE_DIR}dhcp.pcap" -w - > ./testout.pcap 2>./testout.txt
57         RETURNVALUE=$?
58         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
59                 test_step_failed "exit status of $DUT: $RETURNVALUE"
60                 $TSHARK -D
61                 return
62         fi
63
64         # we should have an output file now
65         if [ ! -f "./testout.pcap" ]; then
66                 test_step_failed "No output file!"
67                 return
68         fi
69
70         # ok, we got a capture file, does it contain exactly 10 packets?
71         $CAPINFOS ./testout.pcap > ./testout2.txt 2>&1
72         grep -Ei 'Number of packets:[[:blank:]]+4' ./testout2.txt > /dev/null
73         if [ $? -eq 0 ]; then
74                 test_step_ok
75         else
76                 echo
77                 cat ./testout.txt
78                 cat ./testout2.txt
79                 $TSHARK -D
80                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
81         fi
82 }
83
84 # piping input file to stdout using "-w -"
85 io_step_input_piping() {
86         cat -B "${CAPTURE_DIR}dhcp.pcap" | $DUT -r - -w ./testout.pcap 2>./testout.txt
87         RETURNVALUE=$?
88         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
89                 $TSHARK -D
90                 echo
91                 cat ./testout.txt
92                 test_step_failed "exit status of $DUT: $RETURNVALUE"
93                 return
94         fi
95
96         # we should have an output file now
97         if [ ! -f "./testout.pcap" ]; then
98                 test_step_failed "No output file!"
99                 return
100         fi
101
102         # ok, we got a capture file, does it contain exactly 10 packets?
103         $CAPINFOS ./testout.pcap > ./testout2.txt 2>&1
104         grep -Ei 'Number of packets:[[:blank:]]+4' ./testout2.txt > /dev/null
105         if [ $? -eq 0 ]; then
106                 test_step_ok
107         else
108                 echo
109                 cat ./testout.txt
110                 cat ./testout2.txt
111                 $TSHARK -D
112                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
113         fi
114 }
115
116 # Read a pcap from stdin
117 io_step_rawshark_pcap_stdin() {
118         if [ $ENDIANNESS != "little" ] ; then
119                 test_step_skipped
120                 return
121         fi
122         tail -c +25 "${CAPTURE_DIR}dhcp.pcap" | $RAWSHARK -dencap:1 -R "udp.port==68" -nr - > $IO_RAWSHARK_DHCP_PCAP_TESTOUT 2> /dev/null
123         diff -u --strip-trailing-cr $IO_RAWSHARK_DHCP_PCAP_BASELINE $IO_RAWSHARK_DHCP_PCAP_TESTOUT > $DIFF_OUT 2>&1
124         RETURNVALUE=$?
125         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
126                 test_step_failed "Output of rawshark read pcap via stdin differs from baseline"
127                 cat $DIFF_OUT
128                 return
129         fi
130         test_step_ok
131 }
132
133
134 wireshark_io_suite() {
135         # Q: quit after cap, k: start capture immediately
136         DUT="$WIRESHARK"
137         test_step_add "Input file" io_step_input_file
138 }
139
140 tshark_io_suite() {
141         DUT=$TSHARK
142         test_step_add "Input file" io_step_input_file
143         test_step_add "Output piping" io_step_output_piping
144         #test_step_add "Piping" io_step_input_piping
145 }
146
147 dumpcap_io_suite() {
148         #DUT="$DUMPCAP -Q"
149         DUT=$DUMPCAP
150
151         test_step_add "Input file" io_step_input_file
152 }
153
154 rawshark_io_suite() {
155         test_step_add "Rawshark pcap stdin" io_step_rawshark_pcap_stdin
156 }
157
158 io_cleanup_step() {
159         rm -f ./testout.txt
160         rm -f ./testout2.txt
161         rm -f ./testout.pcap
162         rm -f ./testout2.pcap
163         rm -f $IO_RAWSHARK_DHCP_PCAP_TESTOUT
164 }
165
166 io_suite() {
167         test_step_set_pre io_cleanup_step
168         test_step_set_post io_cleanup_step
169         test_suite_add "TShark file I/O" tshark_io_suite
170         #test_suite_add "Dumpcap file I/O" dumpcap_io_suite
171         test_suite_add "Rawshark file I/O" rawshark_io_suite
172 }
173 #
174 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
175 #
176 # Local variables:
177 # sh-basic-offset: 8
178 # tab-width: 8
179 # indent-tabs-mode: t
180 # End:
181 #
182 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
183 # :indentSize=8:tabSize=8:noTabs=false:
184 #