-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: tx fuzz testing #311
base: main
Are you sure you want to change the base?
WIP: tx fuzz testing #311
Conversation
@@ -1,4 +1,4 @@ | |||
PACKAGES=$(shell go list ./... | grep -v '/simulation') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation mentions that in order to get access to the beta fuzz feature you need to install gotip and use that in place of the go command:
$ go get golang.org/dl/gotip
$ gotip download dev.fuzz
@@ -79,6 +84,102 @@ func TestAddToOutgoingPool(t *testing.T) { | |||
assert.Equal(t, exp, got) | |||
} | |||
|
|||
func createSeedUint64ByteArrayWithValue(numElts int, value uint64) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fuzzing currently doesn't support typed arrays, only byte arrays. These methods make it easy to "seed" the fuzzer with example data to work off of
|
||
func FuzzAddToOutgoingPool(f *testing.F) { | ||
numInputs := 6 | ||
ones := createSeedUint64ByteArrayWithValue(numInputs, uint64(1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create some example uint64 arrays. I wanted to start out with fees all equal to 1, and amounts all equal to 100
ones := createSeedUint64ByteArrayWithValue(numInputs, uint64(1)) | ||
oneHundreds := createSeedUint64ByteArrayWithValue(numInputs, uint64(100)) | ||
|
||
f.Add(ones, oneHundreds, "0000000000000000000000000000000000000000") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Add function creates a single point of seed input into the fuzzer. I don't fully understand how it will use the seed data, but I'm guessing it just speeds up the time it takes to randomly generate acceptable inputs
|
||
f.Add(ones, oneHundreds, "0000000000000000000000000000000000000000") | ||
f.Fuzz(func(t *testing.T, feez []byte, amountz []byte, contractAddr string) { | ||
if types.ValidateEthAddress(contractAddr) != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plenty of the input strings were too short, if the fuzzer generates a bad string then we just skip with t.Skip()
amounts[j] = binary.BigEndian.Uint64(amountz[64*j : 64*(j+1)]) | ||
} | ||
|
||
input := CreateTestEnv(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From this point onward this test is basically a copy of the TestAddToOutgoingPool test above it
for i, _ := range fees { | ||
txAmt := amounts[i] | ||
txFee := fees[i] | ||
if uint64(txAmt+txFee) >= balance { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the account doesn't have enough balance (starts with uint64 max) to cover this transaction, we modify the amount and fee to make the transaction work out. Later on we check that there are a correct number of transactions created so I didn't want to just skip.
@@ -0,0 +1,4 @@ | |||
go test fuzz v1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file was generated automatically when I ran gotip test -fuzz=FuzzAddToOutgoingPool
in the terminal. This is somehow related to the fuzzer's seed method f.Add I mentioned above, but I'm not sure how it gets generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be git-ignored
2812722
to
dd6b02b
Compare
dd6b02b
to
9c05fcc
Compare
Example of a Fuzz test using the recently released (June 3 2021) beta fuzz testing mechanism