Disable capture tests and unit tests until possible issues corrected.
[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, writeto 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 capture_test_output_print() {
33         wait
34         for f in "$@"; do
35                 if [[ -f "$f" ]]; then
36                 printf " --> $f\n"
37                 cat "$f"
38                 printf "\n"
39                 fi
40         done
41 }
42
43 traffic_gen_ping() {
44         # Generate some traffic for quiet networks.
45         # This will have to be adjusted for non-Windows systems.
46         {
47         date 
48         for (( x=20; x<=50; x++ ))
49         do
50                 ping -n 1 -l $x www.wireshark.org # in effect: number the packets
51                 sleep 1
52         done
53         date
54         } > ./testout_ping.txt 2>&1 &
55 }
56
57 ping_cleanup() {
58         wait
59         rm -f ./testout_ping.txt
60 }
61
62 # capture exactly 10 packets
63 capture_step_10packets() {
64         if [ "$WS_SYSTEM" != "Windows" ] ; then
65                 test_step_skipped
66                 return
67         fi
68
69         traffic_gen_ping
70
71         date > ./testout.txt
72         $DUT -i $TRAFFIC_CAPTURE_IFACE $TRAFFIC_CAPTURE_PROMISC \
73                 -w ./testout.pcap \
74                 -c 10  \
75                 -a duration:$TRAFFIC_CAPTURE_DURATION \
76                 -f icmp \
77                 >> ./testout.txt 2>&1
78         RETURNVALUE=$?
79         date >> ./testout.txt
80         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
81                 echo
82                 capture_test_output_print ./testout.txt
83                 # part of the Prerequisite checks
84                 # wrong interface ? output the possible interfaces
85                 $TSHARK -D
86                 test_step_failed "exit status of $DUT: $RETURNVALUE"
87                 return
88         fi
89
90         # we should have an output file now
91         if [ ! -f "./testout.pcap" ]; then
92         capture_test_output ./testout.txt
93                 test_step_failed "No output file!"
94                 return
95         fi
96
97         # ok, we got a capture file, does it contain exactly 10 packets?
98         $CAPINFOS ./testout.pcap > ./testout2.txt
99         grep -i 'Number of packets: 10' ./testout2.txt > /dev/null
100         if [ $? -eq 0 ]; then
101                 test_step_ok
102         else
103                 echo
104                 $TSHARK -ta -r ./testout.pcap >> ./testout2.txt
105                 capture_test_output_print ./testout_ping.txt ./testout.txt ./testout2.txt
106                 # part of the Prerequisite checks
107                 # probably wrong interface, output the possible interfaces
108                 $TSHARK -D
109                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
110         fi
111 }
112
113 # capture exactly 10 packets using "-w -" (piping to stdout)
114 capture_step_10packets_stdout() {
115         if [ "$WS_SYSTEM" != "Windows" ] ; then
116                 test_step_skipped
117                 return
118         fi
119
120         traffic_gen_ping
121
122         date > ./testout.txt
123         $DUT -i $TRAFFIC_CAPTURE_IFACE $TRAFFIC_CAPTURE_PROMISC \
124                 -c 10 \
125                 -a duration:$TRAFFIC_CAPTURE_DURATION \
126                 -w - \
127                 -f icmp \
128                 > ./testout.pcap 2>>./testout.txt
129         RETURNVALUE=$?
130         date >> ./testout.txt
131         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
132                 echo
133                 cat ./testout.txt
134                 $TSHARK -D
135                 test_step_failed "exit status of $DUT: $RETURNVALUE"
136                 return
137         fi
138
139         # we should have an output file now
140         if [ ! -f "./testout.pcap" ]; then
141                 test_step_failed "No output file!"
142                 return
143         fi
144
145         # ok, we got a capture file, does it contain exactly 10 packets?
146         $CAPINFOS ./testout.pcap > ./testout2.txt 2>&1
147         grep -i 'Number of packets: 10' ./testout2.txt > /dev/null
148         if [ $? -eq 0 ]; then
149                 test_step_ok
150         else
151                 echo
152                 cat ./testout.txt
153                 cat ./testout2.txt
154                 $TSHARK -D
155                 test_step_failed "No or not enough traffic captured. Probably the wrong interface: $TRAFFIC_CAPTURE_IFACE!"
156         fi
157 }
158
159 # capture packets via a fifo
160 capture_step_fifo() {
161         mkfifo 'fifo'
162         (cat $CAPFILE; sleep 1; tail -c +25 $CAPFILE) > fifo &
163         $DUT -i fifo $TRAFFIC_CAPTURE_PROMISC \
164                 -w ./testout.pcap \
165                 -a duration:$TRAFFIC_CAPTURE_DURATION \
166                 > ./testout.txt 2>&1
167         RETURNVALUE=$?
168         rm 'fifo'
169         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
170                 test_step_failed "exit status of $DUT: $RETURNVALUE"
171                 return
172         fi
173
174         # we should have an output file now
175         if [ ! -f "./testout.pcap" ]; then
176                 test_step_failed "No output file!"
177                 return
178         fi
179
180         # ok, we got a capture file, does it contain exactly 8 packets?
181         $CAPINFOS ./testout.pcap > ./testout.txt
182         grep -i 'Number of packets: 8' ./testout.txt > /dev/null
183         if [ $? -eq 0 ]; then
184                 test_step_ok
185         else
186                 echo
187                 cat ./testout.txt
188                 test_step_failed "No or not enough traffic captured."
189         fi
190 }
191
192 # capture exactly 2 times 10 packets (multiple files)
193 capture_step_2multi_10packets() {
194         if [ "$WS_SYSTEM" != "Windows" ] ; then
195                 test_step_skipped
196                 return
197         fi
198
199         traffic_gen_ping
200
201         date > ./testout.txt
202         $DUT -i $TRAFFIC_CAPTURE_IFACE $TRAFFIC_CAPTURE_PROMISC \
203                 -w ./testout.pcap \
204                 -c 10 \
205                 -a duration:$TRAFFIC_CAPTURE_DURATION \
206                 -f icmp \
207                 >> ./testout.txt 2>&1
208
209         RETURNVALUE=$?
210         date >> ./testout.txt
211         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
212                 echo
213                 cat ./testout.txt
214                 # part of the Prerequisite checks
215                 # probably wrong interface, output the possible interfaces
216                 $TSHARK -D
217                 test_step_failed "exit status of $DUT: $RETURNVALUE"
218                 return
219         fi
220
221         # we should have an output file now
222         if [ ! -f "./testout.pcap" ]; then
223                 test_step_failed "No output file!"
224                 return
225         fi
226
227         # ok, we got a capture file, does it contain exactly 10 packets?
228         $CAPINFOS ./testout.pcap > ./testout.txt
229         grep -i 'Number of packets: 10' ./testout.txt > /dev/null
230         if [ $? -eq 0 ]; then
231                 test_step_ok
232         else
233                 echo
234                 cat ./testout.txt
235                 test_step_failed "Probably the wrong interface (no traffic captured)!"
236         fi
237 }
238
239 # capture with a very unlikely read filter, packets must be zero afterwards
240 capture_step_read_filter() {
241         if [ "$WS_SYSTEM" != "Windows" ] ; then
242                 test_step_skipped
243                 return
244         fi
245
246         traffic_gen_ping
247
248         # valid, but very unlikely filter
249         date > ./testout.txt
250         $DUT -i $TRAFFIC_CAPTURE_IFACE $TRAFFIC_CAPTURE_PROMISC \
251                 -w ./testout.pcap \
252                 -a duration:$TRAFFIC_CAPTURE_DURATION \
253                 -R 'dcerpc.cn_call_id==123456' \
254                 -c 10 \
255                 -f icmp \
256                 >> ./testout.txt 2>&1
257         RETURNVALUE=$?
258         date >> ./testout.txt
259         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
260                 echo
261                 cat ./testout.txt
262                 # part of the Prerequisite checks
263                 # wrong interface ? output the possible interfaces
264                 $TSHARK -D
265                 test_step_failed "exit status: $RETURNVALUE"
266                 return
267         fi
268
269         # we should have an output file now
270         if [ ! -f "./testout.pcap" ]; then
271                 test_step_failed "No output file!"
272                 return
273         fi
274
275         # ok, we got a capture file, does it contain exactly 0 packets?
276         $CAPINFOS ./testout.pcap > ./testout.txt
277         grep -i 'Number of packets: 0' ./testout.txt > /dev/null
278         if [ $? -eq 0 ]; then
279                 test_step_ok
280         else
281                 echo
282                 cat ./testout.txt
283                 test_step_failed "Capture file should contain zero packets!"
284         fi
285 }
286
287
288 # capture with a snapshot length
289 capture_step_snapshot() {
290         if [ "$WS_SYSTEM" != "Windows" ] ; then
291                 test_step_skipped
292                 return
293         fi
294
295         traffic_gen_ping
296
297         # capture with a snapshot length of 68 bytes for $TRAFFIC_CAPTURE_DURATION seconds
298         # this should result in no packets greater than 68 bytes
299         date > ./testout.txt
300         $DUT -i $TRAFFIC_CAPTURE_IFACE $TRAFFIC_CAPTURE_PROMISC \
301                 -w ./testout.pcap \
302                 -s 68 \
303                 -a duration:$TRAFFIC_CAPTURE_DURATION \
304                 -f icmp \
305                 >> ./testout.txt 2>&1
306         RETURNVALUE=$?
307         date >> ./testout.txt
308         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
309                 echo
310                 cat ./testout.txt
311                 # part of the Prerequisite checks
312                 # wrong interface ? output the possible interfaces
313                 $TSHARK -D
314                 test_step_failed "exit status: $RETURNVALUE"
315                 return
316         fi
317
318         # we should have an output file now
319         if [ ! -f "./testout.pcap" ]; then
320                 test_step_failed "No output file!"
321                 return
322         fi
323
324         # use tshark to filter out all packets, which are larger than 68 bytes
325         $TSHARK -r ./testout.pcap -w ./testout2.pcap -R 'frame.cap_len>68' > ./testout.txt 2>&1
326
327         # ok, we got a capture file, does it contain exactly 0 packets?
328         $CAPINFOS ./testout2.pcap > ./testout.txt
329         grep -i 'Number of packets: 0' ./testout.txt > /dev/null
330         if [ $? -eq 0 ]; then
331                 test_step_ok
332         else
333                 echo
334                 cat ./testout.txt
335                 test_step_failed "Capture file should contain zero packets!"
336                 return
337         fi
338 }
339
340 wireshark_capture_suite() {
341         # Q: quit after cap, k: start capture immediately
342         DUT="$WIRESHARK -Q -k"
343         test_step_add "Capture 10 packets" capture_step_10packets
344         # piping to stdout doesn't work with Wireshark and capturing!
345         #test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
346         # read filter doesn't work with Wireshark and capturing!
347         #test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
348         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
349 }
350
351 tshark_capture_suite() {
352         DUT=$TSHARK
353         test_step_add "Capture 10 packets" capture_step_10packets
354         test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
355         if [ $TEST_FIFO ]; then
356                 test_step_add "Capture via fifo" capture_step_fifo
357         fi
358         test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
359         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
360 }
361
362 dumpcap_capture_suite() {
363         #DUT="$DUMPCAP -Q"
364         DUT=$DUMPCAP
365         test_step_add "Capture 10 packets" capture_step_10packets
366         test_step_add "Capture 10 packets using stdout: -w -" capture_step_10packets_stdout
367         if [ $TEST_FIFO ]; then
368                 test_step_add "Capture via fifo" capture_step_fifo
369         fi
370         # read (display) filters intentionally doesn't work with dumpcap!
371         #test_step_add "Capture read filter (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_read_filter
372         test_step_add "Capture snapshot length 68 bytes (${TRAFFIC_CAPTURE_DURATION}s)" capture_step_snapshot
373 }
374
375 capture_cleanup_step() {
376         ping_cleanup
377         rm -f ./testout.txt
378         rm -f ./testout2.txt
379         rm -f ./testout.pcap
380         rm -f ./testout2.pcap
381 }
382
383 capture_suite() {
384         test_step_set_pre capture_cleanup_step
385         test_step_set_post capture_cleanup_step
386         test_remark_add "Capture - need some traffic on interface: \"$TRAFFIC_CAPTURE_IFACE\""
387 ##      test_suite_add "TShark capture" tshark_capture_suite
388 ##      test_suite_add "Wireshark capture" wireshark_capture_suite
389 ##      test_suite_add "Dumpcap capture" dumpcap_capture_suite
390 }