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
/
protocol_test.go
386 lines (332 loc) · 9.25 KB
/
protocol_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
package onet
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
"golang.org/x/xerrors"
uuid "gopkg.in/satori/go.uuid.v1"
)
var testProto = "test"
func init() {
network.RegisterMessage(SimpleMessage{})
}
// ProtocolTest is the most simple protocol to be implemented, ignoring
// everything it receives.
type ProtocolTest struct {
*TreeNodeInstance
StartMsg chan string
DispMsg chan string
}
// NewProtocolTest is used to create a new protocolTest-instance
func NewProtocolTest(n *TreeNodeInstance) (ProtocolInstance, error) {
return &ProtocolTest{
TreeNodeInstance: n,
StartMsg: make(chan string, 1),
DispMsg: make(chan string),
}, nil
}
// Dispatch is used to send the messages further - here everything is
// copied to /dev/null
func (p *ProtocolTest) Dispatch() error {
log.Lvl2("ProtocolTest.Dispatch()")
p.DispMsg <- "Dispatch"
p.Done()
return nil
}
func (p *ProtocolTest) Start() error {
log.Lvl2("ProtocolTest.Start()")
p.StartMsg <- "Start"
p.Done()
return nil
}
type SimpleProtocol struct {
// chan to get back to testing
Chan chan bool
Error error
*TreeNodeInstance
}
// Sends a simple message to its first children
func (p *SimpleProtocol) Start() error {
err := p.SendTo(p.Children()[0], &SimpleMessage{10})
if err != nil {
return err
}
p.Chan <- true
return nil
}
// Dispatch analyses the message and does nothing else
func (p *SimpleProtocol) ReceiveMessage(msg MsgSimpleMessage) error {
if msg.I != 10 {
return xerrors.New("Not the value expected")
}
p.Chan <- true
p.Done()
return nil
}
// ReturnError sends a message to the parent, and if it's the parent
// receiving the message, it triggers the channel
func (p *SimpleProtocol) ReturnError(msg MsgSimpleMessage) error {
if msg.I == 10 {
p.SendToParent(&SimpleMessage{9})
} else {
p.Chan <- true
}
p.Done()
return p.Error
}
type SimpleMessage struct {
I int64
}
type MsgSimpleMessage struct {
*TreeNode
SimpleMessage
}
// Test simple protocol-implementation
// - registration
func TestProtocolRegistration(t *testing.T) {
testProtoName := "testProto"
testProtoID, err := GlobalProtocolRegister(testProtoName, NewProtocolTest)
log.ErrFatal(err)
_, err = GlobalProtocolRegister(testProtoName, NewProtocolTest)
require.NotNil(t, err)
if !protocols.ProtocolExists(testProtoID) {
t.Fatal("Test should exist now")
}
if !ProtocolNameToID(testProtoName).Equal(testProtoID) {
t.Fatal("Not correct translation from string to ID")
}
require.Equal(t, "", protocols.ProtocolIDToName(ProtocolID(uuid.Nil)))
if protocols.ProtocolIDToName(testProtoID) != testProtoName {
t.Fatal("Not correct translation from ID to String")
}
}
// This makes h2 the leader, so it creates a tree and entity list
// and start a protocol. H1 should receive that message and request the entity
// list and the treelist and then instantiate the protocol.
func TestProtocolAutomaticInstantiation(t *testing.T) {
var simpleProto = "simpleAI"
// setup
chanH1 := make(chan bool)
chanH2 := make(chan bool)
chans := []chan bool{chanH1, chanH2}
id := 0
// custom creation function so we know the step due to the channels
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := SimpleProtocol{
TreeNodeInstance: n,
Chan: chans[id],
}
log.ErrFatal(ps.RegisterHandler(ps.ReceiveMessage))
id++
return &ps, nil
}
_, err := GlobalProtocolRegister(simpleProto, fn)
require.Nil(t, err)
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
h, _, tree := local.GenTree(2, true)
h1 := h[0]
var pi ProtocolInstance
started := make(chan bool)
// start the protocol
go func() {
var err error
pi, err = h1.StartProtocol(simpleProto, tree)
if err != nil {
t.Fatal(fmt.Sprintf("Could not start protocol %v", err))
}
started <- true
}()
// we are supposed to receive something from host1 from Start()
<-chanH1
// Then we are supposed to receive from h2 after he got the tree and the
// entity list from h1
<-chanH2
<-started
pi.(*SimpleProtocol).Done()
}
func TestProtocolError(t *testing.T) {
var simpleProto = "simplePE"
done := make(chan bool)
// The simplePE-protocol sends a message from the root to its
// children, which sends a message back and returns an error.
// When the root receives the message back, the second message
// is sent through the 'done'-channel. Like this we're sure that
// the children-message-handler had the time to return an error.
var protocolError error
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := SimpleProtocol{
TreeNodeInstance: n,
Chan: done,
}
ps.Error = protocolError
log.ErrFatal(ps.RegisterHandler(ps.ReturnError))
return &ps, nil
}
_, err := GlobalProtocolRegister(simpleProto, fn)
require.Nil(t, err)
local := NewLocalTest(localTestBuilder)
h, _, tree := local.GenTree(2, true)
h1 := h[0]
oldlvl := log.DebugVisible()
// The error won't show if the DebugVisible is < 1
if oldlvl < 1 {
log.SetDebugVisible(1)
}
// Redirecting stderr, so we can catch the error
log.OutputToBuf()
defer func() {
log.OutputToOs()
log.SetDebugVisible(oldlvl)
}()
// Empty it of previous messages before running our test.
_ = log.GetStdErr()
// start the protocol
go func() {
_, err := h1.StartProtocol(simpleProto, tree)
if err != nil {
t.Fatal(fmt.Sprintf("Could not start protocol %v", err))
}
}()
// Start is finished
<-done
// Return message is received
<-done
assert.Equal(t, "", log.GetStdErr(), "This should yield no error")
protocolError = xerrors.New("Protocol Error")
// start the protocol
go func() {
_, err := h1.StartProtocol(simpleProto, tree)
if err != nil {
t.Fatal(fmt.Sprintf("Could not start protocol %v", err))
}
}()
// Start is finished
<-done
// Return message is received
<-done
local.CloseAll()
str := log.GetStdErr()
assert.NotEqual(t, "", str, "No error output")
}
func TestGlobalProtocolRegisterTooLate(t *testing.T) {
var simpleProto = "simplePE"
done := make(chan bool)
fn := func(n *TreeNodeInstance) (ProtocolInstance, error) {
ps := SimpleProtocol{
TreeNodeInstance: n,
Chan: done,
}
log.ErrFatal(ps.RegisterHandler(ps.ReturnError))
return &ps, nil
}
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
local.GenTree(2, true)
fnShouldPanic := func() {
GlobalProtocolRegister(simpleProto, fn)
}
assert.Panics(t, fnShouldPanic)
}
func TestMessageProxyFactory(t *testing.T) {
defer eraseAllMessageProxy()
RegisterMessageProxy(NewTestMessageProxyChan)
assert.True(t, len(messageProxyFactory.factories) == 1)
}
func TestMessageProxyStore(t *testing.T) {
defer eraseAllMessageProxy()
local := NewLocalTest(localTestBuilder)
defer local.CloseAll()
RegisterMessageProxy(NewTestMessageProxy)
_, err := GlobalProtocolRegister(testProtoIOName, newTestProtocolInstance)
require.Nil(t, err)
h, _, tree := local.GenTree(2, true)
go func() {
// first time to wrap
res := <-chanProtoIOFeedback
require.Equal(t, "", res)
// second time to unwrap
res = <-chanProtoIOFeedback
require.Equal(t, "", res)
}()
pi, err := h[0].StartProtocol(testProtoIOName, tree)
require.Nil(t, err)
res := <-chanTestProtoInstance
assert.True(t, res)
pi.(*TestProtocolInstance).Done()
}
// MessageProxy part
var chanProtoIOCreation = make(chan bool)
var chanProtoIOFeedback = make(chan string)
const testProtoIOName = "TestIO"
type OuterPacket struct {
Info *OverlayMsg
Inner *SimpleMessage
}
var OuterPacketType = network.RegisterMessage(OuterPacket{})
type TestMessageProxy struct{}
func NewTestMessageProxyChan() MessageProxy {
chanProtoIOCreation <- true
return &TestMessageProxy{}
}
func NewTestMessageProxy() MessageProxy {
return &TestMessageProxy{}
}
func eraseAllMessageProxy() {
messageProxyFactory.factories = nil
}
func (t *TestMessageProxy) Wrap(msg interface{}, info *OverlayMsg) (interface{}, error) {
outer := &OuterPacket{}
inner, ok := msg.(*SimpleMessage)
if !ok {
chanProtoIOFeedback <- "wrong message type in wrap"
}
outer.Inner = inner
outer.Info = info
chanProtoIOFeedback <- ""
return outer, nil
}
func (t *TestMessageProxy) Unwrap(msg interface{}) (interface{}, *OverlayMsg, error) {
if msg == nil {
chanProtoIOFeedback <- "message nil!"
return nil, nil, xerrors.New("message nil")
}
real, ok := msg.(*OuterPacket)
if !ok {
chanProtoIOFeedback <- "wrong type of message in unwrap"
return nil, nil, xerrors.New("wrong message")
}
chanProtoIOFeedback <- ""
return real.Inner, real.Info, nil
}
func (t *TestMessageProxy) PacketType() network.MessageTypeID {
return OuterPacketType
}
func (t *TestMessageProxy) Name() string {
return testProtoIOName
}
var chanTestProtoInstance = make(chan bool)
// ProtocolInstance part
type TestProtocolInstance struct {
*TreeNodeInstance
}
func newTestProtocolInstance(n *TreeNodeInstance) (ProtocolInstance, error) {
pi := &TestProtocolInstance{n}
n.RegisterHandler(pi.handleSimpleMessage)
return pi, nil
}
func (t *TestProtocolInstance) Start() error {
t.SendTo(t.Root(), &SimpleMessage{12})
return nil
}
type SimpleMessageHandler struct {
*TreeNode
SimpleMessage
}
func (t TestProtocolInstance) handleSimpleMessage(h SimpleMessageHandler) error {
chanTestProtoInstance <- h.SimpleMessage.I == 12
return nil
}