5 # Randpkt testing script for TShark
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.
11 # Tweak the following to your liking.
15 # This needs to point to a 'date' that supports %s.
17 BASE_NAME=randpkt-`$DATE +%Y-%m-%d`-$$
19 # Temporary file directory and names.
20 # (had problems with this on cygwin, tried TMP_DIR=./ which worked)
22 TMP_FILE=$BASE_NAME.pcap
23 ERR_FILE=$BASE_NAME.err
25 # Loop this many times (< 1 loops forever)
28 # These may be set to your liking
29 # Stop the child process, if it's running longer than x seconds
31 # Stop the child process, if it's using more than y * 1024 bytes
33 # Trigger an abort if a dissector finds a bug.
34 # Uncomment to disable
35 WIRESHARK_ABORT_ON_DISSECTOR_BUG="True"
37 PKT_TYPES=`$RANDPKT -h | awk '/^\t/ {print $1}'`
39 # To do: add options for file names and limits
40 while getopts ":d:p:t:" OPTCHAR ; do
43 p) MAX_PASSES=$OPTARG ;;
44 t) PKT_TYPES=$OPTARG ;;
47 shift $(($OPTIND - 1))
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
54 ### usually you won't have to change anything below this line ###
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"
65 for i in "$TSHARK" "$RANDPKT" "$DATE" "$TMP_DIR" ; do
67 echo "Couldn't find $i"
71 if [ $NOTFOUND -eq 1 ]; then
76 if [ $MAX_PASSES -gt 0 ]; then
77 HOWMANY="$MAX_PASSES passes"
79 echo -n "Running $TSHARK with args: "
80 printf "\"%s\" " "${TSHARK_ARGS[@]}"
82 echo "Running $RANDPKT with args: $RANDPKT_ARGS"
85 trap "MAX_PASSES=1; echo 'Caught signal'" HUP INT TERM
87 function exit_error() {
89 echo -e "Processing failed. Capture info follows:\n"
90 echo " Input file: $CF"
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
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
109 echo -e "stderr follows:\n"
110 cat $TMP_DIR/$ERR_FILE
115 # Iterate over our capture files.
117 while [ $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 ] ; do
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
125 echo -n " $PKT_TYPE: "
129 "$RANDPKT" $RANDPKT_ARGS -t $PKT_TYPE $TMP_DIR/$TMP_FILE \
132 for ARGS in "${TSHARK_ARGS[@]}" ; do
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
138 if [ $RETVAL -ne 0 ] ; then break ; fi
140 grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
141 > /dev/null 2>&1 && DISSECTOR_BUG=1
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"
151 rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE