Skip to content

Commit

Permalink
test(client): add a fuzz test for QueryBalance (#15763)
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Apr 13, 2023
1 parent 491cf3b commit c3fd0e7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
60 changes: 60 additions & 0 deletions client/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package client_test

import (
"context"
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

type fuzzSuite struct {
IntegrationTestSuite
}

func (fz *fuzzSuite) FuzzQueryBalance(f *testing.F) {
if testing.Short() {
f.Skip("In -short mode")
}

// gRPC query to test service should work
testRes, err := fz.testClient.Echo(context.Background(), &testdata.EchoRequest{Message: "hello"})
fz.Require().NoError(err)
fz.Require().Equal("hello", testRes.Message)

// 1. Generate some seeds.
bz, err := fz.cdc.Marshal(&types.QueryBalanceRequest{
Address: fz.genesisAccount.GetAddress().String(),
Denom: sdk.DefaultBondDenom,
})
fz.Require().NoError(err)
f.Add(bz)

// 2. Now fuzz it and ensure that we don't get any panics.
ctx := context.Background()
f.Fuzz(func(t *testing.T, in []byte) {
qbReq := new(types.QueryBalanceRequest)
if err := fz.cdc.Unmarshal(in, qbReq); err != nil {
return
}

// gRPC query to bank service should work
var header metadata.MD
_, _ = fz.bankClient.Balance(
ctx,
qbReq,
grpc.Header(&header),
)
})
}

func FuzzQueryBalance(f *testing.F) {
fzs := new(fuzzSuite)
fzs.SetT(new(testing.T))
fzs.SetupSuite()
fzs.FuzzQueryBalance(f)
}
2 changes: 2 additions & 0 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type IntegrationTestSuite struct {
suite.Suite

ctx sdk.Context
cdc codec.Codec
genesisAccount *authtypes.BaseAccount
bankClient types.QueryClient
testClient testdata.QueryClient
Expand Down Expand Up @@ -96,6 +97,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
// end of app init

s.ctx = app.BaseApp.NewContext(false, cmtproto.Header{})
s.cdc = cdc
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, interfaceRegistry)
types.RegisterQueryServer(queryHelper, bankKeeper)
testdata.RegisterQueryServer(queryHelper, testdata.QueryImpl{})
Expand Down
2 changes: 2 additions & 0 deletions client/testdata/fuzz/FuzzQueryBalance/4bf93f2225e526ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\n-cosmos1wrq8cagsama0xwf2vmlzgrkyynfsxuyhturvyz\x12\x0500000")

0 comments on commit c3fd0e7

Please sign in to comment.