Skip to content

Commit

Permalink
Add initial fuzz tests (#2216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Nov 15, 2022
1 parent 91c5e26 commit 680bf89
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ if [[ -z $(git status -s) ]]; then
# exit 1
fi
"$AVALANCHE_PATH"/scripts/build_test.sh
"$AVALANCHE_PATH"/scripts/build_fuzz.sh
25 changes: 25 additions & 0 deletions scripts/build_fuzz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# Mostly taken from https://github.com/golang/go/issues/46312#issuecomment-1153345129

fuzzTime=${1:-1}
files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
failed=false
for file in ${files}
do
funcs=$(grep -oP 'func \K(Fuzz\w*)' $file)
for func in ${funcs}
do
echo "Fuzzing $func in $file"
parentDir=$(dirname $file)
go test $parentDir -run=$func -fuzz=$func -fuzztime=${fuzzTime}s
# If any of the fuzz tests fail, return exit code 1
if [ $? -ne 0 ]; then
failed=true
fi
done
done

if $failed; then
exit 1
fi
16 changes: 16 additions & 0 deletions utils/cb58/cb58_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ func TestEncodeDecode(t *testing.T) {
}
}
}

func FuzzEncodeDecode(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
require := require.New(t)

// Encode bytes to string
dataStr, err := Encode(data)
require.NoError(err)

// Decode string to bytes
gotData, err := Decode(dataStr)
require.NoError(err)

require.Equal(data, gotData)
})
}
21 changes: 21 additions & 0 deletions utils/compression/gzip_compressor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,24 @@ func TestNewGzipCompressorWithInvalidLimit(t *testing.T) {
_, err := NewGzipCompressor(math.MaxInt64)
require.ErrorIs(err, ErrInvalidMaxSizeGzipCompressor)
}

func FuzzGzipCompressor(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
require := require.New(t)

if len(data) > 2*units.MiB {
t.SkipNow()
}

compressor, err := NewGzipCompressor(2 * units.MiB)
require.NoError(err)

compressed, err := compressor.Compress(data)
require.NoError(err)

decompressed, err := compressor.Decompress(compressed)
require.NoError(err)

require.Equal(data, decompressed)
})
}
14 changes: 14 additions & 0 deletions utils/formatting/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,17 @@ func TestDecodeNil(t *testing.T) {
t.Fatal("decoding the empty string should return an empty byte slice")
}
}

func FuzzEncodeDecode(f *testing.F) {
f.Fuzz(func(t *testing.T, bytes []byte) {
require := require.New(t)

str, err := Encode(Hex, bytes)
require.NoError(err)

decoded, err := Decode(Hex, str)
require.NoError(err)

require.Equal(bytes, decoded)
})
}

0 comments on commit 680bf89

Please sign in to comment.