regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / tools / randpkt-test.sh
bloba49e3002e320c83034bfe7691ff45f5babcd910d
1 #!/bin/bash
3 # Randpkt testing script for TShark
5 # This script uses Randpkt to generate capture files with randomized
6 # content. It runs TShark on each generated file and checks for errors.
7 # The files are processed repeatedly until an error is found.
9 TEST_TYPE="randpkt"
10 # shellcheck source=tools/test-common.sh
11 . "$( dirname "$0" )"/test-common.sh || exit 1
13 # Run under valgrind ?
14 VALGRIND=0
16 # Run under AddressSanitizer ?
17 ASAN=$CONFIGURED_WITH_ASAN
19 # Trigger an abort if a dissector finds a bug.
20 # Uncomment to disable
21 export WIRESHARK_ABORT_ON_DISSECTOR_BUG="True"
23 # The maximum permitted amount of memory leaked. Eventually this should be
24 # worked down to zero, but right now that would fail on every single capture.
25 # Only has effect when running under valgrind.
26 MAX_LEAK=$(( 1024 * 100 ))
28 # To do: add options for file names and limits
29 while getopts "ab:d:gp:t:" OPTCHAR ; do
30 case $OPTCHAR in
31 a) ASAN=1 ;;
32 b) WIRESHARK_BIN_DIR=$OPTARG ;;
33 d) TMP_DIR=$OPTARG ;;
34 g) VALGRIND=1 ;;
35 p) MAX_PASSES=$OPTARG ;;
36 t) PKT_TYPES=$OPTARG ;;
37 *) printf "Unknown option: %s\\n" "$OPTARG"
38 esac
39 done
40 shift $(( OPTIND - 1 ))
42 ### usually you won't have to change anything below this line ###
44 ws_bind_exec_paths
45 ws_check_exec "$TSHARK" "$RANDPKT" "$DATE" "$TMP_DIR"
47 [[ -z "$PKT_TYPES" ]] && PKT_TYPES=$($RANDPKT -h | awk '/^\t/ {print $1}')
49 if [ $VALGRIND -eq 1 ]; then
50 RUNNER="$( dirname "$0" )/valgrind-wireshark.sh"
51 COMMON_ARGS="-b $WIRESHARK_BIN_DIR $COMMON_ARGS"
52 declare -a RUNNER_ARGS=("" "-T")
53 # Valgrind requires more resources, so permit 1.5x memory and 3x time
54 # (1.5x time is too small for a few large captures in the menagerie)
55 MAX_CPU_TIME=$(( 3 * "$MAX_CPU_TIME" ))
56 MAX_VMEM=$(( 3 * "$MAX_VMEM" / 2 ))
57 else
58 # Not using valgrind, use regular tshark.
59 # TShark arguments (you won't have to change these)
60 # n Disable network object name resolution
61 # V Print a view of the details of the packet rather than a one-line summary of the packet
62 # x Cause TShark to print a hex and ASCII dump of the packet data after printing the summary or details
63 # r Read packet data from the following infile
64 RUNNER="$TSHARK"
65 declare -a RUNNER_ARGS=("-nVxr" "-nr")
67 RANDPKT_ARGS="-b 2000 -c 5000"
69 if [ $ASAN -ne 0 ]; then
70 echo -n "ASan enabled. Virtual memory limit is "
71 ulimit -v
72 else
73 echo "ASan disabled. Virtual memory limit is $MAX_VMEM"
76 HOWMANY="forever"
77 if [ "$MAX_PASSES" -gt 0 ]; then
78 HOWMANY="$MAX_PASSES passes"
80 echo -n "Running $RUNNER with args: "
81 printf "\"%s\" " "${RUNNER_ARGS[@]}"
82 echo "($HOWMANY)"
83 echo "Running $RANDPKT with args: $RANDPKT_ARGS"
84 echo ""
86 # Clean up on <ctrl>C, etc
87 trap_all() {
88 printf '\n\nCaught signal. Exiting.\n'
89 rm -f "$TMP_DIR/$TMP_FILE" "$TMP_DIR/$ERR_FILE"
90 exit 0
93 trap trap_all HUP INT TERM ABRT
95 # Iterate over our capture files.
96 PASS=0
97 while [ $PASS -lt "$MAX_PASSES" ] || [ "$MAX_PASSES" -lt 1 ] ; do
98 PASS=$(( PASS + 1 ))
99 echo "Pass $PASS:"
101 for PKT_TYPE in $PKT_TYPES ; do
102 if [ $PASS -gt "$MAX_PASSES" ] && [ "$MAX_PASSES" -ge 1 ] ; then
103 break # We caught a signal
105 echo -n " $PKT_TYPE: "
107 DISSECTOR_BUG=0
108 VG_ERR_CNT=0
110 # shellcheck disable=SC2086
111 "$RANDPKT" $RANDPKT_ARGS -t "$PKT_TYPE" "$TMP_DIR/$TMP_FILE" \
112 > /dev/null 2>&1
114 for ARGS in "${RUNNER_ARGS[@]}" ; do
115 echo -n "($ARGS) "
116 echo -e "Command and args: $RUNNER $ARGS\\n" > "$TMP_DIR/$ERR_FILE"
118 # Run in a child process with limits.
120 # Set some limits to the child processes, e.g. stop it if
121 # it's running longer than MAX_CPU_TIME seconds. (ulimit
122 # is not supported well on cygwin - it shows some warnings -
123 # and the features we use may not all be supported on some
124 # UN*X platforms.)
125 ulimit -S -t $MAX_CPU_TIME -s $MAX_STACK
127 # Allow core files to be generated
128 ulimit -c unlimited
130 # Don't enable ulimit -v when using ASAN. See
131 # https://github.com/google/sanitizers/wiki/AddressSanitizer#ulimit--v
132 if [ $ASAN -eq 0 ]; then
133 ulimit -S -v $MAX_VMEM
136 # shellcheck disable=SC2086
137 "$RUNNER" $ARGS "$TMP_DIR/$TMP_FILE" \
138 > /dev/null 2>> "$TMP_DIR/$ERR_FILE"
140 RETVAL=$?
142 if [ $VALGRIND -eq 1 ]; then
143 VG_ERR_CNT=$( grep "ERROR SUMMARY:" "$TMP_DIR/$ERR_FILE" | cut -f4 -d' ' )
144 VG_DEF_LEAKED=$( grep "definitely lost:" "$TMP_DIR/$ERR_FILE" | cut -f7 -d' ' | tr -d , )
145 VG_IND_LEAKED=$( grep "indirectly lost:" "$TMP_DIR/$ERR_FILE" | cut -f7 -d' ' | tr -d , )
146 VG_TOTAL_LEAKED=$(( "$VG_DEF_LEAKED" + "$VG_IND_LEAKED" ))
147 if [ $RETVAL -ne 0 ] ; then
148 echo "General Valgrind failure."
149 VG_ERR_CNT=1
150 elif [ "$VG_TOTAL_LEAKED" -gt "$MAX_LEAK" ] ; then
151 echo "Definitely + indirectly ($VG_DEF_LEAKED + $VG_IND_LEAKED) exceeds max ($MAX_LEAK)."
152 echo "Definitely + indirectly ($VG_DEF_LEAKED + $VG_IND_LEAKED) exceeds max ($MAX_LEAK)." >> "$TMP_DIR/$ERR_FILE"
153 VG_ERR_CNT=1
155 if grep -q "Valgrind cannot continue" "$TMP_DIR/$ERR_FILE" ; then
156 echo "Valgrind unable to continue."
157 VG_ERR_CNT=-1
160 if [ $RETVAL -ne 0 ] ; then break ; fi
161 done
162 grep -i "dissector bug" "$TMP_DIR/$ERR_FILE" \
163 > /dev/null 2>&1 && DISSECTOR_BUG=1
165 if [ "$RETVAL" -ne 0 ] || [ $DISSECTOR_BUG -ne 0 ] || [ "$VG_ERR_CNT" -ne 0 ] ; then
166 ws_exit_error
168 echo " OK"
169 rm -f "$TMP_DIR/$TMP_FILE" "$TMP_DIR/$ERR_FILE"
170 done
171 done