github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/test_fuzz.go (about)

     1  package keeper
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	tmBytes "github.com/fibonacci-chain/fbc/libs/tendermint/libs/bytes"
     8  	fuzz "github.com/google/gofuzz"
     9  
    10  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    11  )
    12  
    13  var ModelFuzzers = []interface{}{FuzzAddr, FuzzAddrString, FuzzAbsoluteTxPosition, FuzzContractInfo, FuzzStateModel, FuzzAccessType, FuzzAccessConfig, FuzzContractCodeHistory}
    14  
    15  func FuzzAddr(m *sdk.AccAddress, c fuzz.Continue) {
    16  	*m = make([]byte, 20)
    17  	c.Read(*m)
    18  }
    19  
    20  func FuzzAddrString(m *string, c fuzz.Continue) {
    21  	var x sdk.AccAddress
    22  	FuzzAddr(&x, c)
    23  	*m = x.String()
    24  }
    25  
    26  func FuzzAbsoluteTxPosition(m *types.AbsoluteTxPosition, c fuzz.Continue) {
    27  	m.BlockHeight = c.RandUint64()
    28  	m.TxIndex = c.RandUint64()
    29  }
    30  
    31  func FuzzContractInfo(m *types.ContractInfo, c fuzz.Continue) {
    32  	m.CodeID = c.RandUint64()
    33  	FuzzAddrString(&m.Creator, c)
    34  	FuzzAddrString(&m.Admin, c)
    35  	m.Label = c.RandString()
    36  	c.Fuzz(&m.Created)
    37  }
    38  
    39  func FuzzContractCodeHistory(m *types.ContractCodeHistoryEntry, c fuzz.Continue) {
    40  	const maxMsgSize = 128
    41  	m.CodeID = c.RandUint64()
    42  	msg := make([]byte, c.RandUint64()%maxMsgSize)
    43  	c.Read(msg)
    44  	var err error
    45  	if m.Msg, err = json.Marshal(msg); err != nil {
    46  		panic(err)
    47  	}
    48  	c.Fuzz(&m.Updated)
    49  	m.Operation = types.AllCodeHistoryTypes[c.Int()%len(types.AllCodeHistoryTypes)]
    50  }
    51  
    52  func FuzzStateModel(m *types.Model, c fuzz.Continue) {
    53  	m.Key = tmBytes.HexBytes(c.RandString())
    54  	if len(m.Key) == 0 {
    55  		m.Key = tmBytes.HexBytes("non empty key")
    56  	}
    57  	c.Fuzz(&m.Value)
    58  }
    59  
    60  func FuzzAccessType(m *types.AccessType, c fuzz.Continue) {
    61  	pos := c.Int() % len(types.AllAccessTypes)
    62  	for _, v := range types.AllAccessTypes {
    63  		if pos == 0 {
    64  			*m = v
    65  			return
    66  		}
    67  		pos--
    68  	}
    69  }
    70  
    71  func FuzzAccessConfig(m *types.AccessConfig, c fuzz.Continue) {
    72  	FuzzAccessType(&m.Permission, c)
    73  	var add sdk.AccAddress
    74  	FuzzAddr(&add, c)
    75  	*m = m.Permission.With(add)
    76  }