-
Notifications
You must be signed in to change notification settings - Fork 3
/
merkletree_test.go
48 lines (44 loc) · 1.05 KB
/
merkletree_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
package merkletree
import (
"fmt"
"testing"
)
func TestRoot(t *testing.T) {
elements := []string{"some", "test", "elements"}
expectedRoot := hashNode(
hashNode(hashLeaf("some"), hashLeaf("test")),
hashNode(hashLeaf("elements"), hashLeaf("")),
)
testname := fmt.Sprintf("computes correct root")
t.Run(testname, func(t *testing.T) {
mt, err := NewMerkleTree(elements)
if err != nil {
t.Error(err)
}
if mt.GetRoot() != expectedRoot {
t.Errorf("got %s, want %s", mt.GetRoot(), expectedRoot)
}
})
}
func TestProof(t *testing.T) {
elements := []string{"some", "test", "elements"}
mt, err := NewMerkleTree(elements)
if err != nil {
t.Error(err)
}
for i, elem := range elements {
testname := fmt.Sprintf("valid proof for element: %d", i)
t.Run(testname, func(t *testing.T) {
proof, err := mt.GetProof(uint64(i))
if err != nil {
t.Error(err)
}
if !VerifyProof(mt.GetRoot(), proof) {
t.Error("invalid proof")
}
if hashLeaf(elem) != proof.hElement {
t.Errorf("got %s, want %s", elem, proof.hElement)
}
})
}
}