If our error output is huge (> 5 MB) trim out the middle of the file.
[metze/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 declare -a TSHARK_ARGS=("-nVxr" "-nr")
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 -n "Running $TSHARK with args: "
80 printf "\"%s\" " "${TSHARK_ARGS[@]}"
81 echo "($HOWMANY)"
82 echo "Running $RANDPKT with args: $RANDPKT_ARGS"
83 echo ""
84
85 trap "MAX_PASSES=1; echo 'Caught signal'" HUP INT TERM
86
87 function exit_error() {
88     echo -e "\n ERROR"
89     echo -e "Processing failed. Capture info follows:\n"
90     echo "  Input file: $CF"
91
92     ERR_SIZE=$(du -sk $TMP_DIR/$ERR_FILE | awk '{ print $1 }')
93     if [ $ERR_SIZE -ge 5000 ] ; then
94         mv $TMP_DIR/$ERR_FILE $TMP_DIR/${ERR_FILE}.full
95         head -n 2000 $TMP_DIR/${ERR_FILE}.full > $TMP_DIR/$ERR_FILE
96         echo -e "\n\n[ Output removed ]\n\n" >> $TMP_DIR/$ERR_FILE
97         tail -n 2000 $TMP_DIR/${ERR_FILE}.full >> $TMP_DIR/$ERR_FILE
98         rm -f $TMP_DIR/${ERR_FILE}.full
99     fi
100
101     if [ -d .svn ] ; then
102         echo -e "\nSubversion revision" >> $TMP_DIR/$ERR_FILE
103         svn log -l 1 >> $TMP_DIR/$ERR_FILE
104     elif [ -d .git ] ; then
105         echo -e "\nGit commit" >> $TMP_DIR/$ERR_FILE
106         git log -1 >> $TMP_DIR/$ERR_FILE
107     fi
108
109     echo -e "stderr follows:\n"
110     cat $TMP_DIR/$ERR_FILE
111
112     exit 1
113 }
114
115 # Iterate over our capture files.
116 PASS=0
117 while [ $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 ] ; do
118     let PASS=$PASS+1
119     echo "Pass $PASS:"
120
121     for PKT_TYPE in $PKT_TYPES ; do
122         if [ $PASS -gt $MAX_PASSES -a $MAX_PASSES -ge 1 ] ; then
123             break # We caught a signal
124         fi
125         echo -n "    $PKT_TYPE: "
126
127         DISSECTOR_BUG=0
128
129         "$RANDPKT" $RANDPKT_ARGS -t $PKT_TYPE $TMP_DIR/$TMP_FILE \
130             > /dev/null 2>&1
131
132         for ARGS in "${TSHARK_ARGS[@]}" ; do
133             echo -n "($ARGS) "
134             echo -e "Command and args: $TSHARK $ARGS\n" > $TMP_DIR/$ERR_FILE
135             "$TSHARK" $ARGS $TMP_DIR/$TMP_FILE \
136                 > /dev/null 2>> $TMP_DIR/$ERR_FILE
137             RETVAL=$?
138             if [ $RETVAL -ne 0 ] ; then break ; fi
139         done
140         grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
141             > /dev/null 2>&1 && DISSECTOR_BUG=1
142
143         if [ $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 ] ; then
144             RAND_FILE="randpkt-`$DATE +%Y-%m-%d`-$$.pcap"
145             mv $TMP_DIR/$TMP_FILE $TMP_DIR/$RAND_FILE
146             echo "  Output file: $TMP_DIR/$RAND_FILE"
147
148             exit_error
149         fi
150         echo " OK"
151         rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE
152     done
153 done