-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-2550 Add fuzzer to bson packages (#1077)
- Loading branch information
1 parent
5fcb147
commit 6f84f7e
Showing
9 changed files
with
241 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/bin/bash | ||
|
||
set -o errexit # Exit the script with error if any of the commands fail | ||
|
||
FUZZTIME=10m | ||
|
||
# Change the working directory to the root of the mongo repository directory | ||
cd $PROJECT_DIRECTORY | ||
|
||
# Get all go test files that contain a fuzz test. | ||
FILES=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .) | ||
|
||
# For each file, run all of the fuzz tests in sequence, each for -fuzztime=FUZZTIME. | ||
for FILE in ${FILES} | ||
do | ||
PARENTDIR="$(dirname -- "$FILE")" | ||
|
||
# Get a list of all fuzz tests in the file. | ||
FUNCS=$(grep -o 'func Fuzz[A-Za-z0-9]*' $FILE | cut -d' ' -f2) | ||
|
||
# For each fuzz test in the file, run it for FUZZTIME. | ||
for FUNC in ${FUNCS} | ||
do | ||
echo "Fuzzing \"${FUNC}\" in \"${FILE}\"" | ||
|
||
# Create a set of directories that are already in the subdirectories testdata/fuzz/$fuzzer corpus. This | ||
# set will be used to differentiate between new and old corpus files. | ||
declare -a cset | ||
|
||
if [ -d $PARENTDIR/testdata/fuzz/$FUNC ]; then | ||
# Iterate over the files in the corpus directory and add them to the set. | ||
for SEED in $PARENTDIR/testdata/fuzz/$FUNC/* | ||
do | ||
cset+=("$SEED") | ||
done | ||
fi | ||
|
||
go test ${PARENTDIR} -run=${FUNC} -fuzz=${FUNC} -fuzztime=${FUZZTIME} || true | ||
|
||
# Check if any new corpus files were generated for the fuzzer. If there are new corpus files, move them | ||
# to $PROJECT_DIRECTORY/fuzz/$FUNC/* so they can be tarred up and uploaded to S3. | ||
if [ -d $PARENTDIR/testdata/fuzz/$FUNC ]; then | ||
# Iterate over the files in the corpus directory and check if they are in the set. | ||
for CORPUS_FILE in $PARENTDIR/testdata/fuzz/$FUNC/* | ||
do | ||
# Check to see if the value for CORPUS_FILE is in cset. | ||
if [[ ! " ${cset[@]} " =~ " ${CORPUS_FILE} " ]]; then | ||
# Create the directory if it doesn't exist. | ||
if [ ! -d $PROJECT_DIRECTORY/fuzz/$FUNC ]; then | ||
mkdir -p $PROJECT_DIRECTORY/fuzz/$FUNC | ||
fi | ||
|
||
# Move the file to the directory. | ||
mv $CORPUS_FILE $PROJECT_DIRECTORY/fuzz/$FUNC | ||
|
||
echo "Moved $CORPUS_FILE to $PROJECT_DIRECTORY/fuzz/$FUNC" | ||
fi | ||
done | ||
fi | ||
done | ||
done | ||
|
||
# If the fuzz directory exists, then tar it up in preparation to upload to S3. | ||
if [ -d $PROJECT_DIRECTORY/fuzz ]; then | ||
echo "Tarring up fuzz directory" | ||
tar -czf $PROJECT_DIRECTORY/fuzz.tgz $PROJECT_DIRECTORY/fuzz | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package bson | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func FuzzDecode(f *testing.F) { | ||
seedBSONCorpus(f) | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
for _, typ := range []func() interface{}{ | ||
func() interface{} { return new(D) }, | ||
func() interface{} { return new([]E) }, | ||
func() interface{} { return new(M) }, | ||
func() interface{} { return new(interface{}) }, | ||
func() interface{} { return make(map[string]interface{}) }, | ||
func() interface{} { return new([]interface{}) }, | ||
} { | ||
i := typ() | ||
if err := Unmarshal(data, i); err != nil { | ||
return | ||
} | ||
|
||
encoded, err := Marshal(i) | ||
if err != nil { | ||
t.Fatal("failed to marshal", err) | ||
} | ||
|
||
if err := Unmarshal(encoded, i); err != nil { | ||
t.Fatal("failed to unmarshal", err) | ||
} | ||
} | ||
}) | ||
} |
2 changes: 2 additions & 0 deletions
2
...testdata/fuzz/FuzzDecode/002ae7d43f636100116fede772a03d07726ed75c3c3b83da865fe9b718adf8ae
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("\x10\x00\x00\x00\v\x00\x00\x00\b\x00\x00\v\x00\x00\x00\x00") |
2 changes: 2 additions & 0 deletions
2
...testdata/fuzz/FuzzDecode/0de854041b0055ca1e5e6e54a7fb667ed38461db171af267665c21776f9a9ef4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("0\\x00\\x00\\x00\\x0f\\x00000\\x8a00000000000000000000000000000000000000\n") |
2 changes: 2 additions & 0 deletions
2
...testdata/fuzz/FuzzDecode/718592474a0a3626039f3471449b9aa374c746754d4925fcfe4ba747e7101504
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("\\x80\\x00\\x00\\x00\\x03000000\\x00s\\x00\\x00\\x00\\x0300000\\x00g\\x00\\x00\\x00\\x100z\\x000000\\x11\\x00000\\x150000\\x020\\x00\\x02\\x00\\x00\\x000\\x12\\x00\\x050\\x00\\x01\\x00\\x00\\x0000\\x050\\x00\\x01\\x00\\x00\\x0000\\x040\\x00200000\\x00\\x000\\x02\\x00\\x10\\x0000000\\x110\\x0000000000\\x020\\x00\\x02\\x00\\x00\\x000\\x00\\x050\\x00\\x01\\x00\\x00\\x0000\\x050\\x00\\x01\\x00\\x00\\x0000\\x00\\x00\\x00\\x00\n") |
2 changes: 2 additions & 0 deletions
2
...testdata/fuzz/FuzzDecode/93c43e3c1cf35c19b7618a618d128cea0ce05cef0711fdd91e403fe3b2f45628
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("\\x59\\x01\\x00\\x00\\x01\\x64\\x6f\\x75\\x62\\x6c\\x65\\x00\\x9a\\x99\\x99\\x99\\x99\\x99\\xf1\\x3f\\x02\\x73\\x74\\x72\\x69\\x6e\\x67\\x00\\x06\\x00\\x00\\x00\\x68\\x65\\x6c\\x6c\\x6f\\x00\\x03\\x65\\x6d\\x62\\x65\\x64\\x64\\x65\\x64\\x00\\x4b\\x00\\x00\\x00\\x04\\x61\\x72\\x72\\x61\\x79\\x00\\x3f\\x00\\x00\\x00\\x10\\x30\\x00\\x01\\x00\\x00\\x00\\x01\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x02\\x32\\x00\\x02\\x00\\x00\\x00\\x33\\x00\\x04\\x33\\x00\\x0c\\x00\\x00\\x00\\x10\\x30\\x00\\x04\\x00\\x00\\x00\\x00\\x03\\x34\\x00\\x0d\\x00\\x00\\x00\\x03\\x35\\x00\\x05\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\x62\\x69\\x6e\\x61\\x72\\x79\\x00\\x03\\x00\\x00\\x00\\x00\\x01\\x02\\x03\\x07\\x6f\\x62\\x6a\\x65\\x63\\x74\\x69\\x64\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x62\\x6f\\x6f\\x6c\\x65\\x61\\x6e\\x00\\x01\\x09\\x64\\x61\\x74\\x65\\x74\\x69\\x6d\\x65\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0a\\x6e\\x75\\x6c\\x6c\\x00\\x0b\\x72\\x65\\x67\\x65\\x78\\x00\\x68\\x65\\x6c\\x6c\\x6f\\x00\\x69\\x00\\x0d\\x6a\\x73\\x00\\x0e\\x00\\x00\\x00\\x66\\x75\\x6e\\x63\\x74\\x69\\x6f\\x6e\\x28\\x29\\x20\\x7b\\x7d\\x00\\x0f\\x73\\x63\\x6f\\x70\\x65\\x00\\x2c\\x00\\x00\\x00\\x0e\\x00\\x00\\x00\\x66\\x75\\x6e\\x63\\x74\\x69\\x6f\\x6e\\x28\\x29\\x20\\x7b\\x7d\\x00\\x16\\x00\\x00\\x00\\x02\\x68\\x65\\x6c\\x6c\\x6f\\x00\\x06\\x00\\x00\\x00\\x77\\x6f\\x72\\x6c\\x64\\x00\\x00\\x10\\x69\\x6e\\x74\\x33\\x32\\x00\\x20\\x00\\x00\\x00\\x11\\x74\\x69\\x6d\\x65\\x73\\x74\\x61\\x6d\\x70\\x00\\x02\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x12\\x69\\x6e\\x74\\x36\\x34\\x00\\x40\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xff\\x6d\\x69\\x6e\\x6b\\x65\\x79\\x00\\x7f\\x6d\\x61\\x78\\x6b\\x65\\x79\\x00\\x00\"\n") |
2 changes: 2 additions & 0 deletions
2
...testdata/fuzz/FuzzDecode/c3ffbb42eb85b743ede396f00b7706e6ad0529c32689c63ca663dae37d072627
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
go test fuzz v1 | ||
[]byte("\\x05\\xf0\\xff\\x00\\x7f\n") |