1 // Copyright (c) 2020 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.
23 "github.com/lunixbochs/struc"
25 "git.fd.io/govpp.git/api"
26 "git.fd.io/govpp.git/codec"
27 "git.fd.io/govpp.git/codec/testdata/binapi2001/interfaces"
28 "git.fd.io/govpp.git/codec/testdata/binapi2001/ip"
31 // CliInband represents VPP binary API message 'cli_inband'.
32 type CliInband struct {
33 XXX_CmdLen uint32 `struc:"sizeof=Cmd"`
37 func (m *CliInband) Reset() { *m = CliInband{} }
38 func (*CliInband) GetMessageName() string { return "cli_inband" }
39 func (*CliInband) GetCrcString() string { return "f8377302" }
40 func (*CliInband) GetMessageType() api.MessageType { return api.RequestMessage }
42 // CliInbandReply represents VPP binary API message 'cli_inband_reply'.
43 type CliInbandReply struct {
45 XXX_ReplyLen uint32 `struc:"sizeof=Reply"`
49 func (m *CliInbandReply) Reset() { *m = CliInbandReply{} }
50 func (*CliInbandReply) GetMessageName() string { return "cli_inband_reply" }
51 func (*CliInbandReply) GetCrcString() string { return "05879051" }
52 func (*CliInbandReply) GetMessageType() api.MessageType { return api.ReplyMessage }
54 func TestWrapperEncode(t *testing.T) {
59 expectedData := []byte{
61 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x05,
64 0x61, 0x62, 0x63, 0x64, 0x65,
67 c := codec.DefaultCodec
69 data, err := c.EncodeMsg(msg, 100)
71 t.Fatalf("EncodeMsg failed: %v", err)
73 if !bytes.Equal(data, expectedData) {
74 t.Fatalf("unexpected encoded data,\nexpected: % 02x\n got: % 02x\n", expectedData, data)
78 func TestWrapperDecode(t *testing.T) {
81 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x05,
84 0x61, 0x62, 0x63, 0x64, 0x65,
86 expectedMsg := &CliInbandReply{
92 c := codec.DefaultCodec
94 msg := new(CliInbandReply)
95 err := c.DecodeMsg(data, msg)
97 t.Fatalf("DecodeMsg failed: %v", err)
99 if !reflect.DeepEqual(msg, expectedMsg) {
100 t.Fatalf("unexpected decoded msg,\nexpected: %+v\n got: %+v\n", expectedMsg, msg)
104 /*func TestNewCodecEncodeDecode(t *testing.T) {
113 AliasArray: MacAddress{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
114 BaseArray: []uint32{0x00, 0x00, 0x00, 0x00},
115 Enum: IF_STATUS_API_FLAG_LINK_UP,
122 Slice: []byte{10, 20, 30, 40, 0, 0, 0},
123 String: "abcdefghikl",
125 VariableSlice: []SliceType{
126 {Proto: IP_API_PROTO_AH},
127 {Proto: IP_API_PROTO_ESP},
131 Un: AddressUnionIP4(IP4Address{1, 2, 3, 4}),
136 for _, test := range tests {
137 t.Run(test.name, func(t *testing.T) {
138 data, err := test.msg.Marshal()
140 t.Fatalf("expected nil error, got: %v", err)
144 if err := m2.Unmarshal(data); err != nil {
145 t.Fatalf("expected nil error, got: %v", err)
148 t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
150 if !reflect.DeepEqual(m, &m2) {
151 t.Fatalf("newData differs from oldData")
157 func NewTestAllMsg() *TestAllMsg {
161 AliasArray: MacAddress{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
162 BaseArray: []uint32{0x00, 0x00, 0x00, 0x00},
163 Enum: IF_STATUS_API_FLAG_LINK_UP,
170 Slice: []byte{10, 20, 30, 40, 0, 0, 0},
171 String: "abcdefghikl",
173 VariableSlice: []SliceType{
174 {Proto: IP_API_PROTO_AH},
175 {Proto: IP_API_PROTO_ESP},
179 Un: AddressUnionIP4(IP4Address{1, 2, 3, 4}),
184 func TestNewCodecEncodeDecode_(t *testing.T) {
187 data, err := m.Marshal(nil)
189 t.Fatalf("expected nil error, got: %v", err)
193 if err := m2.Unmarshal(data); err != nil {
194 t.Fatalf("expected nil error, got: %v", err)
197 t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
199 if !reflect.DeepEqual(m, &m2) {
200 t.Fatalf("newData differs from oldData")
206 func TestNewCodecEncodeDecode3(t *testing.T) {
209 data, err := m.Marshal(nil)
211 t.Fatalf("expected nil error, got: %v", err)
215 if err := m2.Unmarshal(data); err != nil {
216 t.Fatalf("expected nil error, got: %v", err)
219 t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
221 if !reflect.DeepEqual(m, &m2) {
222 t.Fatalf("newData differs from oldData")
225 func TestNewCodecEncodeDecode4(t *testing.T) {
226 m := &interfaces.SwInterfaceSetRxMode{
227 Mode: interfaces.RX_MODE_API_POLLING,
233 b := make([]byte, 2+m.Size())
235 data, err := m.Marshal(b[2:])
237 t.Fatalf("expected nil error, got: %v", err)
240 t.Logf("ENCODED DATA(%d): % 03x", len(data), data)
242 var m2 interfaces.SwInterfaceSetRxMode
243 if err := m2.Unmarshal(b[2:]); err != nil {
244 t.Fatalf("expected nil error, got: %v", err)
247 t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
249 if !reflect.DeepEqual(m, &m2) {
250 t.Fatalf("newData differs from oldData")
254 /*func TestNewCodecEncodeDecode2(t *testing.T) {
255 m := &sr.SrPoliciesDetails{
256 Bsid: sr.IP6Address{00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
261 SidLists: []sr.Srv6SidList{
265 Sids: [16]sr.IP6Address{
267 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
273 b := make([]byte, m.Size())
274 data, err := m.Marshal(b)
276 t.Fatalf("expected nil error, got: %v", err)
279 t.Logf("ENCODED DATA(%d): % 03x", len(data), data)
281 var m2 sr.SrPoliciesDetails
282 if err := m2.Unmarshal(data); err != nil {
283 t.Fatalf("expected nil error, got: %v", err)
286 t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
288 if !reflect.DeepEqual(m, &m2) {
289 t.Fatalf("newData differs from oldData")
293 func TestNewCodecEncode(t *testing.T) {
294 m := NewIPRouteLookupReply()
295 /*m := &sr.SrPoliciesDetails{
296 Bsid: sr.IP6Address{00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
301 SidLists: []sr.Srv6SidList{
305 Sids: [16]sr.IP6Address{
307 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
314 var oldData, newData []byte
316 w := codec.Wrapper{m}
318 t.Logf("wrapper size: %d", size)
319 oldData, err = w.Marshal(nil)
321 t.Fatalf("expected nil error, got: %v", err)
326 t.Logf("size: %d", size)
327 newData, err = m.Marshal(nil)
329 t.Fatalf("expected nil error, got: %v", err)
332 t.Logf("Data:\nOLD[%d]: % 03x\nNEW[%d]: % 03x", len(oldData), oldData, len(newData), newData)
334 if !bytes.Equal(oldData, newData) {
335 t.Fatalf("newData differs from oldData")
339 func TestNewCodecDecode(t *testing.T) {
341 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
342 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00,
345 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00,
346 0x00, 0x00, 0x08, 0x09, 0x0a, 0x00, 0x00, 0x00,
347 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
348 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
350 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
351 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x09, 0x00,
352 0x00, 0x00, 0x08, 0x07, 0x06, 0x00, 0x00, 0x00,
353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
369 var oldData, newData ip.IPRouteAddDel
371 w := codec.Wrapper{&oldData}
372 err = w.Unmarshal(data)
374 t.Errorf("expected nil error, got: %v", err)
378 err = newData.Unmarshal(data)
380 t.Errorf("expected nil error, got: %v", err)
383 t.Logf("Data:\nOLD: %+v\nNEW: %+v", oldData, newData)
385 if !reflect.DeepEqual(oldData, newData) {
386 t.Fatalf("newData differs from oldData")
390 func NewIPRouteLookupReply() *ip.IPRouteAddDel {
391 return &ip.IPRouteAddDel{
398 Un: ip.AddressUnion{},
414 Address: ip.AddressUnion{},
417 ClassifyTableIndex: 2,
420 LabelStack: [16]ip.FibMplsLabel{
439 Address: ip.AddressUnion{},
442 ClassifyTableIndex: 2,
445 LabelStack: [16]ip.FibMplsLabel{
465 func TestSize(t *testing.T) {
467 size := binary.Size(*m)
468 t.Logf("size: %v", size)
471 func (m *TestAllMsg) Marshal(b []byte) ([]byte, error) {
472 order := binary.BigEndian
473 tmp := make([]byte, 143)
476 tmp[pos] = boolToUint(m.Bool)
482 order.PutUint16(tmp[pos:pos+2], m.Uint16)
485 order.PutUint32(tmp[pos:pos+4], m.Uint32)
488 tmp[pos] = byte(m.Int8)
491 order.PutUint16(tmp[pos:pos+2], uint16(m.Int16))
494 order.PutUint32(tmp[pos:pos+4], uint32(m.Int32))
497 order.PutUint32(tmp[pos:pos+4], uint32(m.AliasUint32))
500 copy(tmp[pos:pos+6], m.AliasArray[:])
503 order.PutUint32(tmp[pos:pos+4], uint32(m.Enum))
506 for i := 0; i < 4; i++ {
508 if i < len(m.BaseArray) {
511 order.PutUint32(tmp[pos:pos+4], uint32(x))
515 copy(tmp[pos:pos+7], m.Slice)
518 copy(tmp[pos:pos+64], m.String)
521 order.PutUint32(tmp[pos:pos+4], uint32(len(m.VlaStr)) /*m.SizeOf*/)
524 copy(tmp[pos:pos+len(m.VlaStr)], m.VlaStr[:])
527 order.PutUint32(tmp[pos:pos+4], uint32(len(m.VariableSlice)) /*m.SizeOf*/)
530 for i := range m.VariableSlice {
531 tmp[pos+i*1] = uint8(m.VariableSlice[i].Proto)
532 //copy(tmp[102+i:103+i], []byte{byte(m.VariableSlice[i].Proto)})
534 pos += len(m.VariableSlice) * 1
536 tmp[pos] = uint8(m.TypeUnion.Af)
539 copy(tmp[pos:pos+16], m.TypeUnion.Un.XXX_UnionData[:])
545 func (m *TestAllMsg) Unmarshal(tmp []byte) error {
546 order := binary.BigEndian
548 //tmp := make([]byte, 143)
551 m.Bool = tmp[pos] != 0
558 //order.PutUint16(tmp[pos:pos+2], m.Uint16)
559 m.Uint16 = order.Uint16(tmp[pos : pos+2])
562 //order.PutUint32(tmp[pos:pos+4], m.Uint32)
563 m.Uint32 = order.Uint32(tmp[pos : pos+4])
566 //tmp[pos] = byte(m.Int8)
567 m.Int8 = int8(tmp[pos])
570 //order.PutUint16(tmp[pos:pos+2], uint16(m.Int16))
571 m.Int16 = int16(order.Uint16(tmp[pos : pos+2]))
574 //order.PutUint32(tmp[pos:pos+4], uint32(m.Int32))
575 m.Int32 = int32(order.Uint32(tmp[pos : pos+4]))
578 //order.PutUint32(tmp[pos:pos+4], uint32(m.AliasUint32))
579 m.AliasUint32 = InterfaceIndex(order.Uint32(tmp[pos : pos+4]))
582 //copy(tmp[pos:pos+6], m.AliasArray[:])
583 copy(m.AliasArray[:], tmp[pos:pos+6])
586 //order.PutUint32(tmp[pos:pos+4], uint32(m.Enum))
587 m.Enum = IfStatusFlags(order.Uint32(tmp[pos : pos+4]))
590 m.BaseArray = make([]uint32, 4)
591 for i := 0; i < 4; i++ {
593 if i < len(m.BaseArray) {
596 order.PutUint32(tmp[pos:pos+4], uint32(x))*/
597 m.BaseArray[i] = order.Uint32(tmp[pos : pos+4])
601 m.Slice = make([]byte, 7)
602 copy(m.Slice[:7], tmp[pos:pos+7])
603 //copy(tmp[pos:pos+7], m.Slice)
606 i := bytes.Index(tmp[pos:pos+64], []byte{0x00})
607 m.String = string(tmp[pos : pos+i])
608 //copy(tmp[pos:pos+64], m.String)
611 //order.PutUint32(tmp[pos:pos+4], uint32(len(m.VlaStr)) /*m.SizeOf*/)
612 VlaStrLen := int(order.Uint32(tmp[pos : pos+4]))
615 m.VlaStr = string(tmp[pos : pos+VlaStrLen])
616 //copy(m.VlaStr[pos:pos+VlaStrLen], tmp[pos:pos+64])
619 m.SizeOf = uint32(order.Uint32(tmp[pos : pos+4]))
622 /*order.PutUint32(tmp[pos:pos+4], uint32(len(m.VariableSlice)))
623 m.VariableSlice = IfStatusFlags(order.Uint32(tmp[pos : pos+4]))
626 m.VariableSlice = make([]SliceType, m.SizeOf)
627 for i := range m.VariableSlice {
628 //tmp[pos+i*1] = uint8(m.VariableSlice[i].Proto)
629 m.VariableSlice[i].Proto = IPProto(tmp[pos+i*1])
630 //copy(tmp[102+i:103+i], []byte{byte(m.VariableSlice[i].Proto)})
632 pos += len(m.VariableSlice) * 1
634 //tmp[pos] = uint8(m.TypeUnion.Af)
635 m.TypeUnion.Af = AddressFamily(tmp[pos])
638 //copy(tmp[pos:pos+16], m.TypeUnion.Un.XXX_UnionData[:])
639 copy(m.TypeUnion.Un.XXX_UnionData[:], tmp[pos:pos+16])
645 func boolToUint(b bool) uint8 {
652 // SwInterfaceDetails represents VPP binary API message 'sw_interface_details'.
653 type TestAllMsg struct {
661 AliasUint32 InterfaceIndex
662 AliasArray MacAddress
664 BaseArray []uint32 `struc:"[4]uint32"`
665 Slice []byte `struc:"[7]byte"`
666 String string `struc:"[64]byte"`
667 XXX_VlaStrLen uint32 `struc:"sizeof=VlaStr"`
669 SizeOf uint32 `struc:"sizeof=VariableSlice"`
670 VariableSlice []SliceType
674 type InterfaceIndex uint32
675 type MacAddress [6]uint8
676 type IfStatusFlags uint32
679 IF_STATUS_API_FLAG_ADMIN_UP IfStatusFlags = 1
680 IF_STATUS_API_FLAG_LINK_UP IfStatusFlags = 2
683 // Address represents VPP binary API type 'address'.
684 type Address struct {
689 // AddressFamily represents VPP binary API enum 'address_family'.
690 type AddressFamily uint8
693 ADDRESS_IP4 AddressFamily = 0
694 ADDRESS_IP6 AddressFamily = 1
697 // AddressUnion represents VPP binary API union 'address_union'.
698 type AddressUnion struct {
699 XXX_UnionData [16]byte
702 func (*AddressUnion) GetTypeName() string { return "address_union" }
704 func AddressUnionIP4(a IP4Address) (u AddressUnion) {
708 func (u *AddressUnion) SetIP4(a IP4Address) {
709 var b = new(bytes.Buffer)
710 if err := struc.Pack(b, &a); err != nil {
713 copy(u.XXX_UnionData[:], b.Bytes())
715 func (u *AddressUnion) GetIP4() (a IP4Address) {
716 var b = bytes.NewReader(u.XXX_UnionData[:])
721 func AddressUnionIP6(a IP6Address) (u AddressUnion) {
725 func (u *AddressUnion) SetIP6(a IP6Address) {
726 var b = new(bytes.Buffer)
727 if err := struc.Pack(b, &a); err != nil {
730 copy(u.XXX_UnionData[:], b.Bytes())
732 func (u *AddressUnion) GetIP6() (a IP6Address) {
733 var b = bytes.NewReader(u.XXX_UnionData[:])
738 // IP4Address represents VPP binary API alias 'ip4_address'.
739 type IP4Address [4]uint8
741 // IP6Address represents VPP binary API alias 'ip6_address'.
742 type IP6Address [16]uint8
744 type SliceType struct {
751 IP_API_PROTO_HOPOPT IPProto = 0
752 IP_API_PROTO_ICMP IPProto = 1
753 IP_API_PROTO_IGMP IPProto = 2
754 IP_API_PROTO_TCP IPProto = 6
755 IP_API_PROTO_UDP IPProto = 17
756 IP_API_PROTO_GRE IPProto = 47
757 IP_API_PROTO_ESP IPProto = 50
758 IP_API_PROTO_AH IPProto = 51
759 IP_API_PROTO_ICMP6 IPProto = 58
760 IP_API_PROTO_EIGRP IPProto = 88
761 IP_API_PROTO_OSPF IPProto = 89
762 IP_API_PROTO_SCTP IPProto = 132
763 IP_API_PROTO_RESERVED IPProto = 255
766 func (m *TestAllMsg) Reset() { *m = TestAllMsg{} }
767 func (*TestAllMsg) GetMessageName() string { return "sw_interface_details" }
768 func (*TestAllMsg) GetCrcString() string { return "17b69fa2" }
769 func (*TestAllMsg) GetMessageType() api.MessageType { return api.ReplyMessage }