Debian Codegraph @

readers_test.go

raw symbols SCIP
1 package merkletree
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "testing"
7 )
8
9 // TestReaderRoot calls ReaderRoot on a manually crafted dataset
10 // and checks the output.
11 func TestReaderRoot(t *testing.T) {
12 mt := CreateMerkleTester(t)
13 bytes8 := []byte{0, 1, 2, 3, 4, 5, 6, 7}
14 reader := bytes.NewReader(bytes8)
15 root, err := ReaderRoot(reader, sha256.New(), 1)
16 if err != nil {
17 t.Fatal(err)
18 }
19 if !bytes.Equal(root, mt.roots[8]) {
20 t.Error("ReaderRoot returned the wrong root")
21 }
22 }
23
24 // TestReaderRootPadding passes ReaderRoot a reader that has too few bytes to
25 // fill the last segment. The segment should not be padded out.
26 func TestReaderRootPadding(t *testing.T) {
27 bytes1 := []byte{1}
28 reader := bytes.NewReader(bytes1)
29 root, err := ReaderRoot(reader, sha256.New(), 2)
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 expectedRoot := sum(sha256.New(), []byte{0, 1})
35 if !bytes.Equal(root, expectedRoot) {
36 t.Error("ReaderRoot returned the wrong root")
37 }
38
39 bytes3 := []byte{1, 2, 3}
40 reader = bytes.NewReader(bytes3)
41 root, err = ReaderRoot(reader, sha256.New(), 2)
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 baseLeft := sum(sha256.New(), []byte{0, 1, 2})
47 baseRight := sum(sha256.New(), []byte{0, 3})
48 expectedRoot = sum(sha256.New(), append(append([]byte{1}, baseLeft...), baseRight...))
49 if !bytes.Equal(root, expectedRoot) {
50 t.Error("ReaderRoot returned the wrong root")
51 }
52 }
53
54 // TestBuildReaderProof calls BuildReaderProof on a manually crafted dataset
55 // and checks the output.
56 func TestBuilReaderProof(t *testing.T) {
57 mt := CreateMerkleTester(t)
58 bytes7 := []byte{0, 1, 2, 3, 4, 5, 6}
59 reader := bytes.NewReader(bytes7)
60 root, proofSet, numLeaves, err := BuildReaderProof(reader, sha256.New(), 1, 5)
61 if err != nil {
62 t.Fatal(err)
63 }
64 if !bytes.Equal(root, mt.roots[7]) {
65 t.Error("BuildReaderProof returned the wrong root")
66 }
67 if len(proofSet) != len(mt.proofSets[7][5]) {
68 t.Fatal("BuildReaderProof returned a proof with the wrong length")
69 }
70 for i := range proofSet {
71 if !bytes.Equal(proofSet[i], mt.proofSets[7][5][i]) {
72 t.Error("BuildReaderProof returned an incorrect proof")
73 }
74 }
75 if numLeaves != 7 {
76 t.Error("BuildReaderProof returned the wrong number of leaves")
77 }
78 }
79
80 // TestBuildReaderProofPadding passes BuildReaderProof a reader that has too
81 // few bytes to fill the last segment. The segment should not be padded out.
82 func TestBuildReaderProofPadding(t *testing.T) {
83 bytes1 := []byte{1}
84 reader := bytes.NewReader(bytes1)
85 root, proofSet, numLeaves, err := BuildReaderProof(reader, sha256.New(), 2, 0)
86 if err != nil {
87 t.Fatal(err)
88 }
89
90 expectedRoot := sum(sha256.New(), []byte{0, 1})
91 if !bytes.Equal(root, expectedRoot) {
92 t.Error("ReaderRoot returned the wrong root")
93 }
94 if len(proofSet) != 1 {
95 t.Fatal("proofSet is the incorrect length")
96 }
97 if !bytes.Equal(proofSet[0], []byte{1}) {
98 t.Error("proofSet is incorrect")
99 }
100 if numLeaves != 1 {
101 t.Error("wrong number of leaves returned")
102 }
103 }
104
105 // TestEmptyReader passes an empty reader into BuildReaderProof.
106 func TestEmptyReader(t *testing.T) {
107 _, _, _, err := BuildReaderProof(new(bytes.Reader), sha256.New(), 64, 5)
108 if err == nil {
109 t.Error(err)
110 }
111 }