-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathstorageproof_test.go
More file actions
191 lines (171 loc) · 4.97 KB
/
storageproof_test.go
File metadata and controls
191 lines (171 loc) · 4.97 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sqlchain
import (
"reflect"
"strings"
"testing"
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
"github.com/CovenantSQL/CovenantSQL/proto"
)
var (
currentNode proto.Node
voidNode proto.Node
answers []Answer
voidAnswer []Answer
previousBlock StorageProofBlock
currentBlock StorageProofBlock
voidBlock StorageProofBlock
)
func TestNewAnswer(t *testing.T) {
wantedAnswer := Answer{
PreviousBlockID: "aaa",
NodeID: "bbb",
Answer: hash.HashH([]byte{1, 2, 3, 4, 5}),
}
answer := NewAnswer("aaa", "bbb", hash.HashH([]byte{1, 2, 3, 4, 5}))
if !reflect.DeepEqual(*answer, wantedAnswer) {
t.Errorf("the answer is %+v, should be %+v", answer, wantedAnswer)
}
}
func TestGetNextPuzzle(t *testing.T) {
var totalRecordsInSQLChain int32 = 10
index, err := getNextPuzzle(answers, previousBlock)
if err != nil {
t.Error(err)
}
var wantedIndex int32
for _, answer := range answers {
wantedIndex += int32(hash.FNVHash32uint(answer.Answer[:]))
}
wantedIndex += int32(hash.FNVHash32uint([]byte(previousBlock.ID)))
wantedIndex %= totalRecordsInSQLChain
if index != wantedIndex {
t.Errorf("the next sql index is %+v, should be %+v. "+
"Answers are %+v, and the previous block is %+v",
index, wantedIndex, answers, previousBlock)
}
// void answer
index, err = getNextPuzzle(voidAnswer, previousBlock)
if err == nil {
t.Errorf("index is %d, but should be failed", index)
}
// void block
index, err = getNextPuzzle(answers, voidBlock)
if err == nil {
t.Errorf("index is %d, but should be failed", index)
}
}
func TestGetNextVerifier(t *testing.T) {
verifier, err := getNextVerifier(previousBlock, currentBlock)
if err != nil {
t.Error(err)
}
wantedVerifier := int32(hash.FNVHash32uint([]byte(previousBlock.ID))) % int32(len(currentBlock.Nodes))
if verifier != wantedVerifier {
t.Errorf("the next verifier is %d, should be %d", verifier, wantedVerifier)
}
// void previousBlock
verifier, err = getNextVerifier(voidBlock, currentBlock)
if err == nil {
t.Errorf("verifier is %d, but should be failed", verifier)
}
// void currentBlock
verifier, err = getNextVerifier(previousBlock, voidBlock)
if err == nil {
t.Errorf("verifier is %d, but should be failed", verifier)
}
}
func TestSelectRecord(t *testing.T) {
if strings.Compare(selectRecord(0), "hello world") != 0 {
t.Error("it should be hello world")
}
}
func TestCheckValid(t *testing.T) {
if !CheckValid(answers) {
t.Error("it should be true")
}
}
func TestGenerateAnswer(t *testing.T) {
answer, err := GenerateAnswer(answers, previousBlock, currentNode)
if err != nil {
t.Error(err)
}
sqlIndex, err := getNextPuzzle(answers, previousBlock)
if err != nil {
t.Error(err)
}
record := []byte(selectRecord(sqlIndex))
answerHash := hash.HashH(append(record, []byte(currentNode.ID)...))
wantedAnswer := NewAnswer(previousBlock.ID, currentNode.ID, answerHash)
if !reflect.DeepEqual(*answer, *wantedAnswer) {
t.Errorf("answer is %s, should be %s", *answer, *wantedAnswer)
}
// void answers
answer, err = GenerateAnswer(voidAnswer, previousBlock, currentNode)
if err == nil {
t.Errorf("answer is %+v, should be failed", answer)
}
// void block
answer, err = GenerateAnswer(answers, voidBlock, currentNode)
if err == nil {
t.Errorf("answer is %+v, should be failed", answer)
}
// void node
answer, err = GenerateAnswer(answers, previousBlock, voidNode)
if err == nil {
t.Errorf("answer is %+v, should be failed", answer)
}
}
func init() {
currentNode = proto.Node{ID: "123456"}
currentBlock = StorageProofBlock{
ID: "def",
Nodes: []proto.Node{{ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}, {ID: "e"}},
}
previousBlock = StorageProofBlock{
ID: "abc",
Nodes: []proto.Node{{ID: "a"}, {ID: "b"}, {ID: "c"}, {ID: "d"}},
}
voidBlock = StorageProofBlock{
ID: "",
Nodes: nil,
}
answers = []Answer{
{
PreviousBlockID: previousBlock.ID,
NodeID: currentNode.ID,
Answer: hash.HashH([]byte{1}),
},
{
PreviousBlockID: previousBlock.ID,
NodeID: currentNode.ID,
Answer: hash.HashH([]byte{2}),
},
{
PreviousBlockID: previousBlock.ID,
NodeID: currentNode.ID,
Answer: hash.HashH([]byte{3}),
},
{
PreviousBlockID: previousBlock.ID,
NodeID: currentNode.ID,
Answer: hash.HashH([]byte{4}),
},
}
voidAnswer = nil
voidNode = proto.Node{}
}