chore: types audit (backport #14258) (#14262)

Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
mergify[bot] 2022-12-12 19:06:15 +01:00 committed by GitHub
parent 3a51859b9f
commit 159c868fa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 27 deletions

2
go.mod
View File

@ -26,6 +26,7 @@ require (
github.com/cosmos/ledger-cosmos-go v0.12.1
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/gofuzz v1.2.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
@ -102,7 +103,6 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect

File diff suppressed because one or more lines are too long

View File

@ -33,7 +33,6 @@ func AddressGenerator(t *rapid.T) *rapid.Generator[sdk.AccAddress] {
}
func testMempoolProperties(t *rapid.T) {
ctx := sdk.NewContext(nil, tmproto.Header{}, false, log.NewNopLogger())
mp := mempool.NewSenderNonceMempool()

View File

@ -166,7 +166,6 @@ func (s *MempoolTestSuite) TestMaxTx() {
ctx = ctx.WithPriority(tx.priority)
err = mp.Insert(ctx, tx2)
require.Equal(t, mempool.ErrMempoolTxMaxCapacity, err)
}
func (s *MempoolTestSuite) TestTxNotFoundOnSender() {
@ -192,5 +191,4 @@ func (s *MempoolTestSuite) TestTxNotFoundOnSender() {
require.NoError(t, err)
err = mp.Remove(tx)
require.Equal(t, mempool.ErrTxNotFound, err)
}

97
types/query/fuzz_test.go Normal file
View File

@ -0,0 +1,97 @@
package query_test
import (
"fmt"
"testing"
"cosmossdk.io/math"
fuzz "github.com/google/gofuzz"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
type fuzzTestSuite struct {
paginationTestSuite
}
func FuzzPagination(f *testing.F) {
if testing.Short() {
f.Skip("In -short mode")
}
suite := new(fuzzTestSuite)
suite.SetT(new(testing.T))
suite.SetupTest()
gf := fuzz.New()
// 1. Set up some seeds.
seeds := []*query.PageRequest{
new(query.PageRequest),
{
Offset: 0,
Limit: 10,
},
}
// 1.5. Use the inprocess fuzzer to mutate variables.
for i := 0; i < 1000; i++ {
qr := new(query.PageRequest)
gf.Fuzz(qr)
seeds = append(seeds, qr)
}
// 2. Now serialize the fuzzers to bytes so that future mutations
// can occur.
for _, seed := range seeds {
seedBlob, err := suite.cdc.Marshal(seed)
if err == nil { // Some seeds could have been invalid so only add those that marshal.
f.Add(seedBlob)
}
}
// 3. Setup the keystore.
var balances sdk.Coins
for i := 0; i < 5; i++ {
denom := fmt.Sprintf("foo%ddenom", i)
balances = append(balances, sdk.NewInt64Coin(denom, int64(100+i)))
}
balances = balances.Sort()
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr1)
suite.accountKeeper.SetAccount(suite.ctx, acc1)
err := testutil.FundAccount(suite.bankKeeper, suite.ctx, addr1, balances)
if err != nil { // should return no error
f.Fatal(err)
}
// 4. Now run that fuzzer!
f.Fuzz(func(t *testing.T, pagBz []byte) {
qr := new(query.PageRequest)
if err := suite.cdc.Unmarshal(pagBz, qr); err != nil {
// Some pagination requests won't unmarshal and that's okay.
return
}
// Now try to paginate it.
req := types.NewQueryAllBalancesRequest(addr1, qr)
balResult := sdk.NewCoins()
authStore := suite.ctx.KVStore(suite.app.UnsafeFindStoreKey(types.StoreKey))
balancesStore := prefix.NewStore(authStore, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, address.MustLengthPrefix(addr1))
_, _ = query.Paginate(accountStore, req.Pagination, func(key []byte, value []byte) error {
var amount math.Int
err := amount.Unmarshal(value)
if err != nil {
return err
}
balResult = append(balResult, sdk.NewCoin(string(key), amount))
return nil
})
})
}

View File

@ -4,24 +4,6 @@ import (
"regexp"
)
var (
// IsAlphaNumeric defines a regular expression for matching against alpha-numeric
// values.
IsAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
// IsAlphaLower defines regular expression to check if the string has lowercase
// alphabetic characters only.
IsAlphaLower = regexp.MustCompile(`^[a-z]+$`).MatchString
// IsAlphaUpper defines regular expression to check if the string has uppercase
// alphabetic characters only.
IsAlphaUpper = regexp.MustCompile(`^[A-Z]+$`).MatchString
// IsAlpha defines regular expression to check if the string has alphabetic
// characters only.
IsAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
// IsNumeric defines regular expression to check if the string has numeric
// characters only.
IsNumeric = regexp.MustCompile(`^[0-9]+$`).MatchString
)
// IsAlphaNumeric defines a regular expression for matching against alpha-numeric
// values.
var IsAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString