Improve binapi generator
[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         "git.fd.io/govpp.git/api"
23         "git.fd.io/govpp.git/binapi/ip_types"
24         "git.fd.io/govpp.git/binapi/sr"
25         "git.fd.io/govpp.git/codec"
26         "git.fd.io/govpp.git/internal/testbinapi/binapi2001/interfaces"
27         "git.fd.io/govpp.git/internal/testbinapi/binapi2001/ip"
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:         interfaces.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 }
170
171 func TestNewCodecEncode(t *testing.T) {
172         m := NewIPRouteLookupReply()
173         /*m := &sr.SrPoliciesDetails{
174                 Bsid:        sr.IP6Address{00, 11, 22, 33, 44, 55, 66, 77, 88, 99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
175                 IsSpray:     true,
176                 IsEncap:     false,
177                 FibTable:    33,
178                 NumSidLists: 1,
179                 SidLists: []sr.Srv6SidList{
180                         {
181                                 Weight:  555,
182                                 NumSids: 2,
183                                 Sids: [16]sr.IP6Address{
184                                         {99},
185                                         {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
186                                 },
187                         },
188                 },
189         }*/
190
191         var err error
192         var oldData, newData []byte
193         {
194                 w := codec.Wrapper{m}
195                 size := m.Size()
196                 t.Logf("wrapper size: %d", size)
197                 oldData, err = w.Marshal(nil)
198                 if err != nil {
199                         t.Fatalf("expected nil error, got: %v", err)
200                 }
201         }
202         {
203                 size := m.Size()
204                 t.Logf("size: %d", size)
205                 newData, err = m.Marshal(nil)
206                 if err != nil {
207                         t.Fatalf("expected nil error, got: %v", err)
208                 }
209         }
210         t.Logf("Data:\nOLD[%d]: % 03x\nNEW[%d]: % 03x", len(oldData), oldData, len(newData), newData)
211
212         if !bytes.Equal(oldData, newData) {
213                 t.Fatalf("newData differs from oldData")
214         }
215 }
216
217 func TestNewCodecDecode(t *testing.T) {
218         data := []byte{
219                 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
220                 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
221                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222                 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00,
223                 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00,
224                 0x00, 0x00, 0x08, 0x09, 0x0a, 0x00, 0x00, 0x00,
225                 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
226                 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228                 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
229                 0x01, 0x00, 0x00, 0x00, 0x02, 0x01, 0x09, 0x00,
230                 0x00, 0x00, 0x08, 0x07, 0x06, 0x00, 0x00, 0x00,
231                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244         }
245
246         var err error
247         var oldData, newData ip.IPRouteAddDel
248         {
249                 w := codec.Wrapper{&oldData}
250                 err = w.Unmarshal(data)
251                 if err != nil {
252                         t.Errorf("expected nil error, got: %v", err)
253                 }
254         }
255         {
256                 err = newData.Unmarshal(data)
257                 if err != nil {
258                         t.Errorf("expected nil error, got: %v", err)
259                 }
260         }
261         t.Logf("Data:\nOLD: %+v\nNEW: %+v", oldData, newData)
262
263         if !reflect.DeepEqual(oldData, newData) {
264                 t.Fatalf("newData differs from oldData")
265         }
266 }
267
268 func NewIPRouteLookupReply() *ip.IPRouteAddDel {
269         return &ip.IPRouteAddDel{
270                 Route: ip.IPRoute{
271                         TableID:    3,
272                         StatsIndex: 5,
273                         Prefix: ip.Prefix{
274                                 Address: ip.Address{
275                                         Af: ip.ADDRESS_IP6,
276                                         Un: ip.AddressUnion{},
277                                 },
278                                 Len: 24,
279                         },
280                         NPaths: 2,
281                         Paths: []ip.FibPath{
282                                 {
283                                         SwIfIndex:  5,
284                                         TableID:    6,
285                                         RpfID:      8,
286                                         Weight:     9,
287                                         Preference: 10,
288                                         Type:       11,
289                                         Flags:      1,
290                                         Proto:      2,
291                                         Nh: ip.FibPathNh{
292                                                 Address:            ip.AddressUnion{},
293                                                 ViaLabel:           3,
294                                                 ObjID:              1,
295                                                 ClassifyTableIndex: 2,
296                                         },
297                                         NLabels: 1,
298                                         LabelStack: [16]ip.FibMplsLabel{
299                                                 {
300                                                         IsUniform: 9,
301                                                         Label:     8,
302                                                         TTL:       7,
303                                                         Exp:       6,
304                                                 },
305                                         },
306                                 },
307                                 {
308                                         SwIfIndex:  7,
309                                         TableID:    6,
310                                         RpfID:      8,
311                                         Weight:     9,
312                                         Preference: 10,
313                                         Type:       11,
314                                         Flags:      1,
315                                         Proto:      1,
316                                         Nh: ip.FibPathNh{
317                                                 Address:            ip.AddressUnion{},
318                                                 ViaLabel:           3,
319                                                 ObjID:              1,
320                                                 ClassifyTableIndex: 2,
321                                         },
322                                         NLabels: 2,
323                                         LabelStack: [16]ip.FibMplsLabel{
324                                                 {
325                                                         IsUniform: 9,
326                                                         Label:     8,
327                                                         TTL:       7,
328                                                         Exp:       6,
329                                                 },
330                                                 {
331                                                         IsUniform: 10,
332                                                         Label:     8,
333                                                         TTL:       7,
334                                                         Exp:       6,
335                                                 },
336                                         },
337                                 },
338                         },
339                 },
340         }
341 }