-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_test.go
352 lines (299 loc) · 7.12 KB
/
database_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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package wallet_test
import (
"os"
"testing"
"github.com/FactomProject/factom"
"github.com/FactomProject/wallet/v2"
)
func TestNewWallet(t *testing.T) {
// create a new database
w1, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// check that the seed got written
seed, err := w1.GetDBSeed()
if err != nil {
t.Error(err)
}
if len(seed.MnemonicSeed) == 0 {
t.Errorf("stored db seed is empty")
}
if err := w1.Close(); err != nil {
t.Error(err)
}
}
func TestOpenWallet(t *testing.T) {
dbpath := os.TempDir() + "/test_wallet-01"
// create a new database
w1, err := wallet.NewOrOpenLevelDBWallet(dbpath)
if err != nil {
t.Error(err)
}
w1.Close()
// make sure we can open the db
w2, err := wallet.NewOrOpenLevelDBWallet(dbpath)
if err != nil {
t.Error(err)
}
// check that the seed is there
seed, err := w1.GetDBSeed()
if len(seed.MnemonicSeed) == 0 {
t.Errorf("stored db seed is empty")
}
if err := w2.Close(); err != nil {
t.Error(err)
}
// remove the testing db
if err := os.RemoveAll(dbpath); err != nil {
t.Error(err)
}
}
func TestPutECAddress(t *testing.T) {
zSec := "Es2Rf7iM6PdsqfYCo3D1tnAR65SkLENyWJG1deUzpRMQmbh9F3eG"
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// write a new ec address to the db
e, err := factom.GetECAddress(zSec)
if err != nil {
t.Error(err)
}
if err := w.InsertECAddress(e); err != nil {
t.Error(err)
}
// Check that the address was written into the db
if _, err := w.GetECAddress(e.PubString()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestPutFCTAddress(t *testing.T) {
zSec := "Fs1KWJrpLdfucvmYwN2nWrwepLn8ercpMbzXshd1g8zyhKXLVLWj"
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// write a new fct address to the db
f, err := factom.GetFactoidAddress(zSec)
if err != nil {
t.Error(err)
}
if err := w.InsertFCTAddress(f); err != nil {
t.Error(err)
}
// Check that the address was written into the db
if _, err := w.GetFCTAddress(f.String()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestGenerateECAddress(t *testing.T) {
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// Generate a new ec address
e, err := w.GenerateECAddress()
if err != nil {
t.Error(err)
}
// Check that the address was written into the db
if _, err := w.GetECAddress(e.PubString()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestGenerateFCTAddress(t *testing.T) {
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// Generate a new fct address
f, err := w.GenerateFCTAddress()
if err != nil {
t.Error(err)
}
// Check that the address was written into the db
if _, err := w.GetFCTAddress(f.String()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestGetAllAddresses(t *testing.T) {
e1Sec := "Es2Rf7iM6PdsqfYCo3D1tnAR65SkLENyWJG1deUzpRMQmbh9F3eG"
f1Sec := "Fs1KWJrpLdfucvmYwN2nWrwepLn8ercpMbzXshd1g8zyhKXLVLWj"
e2Sec := "Es4NQHwo8F4Z4oMnVwndtjV1rzZN3t5pP5u5jtdgiR1RA6FH4Tmc"
f2Sec := "Fs3GFV6GNV6ar4b8eGcQWpGFbFtkNWKfEPdbywmha8ez5p7XMJyk"
correctLen := 2
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// write a new ec address to the db
e1, err := factom.GetECAddress(e1Sec)
if err != nil {
t.Error(err)
}
if err := w.InsertECAddress(e1); err != nil {
t.Error(err)
}
e2, err := factom.GetECAddress(e2Sec)
if err != nil {
t.Error(err)
}
if err := w.InsertECAddress(e2); err != nil {
t.Error(err)
}
// write a new fct address to the db
f1, err := factom.GetFactoidAddress(f1Sec)
if err != nil {
t.Error(err)
}
if err := w.InsertFCTAddress(f1); err != nil {
t.Error(err)
}
f2, err := factom.GetFactoidAddress(f2Sec)
if err != nil {
t.Error(err)
}
if err := w.InsertFCTAddress(f2); err != nil {
t.Error(err)
}
// get all addresses out of db
fs, es, err := w.GetAllAddresses()
if err != nil {
t.Error(err)
} else if fs == nil {
t.Errorf("No Factoid address was retrived")
} else if es == nil {
t.Errorf("No EC address was retrived")
}
// check that all the addresses are there
if len(fs) != correctLen {
t.Errorf("Wrong number of factoid addesses were retrived: %v", fs)
}
if len(es) != correctLen {
t.Errorf("Wrong number of ec addesses were retrived: %v", es)
}
// print the addresses
for _, f := range fs {
t.Logf("%s %s", f, f.SecString())
}
for _, e := range es {
t.Logf("%s %s", e, e.SecString())
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestPutIdentityKey(t *testing.T) {
sec := "idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f"
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// write a new identity key to the db
k, err := factom.GetIdentityKey(sec)
if err != nil {
t.Error(err)
}
if err := w.InsertIdentityKey(k); err != nil {
t.Error(err)
}
// Check that the key was written into the db
if _, err := w.GetIdentityKey(k.PubString()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestGenerateIdentityKey(t *testing.T) {
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// Generate a new identity key
k, err := w.GenerateIdentityKey()
if err != nil {
t.Error(err)
}
// Check that the key was written into the db
if _, err := w.GetIdentityKey(k.PubString()); err != nil {
t.Error(err)
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}
func TestGetAllIdentityKeys(t *testing.T) {
sec1 := "idsec2J3nNoqdiyboCBKDGauqN9Jb33dyFSqaJKZqTs6i5FmztsTn5f"
sec2 := "idsec1xuUyeCCrJhsojf2wLAZqRxPzPFR8Gidd9DRRid1yGy8ncAJG3"
correctLen := 2
// create a new database
w, err := wallet.NewMapDBWallet()
if err != nil {
t.Error(err)
}
// write a new identity keys to the db
k1, err := factom.GetIdentityKey(sec1)
if err != nil {
t.Error(err)
}
if err := w.InsertIdentityKey(k1); err != nil {
t.Error(err)
}
k2, err := factom.GetIdentityKey(sec2)
if err != nil {
t.Error(err)
}
if err := w.InsertIdentityKey(k2); err != nil {
t.Error(err)
}
// get all identity keys out of db
ks, err := w.GetAllIdentityKeys()
if err != nil {
t.Error(err)
} else if ks == nil {
t.Errorf("No Identity key was retrived")
}
// check that all the keys are there
if len(ks) != correctLen {
t.Errorf("Wrong number of identity keys were retrived: %v", ks)
}
// print the keys
for _, k := range ks {
t.Logf("%s %s", k, k.SecString())
}
// close and remove the testing db
if err := w.Close(); err != nil {
t.Error(err)
}
}