Initial fuzz test

This commit is contained in:
Alex Peters
2020-06-12 14:53:10 +02:00
parent 7f7afbf7d5
commit 077d99dbc2
3 changed files with 123 additions and 0 deletions

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect
github.com/cosmos/cosmos-sdk v0.38.3
github.com/golang/mock v1.4.3 // indirect
github.com/google/gofuzz v1.0.0
github.com/gorilla/mux v1.7.4
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect

View File

@@ -0,0 +1,88 @@
package keeper
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/internal/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/staking"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
)
func TestGenesisExportImport(t *testing.T) {
srcKeeper, srcCtx := setupKeeper(t)
wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm")
require.NoError(t, err)
f := fuzz.New().Funcs(FuzzAddr, FuzzAbsoluteTxPosition, FuzzContractInfo, FuzzStateModel)
for i := 0; i < 20; i++ {
var (
codeInfo types.CodeInfo
contract types.ContractInfo
stateModels []types.Model
)
f.Fuzz(&codeInfo)
f.Fuzz(&contract)
f.Fuzz(&stateModels)
codeID, err := srcKeeper.Create(srcCtx, codeInfo.Creator, wasmCode, codeInfo.Source, codeInfo.Builder)
require.NoError(t, err)
contract.CodeID = codeID
contractAddr := srcKeeper.generateContractAddress(srcCtx, codeID)
srcKeeper.setContractInfo(srcCtx, contractAddr, &contract)
srcKeeper.setContractState(srcCtx, contractAddr, stateModels)
}
// export
genesisState := ExportGenesis(srcCtx, srcKeeper)
// re-import
dstKeeper, dstCtx := setupKeeper(t)
InitGenesis(dstCtx, dstKeeper, genesisState)
// compare whole DB
srcIT := srcCtx.KVStore(srcKeeper.storeKey).Iterator(nil, nil)
dstIT := dstCtx.KVStore(dstKeeper.storeKey).Iterator(nil, nil)
for i := 0; srcIT.Valid(); i++ {
require.True(t, dstIT.Valid(), "destination DB has less elements than source. Missing: %q", srcIT.Key())
require.Equal(t, srcIT.Key(), dstIT.Key(), i)
require.Equal(t, srcIT.Value(), dstIT.Value(), i)
srcIT.Next()
dstIT.Next()
}
require.False(t, dstIT.Valid())
}
func setupKeeper(t *testing.T) (Keeper, sdk.Context) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tempDir) })
keyContract := sdk.NewKVStoreKey(wasmTypes.StoreKey)
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(keyContract, sdk.StoreTypeIAVL, db)
require.NoError(t, ms.LoadLatestVersion())
ctx := sdk.NewContext(ms, abci.Header{
Height: 1234567,
Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC),
}, false, log.NewNopLogger())
cdc := MakeTestCodec()
wasmConfig := wasmTypes.DefaultWasmConfig()
srcKeeper := NewKeeper(cdc, keyContract, auth.AccountKeeper{}, nil, staking.Keeper{}, nil, tempDir, wasmConfig, "", nil, nil)
return srcKeeper, ctx
}

View File

@@ -0,0 +1,34 @@
package keeper
import (
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
sdk "github.com/cosmos/cosmos-sdk/types"
fuzz "github.com/google/gofuzz"
tmBytes "github.com/tendermint/tendermint/libs/bytes"
)
func FuzzAddr(m *sdk.AccAddress, c fuzz.Continue) {
*m = make([]byte, 20)
c.Read(*m)
}
func FuzzAbsoluteTxPosition(m *types.AbsoluteTxPosition, c fuzz.Continue) {
m.BlockHeight = int64(c.RandUint64()) // can't be negative
m.TxIndex = c.RandUint64()
}
func FuzzContractInfo(m *types.ContractInfo, c fuzz.Continue) {
const maxSize = 1024
m.CodeID = c.RandUint64()
FuzzAddr(&m.Creator, c)
FuzzAddr(&m.Admin, c)
m.Label = c.RandString()
m.InitMsg = make([]byte, c.RandUint64()%maxSize)
c.Read(m.InitMsg)
c.Fuzz(&m.Created)
c.Fuzz(&m.LastUpdated)
m.PreviousCodeID = c.RandUint64()
}
func FuzzStateModel(m *types.Model, c fuzz.Continue) {
m.Key = tmBytes.HexBytes(c.RandString())
c.Fuzz(&m.Value)
}