Refactored binapi generator with message encoding
[govpp.git] / codec / msg_codec_test.go
1 package codec_test
2
3 import (
4         "bytes"
5         "testing"
6
7         "github.com/lunixbochs/struc"
8
9         "git.fd.io/govpp.git/api"
10         "git.fd.io/govpp.git/codec"
11         "git.fd.io/govpp.git/examples/binapi/ip"
12         "git.fd.io/govpp.git/examples/binapi/sr"
13         "git.fd.io/govpp.git/examples/binapi/vpe"
14 )
15
16 type MyMsg struct {
17         Index uint16
18         Label []byte `struc:"[16]byte"`
19         Port  uint16
20 }
21
22 func (*MyMsg) GetMessageName() string {
23         return "my_msg"
24 }
25 func (*MyMsg) GetCrcString() string {
26         return "xxxxx"
27 }
28 func (*MyMsg) GetMessageType() api.MessageType {
29         return api.OtherMessage
30 }
31
32 func TestEncode(t *testing.T) {
33         tests := []struct {
34                 name    string
35                 msg     api.Message
36                 msgID   uint16
37                 expData []byte
38         }{
39                 /*{name: "basic",
40                         msg:     &MyMsg{Index: 1, Label: []byte("Abcdef"), Port: 1000},
41                         msgID:   100,
42                         expData: []byte{0x00, 0x64, 0x00, 0x01, 0x41, 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8},
43                 },*/
44                 {name: "show version",
45                         msg:     &vpe.ShowVersion{},
46                         msgID:   743,
47                         expData: []byte{0x02, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
48                 },
49                 {name: "ip route",
50                         msg: &ip.IPRouteAddDel{
51                                 IsAdd:       true,
52                                 IsMultipath: true,
53                                 Route: ip.IPRoute{
54                                         TableID:    0,
55                                         StatsIndex: 0,
56                                         Prefix:     ip.Prefix{},
57                                         NPaths:     0,
58                                         Paths: []ip.FibPath{
59                                                 {
60                                                         SwIfIndex:  0,
61                                                         TableID:    0,
62                                                         RpfID:      0,
63                                                         Weight:     0,
64                                                         Preference: 0,
65                                                         Type:       0,
66                                                         Flags:      0,
67                                                         Proto:      0,
68                                                         Nh:         ip.FibPathNh{},
69                                                         NLabels:    5,
70                                                         LabelStack: [16]ip.FibMplsLabel{
71                                                                 {
72                                                                         IsUniform: 1,
73                                                                         Label:     2,
74                                                                         TTL:       3,
75                                                                         Exp:       4,
76                                                                 },
77                                                         },
78                                                 },
79                                         },
80                                 },
81                         },
82                         msgID:   743,
83                         expData: []byte{0x02, 0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
84                 },
85                 /*{name: "sr",
86                         msg: &sr.SrPolicyAdd{
87                                 BsidAddr: sr.IP6Address{},
88                                 Weight:   0,
89                                 IsEncap:  false,
90                                 IsSpray:  false,
91                                 FibTable: 0,
92                                 Sids:     sr.Srv6SidList{},
93                         },
94                         msgID:   99,
95                         expData: []byte{0x00, 0x64, 0x00, 0x01, 0x41, 0x62, 0x63, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE8},
96                 },*/
97         }
98         for _, test := range tests {
99                 t.Run(test.name, func(t *testing.T) {
100                         c := &codec.MsgCodec{}
101                         //c := &codec.NewCodec{}
102
103                         data, err := c.EncodeMsg(test.msg, test.msgID)
104                         if err != nil {
105                                 t.Fatalf("expected nil error, got: %v", err)
106                         }
107                         if !bytes.Equal(data, test.expData) {
108                                 t.Fatalf("expected data: % 0X, got: % 0X", test.expData, data)
109                         }
110                 })
111         }
112 }
113
114 func TestEncodePanic(t *testing.T) {
115         c := &codec.MsgCodec{}
116
117         msg := &MyMsg{Index: 1, Label: []byte("thisIsLongerThan16Bytes"), Port: 1000}
118
119         _, err := c.EncodeMsg(msg, 100)
120         if err == nil {
121                 t.Fatalf("expected non-nil error, got: %v", err)
122         }
123 }
124
125 func TestEncodeSr(t *testing.T) {
126         msg := sr.Srv6SidList{
127                 NumSids: 0,
128                 Weight:  0,
129                 //Sids:    nil,
130         }
131         buf := new(bytes.Buffer)
132
133         if err := struc.Pack(buf, msg); err != nil {
134                 t.Fatal(err)
135         }
136 }