Binary API generator improvements
[govpp.git] / codec / msg_codec_test.go
1 package codec_test
2
3 import (
4         "bytes"
5         "git.fd.io/govpp.git/examples/binapi/ip_types"
6         "testing"
7
8         "git.fd.io/govpp.git/api"
9         "git.fd.io/govpp.git/codec"
10         "git.fd.io/govpp.git/examples/binapi/ip"
11         "git.fd.io/govpp.git/examples/binapi/vpe"
12 )
13
14 type MyMsg struct {
15         Index uint16
16         Label []byte `struc:"[16]byte"`
17         Port  uint16
18 }
19
20 func (*MyMsg) GetMessageName() string {
21         return "my_msg"
22 }
23 func (*MyMsg) GetCrcString() string {
24         return "xxxxx"
25 }
26 func (*MyMsg) GetMessageType() api.MessageType {
27         return api.OtherMessage
28 }
29
30 func TestEncode(t *testing.T) {
31         tests := []struct {
32                 name    string
33                 msg     api.Message
34                 msgID   uint16
35                 expData []byte
36         }{
37                 {name: "basic",
38                         msg:     &MyMsg{Index: 1, Label: []byte("Abcdef"), Port: 1000},
39                         msgID:   100,
40                         expData: []byte{0x00, 0x64, 0x00, 0x01, 0x41, 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8},
41                 },
42                 {name: "show version",
43                         msg:     &vpe.ShowVersion{},
44                         msgID:   743,
45                         expData: []byte{0x02, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
46                 },
47                 {name: "ip route",
48                         msg: &ip.IPRouteAddDel{
49                                 IsAdd:       true,
50                                 IsMultipath: true,
51                                 Route: ip.IPRoute{
52                                         TableID:    0,
53                                         StatsIndex: 0,
54                                         Prefix:     ip_types.Prefix{},
55                                         NPaths:     0,
56                                 },
57                         },
58                         msgID:   743,
59                         expData: []byte{0x02, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
60                 },
61                 /*{name: "sr",
62                         msg: &sr.SrPolicyAdd{
63                                 BsidAddr: sr.IP6Address{},
64                                 Weight:   0,
65                                 IsEncap:  false,
66                                 IsSpray:  false,
67                                 FibTable: 0,
68                                 Sids:     sr.Srv6SidList{},
69                         },
70                         msgID:   99,
71                         expData: []byte{0x00, 0x64, 0x00, 0x01, 0x41, 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8},
72                 },*/
73         }
74         for _, test := range tests {
75                 t.Run(test.name, func(t *testing.T) {
76                         c := codec.DefaultCodec
77
78                         data, err := c.EncodeMsg(test.msg, test.msgID)
79                         if err != nil {
80                                 t.Fatalf("expected nil error, got: %v", err)
81                         }
82                         if !bytes.Equal(data, test.expData) {
83                                 t.Fatalf("expected data: % 0X, got: % 0X", test.expData, data)
84                         }
85                 })
86         }
87 }
88
89 func TestEncodePanic(t *testing.T) {
90         c := codec.DefaultCodec
91
92         msg := &MyMsg{Index: 1, Label: []byte("thisIsLongerThan16Bytes"), Port: 1000}
93
94         _, err := c.EncodeMsg(msg, 100)
95         if err == nil {
96                 t.Fatalf("expected non-nil error, got: %v", err)
97         }
98 }