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