forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
194 lines (165 loc) · 4.42 KB
/
processor_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
package onet
import (
"errors"
"testing"
"reflect"
"github.com/dedis/onet/log"
"github.com/dedis/onet/network"
"github.com/dedis/protobuf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testServiceName = "testService"
func init() {
RegisterNewService(testServiceName, newTestService)
ServiceFactory.ServiceID(testServiceName)
network.RegisterMessage(&testMsg{})
}
func TestProcessor_AddMessage(t *testing.T) {
h1 := NewLocalServer(tSuite, 2000)
defer h1.Close()
p := NewServiceProcessor(&Context{server: h1})
log.ErrFatal(p.RegisterHandler(procMsg))
if len(p.handlers) != 1 {
t.Fatal("Should have registered one function")
}
mt := network.MessageType(&testMsg{})
if mt.Equal(network.ErrorType) {
t.Fatal("Didn't register message-type correctly")
}
var wrongFunctions = []interface{}{
procMsgWrong1,
procMsgWrong2,
procMsgWrong3,
procMsgWrong4,
procMsgWrong5,
procMsgWrong6,
procMsgWrong7,
}
for _, f := range wrongFunctions {
fsig := reflect.TypeOf(f).String()
log.Lvl2("Checking function", fsig)
assert.Error(t, p.RegisterHandler(f),
"Could register wrong function: "+fsig)
}
}
func TestProcessor_RegisterMessages(t *testing.T) {
h1 := NewLocalServer(tSuite, 2000)
defer h1.Close()
p := NewServiceProcessor(&Context{server: h1})
log.ErrFatal(p.RegisterHandlers(procMsg, procMsg2, procMsg3, procMsg4))
assert.Error(t, p.RegisterHandlers(procMsg3, procMsgWrong4))
}
func TestServiceProcessor_ProcessClientRequest(t *testing.T) {
h1 := NewLocalServer(tSuite, 2000)
defer h1.Close()
p := NewServiceProcessor(&Context{server: h1})
log.ErrFatal(p.RegisterHandlers(procMsg, procMsg2))
buf, err := protobuf.Encode(&testMsg{11})
log.ErrFatal(err)
rep, err := p.ProcessClientRequest(nil, "testMsg", buf)
require.Equal(t, nil, err)
val := &testMsg{}
log.ErrFatal(protobuf.Decode(rep, val))
if val.I != 11 {
t.Fatal("Value got lost - should be 11")
}
buf, err = protobuf.Encode(&testMsg{42})
log.ErrFatal(err)
_, err = p.ProcessClientRequest(nil, "testMsg", buf)
assert.NotNil(t, err)
buf, err = protobuf.Encode(&testMsg2{42})
log.ErrFatal(err)
_, err = p.ProcessClientRequest(nil, "testMsg2", buf)
assert.NotNil(t, err)
// Test non-existing endpoint
buf, err = protobuf.Encode(&testMsg2{42})
log.ErrFatal(err)
log.OutputToBuf()
_, err = p.ProcessClientRequest(nil, "testMsgNotAvailable", buf)
log.OutputToOs()
assert.NotNil(t, err)
assert.NotEqual(t, "", log.GetStdOut())
}
func TestProcessor_ProcessClientRequest(t *testing.T) {
local := NewTCPTest(tSuite)
// generate 5 hosts,
h := local.GenServers(1)[0]
defer local.CloseAll()
client := local.NewClient(testServiceName)
msg := &testMsg{}
err := client.SendProtobuf(h.ServerIdentity, &testMsg{12}, msg)
log.ErrFatal(err)
if msg == nil {
t.Fatal("Msg should not be nil")
}
if msg.I != 12 {
t.Fatal("Didn't send 12")
}
}
type testMsg struct {
I int
}
type testMsg2 testMsg
type testMsg3 testMsg
type testMsg4 testMsg
type testMsg5 testMsg
func procMsg(msg *testMsg) (network.Message, error) {
// Return an error for testing
if msg.I == 42 {
return nil, errors.New("42 is NOT the answer")
}
return msg, nil
}
func procMsg2(msg *testMsg2) (network.Message, error) {
// Return an error for testing
if msg.I == 42 {
return nil, errors.New("42 is NOT the answer")
}
return nil, nil
}
func procMsg3(msg *testMsg3) (network.Message, error) {
return nil, nil
}
func procMsg4(msg *testMsg4) (*testMsg4, error) {
return msg, nil
}
func procMsgWrong1() (network.Message, error) {
return nil, nil
}
func procMsgWrong2(msg testMsg2) (network.Message, error) {
return msg, nil
}
func procMsgWrong3(msg *testMsg3) error {
return nil
}
func procMsgWrong4(msg *testMsg4) (error, network.Message) {
return nil, msg
}
func procMsgWrong5(msg *testMsg) (*network.Message, error) {
return nil, nil
}
func procMsgWrong6(msg *testMsg) (int, error) {
return 10, nil
}
func procMsgWrong7(msg *testMsg) (testMsg, error) {
return *msg, nil
}
type testService struct {
*ServiceProcessor
Msg interface{}
}
func newTestService(c *Context) (Service, error) {
ts := &testService{
ServiceProcessor: NewServiceProcessor(c),
}
log.ErrFatal(ts.RegisterHandler(ts.ProcessMsg))
return ts, nil
}
func (ts *testService) NewProtocol(tn *TreeNodeInstance, conf *GenericConfig) (ProtocolInstance, error) {
return nil, nil
}
func (ts *testService) ProcessMsg(msg *testMsg) (network.Message, error) {
ts.Msg = msg
return msg, nil
}