gopkg.in/dedis/onet.v2@v2.0.0-20181115163211-c8f3724038a7/processor_test.go (about) 1 package onet 2 3 import ( 4 "errors" 5 "testing" 6 7 "reflect" 8 9 "github.com/dedis/protobuf" 10 "github.com/stretchr/testify/require" 11 "gopkg.in/dedis/onet.v2/log" 12 "gopkg.in/dedis/onet.v2/network" 13 ) 14 15 const testServiceName = "testService" 16 17 func init() { 18 RegisterNewService(testServiceName, newTestService) 19 ServiceFactory.ServiceID(testServiceName) 20 network.RegisterMessage(&testMsg{}) 21 } 22 23 func TestProcessor_AddMessage(t *testing.T) { 24 h1 := NewLocalServer(tSuite, 2000) 25 defer h1.Close() 26 p := NewServiceProcessor(&Context{server: h1}) 27 require.Nil(t, p.RegisterHandler(procMsg)) 28 if len(p.handlers) != 1 { 29 require.Fail(t, "Should have registered one function") 30 } 31 mt := network.MessageType(&testMsg{}) 32 if mt.Equal(network.ErrorType) { 33 require.Fail(t, "Didn't register message-type correctly") 34 } 35 var wrongFunctions = []interface{}{ 36 procMsgWrong1, 37 procMsgWrong2, 38 procMsgWrong3, 39 procMsgWrong4, 40 procMsgWrong5, 41 procMsgWrong6, 42 procMsgWrong7, 43 } 44 for _, f := range wrongFunctions { 45 fsig := reflect.TypeOf(f).String() 46 log.Lvl2("Checking function", fsig) 47 require.Error(t, p.RegisterHandler(f), 48 "Could register wrong function: "+fsig) 49 } 50 } 51 52 func TestProcessor_RegisterMessages(t *testing.T) { 53 h1 := NewLocalServer(tSuite, 2000) 54 defer h1.Close() 55 p := NewServiceProcessor(&Context{server: h1}) 56 require.Nil(t, p.RegisterHandlers(procMsg, procMsg2, procMsg3, procMsg4)) 57 require.Error(t, p.RegisterHandlers(procMsg3, procMsgWrong4)) 58 } 59 60 func TestProcessor_RegisterStreamingMessage(t *testing.T) { 61 h1 := NewLocalServer(tSuite, 2000) 62 defer h1.Close() 63 p := NewServiceProcessor(&Context{server: h1}) 64 65 // correct registration 66 f1 := func(m *testMsg) (chan network.Message, chan bool, error) { 67 return make(chan network.Message), make(chan bool), nil 68 } 69 f2 := func(m *testMsg) (chan *testMsg, chan bool, error) { 70 return make(chan *testMsg), make(chan bool), nil 71 } 72 require.Nil(t, p.RegisterStreamingHandlers(f1, f2)) 73 74 // wrong registrations 75 require.Error(t, p.RegisterStreamingHandler( 76 func(m int) (chan network.Message, chan bool, error) { 77 return nil, nil, nil 78 }), "input must be a pointer to struct") 79 require.Error(t, p.RegisterStreamingHandler( 80 func(m testMsg) (chan network.Message, chan bool, error) { 81 return nil, nil, nil 82 }), "input must be a pointer to struct") 83 require.Error(t, p.RegisterStreamingHandler( 84 func(m *testMsg) (chan int, chan bool, error) { 85 return nil, nil, nil 86 }), "first return must be a pointer or interface") 87 require.Error(t, p.RegisterStreamingHandler( 88 func(m *testMsg) (chan testMsg, chan bool, error) { 89 return nil, nil, nil 90 }), "first return must be a pointer or interface") 91 require.Error(t, p.RegisterStreamingHandler( 92 func(m *testMsg) (chan testMsg, error) { 93 return nil, nil 94 }), "must have three return values") 95 require.Error(t, p.RegisterStreamingHandler( 96 func(m *testMsg) (chan testMsg, chan int, error) { 97 return nil, nil, nil 98 }), "second return must be a boolean channel") 99 require.Error(t, p.RegisterStreamingHandler( 100 func(m *testMsg) (chan testMsg, []int, error) { 101 return nil, nil, nil 102 }), "second return must be a boolean channel") 103 require.Error(t, p.RegisterStreamingHandler( 104 func(m *testMsg) (chan testMsg, chan int, []byte) { 105 return nil, nil, nil 106 }), "must return an error") 107 } 108 109 func TestServiceProcessor_ProcessClientRequest(t *testing.T) { 110 h1 := NewLocalServer(tSuite, 2000) 111 defer h1.Close() 112 p := NewServiceProcessor(&Context{server: h1}) 113 require.Nil(t, p.RegisterHandlers(procMsg, procMsg2)) 114 115 buf, err := protobuf.Encode(&testMsg{11}) 116 require.Nil(t, err) 117 rep, repChan, err := p.ProcessClientRequest(nil, "testMsg", buf) 118 require.Nil(t, repChan) 119 require.NoError(t, err) 120 val := &testMsg{} 121 require.Nil(t, protobuf.Decode(rep, val)) 122 if val.I != 11 { 123 require.Fail(t, "Value got lost - should be 11") 124 } 125 126 buf, err = protobuf.Encode(&testMsg{42}) 127 require.Nil(t, err) 128 _, _, err = p.ProcessClientRequest(nil, "testMsg", buf) 129 require.NotNil(t, err) 130 131 buf, err = protobuf.Encode(&testMsg2{42}) 132 require.Nil(t, err) 133 _, _, err = p.ProcessClientRequest(nil, "testMsg2", buf) 134 require.NotNil(t, err) 135 136 // Test non-existing endpoint 137 buf, err = protobuf.Encode(&testMsg2{42}) 138 require.Nil(t, err) 139 lvl := log.DebugVisible() 140 log.SetDebugVisible(0) 141 log.OutputToBuf() 142 _, _, err = p.ProcessClientRequest(nil, "testMsgNotAvailable", buf) 143 log.OutputToOs() 144 log.SetDebugVisible(lvl) 145 require.NotNil(t, err) 146 require.NotEqual(t, "", log.GetStdOut()) 147 } 148 149 func TestServiceProcessor_ProcessClientRequest_Streaming(t *testing.T) { 150 h1 := NewLocalServer(tSuite, 2000) 151 defer h1.Close() 152 153 p := NewServiceProcessor(&Context{server: h1}) 154 h := func(m *testMsg) (chan network.Message, chan bool, error) { 155 outChan := make(chan network.Message) 156 go func() { 157 for i := 0; i < m.I; i++ { 158 outChan <- m 159 } 160 close(outChan) 161 }() 162 return outChan, nil, nil 163 } 164 require.Nil(t, p.RegisterStreamingHandler(h)) 165 166 n := 5 167 buf, err := protobuf.Encode(&testMsg{n}) 168 require.NoError(t, err) 169 rep, tun, err := p.ProcessClientRequest(nil, "testMsg", buf) 170 require.Nil(t, rep) 171 require.NoError(t, err) 172 173 for i := 0; i < n+1; i++ { 174 if i >= n { 175 _, ok := <-tun.out 176 require.False(t, ok) 177 } else { 178 buf := <-tun.out 179 val := &testMsg{} 180 require.Nil(t, protobuf.Decode(buf, val)) 181 require.Equal(t, val.I, n) 182 } 183 } 184 } 185 186 func TestProcessor_ProcessClientRequest(t *testing.T) { 187 local := NewTCPTest(tSuite) 188 189 // generate 5 hosts, 190 h := local.GenServers(1)[0] 191 defer local.CloseAll() 192 193 client := local.NewClient(testServiceName) 194 msg := &testMsg{} 195 err := client.SendProtobuf(h.ServerIdentity, &testMsg{12}, msg) 196 require.Nil(t, err) 197 if msg == nil { 198 require.Fail(t, "Msg should not be nil") 199 } 200 if msg.I != 12 { 201 require.Fail(t, "Didn't send 12") 202 } 203 } 204 205 type testMsg struct { 206 I int 207 } 208 209 type testMsg2 testMsg 210 type testMsg3 testMsg 211 type testMsg4 testMsg 212 type testMsg5 testMsg 213 214 func procMsg(msg *testMsg) (network.Message, error) { 215 // Return an error for testing 216 if msg.I == 42 { 217 return nil, errors.New("42 is NOT the answer") 218 } 219 return msg, nil 220 } 221 222 func procMsg2(msg *testMsg2) (network.Message, error) { 223 // Return an error for testing 224 if msg.I == 42 { 225 return nil, errors.New("42 is NOT the answer") 226 } 227 return nil, nil 228 } 229 func procMsg3(msg *testMsg3) (network.Message, error) { 230 return nil, nil 231 } 232 func procMsg4(msg *testMsg4) (*testMsg4, error) { 233 return msg, nil 234 } 235 236 func procMsgWrong1() (network.Message, error) { 237 return nil, nil 238 } 239 240 func procMsgWrong2(msg testMsg2) (network.Message, error) { 241 return msg, nil 242 } 243 244 func procMsgWrong3(msg *testMsg3) error { 245 return nil 246 } 247 248 func procMsgWrong4(msg *testMsg4) (error, network.Message) { 249 return nil, msg 250 } 251 252 func procMsgWrong5(msg *testMsg) (*network.Message, error) { 253 return nil, nil 254 } 255 256 func procMsgWrong6(msg *testMsg) (int, error) { 257 return 10, nil 258 } 259 func procMsgWrong7(msg *testMsg) (testMsg, error) { 260 return *msg, nil 261 } 262 263 type testService struct { 264 *ServiceProcessor 265 Msg interface{} 266 } 267 268 func newTestService(c *Context) (Service, error) { 269 ts := &testService{ 270 ServiceProcessor: NewServiceProcessor(c), 271 } 272 if err := ts.RegisterHandler(ts.ProcessMsg); err != nil { 273 panic(err.Error()) 274 } 275 return ts, nil 276 } 277 278 func (ts *testService) NewProtocol(tn *TreeNodeInstance, conf *GenericConfig) (ProtocolInstance, error) { 279 return nil, nil 280 } 281 282 func (ts *testService) ProcessMsg(msg *testMsg) (network.Message, error) { 283 ts.Msg = msg 284 return msg, nil 285 }