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