...
1#!/bin/bash
2
3set -o errexit # Exit the script with error if any of the commands fail
4
5FUZZTIME=10m
6
7# Change the working directory to the root of the mongo repository directory
8cd $PROJECT_DIRECTORY
9
10# Get all go test files that contain a fuzz test.
11FILES=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
12
13# For each file, run all of the fuzz tests in sequence, each for -fuzztime=FUZZTIME.
14for FILE in ${FILES}
15do
16 PARENTDIR="$(dirname -- "$FILE")"
17
18 # Get a list of all fuzz tests in the file.
19 FUNCS=$(grep -o 'func Fuzz[A-Za-z0-9]*' $FILE | cut -d' ' -f2)
20
21 # For each fuzz test in the file, run it for FUZZTIME.
22 for FUNC in ${FUNCS}
23 do
24 echo "Fuzzing \"${FUNC}\" in \"${FILE}\""
25
26 # Create a set of directories that are already in the subdirectories testdata/fuzz/$fuzzer corpus. This
27 # set will be used to differentiate between new and old corpus files.
28 declare -a cset
29
30 if [ -d $PARENTDIR/testdata/fuzz/$FUNC ]; then
31 # Iterate over the files in the corpus directory and add them to the set.
32 for SEED in $PARENTDIR/testdata/fuzz/$FUNC/*
33 do
34 cset+=("$SEED")
35 done
36 fi
37
38 go test ${PARENTDIR} -run=${FUNC} -fuzz=${FUNC} -fuzztime=${FUZZTIME} || true
39
40 # Check if any new corpus files were generated for the fuzzer. If there are new corpus files, move them
41 # to $PROJECT_DIRECTORY/fuzz/$FUNC/* so they can be tarred up and uploaded to S3.
42 if [ -d $PARENTDIR/testdata/fuzz/$FUNC ]; then
43 # Iterate over the files in the corpus directory and check if they are in the set.
44 for CORPUS_FILE in $PARENTDIR/testdata/fuzz/$FUNC/*
45 do
46 # Check to see if the value for CORPUS_FILE is in cset.
47 if [[ ! " ${cset[*]} " =~ " ${CORPUS_FILE} " ]]; then
48 # Create the directory if it doesn't exist.
49 if [ ! -d $PROJECT_DIRECTORY/fuzz/$FUNC ]; then
50 mkdir -p $PROJECT_DIRECTORY/fuzz/$FUNC
51 fi
52
53 # Move the file to the directory.
54 mv $CORPUS_FILE $PROJECT_DIRECTORY/fuzz/$FUNC
55
56 echo "Moved $CORPUS_FILE to $PROJECT_DIRECTORY/fuzz/$FUNC"
57 fi
58 done
59 fi
60 done
61done
62
63# If the fuzz directory exists, then tar it up in preparation to upload to S3.
64if [ -d $PROJECT_DIRECTORY/fuzz ]; then
65 echo "Tarring up fuzz directory"
66 tar -czf $PROJECT_DIRECTORY/fuzz.tgz $PROJECT_DIRECTORY/fuzz
67
68 # Exit with code 1 to indicate that errors occurred in fuzz tests, resulting in corpus files being generated.
69 # This will trigger a notification to be sent to the Go Driver team.
70 exit 1
71fi
View as plain text