Fix a bunch of Win64 compilation errors by cowardly casting size_ts to ints.
[metze/wireshark/wip.git] / tools / fuzz-test.sh
1 #!/bin/bash
2 #
3 # $Id$
4
5 # Fuzz-testing script for TShark
6 #
7 # This script uses Editcap to add random errors ("fuzz") to a set of
8 # capture files specified on the command line.  It runs TShark on
9 # each fuzzed file and checks for errors.  The files are processed
10 # repeatedly until an error is found.
11
12 # This needs to point to a 'date' that supports %s.
13 DATE=/bin/date
14 BASE_NAME=fuzz-`$DATE +%Y-%m-%d`-$$
15
16 # Directory containing binaries.  Default current directory.
17 BIN_DIR=.
18
19 # Temporary file directory and names.
20 # (had problems with this on cygwin, tried TMP_DIR=./ which worked)
21 TMP_DIR=/tmp
22 if [ "$OSTYPE" == "cygwin" ] ; then
23         TMP_DIR=`cygpath --windows "$TMP_DIR"`
24 fi
25 TMP_FILE=$BASE_NAME.pcap
26 ERR_FILE=$BASE_NAME.err
27
28 # Loop this many times (< 1 loops forever)
29 MAX_PASSES=0
30
31 # These may be set to your liking
32 # Stop the child process, if it's running longer than x seconds
33 MAX_CPU_TIME=900
34 # Stop the child process, if it's using more than y * 1024 bytes
35 MAX_VMEM=500000
36 # Insert z times an error into the capture file (0.02 seems to be a good value to find errors)
37 ERR_PROB=0.02
38 # Trigger an abort if a dissector finds a bug.
39 # Uncomment to disable
40 WIRESHARK_ABORT_ON_DISSECTOR_BUG="True"
41
42
43 # To do: add options for file names and limits
44 while getopts ":b:d:p:" OPTCHAR ; do
45         case $OPTCHAR in
46                 b) BIN_DIR=$OPTARG ;;
47                 d) TMP_DIR=$OPTARG ;;
48                 p) MAX_PASSES=$OPTARG ;;
49         esac
50 done
51 shift $(($OPTIND - 1))
52
53 # Tweak the following to your liking.  Editcap must support "-E".
54 TSHARK="$BIN_DIR/tshark"
55 EDITCAP="$BIN_DIR/editcap"
56 CAPINFOS="$BIN_DIR/capinfos"
57
58 # set some limits to the child processes, e.g. stop it if it's running longer then MAX_CPU_TIME seconds
59 # (ulimit is not supported well on cygwin and probably other platforms, e.g. cygwin shows some warnings)
60 ulimit -S -t $MAX_CPU_TIME -v $MAX_VMEM
61 ulimit -c unlimited
62
63 ### usually you won't have to change anything below this line ###
64
65 # TShark arguments (you won't have to change these)
66 # n Disable network object name resolution
67 # V Print a view of the details of the packet rather than a one-line summary of the packet
68 # x Cause TShark to print a hex and ASCII dump of the packet data after printing the summary or details
69 # r Read packet data from the following infile
70 TSHARK_ARGS="-nVxr"
71
72 NOTFOUND=0
73 for i in "$TSHARK" "$EDITCAP" "$CAPINFOS" "$DATE" "$TMP_DIR" ; do
74         if [ ! -x $i ]; then
75                 echo "Couldn't find $i"
76                 NOTFOUND=1
77         fi
78 done
79 if [ $NOTFOUND -eq 1 ]; then
80         exit 1
81 fi
82
83 # Make sure we have a valid test set
84 FOUND=0
85 for CF in "$@" ; do
86     if [ "$OSTYPE" == "cygwin" ] ; then
87         CF=`cygpath --windows "$CF"`
88     fi
89     "$CAPINFOS" "$CF" > /dev/null 2>&1 && FOUND=1
90     if [ $FOUND -eq 1 ] ; then break ; fi
91 done
92
93 if [ $FOUND -eq 0 ] ; then
94     cat <<FIN
95 Error: No valid capture files found.
96
97 Usage: `basename $0` [-p passes] [-d work_dir] capture file 1 [capture file 2]...
98 FIN
99     exit 1
100 fi
101
102 HOWMANY="forever"
103 if [ $MAX_PASSES -gt 0 ]; then
104         HOWMANY="$MAX_PASSES passes"
105 fi
106 echo "Running $TSHARK with args: $TSHARK_ARGS ($HOWMANY)"
107 echo ""
108
109 # Clean up on <ctrl>C, etc
110 trap "rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE; echo ""; exit 1" HUP INT TERM
111
112 # Iterate over our capture files.
113 PASS=0
114 while [ $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 ] ; do
115     PASS=`expr $PASS + 1`
116     echo "Starting pass $PASS:"
117     RUN=0
118
119     for CF in "$@" ; do
120         RUN=$(( $RUN + 1 ))
121         if [ $(( $RUN % 50 )) -eq 0 ] ; then
122             echo "    [Pass $PASS]"
123         fi
124         if [ "$OSTYPE" == "cygwin" ] ; then
125             CF=`cygpath --windows "$CF"`
126         fi
127         echo -n "    $CF: "
128
129         "$CAPINFOS" "$CF" > /dev/null 2> $TMP_DIR/$ERR_FILE
130         RETVAL=$?
131         if [ $RETVAL -eq 0 ] ; then
132             # have a valid file
133             rm -f $TMP_DIR/$ERR_FILE
134         elif [ $RETVAL -eq 1 ] ; then
135             echo "Not a valid capture file"
136             rm -f $TMP_DIR/$ERR_FILE
137             continue
138         else
139             echo ""
140             echo " ERROR"
141             echo -e "Processing failed.  Capture info follows:\n"
142             echo "  Input file: $CF"
143             echo -e "stderr follows:\n"
144             cat $TMP_DIR/$ERR_FILE
145             exit 1
146         fi
147
148         DISSECTOR_BUG=0
149
150         "$EDITCAP" -E $ERR_PROB "$CF" $TMP_DIR/$TMP_FILE > /dev/null 2>&1
151         if [ $? -ne 0 ] ; then
152             "$EDITCAP" -E $ERR_PROB -T ether "$CF" $TMP_DIR/$TMP_FILE \
153                 > /dev/null 2>&1
154             if [ $? -ne 0 ] ; then
155                 echo "Invalid format for editcap"
156                 continue
157             fi
158         fi
159
160         "$TSHARK" $TSHARK_ARGS $TMP_DIR/$TMP_FILE \
161                 > /dev/null 2> $TMP_DIR/$ERR_FILE
162         RETVAL=$?
163         grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
164             > /dev/null 2>&1 && DISSECTOR_BUG=1
165         if [ $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 ] ; then
166             echo ""
167             echo " ERROR"
168             echo -e "Processing failed.  Capture info follows:\n"
169             echo "  Output file: $TMP_DIR/$TMP_FILE"
170             if [ $DISSECTOR_BUG -ne 0 ] ; then
171                 echo -e "stderr follows:\n"
172                 cat $TMP_DIR/$ERR_FILE
173             fi
174             exit 1
175         fi
176         echo " OK"
177         rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE
178     done
179 done
180