-
Notifications
You must be signed in to change notification settings - Fork 26
/
node_inner_test.go
131 lines (102 loc) · 3.91 KB
/
node_inner_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
package graviton
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestInnerUnMarshal(t *testing.T) {
var buffs = [][]byte{
[]byte{0, 0}, // small buf error
[]byte{24, innerNODE, 0, 0, 0, 0, 0x88, 0x88, 0x88, 0x88, 0x88, 0x8}, // more than 4 billion findex left node
[]byte{24, innerNODE, 0, 0, 0, 0, 0x10, 0x88, 0x88, 0x88, 0x88, 0x88, 0x8}, // more than 4 billion fpos left node
[]byte{24, innerNODE, 0, 0, 0, 0, 0x10, 0x10, 0x11, 0x11}, // buffer doesn't contain hash size, left
[]byte{24, 0, leafNODE, 0, 0, 0, 0x88, 0x88, 0x88, 0x88, 0x88, 0x8}, // more than 4 billion findex left node
[]byte{24, 0, leafNODE, 0, 0, 0, 0x10, 0x88, 0x88, 0x88, 0x88, 0x88, 0x8}, // more than 4 billion fpos left node
[]byte{24, 0, leafNODE, 0, 0, 0, 0x10, 0x10, 0x11, 0x11}, // buffer doesn't contain hash size
[]byte{24, 0, 3, 0, 0, 0, 0x10, 0x10, 0x11, 0x11}, // unknown node type
}
var in inner
for _, buf := range buffs {
err := in.Unmarshal(buf)
require.Error(t, err)
}
}
func TestIsOnlyChildleafPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
if n, ok := isOnlyChildleaf(nil); n != nil || ok != false { // result should be nil, false
t.Errorf("isOnlyChildleaf failed for nil")
}
if n, ok := isOnlyChildleaf(&leaf{}); n != nil || ok != false { // result should be nil, false
t.Errorf("isOnlyChildleaf failed for leaf")
}
isOnlyChildleaf(&dummynode{}) // it will panic and thus successfully complete the test
}
func TestLoadinnerfromstore(t *testing.T) {
store, err := NewMemStore()
//store,err := NewDiskStore("/tmp/test")
require.NoError(t, err)
var in inner
require.Error(t, in.loadinnerfromstore(store))
//in.fpos = 10000
//require.Error(t, in.loadinnerfromstore(store)) // invalid position
in.findex = 1000000
require.Error(t, in.loadinnerfromstore(store)) // invalid findex
in.loaded_partial = true
if hash, _ := in.Hash(store); hash != nil { // invalid findex for hash
t.Fatalf("Hash inner node loading failed")
}
_, err = in.Get(store, sum([]byte("dummykey"))) // trigger load error for Get
require.Error(t, err)
var l leaf
err = in.Insert(store, &l) // trigger load error for Insert
require.Error(t, err)
//var p Proof
//err = in.Prove(store, sum([]byte("dummykey")), &p) // trigger load error for Prove
//require.Error(t, err)
_, _, err = in.Delete(store, sum([]byte("dummykey"))) // trigger load error for Delete
require.Error(t, err)
{ // n level deep Delete errors are simulated here
var in, inlevel1 inner
in.loaded_partial = false
inlevel1.loaded_partial = true
in.left = &inlevel1
in.findex = 0
_, _, err = in.Delete(store, [HASHSIZE]byte{0}) // trigger load error for Delete
require.Error(t, err)
in.right = &inlevel1
_, _, err = in.Delete(store, [HASHSIZE]byte{0x80}) // trigger load error for Delete
require.Error(t, err)
}
{ // n level deep insert errors are simulated here
var in inner
l := &leaf{loaded_partial: true, keyhash: [HASHSIZE]byte{0x00, 1, 3}}
l2 := &leaf{loaded_partial: true, keyhash: [HASHSIZE]byte{0x00, 1, 3, 4}}
r := &leaf{loaded_partial: true, keyhash: [HASHSIZE]byte{0x80, 1, 3}}
r2 := &leaf{loaded_partial: true, keyhash: [HASHSIZE]byte{0x80, 1, 3, 4}}
in.left = l
err = in.Insert(store, l2) // trigger load error for Insert
require.Error(t, err)
in.right = r
err = in.Insert(store, r2) // trigger load error for right Insert
require.Error(t, err)
}
}
func TestInnerCoverage(t *testing.T) {
_, tree := setupDeterministicTree(t, 0)
value := []byte("testdeadend")
order := []byte{
0x0, //00000000,
0xc0, //11000000,
0xd0, //11010000,
0x8, //10000000,
}
for i := range order {
key := [HASHSIZE]byte{}
key[0] = order[i]
require.NoError(t, tree.putRaw(key, nil, value))
}
require.Error(t, tree.Commit()) // actually an error should be reported due to corruption
}