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