-
Notifications
You must be signed in to change notification settings - Fork 9
/
family_linux_test.go
281 lines (257 loc) · 6.09 KB
/
family_linux_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//go:build linux
// +build linux
package genetlink_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/genetlink/genltest"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
"github.com/mdlayher/netlink/nltest"
"golang.org/x/sys/unix"
)
func TestConnGetFamily(t *testing.T) {
const (
name = "nlctrl"
version = 1
flags = netlink.Request
)
wantgenl := genetlink.Message{
Header: genetlink.Header{
Command: unix.CTRL_CMD_GETFAMILY,
Version: version,
},
Data: nltest.MustMarshalAttributes([]netlink.Attribute{{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes(name),
}}),
}
c := genltest.Dial(func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) {
if diff := cmp.Diff(flags, nreq.Header.Flags); diff != "" {
t.Fatalf("unexpected netlink header flags (-want +got):\n%s", diff)
}
if diff := cmp.Diff(wantgenl, greq); diff != "" {
t.Fatalf("unexpected generic netlink message (-want +got):\n%s", diff)
}
return []genetlink.Message{{
Header: genetlink.Header{
Command: unix.CTRL_CMD_NEWFAMILY,
Version: version,
},
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes(name),
},
{
Type: unix.CTRL_ATTR_FAMILY_ID,
Data: nlenc.Uint16Bytes(16),
},
{
Type: unix.CTRL_ATTR_VERSION,
Data: nlenc.Uint32Bytes(2),
},
}),
}}, nil
})
family, err := c.GetFamily(name)
if err != nil {
t.Fatalf("failed to execute: %v", err)
}
wantFamily := genetlink.Family{
ID: 16,
Version: 2,
Name: name,
}
if diff := cmp.Diff(wantFamily, family); diff != "" {
t.Fatalf("unexpected generic netlink family (-want +got):\n%s", diff)
}
}
func TestConnFamilyList(t *testing.T) {
const (
version = 1
flags = netlink.Request | netlink.Dump
)
wantgenl := genetlink.Message{
Header: genetlink.Header{
Command: unix.CTRL_CMD_GETFAMILY,
Version: version,
},
Data: []byte{},
}
c := genltest.Dial(func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) {
if diff := cmp.Diff(flags, nreq.Header.Flags); diff != "" {
t.Fatalf("unexpected netlink header flags (-want +got):\n%s", diff)
}
if diff := cmp.Diff(wantgenl, greq); diff != "" {
t.Fatalf("unexpected generic netlink message (-want +got):\n%s", diff)
}
return []genetlink.Message{
{
Header: genetlink.Header{
Command: unix.CTRL_CMD_NEWFAMILY,
Version: version,
},
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes("nlctrl"),
},
{
Type: unix.CTRL_ATTR_FAMILY_ID,
Data: nlenc.Uint16Bytes(16),
},
{
Type: unix.CTRL_ATTR_VERSION,
Data: nlenc.Uint32Bytes(2),
},
}),
},
{
Header: genetlink.Header{
Command: unix.CTRL_CMD_NEWFAMILY,
Version: version,
},
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes("nl80211"),
},
{
Type: unix.CTRL_ATTR_FAMILY_ID,
Data: nlenc.Uint16Bytes(26),
},
{
Type: unix.CTRL_ATTR_VERSION,
Data: nlenc.Uint32Bytes(1),
},
}),
// Normally a "multi-part" done message would be here, but package
// netlink takes care of trimming that away for us, so this package
// assumes that has already been taken care of
},
}, nil
})
families, err := c.ListFamilies()
if err != nil {
t.Fatalf("failed to execute: %v", err)
}
wantFamilies := []genetlink.Family{
{
ID: 16,
Version: 2,
Name: "nlctrl",
},
{
ID: 26,
Version: 1,
Name: "nl80211",
},
}
if diff := cmp.Diff(wantFamilies, families); diff != "" {
t.Fatalf("unexpected generic netlink families (-want +got):\n%s", diff)
}
}
func TestFamily_parseAttributes(t *testing.T) {
tests := []struct {
name string
attrs []netlink.Attribute
f genetlink.Family
ok bool
}{
{
name: "version too large",
attrs: []netlink.Attribute{{
Type: unix.CTRL_ATTR_VERSION,
Data: []byte{0xff, 0x01, 0x00, 0x00},
}},
},
{
name: "OK",
attrs: []netlink.Attribute{
{
Type: unix.CTRL_ATTR_FAMILY_ID,
Data: []byte{0x10, 0x00},
},
{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes("nlctrl"),
},
{
Type: unix.CTRL_ATTR_VERSION,
Data: []byte{0x02, 0x00, 0x00, 0x00},
},
{
Type: unix.CTRL_ATTR_MCAST_GROUPS,
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: 1,
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.CTRL_ATTR_MCAST_GRP_ID,
Data: nlenc.Uint32Bytes(16),
},
{
Type: unix.CTRL_ATTR_MCAST_GRP_NAME,
Data: nlenc.Bytes("notify"),
},
}),
},
{
Type: 2,
Data: nltest.MustMarshalAttributes([]netlink.Attribute{
{
Type: unix.CTRL_ATTR_MCAST_GRP_ID,
Data: nlenc.Uint32Bytes(17),
},
{
Type: unix.CTRL_ATTR_MCAST_GRP_NAME,
Data: nlenc.Bytes("foobar"),
},
}),
},
}),
},
},
f: genetlink.Family{
ID: 16,
Version: 2,
Name: "nlctrl",
Groups: []genetlink.MulticastGroup{
{
ID: 16,
Name: "notify",
},
{
ID: 17,
Name: "foobar",
},
},
},
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) {
return []genetlink.Message{{
Data: nltest.MustMarshalAttributes(tt.attrs),
}}, nil
})
family, err := c.GetFamily("")
if err != nil && tt.ok {
t.Fatalf("unexpected error: %v", err)
}
if err == nil && !tt.ok {
t.Fatal("expected an error, but none occurred")
}
if err != nil {
return
}
if diff := cmp.Diff(tt.f, family); diff != "" {
t.Fatalf("unexpected family (-want +got):\n%s", diff)
}
})
}
}