summaryrefslogtreecommitdiffstats
path: root/core/core_test.go
blob: 981ff19f36bccfdc12e2216544013245e0b8d157 (plain)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package core_test

import (
	"testing"

	"git.fd.io/govpp.git/adapter/mock"
	"git.fd.io/govpp.git/api"
	"git.fd.io/govpp.git/core"
	"git.fd.io/govpp.git/core/bin_api/vpe"
	"git.fd.io/govpp.git/examples/bin_api/interfaces"
	"git.fd.io/govpp.git/examples/bin_api/stats"

	. "github.com/onsi/gomega"
)

type testCtx struct {
	mockVpp *mock.VppAdapter
	conn    *core.Connection
	ch      *api.Channel
}

func setupTest(t *testing.T, bufferedChan bool) *testCtx {
	RegisterTestingT(t)

	ctx := &testCtx{}
	ctx.mockVpp = &mock.VppAdapter{}

	var err error
	ctx.conn, err = core.Connect(ctx.mockVpp)
	Expect(err).ShouldNot(HaveOccurred())

	if bufferedChan {
		ctx.ch, err = ctx.conn.NewAPIChannelBuffered(100, 100)
	} else {
		ctx.ch, err = ctx.conn.NewAPIChannel()
	}
	Expect(err).ShouldNot(HaveOccurred())

	return ctx
}

func (ctx *testCtx) teardownTest() {
	ctx.ch.Close()
	ctx.conn.Disconnect()
}

func TestSimpleRequest(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: -5}, false)

	req := &vpe.ControlPing{}
	reply := &vpe.ControlPingReply{}

	// send the request and receive a reply
	ctx.ch.ReqChan <- &api.VppRequest{Message: req}
	vppReply := <-ctx.ch.ReplyChan

	Expect(vppReply).ShouldNot(BeNil())
	Expect(vppReply.Error).ShouldNot(HaveOccurred())

	// decode the message
	err := ctx.ch.MsgDecoder.DecodeMsg(vppReply.Data, reply)
	Expect(err).ShouldNot(HaveOccurred())

	Expect(reply.Retval).To(BeEquivalentTo(-5))
}

func TestMultiRequest(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	for m := 0; m < 10; m++ {
		ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{}, true)
	}
	ctx.mockVpp.MockReply(&vpe.ControlPingReply{}, true)

	// send multipart request
	ctx.ch.ReqChan <- &api.VppRequest{Message: &interfaces.SwInterfaceDump{}, Multipart: true}

	cnt := 0
	for {
		// receive a reply
		vppReply := <-ctx.ch.ReplyChan
		if vppReply.LastReplyReceived {
			break // break out of the loop
		}
		Expect(vppReply.Error).ShouldNot(HaveOccurred())

		// decode the message
		reply := &interfaces.SwInterfaceDetails{}
		err := ctx.ch.MsgDecoder.DecodeMsg(vppReply.Data, reply)
		Expect(err).ShouldNot(HaveOccurred())
		cnt++
	}

	Expect(cnt).To(BeEquivalentTo(10))
}

func TestNotifications(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	// subscribe for notification
	notifChan := make(chan api.Message, 1)
	subscription := &api.NotifSubscription{
		NotifChan:  notifChan,
		MsgFactory: interfaces.NewSwInterfaceSetFlags,
	}
	ctx.ch.NotifSubsChan <- &api.NotifSubscribeRequest{
		Subscription: subscription,
		Subscribe:    true,
	}
	err := <-ctx.ch.NotifSubsReplyChan
	Expect(err).ShouldNot(HaveOccurred())

	// mock the notification and force its delivery
	ctx.mockVpp.MockReply(&interfaces.SwInterfaceSetFlags{
		SwIfIndex:   3,
		AdminUpDown: 1,
	}, false)
	ctx.mockVpp.SendMsg(0, []byte{0})

	// receive the notification
	notif := (<-notifChan).(*interfaces.SwInterfaceSetFlags)

	Expect(notif.SwIfIndex).To(BeEquivalentTo(3))

	// unsubscribe notification
	ctx.ch.NotifSubsChan <- &api.NotifSubscribeRequest{
		Subscription: subscription,
		Subscribe:    false,
	}
	err = <-ctx.ch.NotifSubsReplyChan
	Expect(err).ShouldNot(HaveOccurred())
}

func TestNilConnection(t *testing.T) {
	RegisterTestingT(t)
	var conn *core.Connection

	ch, err := conn.NewAPIChannel()
	Expect(ch).Should(BeNil())
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("nil"))

	ch, err = conn.NewAPIChannelBuffered(1, 1)
	Expect(ch).Should(BeNil())
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("nil"))
}

func TestDoubleConnection(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	conn, err := core.Connect(ctx.mockVpp)
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("only one connection per process"))
	Expect(conn).Should(BeNil())
}

func TestAsyncConnection(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	ctx.conn.Disconnect()
	conn, ch, err := core.AsyncConnect(ctx.mockVpp)
	ctx.conn = conn

	Expect(err).ShouldNot(HaveOccurred())
	Expect(conn).ShouldNot(BeNil())

	ev := <-ch
	Expect(ev.State).Should(BeEquivalentTo(core.Connected))
}

func TestFullBuffer(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	// close the default API channel
	ctx.ch.Close()

	// create a new channel with limited buffer sizes
	var err error
	ctx.ch, err = ctx.conn.NewAPIChannelBuffered(10, 1)
	Expect(err).ShouldNot(HaveOccurred())

	// send multiple requests, only one reply should be read
	for i := 0; i < 20; i++ {
		ctx.mockVpp.MockReply(&vpe.ControlPingReply{}, false)
		ctx.ch.ReqChan <- &api.VppRequest{Message: &vpe.ControlPing{}}
	}

	vppReply := <-ctx.ch.ReplyChan
	Expect(vppReply).ShouldNot(BeNil())

	var received bool
	select {
	case <-ctx.ch.ReplyChan:
		received = true // this should not happen
	default:
		received = false // no reply to be received
	}
	Expect(received).Should(BeFalse(), "A reply has been recieved, should had been ignored.")
}

func TestCodec(t *testing.T) {
	RegisterTestingT(t)

	codec := &core.MsgCodec{}

	// request
	data, err := codec.EncodeMsg(&interfaces.CreateLoopback{MacAddress: []byte{1, 2, 3, 4, 5, 6}}, 11)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(data).ShouldNot(BeEmpty())

	msg1 := &interfaces.CreateLoopback{}
	err = codec.DecodeMsg(data, msg1)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(msg1.MacAddress).To(BeEquivalentTo([]byte{1, 2, 3, 4, 5, 6}))

	// reply
	data, err = codec.EncodeMsg(&vpe.ControlPingReply{Retval: 55}, 22)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(data).ShouldNot(BeEmpty())

	msg2 := &vpe.ControlPingReply{}
	err = codec.DecodeMsg(data, msg2)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(msg2.Retval).To(BeEquivalentTo(55))

	// other
	data, err = codec.EncodeMsg(&stats.VnetIP4FibCounters{VrfID: 77}, 33)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(data).ShouldNot(BeEmpty())

	msg3 := &stats.VnetIP4FibCounters{}
	err = codec.DecodeMsg(data, msg3)
	Expect(err).ShouldNot(HaveOccurred())
	Expect(msg3.VrfID).To(BeEquivalentTo(77))
}

func TestCodecNegative(t *testing.T) {
	RegisterTestingT(t)

	codec := &core.MsgCodec{}

	// nil message for encoding
	data, err := codec.EncodeMsg(nil, 15)
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("nil message"))
	Expect(data).Should(BeNil())

	// nil message for decoding
	err = codec.DecodeMsg(data, nil)
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("nil message"))

	// nil data for decoding
	err = codec.DecodeMsg(nil, &vpe.ControlPingReply{})
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("EOF"))
}

func TestSimpleRequestsWithSequenceNumbers(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	var reqCtx []*api.RequestCtx
	for i := 0; i < 10; i++ {
		ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: int32(i)}, false)
		req := &vpe.ControlPing{}
		reqCtx = append(reqCtx, ctx.ch.SendRequest(req))
	}

	for i := 0; i < 10; i++ {
		reply := &vpe.ControlPingReply{}
		err := reqCtx[i].ReceiveReply(reply)
		Expect(err).ShouldNot(HaveOccurred())
		Expect(reply.Retval).To(BeEquivalentTo(i))
	}
}

func TestMultiRequestsWithSequenceNumbers(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	for i := 0; i < 10; i++ {
		ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{SwIfIndex: uint32(i)}, true)
	}
	ctx.mockVpp.MockReply(&vpe.ControlPingReply{}, true)

	// send multipart request
	reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})

	cnt := 0
	for {
		Expect(cnt < 11).To(BeTrue())

		// receive a reply
		reply := &interfaces.SwInterfaceDetails{}
		lastReplyReceived, err := reqCtx.ReceiveReply(reply)

		if lastReplyReceived {
			break // break out of the loop
		}

		Expect(err).ShouldNot(HaveOccurred())
		Expect(reply.SwIfIndex).To(BeEquivalentTo(cnt))

		cnt++
	}

	Expect(cnt).To(BeEquivalentTo(10))
}

func TestSimpleRequestWithTimeout(t *testing.T) {
	ctx := setupTest(t, true)
	defer ctx.teardownTest()

	// reply for a previous timeouted requests to be ignored
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 1}, false,0)

	// send reply later
	req1 := &vpe.ControlPing{}
	reqCtx1 := ctx.ch.SendRequest(req1)

	reply := &vpe.ControlPingReply{}
	err := reqCtx1.ReceiveReply(reply)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(HavePrefix("no reply received within the timeout period"))

	// reply for the previous request
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 1}, false,1)

	// next request
	ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: 2}, false)
	req2 := &vpe.ControlPing{}
	reqCtx2 := ctx.ch.SendRequest(req2)

	// second request should ignore the first reply and return the second one
	reply = &vpe.ControlPingReply{}
	err = reqCtx2.ReceiveReply(reply)
	Expect(err).To(BeNil())
	Expect(reply.Retval).To(BeEquivalentTo(2))
}

func TestSimpleRequestsWithMissingReply(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	// request without reply
	req1 := &vpe.ControlPing{}
	reqCtx1 := ctx.ch.SendRequest(req1)

	// another request without reply
	req2 := &vpe.ControlPing{}
	reqCtx2 := ctx.ch.SendRequest(req2)

	// third request with reply
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 3}, false, 3)
	req3 := &vpe.ControlPing{}
	reqCtx3 := ctx.ch.SendRequest(req3)

	// the first two should fail, but not consume reply for the 3rd
	reply := &vpe.ControlPingReply{}
	err := reqCtx1.ReceiveReply(reply)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 1"))

	reply = &vpe.ControlPingReply{}
	err = reqCtx2.ReceiveReply(reply)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 2"))

	// the second request should succeed
	reply = &vpe.ControlPingReply{}
	err = reqCtx3.ReceiveReply(reply)
	Expect(err).To(BeNil())
	Expect(reply.Retval).To(BeEquivalentTo(3))
}

func TestMultiRequestsWithErrors(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	// reply for a previous timeouted requests to be ignored
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 1}, false,0xffff - 1)
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 1}, false,0xffff)
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 1}, false,0)

	for i := 0; i < 10; i++ {
		ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{SwIfIndex: uint32(i)}, true)
	}
	// missing finalizing control ping

	// reply for a next request
	ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: 2}, false,2)

	// send multipart request
	reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})

	for i := 0; i < 10; i++ {
		// receive multi-part replies
		reply := &interfaces.SwInterfaceDetails{}
		lastReplyReceived, err := reqCtx.ReceiveReply(reply)

		Expect(lastReplyReceived).To(BeFalse())
		Expect(err).ShouldNot(HaveOccurred())
		Expect(reply.SwIfIndex).To(BeEquivalentTo(i))
	}

	// missing closing control ping
	reply := &interfaces.SwInterfaceDetails{}
	_, err := reqCtx.ReceiveReply(reply)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 1"))

	// try again - still fails and nothing consumed
	_, err = reqCtx.ReceiveReply(reply)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 1"))

	// reply for the second request has not been consumed
	reqCtx2 := ctx.ch.SendRequest(&vpe.ControlPing{})
	reply2 := &vpe.ControlPingReply{}
	err = reqCtx2.ReceiveReply(reply2)
	Expect(err).To(BeNil())
	Expect(reply2.Retval).To(BeEquivalentTo(2))
}

func TestRequestsOrdering(t *testing.T) {
	ctx := setupTest(t, false)
	defer ctx.teardownTest()

	// the orderings of SendRequest and ReceiveReply calls should match, otherwise
	// some replies will get thrown away

	// first request
	ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: 1}, false)
	req1 := &vpe.ControlPing{}
	reqCtx1 := ctx.ch.SendRequest(req1)

	// second request
	ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: 2}, false)
	req2 := &vpe.ControlPing{}
	reqCtx2 := ctx.ch.SendRequest(req2)

	// if reply for the second request is read first, the reply for the first
	// request gets thrown away.
	reply2 := &vpe.ControlPingReply{}
	err := reqCtx2.ReceiveReply(reply2)
	Expect(err).To(BeNil())
	Expect(reply2.Retval).To(BeEquivalentTo(2))

	// first request has already been considered closed
	reply1 := &vpe.ControlPingReply{}
	err = reqCtx1.ReceiveReply(reply1)
	Expect(err).ToNot(BeNil())
	Expect(err.Error()).To(HavePrefix("no reply received within the timeout period"))
}

func TestCycleOverSetOfSequenceNumbers(t *testing.T) {
	ctx := setupTest(t, true)
	defer ctx.teardownTest()

	numIters := 0xffff + 100
	reqCtx := make(map[int]*api.RequestCtx)

	var seqNum uint16 = 0
	for i := 0; i < numIters + 30 /* receiver is 30 reqs behind */; i++ {
		seqNum++
		if i < numIters {
			ctx.mockVpp.MockReplyWithSeqNum(&vpe.ControlPingReply{Retval: int32(i)}, false, seqNum)
			req := &vpe.ControlPing{}
			reqCtx[i] = ctx.ch.SendRequest(req)
		}
		if i > 30 {
			reply := &vpe.ControlPingReply{}
			err := reqCtx[i-30].ReceiveReply(reply)
			Expect(err).ShouldNot(HaveOccurred())
			Expect(reply.Retval).To(BeEquivalentTo(i-30))
		}
	}
}