Change module name to go.fd.io/govpp
[govpp.git] / codec / marshaler_test.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package codec_test
16
17 import (
18         "bytes"
19         "reflect"
20         "testing"
21
22         "go.fd.io/govpp/api"
23         "go.fd.io/govpp/binapi/ip_types"
24         "go.fd.io/govpp/binapi/sr"
25         "go.fd.io/govpp/codec"
26         interfaces "go.fd.io/govpp/binapi/interface"
27         "go.fd.io/govpp/binapi/interface_types"
28 )
29
30 // CliInband represents VPP binary API message 'cli_inband'.
31 type CliInband struct {
32         XXX_CmdLen uint32 `struc:"sizeof=Cmd"`
33         Cmd        string
34 }
35
36 func (m *CliInband) Reset()                        { *m = CliInband{} }
37 func (*CliInband) GetMessageName() string          { return "cli_inband" }
38 func (*CliInband) GetCrcString() string            { return "f8377302" }
39 func (*CliInband) GetMessageType() api.MessageType { return api.RequestMessage }
40
41 // CliInbandReply represents VPP binary API message 'cli_inband_reply'.
42 type CliInbandReply struct {
43         Retval       int32
44         XXX_ReplyLen uint32 `struc:"sizeof=Reply"`
45         Reply        string
46 }
47
48 func (m *CliInbandReply) Reset()                        { *m = CliInbandReply{} }
49 func (*CliInbandReply) GetMessageName() string          { return "cli_inband_reply" }
50 func (*CliInbandReply) GetCrcString() string            { return "05879051" }
51 func (*CliInbandReply) GetMessageType() api.MessageType { return api.ReplyMessage }
52
53 func TestWrapperEncode(t *testing.T) {
54         msg := &CliInband{
55                 XXX_CmdLen: 5,
56                 Cmd:        "abcde",
57         }
58         expectedData := []byte{
59                 0x00, 0x64,
60                 0x00, 0x00, 0x00, 0x00,
61                 0x00, 0x00, 0x00, 0x00,
62                 0x00, 0x00, 0x00, 0x05,
63                 0x61, 0x62, 0x63, 0x64, 0x65,
64         }
65
66         c := codec.DefaultCodec
67
68         data, err := c.EncodeMsg(msg, 100)
69         if err != nil {
70                 t.Fatalf("EncodeMsg failed: %v", err)
71         }
72         if !bytes.Equal(data, expectedData) {
73                 t.Fatalf("unexpected encoded data,\nexpected: % 02x\n     got: % 02x\n", expectedData, data)
74         }
75 }
76
77 func TestWrapperDecode(t *testing.T) {
78         data := []byte{
79                 0x00, 0x64,
80                 0x00, 0x00, 0x00, 0x00,
81                 0x00, 0x00, 0x00, 0x00,
82                 0x00, 0x00, 0x00, 0x05,
83                 0x61, 0x62, 0x63, 0x64, 0x65,
84         }
85         expectedMsg := &CliInbandReply{
86                 Retval:       0,
87                 XXX_ReplyLen: 5,
88                 Reply:        "abcde",
89         }
90
91         c := codec.DefaultCodec
92
93         msg := new(CliInbandReply)
94         err := c.DecodeMsg(data, msg)
95         if err != nil {
96                 t.Fatalf("DecodeMsg failed: %v", err)
97         }
98         if !reflect.DeepEqual(msg, expectedMsg) {
99                 t.Fatalf("unexpected decoded msg,\nexpected: %+v\n     got: %+v\n", expectedMsg, msg)
100         }
101 }
102
103 func TestNewCodecEncodeDecode4(t *testing.T) {
104         m := &interfaces.SwInterfaceSetRxMode{
105                 Mode:         interface_types.RX_MODE_API_POLLING,
106                 QueueID:      70000,
107                 QueueIDValid: true,
108                 SwIfIndex:    300,
109         }
110
111         b := make([]byte, 2+m.Size())
112
113         data, err := m.Marshal(b[2:])
114         if err != nil {
115                 t.Fatalf("expected nil error, got: %v", err)
116         }
117
118         t.Logf("ENCODED DATA(%d): % 03x", len(data), data)
119
120         var m2 interfaces.SwInterfaceSetRxMode
121         if err := m2.Unmarshal(b[2:]); err != nil {
122                 t.Fatalf("expected nil error, got: %v", err)
123         }
124
125         t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
126
127         if !reflect.DeepEqual(m, &m2) {
128                 t.Fatalf("newData differs from oldData")
129         }
130 }
131
132 func TestNewCodecEncodeDecode2(t *testing.T) {
133         m := &sr.SrPoliciesDetails{
134                 Bsid:        ip_types.IP6Address{00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
135                 IsSpray:     true,
136                 IsEncap:     false,
137                 FibTable:    33,
138                 NumSidLists: 1,
139                 SidLists: []sr.Srv6SidList{
140                         {
141                                 Weight:  555,
142                                 NumSids: 2,
143                                 Sids: [16]ip_types.IP6Address{
144                                         {99},
145                                         {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
146                                 },
147                         },
148                 },
149         }
150
151         b := make([]byte, m.Size())
152         data, err := m.Marshal(b)
153         if err != nil {
154                 t.Fatalf("expected nil error, got: %v", err)
155         }
156
157         t.Logf("ENCODED DATA(%d): % 03x", len(data), data)
158
159         var m2 sr.SrPoliciesDetails
160         if err := m2.Unmarshal(data); err != nil {
161                 t.Fatalf("expected nil error, got: %v", err)
162         }
163
164         t.Logf("Data:\nOLD: %+v\nNEW: %+v", m, &m2)
165
166         if !reflect.DeepEqual(m, &m2) {
167                 t.Fatalf("newData differs from oldData")
168         }
169 }