- simple Information Objects support
[obnox/wireshark/wip.git] / tools / randpkt-test.sh
1 #!/bin/bash
2 #
3 # $Id$
4
5 # Randpkt testing script for TShark
6 #
7 # This script uses Randpkt to generate capture files with randomized
8 # content.  It runs TShark on each generated file and checks for errors.
9 # The files are processed repeatedly until an error is found.
10
11 # Tweak the following to your liking.
12 TSHARK=./tshark
13 RANDPKT=./randpkt
14
15 # This needs to point to a 'date' that supports %s.
16 DATE=/bin/date
17 BASE_NAME=randpkt-`$DATE +%Y-%m-%d`-$$
18
19 # Temporary file directory and names.
20 # (had problems with this on cygwin, tried TMP_DIR=./ which worked)
21 TMP_DIR=/tmp
22 TMP_FILE=$BASE_NAME.pcap
23 ERR_FILE=$BASE_NAME.err
24
25 # Loop this many times (< 1 loops forever)
26 MAX_PASSES=0
27
28 # These may be set to your liking
29 # Stop the child process, if it's running longer than x seconds
30 MAX_CPU_TIME=900
31 # Stop the child process, if it's using more than y * 1024 bytes
32 MAX_VMEM=500000
33 # Trigger an abort if a dissector finds a bug.
34 # Uncomment to disable
35 WIRESHARK_ABORT_ON_DISSECTOR_BUG="True"
36
37 PKT_TYPES=`$RANDPKT -h | awk '/^\t/ {print $1}'`
38
39 # To do: add options for file names and limits
40 while getopts ":d:p:t:" OPTCHAR ; do
41     case $OPTCHAR in
42         d) TMP_DIR=$OPTARG ;;
43         p) MAX_PASSES=$OPTARG ;;
44         t) PKT_TYPES=$OPTARG ;;
45     esac
46 done
47 shift $(($OPTIND - 1))
48
49 # set some limits to the child processes, e.g. stop it if it's running longer then MAX_CPU_TIME seconds
50 # (ulimit is not supported well on cygwin and probably other platforms, e.g. cygwin shows some warnings)
51 ulimit -S -t $MAX_CPU_TIME -v $MAX_VMEM
52 ulimit -c unlimited
53
54 ### usually you won't have to change anything below this line ###
55
56 # TShark arguments (you won't have to change these)
57 # n Disable network object name resolution
58 # V Print a view of the details of the packet rather than a one-line summary of the packet
59 # x Cause TShark to print a hex and ASCII dump of the packet data after printing the summary or details
60 # r Read packet data from the following infile
61 TSHARK_ARGS="-nVxr"
62 RANDPKT_ARGS="-b 2000 -c 5000"
63
64 NOTFOUND=0
65 for i in "$TSHARK" "$RANDPKT" "$DATE" "$TMP_DIR" ; do
66     if [ ! -x $i ]; then
67         echo "Couldn't find $i"
68         NOTFOUND=1
69     fi
70 done
71 if [ $NOTFOUND -eq 1 ]; then
72     exit 1
73 fi
74
75 HOWMANY="forever"
76 if [ $MAX_PASSES -gt 0 ]; then
77     HOWMANY="$MAX_PASSES passes"
78 fi
79 echo "Running $TSHARK with args: $TSHARK_ARGS ($HOWMANY)"
80 echo "Running $RANDPKT with args: $RANDPKT_ARGS"
81 echo ""
82
83 # Not yet - properly handle empty filenames
84 #trap "rm $TMP_DIR/$TMP_FILE $TMP_DIR/$RAND_FILE; exit 1" 1 2 15
85
86 # Iterate over our capture files.
87 PASS=0
88 while [ $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 ] ; do
89     PASS=`expr $PASS + 1`
90     echo "Pass $PASS:"
91
92     for PKT_TYPE in $PKT_TYPES ; do
93         echo -n "    $PKT_TYPE: "
94
95         DISSECTOR_BUG=0
96
97         "$RANDPKT" $RANDPKT_ARGS -t $PKT_TYPE $TMP_DIR/$TMP_FILE \
98             > /dev/null 2>&1
99
100         "$TSHARK" $TSHARK_ARGS $TMP_DIR/$TMP_FILE \
101                 > /dev/null 2> $TMP_DIR/$ERR_FILE
102         RETVAL=$?
103         grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
104             > /dev/null 2>&1 && DISSECTOR_BUG=1
105         if [ $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 ] ; then
106             RAND_FILE="rand-`$DATE +%Y-%m-%d`-$$.pcap"
107             echo ""
108             echo " ERROR"
109             echo -e "Processing failed.  Capture info follows:\n"
110             mv $TMP_DIR/$TMP_FILE $TMP_DIR/$RAND_FILE
111             echo "  Output file: $TMP_DIR/$RAND_FILE"
112             if [ $DISSECTOR_BUG -ne 0 ] ; then
113                 echo -e "stderr follows:\n"
114                 cat $TMP_DIR/$ERR_FILE
115             fi
116             exit 1
117         fi
118         echo " OK"
119         rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE
120     done
121 done