-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathproof_test.go
152 lines (137 loc) · 3.5 KB
/
proof_test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ics23
import (
"bytes"
"fmt"
"testing"
)
func TestExistenceProof(t *testing.T) {
cases := ExistenceProofTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res, err := tc.Proof.Calculate()
// short-circuit with error case
if tc.IsErr {
if err == nil {
t.Fatal("Expected error, but got none")
}
} else {
if err != nil {
t.Fatal(err)
}
}
if !bytes.Equal(res, tc.Expected) {
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))
}
})
}
}
func TestCheckLeaf(t *testing.T) {
cases := CheckLeafTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
err := tc.Leaf.CheckAgainstSpec(&ProofSpec{LeafSpec: tc.Spec})
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got nil")
} else if !tc.IsErr && err != nil {
t.Fatalf("Unexpected error: %v", err)
}
})
}
}
func TestCheckAgainstSpec(t *testing.T) {
cases := CheckAgainstSpecTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
err := tc.Proof.CheckAgainstSpec(tc.Spec)
if tc.Err == "" && err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if tc.Err != "" && tc.Err != err.Error() {
t.Fatalf("Expected error: %s, got: %s", tc.Err, err.Error())
}
})
}
}
func TestEmptyBranch(t *testing.T) {
cases := EmptyBranchTestData(t)
for _, tc := range cases {
t.Run("case", func(t *testing.T) {
if err := tc.Op.CheckAgainstSpec(tc.Spec, 1); err != nil {
t.Errorf("Invalid InnerOp: %v", err)
}
leftBranchesAreEmpty, err := leftBranchesAreEmpty(tc.Spec.InnerSpec, tc.Op)
if err != nil {
t.Errorf("Error: %v", err)
}
if leftBranchesAreEmpty != tc.IsLeft {
t.Errorf("Expected leftBranchesAreEmpty to be %t but it wasn't", tc.IsLeft)
}
rightBranchesAreEmpty, err := rightBranchesAreEmpty(tc.Spec.InnerSpec, tc.Op)
if err != nil {
t.Errorf("Error: %v", err)
}
if rightBranchesAreEmpty != tc.IsRight {
t.Errorf("Expected rightBranchesAreEmpty to be %t but it wasn't", tc.IsRight)
}
})
}
}
func TestGetPosition(t *testing.T) {
cases := []struct {
name string
order []int32
branch int32
expPos int
expError error
}{
{
name: "success: first branch",
order: IavlSpec.InnerSpec.ChildOrder,
branch: 0,
expPos: 0,
expError: nil,
},
{
name: "success: second branch",
order: IavlSpec.InnerSpec.ChildOrder,
branch: 1,
expPos: 1,
expError: nil,
},
{
name: "failure: negative branch value",
order: IavlSpec.InnerSpec.ChildOrder,
branch: -1,
expPos: -1,
expError: fmt.Errorf("invalid branch: %d", int32(-1)),
},
{
name: "failure: branch is greater than length of child order",
order: IavlSpec.InnerSpec.ChildOrder,
branch: 3,
expPos: -1,
expError: fmt.Errorf("invalid branch: %d", int32(3)),
},
{
name: "failure: branch not found in child order",
order: []int32{0, 2},
branch: 1,
expPos: -1,
expError: fmt.Errorf("branch %d not found in order %v", 1, []int32{0, 2}),
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
pos, err := getPosition(tc.order, tc.branch)
if tc.expError == nil && err != nil {
t.Fatal(err)
}
if tc.expError != nil && (err.Error() != tc.expError.Error() || pos != -1) {
t.Fatalf("expected: %v, got: %v", tc.expError, err)
}
if tc.expPos != pos {
t.Fatalf("expected position: %d, got: %d", tc.expPos, pos)
}
})
}
}