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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package message_test
import (
"bytes"
"reflect"
"testing"
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/marshal"
"gvisor.dev/gvisor/pkg/marshal/primitive"
"gvisor.dev/gvisor/pkg/sentry/socket/netlink"
)
func TestParseMessage(t *testing.T) {
dummyNetlinkMsg := primitive.Uint16(0x3130)
tests := []struct {
desc string
input []byte
header linux.NetlinkMessageHeader
dataMsg marshal.Marshallable
restLen int
ok bool
}{
{
desc: "valid",
input: []byte{
0x14, 0x00, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, 0x00, 0x00, // Data message with 2 bytes padding
},
header: linux.NetlinkMessageHeader{
Length: 20,
Type: 1,
Flags: 2,
Seq: 3,
PortID: 4,
},
dataMsg: &dummyNetlinkMsg,
restLen: 0,
ok: true,
},
{
desc: "valid with next message",
input: []byte{
0x14, 0x00, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, 0x00, 0x00, // Data message with 2 bytes padding
0xFF, // Next message (rest)
},
header: linux.NetlinkMessageHeader{
Length: 20,
Type: 1,
Flags: 2,
Seq: 3,
PortID: 4,
},
dataMsg: &dummyNetlinkMsg,
restLen: 1,
ok: true,
},
{
desc: "valid for last message without padding",
input: []byte{
0x12, 0x00, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, // Data message
},
header: linux.NetlinkMessageHeader{
Length: 18,
Type: 1,
Flags: 2,
Seq: 3,
PortID: 4,
},
dataMsg: &dummyNetlinkMsg,
restLen: 0,
ok: true,
},
{
desc: "valid for last message not to be aligned",
input: []byte{
0x13, 0x00, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, // Data message
0x00, // Excessive 1 byte permitted at end
},
header: linux.NetlinkMessageHeader{
Length: 19,
Type: 1,
Flags: 2,
Seq: 3,
PortID: 4,
},
dataMsg: &dummyNetlinkMsg,
restLen: 0,
ok: true,
},
{
desc: "header.Length too short",
input: []byte{
0x04, 0x00, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, 0x00, 0x00, // Data message with 2 bytes padding
},
ok: false,
},
{
desc: "header.Length too long",
input: []byte{
0xFF, 0xFF, 0x00, 0x00, // Length
0x01, 0x00, // Type
0x02, 0x00, // Flags
0x03, 0x00, 0x00, 0x00, // Seq
0x04, 0x00, 0x00, 0x00, // PortID
0x30, 0x31, 0x00, 0x00, // Data message with 2 bytes padding
},
ok: false,
},
{
desc: "header incomplete",
input: []byte{
0x04, 0x00, 0x00, 0x00, // Length
},
ok: false,
},
{
desc: "empty message",
input: []byte{},
ok: false,
},
}
for _, test := range tests {
msg, rest, ok := netlink.ParseMessage(test.input)
if ok != test.ok {
t.Errorf("%v: got ok = %v, want = %v", test.desc, ok, test.ok)
continue
}
if !test.ok {
continue
}
if !reflect.DeepEqual(msg.Header(), test.header) {
t.Errorf("%v: got hdr = %+v, want = %+v", test.desc, msg.Header(), test.header)
}
var dataMsg primitive.Uint16
_, dataOk := msg.GetData(&dataMsg)
if !dataOk {
t.Errorf("%v: GetData.ok = %v, want = true", test.desc, dataOk)
} else if !reflect.DeepEqual(&dataMsg, test.dataMsg) {
t.Errorf("%v: GetData.msg = %+v, want = %+v", test.desc, dataMsg, test.dataMsg)
}
if got, want := rest, test.input[len(test.input)-test.restLen:]; !bytes.Equal(got, want) {
t.Errorf("%v: got rest = %v, want = %v", test.desc, got, want)
}
}
}
func TestAttrView(t *testing.T) {
tests := []struct {
desc string
input []byte
// Outputs for ParseFirst.
hdr linux.NetlinkAttrHeader
value []byte
restLen int
ok bool
// Outputs for Empty.
isEmpty bool
}{
{
desc: "valid",
input: []byte{
0x06, 0x00, // Length
0x01, 0x00, // Type
0x30, 0x31, 0x00, 0x00, // Data with 2 bytes padding
},
hdr: linux.NetlinkAttrHeader{
Length: 6,
Type: 1,
},
value: []byte{0x30, 0x31},
restLen: 0,
ok: true,
isEmpty: false,
},
{
desc: "at alignment",
input: []byte{
0x08, 0x00, // Length
0x01, 0x00, // Type
0x30, 0x31, 0x32, 0x33, // Data
},
hdr: linux.NetlinkAttrHeader{
Length: 8,
Type: 1,
},
value: []byte{0x30, 0x31, 0x32, 0x33},
restLen: 0,
ok: true,
isEmpty: false,
},
{
desc: "at alignment with rest data",
input: []byte{
0x08, 0x00, // Length
0x01, 0x00, // Type
0x30, 0x31, 0x32, 0x33, // Data
0xFF, 0xFE, // Rest data
},
hdr: linux.NetlinkAttrHeader{
Length: 8,
Type: 1,
},
value: []byte{0x30, 0x31, 0x32, 0x33},
restLen: 2,
ok: true,
isEmpty: false,
},
{
desc: "hdr.Length too long",
input: []byte{
0xFF, 0x00, // Length
0x01, 0x00, // Type
0x30, 0x31, 0x32, 0x33, // Data
},
ok: false,
isEmpty: false,
},
{
desc: "hdr.Length too short",
input: []byte{
0x01, 0x00, // Length
0x01, 0x00, // Type
0x30, 0x31, 0x32, 0x33, // Data
},
ok: false,
isEmpty: false,
},
{
desc: "empty",
input: []byte{},
ok: false,
isEmpty: true,
},
}
for _, test := range tests {
attrs := netlink.AttrsView(test.input)
// Test ParseFirst().
hdr, value, rest, ok := attrs.ParseFirst()
if ok != test.ok {
t.Errorf("%v: got ok = %v, want = %v", test.desc, ok, test.ok)
} else if test.ok {
if !reflect.DeepEqual(hdr, test.hdr) {
t.Errorf("%v: got hdr = %+v, want = %+v", test.desc, hdr, test.hdr)
}
if !bytes.Equal(value, test.value) {
t.Errorf("%v: got value = %v, want = %v", test.desc, value, test.value)
}
if wantRest := test.input[len(test.input)-test.restLen:]; !bytes.Equal(rest, wantRest) {
t.Errorf("%v: got rest = %v, want = %v", test.desc, rest, wantRest)
}
}
// Test Empty().
if got, want := attrs.Empty(), test.isEmpty; got != want {
t.Errorf("%v: got empty = %v, want = %v", test.desc, got, want)
}
}
}
|