1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
20 "git.fd.io/govpp.git/adapter/mock"
21 "git.fd.io/govpp.git/api"
22 "git.fd.io/govpp.git/core/bin_api/vpe"
23 "git.fd.io/govpp.git/examples/bin_api/interfaces"
25 . "github.com/onsi/gomega"
29 mockVpp *mock.VppAdapter
34 func setupTest(t *testing.T) *testCtx {
38 ctx.mockVpp = &mock.VppAdapter{}
41 ctx.conn, err = Connect(ctx.mockVpp)
42 Expect(err).ShouldNot(HaveOccurred())
44 ctx.ch, err = ctx.conn.NewAPIChannel()
45 Expect(err).ShouldNot(HaveOccurred())
50 func (ctx *testCtx) teardownTest() {
55 func TestSimpleRequest(t *testing.T) {
57 defer ctx.teardownTest()
59 ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: -5})
61 req := &vpe.ControlPing{}
62 reply := &vpe.ControlPingReply{}
64 // send the request and receive a reply
65 ctx.ch.ReqChan <- &api.VppRequest{Message: req}
66 vppReply := <-ctx.ch.ReplyChan
68 Expect(vppReply).ShouldNot(BeNil())
69 Expect(vppReply.Error).ShouldNot(HaveOccurred())
72 err := ctx.ch.MsgDecoder.DecodeMsg(vppReply.Data, reply)
73 Expect(err).ShouldNot(HaveOccurred())
75 Expect(reply.Retval).To(BeEquivalentTo(-5))
78 func TestMultiRequest(t *testing.T) {
80 defer ctx.teardownTest()
82 for m := 0; m < 10; m++ {
83 ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{})
85 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
87 // send multipart request
88 ctx.ch.ReqChan <- &api.VppRequest{Message: &interfaces.SwInterfaceDump{}, Multipart: true}
93 vppReply := <-ctx.ch.ReplyChan
94 if vppReply.LastReplyReceived {
95 break // break out of the loop
97 Expect(vppReply.Error).ShouldNot(HaveOccurred())
100 reply := &interfaces.SwInterfaceDetails{}
101 err := ctx.ch.MsgDecoder.DecodeMsg(vppReply.Data, reply)
102 Expect(err).ShouldNot(HaveOccurred())
106 Expect(cnt).To(BeEquivalentTo(10))
109 func TestNotifications(t *testing.T) {
111 defer ctx.teardownTest()
113 // subscribe for notification
114 notifChan := make(chan api.Message, 1)
115 subscription := &api.NotifSubscription{
116 NotifChan: notifChan,
117 MsgFactory: interfaces.NewSwInterfaceSetFlags,
119 ctx.ch.NotifSubsChan <- &api.NotifSubscribeRequest{
120 Subscription: subscription,
123 err := <-ctx.ch.NotifSubsReplyChan
124 Expect(err).ShouldNot(HaveOccurred())
126 // mock the notification and force its delivery
127 ctx.mockVpp.MockReply(&interfaces.SwInterfaceSetFlags{
131 ctx.mockVpp.SendMsg(0, []byte{0})
133 // receive the notification
134 notif := (<-notifChan).(*interfaces.SwInterfaceSetFlags)
136 Expect(notif.SwIfIndex).To(BeEquivalentTo(3))
138 // unsubscribe notification
139 ctx.ch.NotifSubsChan <- &api.NotifSubscribeRequest{
140 Subscription: subscription,
143 err = <-ctx.ch.NotifSubsReplyChan
144 Expect(err).ShouldNot(HaveOccurred())
147 func TestNilConnection(t *testing.T) {
150 ch, err := conn.NewAPIChannel()
151 Expect(ch).Should(BeNil())
152 Expect(err).Should(HaveOccurred())
153 Expect(err.Error()).To(ContainSubstring("nil"))
155 ch, err = conn.NewAPIChannelBuffered(1, 1)
156 Expect(ch).Should(BeNil())
157 Expect(err).Should(HaveOccurred())
158 Expect(err.Error()).To(ContainSubstring("nil"))
161 func TestDoubleConnection(t *testing.T) {
163 defer ctx.teardownTest()
165 conn, err := Connect(ctx.mockVpp)
166 Expect(err).Should(HaveOccurred())
167 Expect(err.Error()).To(ContainSubstring("only one connection per process"))
168 Expect(conn).Should(BeNil())
171 func TestFullBuffer(t *testing.T) {
173 defer ctx.teardownTest()
175 // close the default API channel
178 // create a new channel with limited buffer sizes
180 ctx.ch, err = ctx.conn.NewAPIChannelBuffered(10, 1)
181 Expect(err).ShouldNot(HaveOccurred())
183 // send multiple requests, only one reply should be read
184 for i := 0; i < 20; i++ {
185 ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
186 ctx.ch.ReqChan <- &api.VppRequest{Message: &vpe.ControlPing{}}
189 vppReply := <-ctx.ch.ReplyChan
190 Expect(vppReply).ShouldNot(BeNil())
194 case vppReply = <-ctx.ch.ReplyChan:
195 received = true // this should not happen
197 received = false // no reply to be received
199 Expect(received).Should(BeFalse(), "A reply has been recieved, should had been ignored.")
202 func TestCodec(t *testing.T) {
208 data, err := codec.EncodeMsg(&vpe.CreateLoopback{MacAddress: []byte{1, 2, 3, 4, 5, 6}}, 11)
209 Expect(err).ShouldNot(HaveOccurred())
210 Expect(data).ShouldNot(BeEmpty())
212 msg1 := &vpe.CreateLoopback{}
213 err = codec.DecodeMsg(data, msg1)
214 Expect(err).ShouldNot(HaveOccurred())
215 Expect(msg1.MacAddress).To(BeEquivalentTo([]byte{1, 2, 3, 4, 5, 6}))
218 data, err = codec.EncodeMsg(&vpe.ControlPingReply{Retval: 55}, 22)
219 Expect(err).ShouldNot(HaveOccurred())
220 Expect(data).ShouldNot(BeEmpty())
222 msg2 := &vpe.ControlPingReply{}
223 err = codec.DecodeMsg(data, msg2)
224 Expect(err).ShouldNot(HaveOccurred())
225 Expect(msg2.Retval).To(BeEquivalentTo(55))
228 data, err = codec.EncodeMsg(&vpe.VnetIP4FibCounters{VrfID: 77}, 33)
229 Expect(err).ShouldNot(HaveOccurred())
230 Expect(data).ShouldNot(BeEmpty())
232 msg3 := &vpe.VnetIP4FibCounters{}
233 err = codec.DecodeMsg(data, msg3)
234 Expect(err).ShouldNot(HaveOccurred())
235 Expect(msg3.VrfID).To(BeEquivalentTo(77))
238 func TestCodecNegative(t *testing.T) {
243 // nil message for encoding
244 data, err := codec.EncodeMsg(nil, 15)
245 Expect(err).Should(HaveOccurred())
246 Expect(err.Error()).To(ContainSubstring("nil message"))
247 Expect(data).Should(BeNil())
249 // nil message for decoding
250 err = codec.DecodeMsg(data, nil)
251 Expect(err).Should(HaveOccurred())
252 Expect(err.Error()).To(ContainSubstring("nil message"))
254 // nil data for decoding
255 err = codec.DecodeMsg(nil, &vpe.ControlPingReply{})
256 Expect(err).Should(HaveOccurred())
257 Expect(err.Error()).To(ContainSubstring("EOF"))