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.
10 # shellcheck source=tools/test-common.sh
11 .
"$( dirname "$0" )"/test-common.sh ||
exit 1
13 # Run under valgrind ?
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
32 b
) WIRESHARK_BIN_DIR
=$OPTARG ;;
35 p
) MAX_PASSES
=$OPTARG ;;
36 t
) PKT_TYPES
=$OPTARG ;;
37 *) printf "Unknown option: %s\\n" "$OPTARG"
40 shift $
(( OPTIND
- 1 ))
42 ### usually you won't have to change anything below this line ###
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 ))
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
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 "
73 echo "ASan disabled. Virtual memory limit is $MAX_VMEM"
77 if [ "$MAX_PASSES" -gt 0 ]; then
78 HOWMANY
="$MAX_PASSES passes"
80 echo -n "Running $RUNNER with args: "
81 printf "\"%s\" " "${RUNNER_ARGS[@]}"
83 echo "Running $RANDPKT with args: $RANDPKT_ARGS"
86 # Clean up on <ctrl>C, etc
88 printf '\n\nCaught signal. Exiting.\n'
89 rm -f "$TMP_DIR/$TMP_FILE" "$TMP_DIR/$ERR_FILE"
93 trap trap_all HUP INT TERM ABRT
95 # Iterate over our capture files.
97 while [ $PASS -lt "$MAX_PASSES" ] ||
[ "$MAX_PASSES" -lt 1 ] ; do
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: "
110 # shellcheck disable=SC2086
111 "$RANDPKT" $RANDPKT_ARGS -t "$PKT_TYPE" "$TMP_DIR/$TMP_FILE" \
114 for ARGS
in "${RUNNER_ARGS[@]}" ; do
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
125 ulimit -S -t $MAX_CPU_TIME -s $MAX_STACK
127 # Allow core files to be generated
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"
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."
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"
155 if grep -q "Valgrind cannot continue" "$TMP_DIR/$ERR_FILE" ; then
156 echo "Valgrind unable to continue."
160 if [ $RETVAL -ne 0 ] ; then break ; fi
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
169 rm -f "$TMP_DIR/$TMP_FILE" "$TMP_DIR/$ERR_FILE"