ethereal to wireshark conversion
[obnox/wireshark/wip.git] / test / suite-capture.sh
1 #!/bin/bash
2 #
3 # Test the capture engine 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
25
26
27 # common exit status values
28 EXIT_OK=0
29 EXIT_COMMAND_LINE=1
30 EXIT_ERROR=2
31
32
33 # capture exactly 10 packets
34 capture_step_10packets() {
35         $DUT -i $TRAFFIC_CAPTURE_IFACE -w ./testout.pcap -c 10  -a duration:$TRAFFIC_CAPTURE_DURATION > ./testout.txt 2>&1
36         RETURNVALUE=$?
37         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
38                 test_step_failed "exit status of $DUT: $RETURNVALUE"
39                 # part of the Prerequisite checks
40                 # probably wrong interface, output the possible interfaces
41                 $TSHARK -D
42                 return
43         fi
44
45         # we should have an output file now
46         if [ ! -f "./testout.pcap" ]; then
47                 test_step_failed "No output file!"
48                 return
49         fi
50         
51         # ok, we got a capture file, does it contain exactly 10 packets?
52         $CAPINFOS ./testout.pcap > ./testout.txt
53         grep -i 'Number of packets: 10' ./testout.txt > /dev/null
54         if [ $? -eq 0 ]; then
55                 test_step_ok
56         else
57                 echo
58                 cat ./testout.txt
59                 # part of the Prerequisite checks
60                 # probably wrong interface, output the possible interfaces
61                 $TSHARK -D
62                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
63         fi
64 }
65
66 # capture exactly 10 packets using "-w -" (piping to stdout)
67 capture_step_10packets_stdout() {
68         $DUT -i $TRAFFIC_CAPTURE_IFACE -c 10 -a duration:$TRAFFIC_CAPTURE_DURATION -w - > ./testout.pcap 2>./testout.txt
69         RETURNVALUE=$?
70         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
71                 test_step_failed "exit status of $DUT: $RETURNVALUE"
72                 $TSHARK -D
73                 return
74         fi
75
76         # we should have an output file now
77         if [ ! -f "./testout.pcap" ]; then
78                 test_step_failed "No output file!"
79                 return
80         fi
81         
82         # ok, we got a capture file, does it contain exactly 10 packets?
83         $CAPINFOS ./testout.pcap > ./testout2.txt 2>&1
84         grep -i 'Number of packets: 10' ./testout2.txt > /dev/null
85         if [ $? -eq 0 ]; then
86                 test_step_ok
87         else
88                 echo
89                 cat ./testout.txt
90                 cat ./testout2.txt
91                 $TSHARK -D
92                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
93         fi
94 }
95
96 # capture exactly 2 times 10 packets (multiple files)
97 capture_step_2multi_10packets() {
98         $DUT -i $TRAFFIC_CAPTURE_IFACE -w ./testout.pcap -c 10  -a duration:$TRAFFIC_CAPTURE_DURATION > ./testout.txt 2>&1
99         RETURNVALUE=$?
100         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
101                 test_step_failed "exit status of $DUT: $RETURNVALUE"
102                 # part of the Prerequisite checks
103                 # probably wrong interface, output the possible interfaces
104                 $TSHARK -D
105                 return
106         fi
107
108         # we should have an output file now
109         if [ ! -f "./testout.pcap" ]; then
110                 test_step_failed "No output file!"
111                 return
112         fi
113         
114         # ok, we got a capture file, does it contain exactly 10 packets?
115         $CAPINFOS ./testout.pcap > ./testout.txt
116         grep -i 'Number of packets: 10' ./testout.txt > /dev/null
117         if [ $? -eq 0 ]; then
118                 test_step_ok
119         else
120                 echo
121                 cat ./testout.txt
122                 test_step_failed "Probably the wrong interface (no traffic captured)!"
123         fi
124 }
125
126 # capture with a very unlikely read filter, packets must be zero afterwards
127 capture_step_read_filter() {
128         # valid, but very unlikely filter
129         $DUT -i $TRAFFIC_CAPTURE_IFACE -w ./testout.pcap -a duration:$TRAFFIC_CAPTURE_DURATION -R 'dcerpc.cn_call_id==123456' -c 10 > ./testout.txt 2>&1
130         RETURNVALUE=$?
131         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
132                 test_step_failed "exit status: $RETURNVALUE"
133                 return
134         fi
135
136         # we should have an output file now
137         if [ ! -f "./testout.pcap" ]; then
138                 test_step_failed "No output file!"
139                 return
140         fi
141
142         # ok, we got a capture file, does it contain exactly 0 packets?
143         $CAPINFOS ./testout.pcap > ./testout.txt
144         grep -i 'Number of packets: 0' ./testout.txt > /dev/null
145         if [ $? -eq 0 ]; then
146                 test_step_ok
147         else
148                 echo
149                 cat ./testout.txt
150                 test_step_failed "Capture file should contain zero packets!"
151         fi
152 }
153
154
155 # capture with a snapshot length
156 capture_step_snapshot() {
157         # capture with a snapshot length of 68 bytes for $TRAFFIC_CAPTURE_DURATION seconds
158         # this should result in no packets
159         $DUT -i $TRAFFIC_CAPTURE_IFACE -w ./testout.pcap -s 68 -a duration:$TRAFFIC_CAPTURE_DURATION > ./testout.txt 2>&1
160         RETURNVALUE=$?
161         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
162                 test_step_failed "exit status: $RETURNVALUE"
163                 return
164         fi
165
166         # we should have an output file now
167         if [ ! -f "./testout.pcap" ]; then
168                 test_step_failed "No output file!"
169                 return
170         fi
171
172         # use tshark to filter out all packets, which are larger than 68 bytes
173         $TSHARK -r ./testout.pcap -w ./testout2.pcap -R 'frame.cap_len>68' > ./testout.txt 2>&1
174
175         # ok, we got a capture file, does it contain exactly 0 packets?
176         $CAPINFOS ./testout2.pcap > ./testout.txt
177         grep -i 'Number of packets: 0' ./testout.txt > /dev/null
178         if [ $? -eq 0 ]; then
179                 test_step_ok
180         else
181                 echo
182                 cat ./testout.txt
183                 test_step_failed "Capture file should contain zero packets!"
184                 return
185         fi
186 }
187
188 wireshark_capture_suite() {
189         # Q: quit after cap, k: start capture immediately
190         DUT="$WIRESHARK -Q -k"
191         test_step_add "Capture 10 packets" capture_step_10packets
192         # piping to stdout doesn't work with Wireshark and capturing!
193         #test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
194         # read filter doesn't work with Wireshark and capturing!
195         #test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
196         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
197 }
198
199 tshark_capture_suite() {
200         DUT=$TSHARK
201         test_step_add "Capture 10 packets" capture_step_10packets
202         test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
203         test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
204         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
205 }
206
207 dumpcap_capture_suite() {
208         #DUT="$DUMPCAP -Q"
209         DUT=$DUMPCAP
210         test_step_add "Capture 10 packets" capture_step_10packets
211         test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
212         # read (display) filters intentionally doesn't work with dumpcap!
213         #test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
214         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
215 }
216
217 capture_cleanup_step() {
218         rm -f ./testout.txt
219         rm -f ./testout2.txt
220         rm -f ./testout.pcap
221         rm -f ./testout2.pcap
222 }
223
224 capture_suite() {
225         test_step_set_pre capture_cleanup_step
226         test_step_set_post capture_cleanup_step
227         test_remark_add "Capture - need some traffic on interface: \"$TRAFFIC_CAPTURE_IFACE\""
228         test_suite_add "TShark capture" tshark_capture_suite
229         test_suite_add "Wireshark capture" wireshark_capture_suite
230         test_suite_add "Dumpcap capture" dumpcap_capture_suite
231 }