forked from drand/drand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
434 lines (372 loc) · 13.3 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
429
430
431
432
433
434
package main
import (
"bytes"
"fmt"
gnet "net"
"os"
"os/exec"
"path"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
"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 TestKeyGen(t *testing.T) {
tmp := path.Join(os.TempDir(), "drand")
defer os.RemoveAll(tmp)
cmd := exec.Command("drand", "--folder", tmp, "generate-keypair", "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.Nil(t, err)
require.NotNil(t, priv.Public)
tmp2 := path.Join(os.TempDir(), "drand2")
defer os.RemoveAll(tmp2)
cmd = exec.Command("drand", "--folder", tmp2, "generate-keypair")
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 TestGroup(t *testing.T) {
n := 5
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
defer os.RemoveAll(tmpPath)
names := make([]string, n, n)
privs := make([]*key.Pair, n, n)
for i := 0; i < n; i++ {
names[i] = path.Join(tmpPath, fmt.Sprintf("drand-%d.public", i))
privs[i] = key.NewKeyPair("127.0.0.1")
require.NoError(t, key.Save(names[i], privs[i].Public, false))
if yes, err := fs.Exists(names[i]); !yes || err != nil {
t.Fatal(err.Error())
}
}
//test not enough keys
cmd := exec.Command("drand", "--folder", tmpPath, "group", names[0])
out, err := cmd.CombinedOutput()
expectedOut := "group command take at least 3 keys as arguments"
fmt.Println(string(out))
require.Error(t, err)
//test valid creation
groupPath := path.Join(tmpPath, key.GroupFolderName)
args := []string{"drand", "--folder", tmpPath, "group"}
args = append(args, names...)
cmd = exec.Command(args[0], args[1:]...)
out, err = cmd.CombinedOutput()
expectedOut = "Copy the following snippet into a new group.toml file " +
"and distribute it to all the participants:"
fmt.Println(string(out))
require.True(t, strings.Contains(string(out), expectedOut))
require.Nil(t, err)
//recreates exactly like in main and saves the group
var threshold = key.DefaultThreshold(n)
publics := make([]*key.Identity, n)
for i, str := range names {
pub := &key.Identity{}
if err := key.Load(str, pub); err != nil {
slog.Fatal(err)
}
publics[i] = pub
}
group := key.NewGroup(publics, threshold)
group.PublicKey = &key.DistPublic{
Coefficients: []kyber.Point{publics[0].Key},
}
require.Nil(t, key.Save(groupPath, group, false))
extraName := path.Join(tmpPath, fmt.Sprintf("drand-%d.public", n))
extraPriv := key.NewKeyPair("127.0.0.1")
require.NoError(t, key.Save(extraName, extraPriv.Public, false))
if yes, err := fs.Exists(extraName); !yes || err != nil {
t.Fatal(err.Error())
}
//test valid merge
cmd = exec.Command("drand", "--folder", tmpPath, "group", "--group", groupPath, extraName)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
//expectedOut = "Copy the following snippet into a new_group.toml file and give it to the upgrade command to do the resharing."
require.True(t, strings.Contains(string(out), expectedOut))
//test could not load group file
wrongGroupPath := "not_here"
cmd = exec.Command("drand", "--folder", tmpPath, "group", "--group", wrongGroupPath, names[0])
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.Error(t, err)
//test reject empty group file
emptyGroupPath := path.Join(tmpPath, "empty.toml")
emptyFile, err := os.Create(emptyGroupPath)
if err != nil {
slog.Fatal(err)
}
defer emptyFile.Close()
cmd = exec.Command("drand", "--folder", tmpPath, "group", "--group", emptyGroupPath, names[0])
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.Error(t, err)
}
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", "--folder", tmpPath, "generate-keypair", "127.0.0.1:8080", "--tls-disable")
require.NoError(t, cmd.Run())
startCh := make(chan bool)
go func() {
cmd = exec.Command("drand", "--folder", tmpPath, "start", "--tls-disable")
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", "--folder", tmpPath, "stop")
require.NoError(t, cmd.Run())
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", "--folder", tmpPath, "start", "--tls-disable")
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) {
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")
addr := "127.0.0.1:8083"
addr2 := "127.0.0.1:8084"
ctrlPort := "8889"
priv := key.NewKeyPair(addr)
require.NoError(t, key.Save(pubPath, priv.Public, false))
config := core.NewConfig(core.WithConfigFolder(tmpPath))
fs := key.NewFileStore(config.ConfigFolder())
fs.SaveKeyPair(priv)
installCmd := exec.Command("go", "install")
_, err := installCmd.Output()
require.NoError(t, err)
os.Args = []string{"drand", "--verbose", "2", "--folder", tmpPath, "start", "--tls-disable"}
go main()
initDKGCmd := exec.Command("drand", "share")
out, err := initDKGCmd.Output()
expectedErr := "needs at least one group.toml file argument"
output := string(out)
require.Error(t, err)
require.True(t, strings.Contains(output, expectedErr))
// fake group
_, group := test.BatchIdentities(5)
priv.Public.TLS = false
group.Nodes[0] = priv.Public
groupPath := path.Join(tmpPath, fmt.Sprintf("groups/drand_group.toml"))
require.NoError(t, key.Save(groupPath, group, false))
//fake share
scalarOne := key.KeyGroup.Scalar().One()
s := &share.PriShare{I: 2, V: scalarOne}
share := &key.Share{Share: s}
fs.SaveShare(share)
// fake dkg outuput
fakeKey := key.KeyGroup.Point().Pick(random.New())
fakeStr := key.PointToString(fakeKey)
distKey := &key.DistPublic{
Coefficients: []kyber.Point{fakeKey},
}
require.NoError(t, fs.SaveDistPublic(distKey))
// Specify different control and listen ports than TLS example so the two
// concurrently running drand instances (one secure, one insecure) don't
// re-use ports.
os.Args = []string{"drand", "--folder", tmpPath, "start", "--listen", addr2, "--control", ctrlPort, "--tls-disable"}
go main()
time.Sleep(300 * time.Millisecond)
cmd := exec.Command("drand", "ping", "--control", ctrlPort)
out, err = cmd.CombinedOutput()
require.NoError(t, err, string(out))
require.NoError(t, toml.NewEncoder(os.Stdout).Encode(group))
cmd = exec.Command("drand", "--verbose", "2", "get", "private", "--tls-disable", groupPath)
out, err = cmd.CombinedOutput()
require.NoError(t, err)
cmd = exec.Command("drand", "get", "cokey", "--tls-disable", groupPath)
out, err = cmd.CombinedOutput()
require.True(t, strings.Contains(string(out), fakeStr))
require.NoError(t, err)
cmd = exec.Command("drand", "show", "share", "--control", ctrlPort)
out, err = cmd.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
cmd = exec.Command("drand", "--folder", tmpPath, "reset")
var in bytes.Buffer
in.WriteString("y\n")
cmd.Stdin = &in
out, err = cmd.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)
groupPath := path.Join(tmpPath, "group.toml")
pubPath := path.Join(tmpPath, "pub.key")
certPath := path.Join(tmpPath, "server.pem")
keyPath := path.Join(tmpPath, "key.pem")
addr := "127.0.0.1:8085"
ctrlPort := "9091"
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] = priv.Public
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)
os.Args = []string{"drand", "--folder", tmpPath, "start", "--tls-cert", certPath, "--tls-key", keyPath, "--control", ctrlPort}
go main()
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, "--nodes", addr, groupPath)
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", "--verbose", "2", "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)
}