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