18df350d6b2f545ad2159ed4a4ccd5fe612624dc
[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 TEST_TYPE="fuzz"
13 . `dirname $0`/test-common.sh || exit 1
14
15 # Directory containing binaries.  Default current directory.
16 BIN_DIR=.
17
18 # Sanity check to make sure we can find our plugins. Zero or less disables.
19 MIN_PLUGINS=0
20
21 # Did we catch a signal?
22 DONE=0
23
24 # Perform a two pass analysis on the capture file?
25 TWO_PASS=
26
27 # Specific config profile ?
28 CONFIG_PROFILE=
29
30 # Run under valgrind ?
31 VALGRIND=0
32
33
34 # To do: add options for file names and limits
35 while getopts ":2b:C:d:e:gp:P:" OPTCHAR ; do
36     case $OPTCHAR in
37         2) TWO_PASS="-2 " ;;
38         b) BIN_DIR=$OPTARG ;;
39         C) CONFIG_PROFILE="-C $OPTARG " ;;
40         d) TMP_DIR=$OPTARG ;;
41         e) ERR_PROB=$OPTARG ;;
42         g) VALGRIND=1 ;;
43         p) MAX_PASSES=$OPTARG ;;
44         P) MIN_PLUGINS=$OPTARG ;;
45     esac
46 done
47 shift $(($OPTIND - 1))
48
49 ### usually you won't have to change anything below this line ###
50
51 if [ $VALGRIND -eq 1 ]; then
52     RUNNER="$BIN_DIR/tools/valgrind-wireshark.sh"
53     declare -a RUNNER_ARGS=("${CONFIG_PROFILE}${TWO_PASS}-T" "${CONFIG_PROFILE}${TWO_PASS} ")
54 else
55     # Not using valgrind, use regular tshark.
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     RUNNER="$TSHARK"
62     declare -a RUNNER_ARGS=("${CONFIG_PROFILE}${TWO_PASS}-nVxr" "${CONFIG_PROFILE}${TWO_PASS}-nr")
63     # Running with a read filter but without generating the tree exposes some
64     # "More than 100000 items in tree" bugs.
65     # Not sure if we want to add even more cycles to the fuzz bot's work load...
66     #declare -a RUNNER_ARGS=("${CONFIG_PROFILE}${TWO_PASS}-nVxr" "${CONFIG_PROFILE}${TWO_PASS}-nr" "-Yframe ${CONFIG_PROFILE}${TWO_PASS}-nr")
67 fi
68
69
70 NOTFOUND=0
71 for i in "$TSHARK" "$EDITCAP" "$CAPINFOS" "$DATE" "$TMP_DIR" ; do
72     if [ ! -x $i ]; then
73         echo "Couldn't find $i"
74         NOTFOUND=1
75     fi
76 done
77 if [ $NOTFOUND -eq 1 ]; then
78     exit 1
79 fi
80
81 # Make sure we have a valid test set
82 FOUND=0
83 for CF in "$@" ; do
84     if [ "$OSTYPE" == "cygwin" ] ; then
85         CF=`cygpath --windows "$CF"`
86     fi
87     "$CAPINFOS" "$CF" > /dev/null 2>&1 && FOUND=1
88     if [ $FOUND -eq 1 ] ; then break ; fi
89 done
90
91 if [ $FOUND -eq 0 ] ; then
92     cat <<FIN
93 Error: No valid capture files found.
94
95 Usage: `basename $0` [-2] [-b bin_dir] [-C config_profile] [-d work_dir] [-e error probability] [-g] [-p passes] capture file 1 [capture file 2]...
96 FIN
97     exit 1
98 fi
99
100 PLUGIN_COUNT=`$TSHARK -G plugins | grep dissector | wc -l`
101 if [ $MIN_PLUGINS -gt 0 -a $PLUGIN_COUNT -lt $MIN_PLUGINS ] ; then
102     echo "Warning: Found fewer plugins than expected ($PLUGIN_COUNT vs $MIN_PLUGINS)."
103     exit 1
104 fi
105
106 HOWMANY="forever"
107 if [ $MAX_PASSES -gt 0 ]; then
108         HOWMANY="$MAX_PASSES passes"
109 fi
110 echo -n "Running $RUNNER with args: "
111 printf "\"%s\" " "${RUNNER_ARGS[@]}"
112 echo "($HOWMANY)"
113 echo ""
114
115 # Clean up on <ctrl>C, etc
116 trap "DONE=1; echo 'Caught signal'" HUP INT TERM
117
118
119 # Iterate over our capture files.
120 PASS=0
121 while [ \( $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 \) -a $DONE -ne 1 ] ; do
122     let PASS=$PASS+1
123     echo "Starting pass $PASS:"
124     RUN=0
125
126     for CF in "$@" ; do
127         if [ $DONE -eq 1 ]; then
128             break # We caught a signal
129         fi
130         RUN=$(( $RUN + 1 ))
131         if [ $(( $RUN % 50 )) -eq 0 ] ; then
132             echo "    [Pass $PASS]"
133         fi
134         if [ "$OSTYPE" == "cygwin" ] ; then
135             CF=`cygpath --windows "$CF"`
136         fi
137         echo -n "    $CF: "
138
139         "$CAPINFOS" "$CF" > /dev/null 2> $TMP_DIR/$ERR_FILE
140         RETVAL=$?
141         if [ $RETVAL -eq 1 ] ; then
142             echo "Not a valid capture file"
143             rm -f $TMP_DIR/$ERR_FILE
144             continue
145         elif [ $RETVAL -ne 0 -a $DONE -ne 1 ] ; then
146             # Some other error
147             exit_error
148         fi
149
150         DISSECTOR_BUG=0
151         VG_ERR_CNT=0
152
153         "$EDITCAP" -E $ERR_PROB "$CF" $TMP_DIR/$TMP_FILE > /dev/null 2>&1
154         if [ $? -ne 0 ] ; then
155             "$EDITCAP" -E $ERR_PROB -T ether "$CF" $TMP_DIR/$TMP_FILE \
156             > /dev/null 2>&1
157             if [ $? -ne 0 ] ; then
158             echo "Invalid format for editcap"
159             continue
160             fi
161         fi
162
163         for ARGS in "${RUNNER_ARGS[@]}" ; do
164             echo -n "($ARGS) "
165             echo -e "Command and args: $RUNNER $ARGS\n" > $TMP_DIR/$ERR_FILE
166
167             # Run in a child process with limits, e.g. stop it if it's running
168             # longer then MAX_CPU_TIME seconds. (ulimit may not be supported
169             # well on some platforms, particularly cygwin.)
170             (
171                 ulimit -S -t $MAX_CPU_TIME -v $MAX_VMEM -s $MAX_STACK
172                 ulimit -c unlimited
173
174                 "$RUNNER" $ARGS $TMP_DIR/$TMP_FILE \
175                     > /dev/null 2>> $TMP_DIR/$ERR_FILE
176             )
177             RETVAL=$?
178             if [ $RETVAL -ne 0 ] ; then break ; fi
179         done
180
181         # Uncomment the next two lines to enable dissector bug
182         # checking.
183         #grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
184         #    > /dev/null 2>&1 && DISSECTOR_BUG=1
185
186         if [ $VALGRIND -eq 1 ]; then
187             VG_ERR_CNT="`grep "ERROR SUMMARY:" $TMP_DIR/$ERR_FILE | cut -f4 -d' '`"
188             if grep -q "Valgrind cannot continue" $TMP_DIR/$ERR_FILE; then
189                     VG_ERR_CNT=-1
190             fi
191         fi
192
193         if [ \( $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 -o $VG_ERR_CNT -ne 0 \) \
194             -a $DONE -ne 1 ] ; then
195
196             exit_error
197         fi
198
199         echo " OK"
200         rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE
201     done
202 done