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