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