OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / merkle / simple_tree_test.go
1 package merkle
2
3 import (
4         "bytes"
5
6         . "github.com/tendermint/tmlibs/common"
7         . "github.com/tendermint/tmlibs/test"
8
9         "testing"
10 )
11
12 type testItem []byte
13
14 func (tI testItem) Hash() []byte {
15         return []byte(tI)
16 }
17
18 func TestSimpleProof(t *testing.T) {
19
20         total := 100
21
22         items := make([]Hashable, total)
23         for i := 0; i < total; i++ {
24                 items[i] = testItem(RandBytes(32))
25         }
26
27         rootHash := SimpleHashFromHashables(items)
28
29         rootHash2, proofs := SimpleProofsFromHashables(items)
30
31         if !bytes.Equal(rootHash, rootHash2) {
32                 t.Errorf("Unmatched root hashes: %X vs %X", rootHash, rootHash2)
33         }
34
35         // For each item, check the trail.
36         for i, item := range items {
37                 itemHash := item.Hash()
38                 proof := proofs[i]
39
40                 // Verify success
41                 ok := proof.Verify(i, total, itemHash, rootHash)
42                 if !ok {
43                         t.Errorf("Verification failed for index %v.", i)
44                 }
45
46                 // Wrong item index should make it fail
47                 {
48                         ok = proof.Verify((i+1)%total, total, itemHash, rootHash)
49                         if ok {
50                                 t.Errorf("Expected verification to fail for wrong index %v.", i)
51                         }
52                 }
53
54                 // Trail too long should make it fail
55                 origAunts := proof.Aunts
56                 proof.Aunts = append(proof.Aunts, RandBytes(32))
57                 {
58                         ok = proof.Verify(i, total, itemHash, rootHash)
59                         if ok {
60                                 t.Errorf("Expected verification to fail for wrong trail length.")
61                         }
62                 }
63                 proof.Aunts = origAunts
64
65                 // Trail too short should make it fail
66                 proof.Aunts = proof.Aunts[0 : len(proof.Aunts)-1]
67                 {
68                         ok = proof.Verify(i, total, itemHash, rootHash)
69                         if ok {
70                                 t.Errorf("Expected verification to fail for wrong trail length.")
71                         }
72                 }
73                 proof.Aunts = origAunts
74
75                 // Mutating the itemHash should make it fail.
76                 ok = proof.Verify(i, total, MutateByteSlice(itemHash), rootHash)
77                 if ok {
78                         t.Errorf("Expected verification to fail for mutated leaf hash")
79                 }
80
81                 // Mutating the rootHash should make it fail.
82                 ok = proof.Verify(i, total, itemHash, MutateByteSlice(rootHash))
83                 if ok {
84                         t.Errorf("Expected verification to fail for mutated root hash")
85                 }
86         }
87 }