@@ -9,6 +9,124 @@ import (
9
9
"testing"
10
10
)
11
11
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 remaining 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
+
12
130
func TestLeafOp (t * testing.T ) {
13
131
cases := LeafOpTestData (t )
14
132
@@ -114,7 +232,7 @@ func TestInnerOpCheckAgainstSpec(t *testing.T) {
114
232
func () {
115
233
innerOp .Prefix = []byte {0x01 }
116
234
},
117
- fmt .Errorf ("wrong value in IAVL leaf op " ),
235
+ fmt .Errorf ("IAVL height (-1) must be non-negative and greater than or equal to the layer number (1) " ),
118
236
},
119
237
{
120
238
"failure: inner prefix starts with leaf prefix" ,
0 commit comments