Skip to content

Commit f4e01fc

Browse files
committed
test: add unit tests
1 parent a14131c commit f4e01fc

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

go/ops.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ func validateIavlOps(op opType, layerNum int) error {
2929

3030
height, err := binary.ReadVarint(r)
3131
if err != nil {
32-
return err
32+
return fmt.Errorf("failed to read IAVL height varint: %w", err)
3333
}
3434
if int(height) < 0 || int(height) < layerNum {
3535
return fmt.Errorf("IAVL height (%d) must be non-negative and greater than or equal to the layer number (%d)", height, layerNum)
3636
}
3737

3838
size, err := binary.ReadVarint(r)
3939
if err != nil {
40-
return err
40+
return fmt.Errorf("failed to read IAVL size varint: %w", err)
4141
}
4242

4343
if int(size) < 0 {
@@ -46,7 +46,7 @@ func validateIavlOps(op opType, layerNum int) error {
4646

4747
version, err := binary.ReadVarint(r)
4848
if err != nil {
49-
return err
49+
return fmt.Errorf("failed to read IAVL version varint: %w", err)
5050
}
5151

5252
if int(version) < 0 {

go/ops_test.go

+118
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,124 @@ import (
99
"testing"
1010
)
1111

12+
func TestValidateIavlOps(t *testing.T) {
13+
var (
14+
op opType
15+
layerNum int
16+
)
17+
cases := []struct {
18+
name string
19+
malleate func()
20+
expError error
21+
}{
22+
{
23+
"success",
24+
func() {},
25+
nil,
26+
},
27+
{
28+
"failure: reading varint",
29+
func() {
30+
op.(*InnerOp).Prefix = []byte{}
31+
},
32+
errors.New("failed to read IAVL height varint: EOF"),
33+
},
34+
{
35+
"failure: invalid height value",
36+
func() {
37+
op.(*InnerOp).Prefix = []byte{1}
38+
},
39+
errors.New("IAVL height (-1) must be non-negative and greater than or equal to the layer number (1)"),
40+
},
41+
{
42+
"failure: invalid size varint",
43+
func() {
44+
var varintBuf [binary.MaxVarintLen64]byte
45+
prefix := convertVarIntToBytes(int64(5), varintBuf)
46+
op.(*InnerOp).Prefix = prefix
47+
},
48+
errors.New("failed to read IAVL size varint: EOF"),
49+
},
50+
{
51+
"failure: invalid size value",
52+
func() {
53+
var varintBuf [binary.MaxVarintLen64]byte
54+
prefix := convertVarIntToBytes(int64(5), varintBuf)
55+
prefix = append(prefix, convertVarIntToBytes(int64(-1), varintBuf)...) // size
56+
op.(*InnerOp).Prefix = prefix
57+
},
58+
errors.New("IAVL size must be non-negative"),
59+
},
60+
{
61+
"failure: invalid version varint",
62+
func() {
63+
var varintBuf [binary.MaxVarintLen64]byte
64+
prefix := convertVarIntToBytes(int64(5), varintBuf)
65+
prefix = append(prefix, convertVarIntToBytes(int64(10), varintBuf)...)
66+
op.(*InnerOp).Prefix = prefix
67+
},
68+
errors.New("failed to read IAVL version varint: EOF"),
69+
},
70+
{
71+
"failure: invalid version value",
72+
func() {
73+
var varintBuf [binary.MaxVarintLen64]byte
74+
prefix := convertVarIntToBytes(int64(5), varintBuf)
75+
prefix = append(prefix, convertVarIntToBytes(int64(10), varintBuf)...)
76+
prefix = append(prefix, convertVarIntToBytes(int64(-1), varintBuf)...) // version
77+
op.(*InnerOp).Prefix = prefix
78+
},
79+
errors.New("IAVL version must be non-negative"),
80+
},
81+
{
82+
"failure: invalid remaining length with layer number is 0",
83+
func() {
84+
layerNum = 0
85+
},
86+
fmt.Errorf("expected remaining prefix length to be 0, got: 1"),
87+
},
88+
{
89+
"failure: invalid reminaing length with non-zero layer number",
90+
func() {
91+
layerNum = 1
92+
op.(*InnerOp).Prefix = append(op.(*InnerOp).Prefix, []byte{1}...)
93+
},
94+
fmt.Errorf("remainder of prefix must be of length 1 or 34, got: 2"),
95+
},
96+
{
97+
"failure: invalid hash",
98+
func() {
99+
op.(*InnerOp).Hash = HashOp_NO_HASH
100+
},
101+
fmt.Errorf("IAVL hash op must be %v", HashOp_SHA256),
102+
},
103+
}
104+
for _, tc := range cases {
105+
tc := tc
106+
107+
t.Run(tc.name, func(t *testing.T) {
108+
op = &InnerOp{
109+
Hash: HashOp_SHA256,
110+
Prefix: generateInnerOpPrefix(),
111+
Suffix: []byte(""),
112+
}
113+
layerNum = 1
114+
115+
tc.malleate()
116+
117+
err := validateIavlOps(op, layerNum)
118+
if tc.expError == nil && err != nil {
119+
t.Fatal(err)
120+
}
121+
122+
if tc.expError != nil && err.Error() != tc.expError.Error() {
123+
t.Fatalf("expected: %v, got: %v", tc.expError, err)
124+
}
125+
})
126+
127+
}
128+
}
129+
12130
func TestLeafOp(t *testing.T) {
13131
cases := LeafOpTestData(t)
14132

0 commit comments

Comments
 (0)