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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
// Copyright 2018 Google LLC
//
// 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 host
import (
"reflect"
"syscall"
"testing"
"gvisor.googlesource.com/gvisor/pkg/fd"
"gvisor.googlesource.com/gvisor/pkg/sentry/context/contexttest"
ktime "gvisor.googlesource.com/gvisor/pkg/sentry/kernel/time"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket"
"gvisor.googlesource.com/gvisor/pkg/sentry/socket/unix/transport"
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
"gvisor.googlesource.com/gvisor/pkg/syserr"
"gvisor.googlesource.com/gvisor/pkg/tcpip"
"gvisor.googlesource.com/gvisor/pkg/waiter"
"gvisor.googlesource.com/gvisor/pkg/waiter/fdnotifier"
)
var (
// Make sure that ConnectedEndpoint implements transport.ConnectedEndpoint.
_ = transport.ConnectedEndpoint(new(ConnectedEndpoint))
// Make sure that ConnectedEndpoint implements transport.Receiver.
_ = transport.Receiver(new(ConnectedEndpoint))
)
func getFl(fd int) (uint32, error) {
fl, _, err := syscall.RawSyscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_GETFL, 0)
if err == 0 {
return uint32(fl), nil
}
return 0, err
}
func TestSocketIsBlocking(t *testing.T) {
// Using socketpair here because it's already connected.
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("host socket creation failed: %v", err)
}
fl, err := getFl(pair[0])
if err != nil {
t.Fatalf("getFl: fcntl(%v, GETFL) => %v", pair[0], err)
}
if fl&syscall.O_NONBLOCK == syscall.O_NONBLOCK {
t.Fatalf("Expected socket %v to be blocking", pair[0])
}
if fl, err = getFl(pair[1]); err != nil {
t.Fatalf("getFl: fcntl(%v, GETFL) => %v", pair[1], err)
}
if fl&syscall.O_NONBLOCK == syscall.O_NONBLOCK {
t.Fatalf("Expected socket %v to be blocking", pair[1])
}
sock, err := newSocket(contexttest.Context(t), pair[0], false)
if err != nil {
t.Fatalf("newSocket(%v) failed => %v", pair[0], err)
}
defer sock.DecRef()
// Test that the socket now is non-blocking.
if fl, err = getFl(pair[0]); err != nil {
t.Fatalf("getFl: fcntl(%v, GETFL) => %v", pair[0], err)
}
if fl&syscall.O_NONBLOCK != syscall.O_NONBLOCK {
t.Errorf("Expected socket %v to have become non-blocking", pair[0])
}
if fl, err = getFl(pair[1]); err != nil {
t.Fatalf("getFl: fcntl(%v, GETFL) => %v", pair[1], err)
}
if fl&syscall.O_NONBLOCK == syscall.O_NONBLOCK {
t.Errorf("Did not expect socket %v to become non-blocking", pair[1])
}
}
func TestSocketWritev(t *testing.T) {
// Using socketpair here because it's already connected.
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("host socket creation failed: %v", err)
}
socket, err := newSocket(contexttest.Context(t), pair[0], false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", pair[0], err)
}
defer socket.DecRef()
buf := []byte("hello world\n")
n, err := socket.Writev(contexttest.Context(t), usermem.BytesIOSequence(buf))
if err != nil {
t.Fatalf("socket writev failed: %v", err)
}
if n != int64(len(buf)) {
t.Fatalf("socket writev wrote incorrect bytes: %d", n)
}
}
func TestSocketWritevLen0(t *testing.T) {
// Using socketpair here because it's already connected.
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("host socket creation failed: %v", err)
}
socket, err := newSocket(contexttest.Context(t), pair[0], false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", pair[0], err)
}
defer socket.DecRef()
n, err := socket.Writev(contexttest.Context(t), usermem.BytesIOSequence(nil))
if err != nil {
t.Fatalf("socket writev failed: %v", err)
}
if n != 0 {
t.Fatalf("socket writev wrote incorrect bytes: %d", n)
}
}
func TestSocketSendMsgLen0(t *testing.T) {
// Using socketpair here because it's already connected.
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("host socket creation failed: %v", err)
}
sfile, err := newSocket(contexttest.Context(t), pair[0], false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", pair[0], err)
}
defer sfile.DecRef()
s := sfile.FileOperations.(socket.Socket)
n, terr := s.SendMsg(nil, usermem.BytesIOSequence(nil), []byte{}, 0, false, ktime.Time{}, socket.ControlMessages{})
if n != 0 {
t.Fatalf("socket sendmsg() failed: %v wrote: %d", terr, n)
}
if terr != nil {
t.Fatalf("socket sendmsg() failed: %v", terr)
}
}
func TestListen(t *testing.T) {
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) => %v", err)
}
sfile1, err := newSocket(contexttest.Context(t), pair[0], false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", pair[0], err)
}
defer sfile1.DecRef()
socket1 := sfile1.FileOperations.(socket.Socket)
sfile2, err := newSocket(contexttest.Context(t), pair[1], false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", pair[1], err)
}
defer sfile2.DecRef()
socket2 := sfile2.FileOperations.(socket.Socket)
// Socketpairs can not be listened to.
if err := socket1.Listen(nil, 64); err != syserr.ErrInvalidEndpointState {
t.Fatalf("socket1.Listen(nil, 64) => %v, want syserr.ErrInvalidEndpointState", err)
}
if err := socket2.Listen(nil, 64); err != syserr.ErrInvalidEndpointState {
t.Fatalf("socket2.Listen(nil, 64) => %v, want syserr.ErrInvalidEndpointState", err)
}
// Create a Unix socket, do not bind it.
sock, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0) => %v", err)
}
sfile3, err := newSocket(contexttest.Context(t), sock, false)
if err != nil {
t.Fatalf("newSocket(%v) => %v", sock, err)
}
defer sfile3.DecRef()
socket3 := sfile3.FileOperations.(socket.Socket)
// This socket is not bound so we can't listen on it.
if err := socket3.Listen(nil, 64); err != syserr.ErrInvalidEndpointState {
t.Fatalf("socket3.Listen(nil, 64) => %v, want syserr.ErrInvalidEndpointState", err)
}
}
func TestSend(t *testing.T) {
e := ConnectedEndpoint{writeClosed: true}
if _, _, err := e.Send(nil, transport.ControlMessages{}, tcpip.FullAddress{}); err != syserr.ErrClosedForSend {
t.Errorf("Got %#v.Send() = %v, want = %v", e, err, syserr.ErrClosedForSend)
}
}
func TestRecv(t *testing.T) {
e := ConnectedEndpoint{readClosed: true}
if _, _, _, _, _, err := e.Recv(nil, false, 0, false); err != syserr.ErrClosedForReceive {
t.Errorf("Got %#v.Recv() = %v, want = %v", e, err, syserr.ErrClosedForReceive)
}
}
func TestPasscred(t *testing.T) {
e := ConnectedEndpoint{}
if got, want := e.Passcred(), false; got != want {
t.Errorf("Got %#v.Passcred() = %t, want = %t", e, got, want)
}
}
func TestGetLocalAddress(t *testing.T) {
e := ConnectedEndpoint{path: "foo"}
want := tcpip.FullAddress{Addr: tcpip.Address("foo")}
if got, err := e.GetLocalAddress(); err != nil || got != want {
t.Errorf("Got %#v.GetLocalAddress() = %#v, %v, want = %#v, %v", e, got, err, want, nil)
}
}
func TestQueuedSize(t *testing.T) {
e := ConnectedEndpoint{}
tests := []struct {
name string
f func() int64
}{
{"SendQueuedSize", e.SendQueuedSize},
{"RecvQueuedSize", e.RecvQueuedSize},
}
for _, test := range tests {
if got, want := test.f(), int64(-1); got != want {
t.Errorf("Got %#v.%s() = %d, want = %d", e, test.name, got, want)
}
}
}
func TestReadable(t *testing.T) {
e := ConnectedEndpoint{readClosed: true}
if got, want := e.Readable(), true; got != want {
t.Errorf("Got %#v.Readable() = %t, want = %t", e, got, want)
}
}
func TestWritable(t *testing.T) {
e := ConnectedEndpoint{writeClosed: true}
if got, want := e.Writable(), true; got != want {
t.Errorf("Got %#v.Writable() = %t, want = %t", e, got, want)
}
}
func TestRelease(t *testing.T) {
f, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c := &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f)}
want := &ConnectedEndpoint{queue: c.queue}
want.ref.DecRef()
fdnotifier.AddFD(int32(c.file.FD()), nil)
c.Release()
if !reflect.DeepEqual(c, want) {
t.Errorf("got = %#v, want = %#v", c, want)
}
}
func TestClose(t *testing.T) {
type testCase struct {
name string
cep *ConnectedEndpoint
addFD bool
f func()
want *ConnectedEndpoint
}
var tests []testCase
// nil is the value used by ConnectedEndpoint to indicate a closed file.
// Non-nil files are used to check if the file gets closed.
f, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c := &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f)}
tests = append(tests, testCase{
name: "First CloseRecv",
cep: c,
addFD: false,
f: c.CloseRecv,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), readClosed: true}
tests = append(tests, testCase{
name: "Second CloseRecv",
cep: c,
addFD: false,
f: c.CloseRecv,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f)}
tests = append(tests, testCase{
name: "First CloseSend",
cep: c,
addFD: false,
f: c.CloseSend,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, writeClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), writeClosed: true}
tests = append(tests, testCase{
name: "Second CloseSend",
cep: c,
addFD: false,
f: c.CloseSend,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, writeClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), writeClosed: true}
tests = append(tests, testCase{
name: "CloseSend then CloseRecv",
cep: c,
addFD: true,
f: c.CloseRecv,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true, writeClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), readClosed: true}
tests = append(tests, testCase{
name: "CloseRecv then CloseSend",
cep: c,
addFD: true,
f: c.CloseSend,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true, writeClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), readClosed: true, writeClosed: true}
tests = append(tests, testCase{
name: "Full close then CloseRecv",
cep: c,
addFD: false,
f: c.CloseRecv,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true, writeClosed: true},
})
f, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, 0)
if err != nil {
t.Fatal("Creating socket:", err)
}
c = &ConnectedEndpoint{queue: &waiter.Queue{}, file: fd.New(f), readClosed: true, writeClosed: true}
tests = append(tests, testCase{
name: "Full close then CloseSend",
cep: c,
addFD: false,
f: c.CloseSend,
want: &ConnectedEndpoint{queue: c.queue, file: c.file, readClosed: true, writeClosed: true},
})
for _, test := range tests {
if test.addFD {
fdnotifier.AddFD(int32(test.cep.file.FD()), nil)
}
if test.f(); !reflect.DeepEqual(test.cep, test.want) {
t.Errorf("%s: got = %#v, want = %#v", test.name, test.cep, test.want)
}
}
}
|