forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
428 lines (368 loc) · 13 KB
/
main_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"bytes"
"context"
"fmt"
gnet "net"
"os"
"os/exec"
"path"
"strconv"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/drand/drand/beacon"
"github.com/drand/drand/core"
"github.com/drand/drand/fs"
"github.com/drand/drand/key"
"github.com/drand/drand/test"
"github.com/drand/kyber"
"github.com/drand/kyber/share"
"github.com/drand/kyber/util/random"
"github.com/kabukky/httpscerts"
"github.com/nikkolasg/slog"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
cmd := exec.Command("go", "install")
if err := cmd.Run(); err != nil {
slog.Fatalf("test failing: %s", err)
}
code := m.Run()
os.Exit(code)
}
func TestDeleteBeacon(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)
var opt = core.WithConfigFolder(tmp)
conf := core.NewConfig(opt)
fs.CreateSecureFolder(conf.DBFolder())
store, err := beacon.NewBoltStore(conf.DBFolder(), conf.BoltOptions())
require.NoError(t, err)
store.Put(&beacon.Beacon{
Round: 1,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 2,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 3,
Signature: []byte("Hello"),
})
store.Put(&beacon.Beacon{
Round: 4,
Signature: []byte("hello"),
})
// try to fetch round 3 and 4
b, err := store.Get(3)
require.NoError(t, err)
require.NotNil(t, b)
b, err = store.Get(4)
require.NoError(t, err)
require.NotNil(t, b)
store.Close()
// that commmand should delete round 3 and 4
cmd := exec.Command("drand", "util", "del-beacon", "--folder", tmp, "3")
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
store, err = beacon.NewBoltStore(conf.DBFolder(), conf.BoltOptions())
require.NoError(t, err)
// try to fetch round 3 and 4 - it should now fail
b, err = store.Get(3)
require.Error(t, err)
require.Nil(t, b)
b, err = store.Get(4)
require.Error(t, err)
require.Nil(t, b)
}
func TestKeyGen(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)
cmd := exec.Command("drand", "generate-keypair", "--folder", tmp, "127.0.0.1:8081")
out, err := cmd.Output()
require.Nil(t, err)
fmt.Println(string(out))
config := core.NewConfig(core.WithConfigFolder(tmp))
fs := key.NewFileStore(config.ConfigFolder())
priv, err := fs.LoadKeyPair()
require.NoError(t, err)
require.NotNil(t, priv.Public)
tmp2 := path.Join(os.TempDir(), "drand2")
defer os.RemoveAll(tmp2)
cmd = exec.Command("drand", "generate-keypair", "--folder", tmp2)
out, err = cmd.Output()
require.Error(t, err)
fmt.Println(string(out))
config = core.NewConfig(core.WithConfigFolder(tmp2))
fs = key.NewFileStore(config.ConfigFolder())
priv, err = fs.LoadKeyPair()
require.Error(t, err)
require.Nil(t, priv)
}
//tests valid commands and then invalid commands
func TestStartAndStop(t *testing.T) {
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
defer os.RemoveAll(tmpPath)
n := 5
_, group := test.BatchIdentities(n)
groupPath := path.Join(tmpPath, fmt.Sprintf("group.toml"))
require.NoError(t, key.Save(groupPath, group, false))
cmd := exec.Command("drand", "generate-keypair", "127.0.0.1:8080", "--tls-disable", "--folder", tmpPath)
require.NoError(t, cmd.Run())
startCh := make(chan bool)
go func() {
cmd = exec.Command("drand", "start", "--tls-disable", "--folder", tmpPath)
startCh <- true
cmd.Run()
startCh <- true
// TODO : figuring out how to not panic in grpc call
// ERROR: 2020/01/23 21:06:28 grpc: server failed to encode response:
// rpc error: code = Internal desc = grpc: error while marshaling: proto:
// Marshal called with nil
//if e, ok := err.(*exec.ExitError); !ok || e.ExitCode() != 0 {
//t.Fatal(err)
//}
}()
<-startCh
time.Sleep(50 * time.Millisecond)
cmd = exec.Command("drand", "stop")
buff, err := cmd.CombinedOutput()
require.NoError(t, err, string(buff))
select {
case <-startCh:
case <-time.After(1 * time.Second):
t.Fatal("drand daemon did not stop")
}
}
func TestStartBeacon(t *testing.T) {
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
defer os.RemoveAll(tmpPath)
varEnv := "CRASHCRASH"
n := 5
_, group := test.BatchIdentities(n)
groupPath := path.Join(tmpPath, fmt.Sprintf("group.toml"))
require.NoError(t, key.Save(groupPath, group, false))
cmd := exec.Command("drand", "start", "--tls-disable", "--folder", tmpPath)
cmd.Env = append(os.Environ(), varEnv+"=1")
out, err := cmd.Output()
fmt.Print(string(out))
if e, ok := err.(*exec.ExitError); ok && e.Success() {
t.Fatal(err)
}
}
func TestStartWithoutGroup(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
defer func() {
if err := os.RemoveAll(tmpPath); err != nil {
fmt.Println(err)
}
}()
pubPath := path.Join(tmpPath, "pub.key")
port1, _ := strconv.Atoi(test.FreePort())
addr := "127.0.0.1:" + strconv.Itoa(port1)
ctrlPort1 := test.FreePort()
ctrlPort2 := test.FreePort()
metricsPort := test.FreePort()
priv := key.NewKeyPair(addr)
require.NoError(t, key.Save(pubPath, priv.Public, false))
config := core.NewConfig(core.WithConfigFolder(tmpPath))
fs := key.NewFileStore(config.ConfigFolder())
require.NoError(t, fs.SaveKeyPair(priv))
installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)
lctx, lcancel := context.WithCancel(context.Background())
start1 := exec.CommandContext(lctx, "drand", "start", "--tls-disable", "--verbose", "2", "--folder", tmpPath, "--control", ctrlPort1, "--metrics", metricsPort)
go start1.Run()
fmt.Println(" DRAND SHARE ---")
initDKGCmd := exec.Command("drand", "share", "--control", ctrlPort1)
out, err := initDKGCmd.Output()
require.Error(t, err)
lcancel()
fmt.Println(" --- DRAND GROUP ---")
// fake group
_, group := test.BatchIdentities(5)
priv.Public.TLS = false
group.Period = 5 * time.Second
group.GenesisTime = time.Now().Unix() - 10
group.Nodes[0] = &key.Node{Identity: priv.Public, Index: 0}
group.Nodes[1] = &key.Node{Identity: priv.Public, Index: 1}
groupPath := path.Join(tmpPath, "drand_group.toml")
require.NoError(t, key.Save(groupPath, group, false))
// save it also to somewhere drand will find it
require.NoError(t, fs.SaveGroup(group))
//fake share
scalarOne := key.KeyGroup.Scalar().One()
s := &share.PriShare{I: 2, V: scalarOne}
share := &key.Share{Share: s}
require.NoError(t, fs.SaveShare(share))
// fake dkg outuput
fakeKey := key.KeyGroup.Point().Pick(random.New())
distKey := &key.DistPublic{
Coefficients: []kyber.Point{fakeKey},
}
require.NoError(t, fs.SaveDistPublic(distKey))
fmt.Println(" --- DRAND START --- control ", ctrlPort1)
start2 := exec.CommandContext(ctx, "drand", "start", "--control", ctrlPort2, "--tls-disable", "--folder", tmpPath, "--verbose")
start2.Stdout = os.Stdout
start2.Stderr = os.Stderr
go start2.Run()
defer exec.Command("drand", "stop", "--control", ctrlPort2).Run()
time.Sleep(500 * time.Millisecond)
fmt.Println(" + running PING command with ", ctrlPort2)
ping := exec.Command("drand", "util", "ping", "--control", ctrlPort2)
out, err = ping.CombinedOutput()
require.NoError(t, err, string(out))
require.NoError(t, toml.NewEncoder(os.Stdout).Encode(group))
fmt.Printf("\n Running GET PRIVATE command with group file at %s\n", groupPath)
loadedGroup := new(key.Group)
require.NoError(t, key.Load(groupPath, loadedGroup))
fmt.Printf("%s", loadedGroup.String())
getCmd := exec.Command("drand", "get", "private", "--tls-disable", groupPath)
out, err = getCmd.CombinedOutput()
require.NoError(t, err, string(out))
fakeStr := key.PointToString(fakeKey)
cokeyCmd := exec.Command("drand", "get", "cokey", "--tls-disable", priv.Public.Address())
out, err = cokeyCmd.CombinedOutput()
require.NoError(t, err, string(out))
require.True(t, strings.Contains(string(out), fakeStr))
shareCmd := exec.Command("drand", "show", "share", "--control", ctrlPort2)
out, err = shareCmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
t.Fatalf("could not run the command : %s", err.Error())
}
expectedOutput := "0000000000000000000000000000000000000000000000000000000000000001"
require.True(t, strings.Contains(string(out), expectedOutput))
require.NoError(t, err)
// reset state
resetCmd := exec.Command("drand", "util", "reset", "--folder", tmpPath)
var in bytes.Buffer
in.WriteString("y\n")
resetCmd.Stdin = &in
out, err = resetCmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
_, err = fs.LoadDistPublic()
require.Error(t, err)
_, err = fs.LoadShare()
require.Error(t, err)
_, err = fs.LoadGroup()
require.Error(t, err)
}
func TestClientTLS(t *testing.T) {
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
defer os.RemoveAll(tmpPath)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
groupPath := path.Join(tmpPath, "group.toml")
pubPath := path.Join(tmpPath, "pub.key")
certPath := path.Join(tmpPath, "server.pem")
keyPath := path.Join(tmpPath, "key.pem")
freePort := test.FreePort()
addr := "127.0.0.1:" + freePort
ctrlPort := test.FreePort()
metricsPort := test.FreePort()
priv := key.NewTLSKeyPair(addr)
require.NoError(t, key.Save(pubPath, priv.Public, false))
config := core.NewConfig(core.WithConfigFolder(tmpPath))
fs := key.NewFileStore(config.ConfigFolder())
fs.SaveKeyPair(priv)
if httpscerts.Check(certPath, keyPath) != nil {
fmt.Println("generating on the fly")
h, _, _ := gnet.SplitHostPort(priv.Public.Address())
if err := httpscerts.Generate(certPath, keyPath, h); err != nil {
panic(err)
}
}
// fake group
_, group := test.BatchTLSIdentities(5)
group.Nodes[0] = &key.Node{Identity: priv.Public, Index: 0}
group.Period = 2 * time.Minute
groupPath = path.Join(tmpPath, fmt.Sprintf("groups/drand_group.toml"))
fs.SaveGroup(group)
// fake dkg outuput
fakeKey := key.KeyGroup.Point().Pick(random.New())
keyStr := key.PointToString(fakeKey)
distKey := &key.DistPublic{
Coefficients: []kyber.Point{fakeKey},
}
require.NoError(t, fs.SaveDistPublic(distKey))
//fake share
scalarOne := key.KeyGroup.Scalar().One()
s := &share.PriShare{I: 2, V: scalarOne}
share := &key.Share{Share: s}
fs.SaveShare(share)
startArgs := []string{"drand", "start", "--tls-cert", certPath, "--tls-key", keyPath, "--control", ctrlPort, "--folder", tmpPath, "--metrics", metricsPort}
startCmd := exec.CommandContext(ctx, startArgs[0], startArgs[1:]...)
startCmd.Stdout = os.Stdout
startCmd.Stderr = os.Stderr
go startCmd.Run()
installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)
cmd := exec.Command("drand", "get", "private", "--tls-cert", certPath, groupPath)
out, err := cmd.CombinedOutput()
fmt.Println("get private = ", string(out))
require.NoError(t, err)
// XXX Commented out test since we can't "fake" anymore in the same way
// a dist public key. One would need to use the real fs path of the daemon
// to save the group at the right place
//
/*cmd = exec.Command("drand", "fetch", "dist_key", "--tls-cert", certPath, addr)*/
//out, err = cmd.CombinedOutput()
//require.True(t, strings.Contains(string(out), keyStr))
//require.NoError(t, err)
//cmd = exec.Command("drand", "control", "share")
//out, err = cmd.CombinedOutput()
//if err != nil {
//t.Fatalf("could not run the command : %s", err.Error())
//}
//expectedOutput := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE="
//require.True(t, strings.Contains(string(out), expectedOutput))
/*require.NoError(t, err)*/
// XXX Can't test public randomness without launching an actual DKG / beacon
// process...
/*cmd = exec.Command("drand", "get", "public", "--tls-cert", certPath, "--nodes", addr, groupPath)*/
//out, err = cmd.CombinedOutput()
//fmt.Println(string(out))
//require.NoError(t, err)
cmd = exec.Command("drand", "get", "cokey", "--tls-cert", certPath, addr)
out, err = cmd.CombinedOutput()
//fmt.Println(string(out))
expectedOutput := keyStr
//fmt.Printf("out = %s\n", string(out))
//fmt.Printf("expected = %s\n", expectedOutput)
//fmt.Printf("contains ? %v\n", strings.Contains(string(out), expectedOutput))
require.Contains(t, string(out), expectedOutput)
require.NoError(t, err)
cmd = exec.Command("drand", "show", "share", "--control", ctrlPort)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
expectedOutput = "0000000000000000000000000000000000000000000000000000000000000001"
require.True(t, strings.Contains(string(out), expectedOutput))
require.NoError(t, err)
cmd = exec.Command("drand", "show", "public", "--control", ctrlPort)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
cmd = exec.Command("drand", "show", "private", "--control", ctrlPort)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
cmd = exec.Command("drand", "show", "cokey", "--control", ctrlPort)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
expectedOutput = keyStr
require.True(t, strings.Contains(string(out), expectedOutput))
require.NoError(t, err)
}