This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay_test.go
374 lines (316 loc) · 9.41 KB
/
overlay_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
package onet
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
uuid "gopkg.in/satori/go.uuid.v1"
)
// A checkableError is a type that implements error and also lets
// you find out, by reading on a channel, how many times it has been
// formatted using Error().
type checkableError struct {
ch chan struct{}
msg string
}
func (ce *checkableError) Error() string {
ce.ch <- struct{}{}
return ce.msg
}
var dispFailErr = &checkableError{
ch: make(chan struct{}, 10),
msg: "Dispatch failed",
}
type ProtocolOverlay struct {
*TreeNodeInstance
done bool
failDispatch bool
failChan chan bool
}
func (po *ProtocolOverlay) Start() error {
// no need to do anything
return nil
}
func (po *ProtocolOverlay) Dispatch() error {
if po.failDispatch {
return dispFailErr
}
return nil
}
func (po *ProtocolOverlay) Release() {
// call the Done function
po.Done()
}
func TestOverlayDispatchFailure(t *testing.T) {
// setup
failChan := make(chan bool, 1)
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := ProtocolOverlay{
TreeNodeInstance: n,
failDispatch: true,
failChan: failChan,
}
return &ps, nil
}
GlobalProtocolRegister("ProtocolOverlay", fn)
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
// Redirect output so we can check for the failure
log.OutputToBuf()
defer log.OutputToOs()
h, _, tree := local.GenTree(1, true)
h1 := h[0]
pi, err := h1.CreateProtocol("ProtocolOverlay", tree)
if err != nil {
t.Fatal("error starting new node", err)
}
// wait for the error message to get formatted by overlay.go
<-dispFailErr.ch
// This test was apparently always a bit fragile, and commit 5931349
// seems to have made it worse. Adding this tiny sleep makes
// 2000 iterations pass where before I could see errors about 1 in 20 times.
time.Sleep(5 * time.Millisecond)
// when using `go test -v`, the error string goes into the stderr buffer
// but with `go test`, it goes into the stdout buffer, so we check both
assert.Contains(t, log.GetStdOut()+log.GetStdErr(), "Dispatch failed")
pi.(*ProtocolOverlay).Done()
}
func TestOverlayDone(t *testing.T) {
log.OutputToBuf()
defer log.OutputToOs()
// setup
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := ProtocolOverlay{
TreeNodeInstance: n,
}
return &ps, nil
}
GlobalProtocolRegister("ProtocolOverlay", fn)
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
h, _, tree := local.GenTree(1, true)
h1 := h[0]
p, err := h1.CreateProtocol("ProtocolOverlay", tree)
if err != nil {
t.Fatal("error starting new node", err)
}
po := p.(*ProtocolOverlay)
// release the resources
var count int
po.OnDoneCallback(func() bool {
count++
if count >= 2 {
return true
}
return false
})
po.Release()
overlay := h1.overlay
if _, ok := overlay.TokenToNode(po.Token()); !ok {
t.Fatal("Node should exists after first call Done()")
}
po.Release()
if _, ok := overlay.TokenToNode(po.Token()); ok {
t.Fatal("Node should NOT exists after call Done()")
}
}
type protocolCatastrophic struct {
*TreeNodeInstance
ChannelMsg chan WrapDummyMsg
done chan bool
}
func (po *protocolCatastrophic) Start() error {
panic("start panic")
}
func (po *protocolCatastrophic) Dispatch() error {
if !po.IsRoot() {
<-po.ChannelMsg
po.SendToParent(&DummyMsg{})
po.Done()
panic("dispatch panic")
}
err := po.SendToChildren(&DummyMsg{})
if err != nil {
return err
}
<-po.ChannelMsg
<-po.ChannelMsg
po.done <- true
po.Done()
panic("root dispatch panic")
}
// TestOverlayCatastrophicFailure checks if a panic during a protocol could
// cause the server to crash
func TestOverlayCatastrophicFailure(t *testing.T) {
log.OutputToBuf()
defer log.OutputToOs()
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := protocolCatastrophic{
TreeNodeInstance: n,
done: make(chan bool),
}
err := ps.RegisterChannel(&ps.ChannelMsg)
return &ps, err
}
GlobalProtocolRegister("ProtocolCatastrophic", fn)
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
h, _, tree := local.GenTree(3, true)
h1 := h[0]
pi, err := h1.StartProtocol("ProtocolCatastrophic", tree)
assert.NoError(t, err)
<-pi.(*protocolCatastrophic).done
// can't have a synchronisation and a panic so we wait for the panic to be handled
time.Sleep(1 * time.Second)
stderr := log.GetStdErr()
assert.Contains(t, stderr, "Start(): start panic")
assert.Contains(t, stderr, "Panic in call to protocol")
assert.Contains(t, stderr, "Dispatch(): root dispatch panic")
}
// Test when a peer receives a New Roster, it can create the trees that are
// waiting on this specific entitiy list, to be constructed.
func TestOverlayPendingTreeMarshal(t *testing.T) {
local := NewLocalTest(localTestBuilder)
hosts, el, tree := local.GenTree(2, false)
defer local.CloseAll()
h1 := hosts[0]
// Add the marshalled version of the tree
local.addPendingTreeMarshal(h1, tree.MakeTreeMarshal())
if _, ok := h1.GetTree(tree.ID); ok {
t.Fatal("host 1 should not have the tree definition yet.")
}
// Now make it check
local.checkPendingTreeMarshal(h1, el)
if _, ok := h1.GetTree(tree.ID); !ok {
t.Fatal("Host 1 should have the tree definition now.")
}
}
// overlayProc is a Processor which handles the management packet of Overlay,
// i.e. Roster & Tree management.
// Each type of message will be sent trhough the appropriate channel
type overlayProc struct {
sendRoster chan *Roster
responseTree chan *ResponseTree
treeMarshal chan *TreeMarshal
requestTree chan *RequestTree
}
func newOverlayProc() *overlayProc {
return &overlayProc{
sendRoster: make(chan *Roster, 1),
responseTree: make(chan *ResponseTree, 1),
treeMarshal: make(chan *TreeMarshal, 1),
requestTree: make(chan *RequestTree, 1),
}
}
func (op *overlayProc) Process(env *network.Envelope) {
switch env.MsgType {
case ResponseTreeMsgID:
op.responseTree <- env.Msg.(*ResponseTree)
case RequestTreeMsgID:
op.requestTree <- env.Msg.(*RequestTree)
}
}
func (op *overlayProc) Types() []network.MessageTypeID {
return []network.MessageTypeID{TreeMarshalTypeID}
}
// Test propagation of tree - both known and unknown
func TestOverlayTreePropagation(t *testing.T) {
local := NewLocalTest(localTestBuilder)
hosts, _, tree := local.GenTree(2, false)
defer local.CloseAll()
h1 := hosts[0]
h2 := hosts[1]
proc := newOverlayProc()
h1.RegisterProcessor(proc, ResponseTreeMsgID)
// h1 needs to expect the tree
h1.Overlay().treeStorage.Register(tree.ID)
// Check that h2 does nothing and doesn't crash
sentLen, err := h1.Send(h2.ServerIdentity, &RequestTree{TreeID: tree.ID, Version: 1})
require.Nil(t, err, "Couldn't send message to h2")
require.NotZero(t, sentLen)
// Now add the list to h2 and try again
h2.AddTree(tree)
sentLen, err = h1.Send(h2.ServerIdentity, &RequestTree{TreeID: tree.ID, Version: 1})
require.Nil(t, err)
require.NotZero(t, sentLen)
msg := <-proc.responseTree
assert.Equal(t, msg.TreeMarshal.TreeID, tree.ID)
sentLen, err = h1.Send(h2.ServerIdentity, &RequestTree{TreeID: tree.ID, Version: 1})
require.Nil(t, err)
require.NotZero(t, sentLen)
// check if we receive the tree then
var tm *ResponseTree
tm = <-proc.responseTree
packet := network.Envelope{
ServerIdentity: h2.ServerIdentity,
Msg: tm,
MsgType: ResponseTreeMsgID,
}
h1.overlay.Process(&packet)
tree2, ok := h1.GetTree(tree.ID)
if !ok {
t.Fatal("List-id not found")
}
if !tree.Equal(tree2) {
t.Fatal("Trees do not match")
}
}
// Tests if a tree can be requested even after a failure
func TestOverlayTreeFailure(t *testing.T) {
local := NewLocalTest(localTestBuilder)
hosts, _, tree := local.GenTree(3, false)
defer local.CloseAll()
h1 := hosts[0]
h1.overlay.treeStorage.Register(tree.ID)
h2 := hosts[1]
h2.AddTree(tree)
h3 := hosts[2]
h3.Close()
proc := newOverlayProc()
h1.RegisterProcessor(proc, ResponseTreeMsgID)
_, err := h1.Send(h3.ServerIdentity, &RequestTree{TreeID: tree.ID, Version: 1})
require.NotNil(t, err)
_, err = h1.Send(h2.ServerIdentity, &RequestTree{TreeID: tree.ID, Version: 1})
require.Nil(t, err)
// check if we have the tree
treeM := <-proc.responseTree
require.NotNil(t, treeM)
}
// Tests that the tree is not registered when bad parameters are provided
func TestOverlayHandlersBadParameters(t *testing.T) {
local := NewLocalTest(localTestBuilder)
hosts, ro, tree := local.GenTree(1, false)
defer local.CloseAll()
h := hosts[0]
h.overlay.handleSendTree(h.ServerIdentity, &ResponseTree{}, nil)
h.overlay.handleSendTree(h.ServerIdentity, &ResponseTree{TreeMarshal: tree.MakeTreeMarshal()}, nil)
h.overlay.handleSendTree(h.ServerIdentity, &ResponseTree{TreeMarshal: tree.MakeTreeMarshal(), Roster: ro}, nil)
}
func TestTokenId(t *testing.T) {
t1 := &Token{
RosterID: RosterID(uuid.NewV1()),
TreeID: TreeID(uuid.NewV1()),
ProtoID: ProtocolID(uuid.NewV1()),
RoundID: RoundID(uuid.NewV1()),
}
id1 := t1.ID()
t2 := &Token{
RosterID: RosterID(uuid.NewV1()),
TreeID: TreeID(uuid.NewV1()),
ProtoID: ProtocolID(uuid.NewV1()),
RoundID: RoundID(uuid.NewV1()),
}
id2 := t2.ID()
if id1.Equal(id2) {
t.Fatal("Both token are the same")
}
if !id1.Equal(t1.ID()) {
t.Fatal("Twice the Id of the same token should be equal")
}
t3 := t1.ChangeTreeNodeID(TreeNodeID(uuid.NewV1()))
if t1.TreeNodeID.Equal(t3.TreeNodeID) {
t.Fatal("OtherToken should modify copy")
}
}