| 1 |
//go:build linux |
| 2 |
// +build linux |
| 3 |
|
| 4 |
package vsock_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"math" |
| 12 |
"net" |
| 13 |
"os" |
| 14 |
"regexp" |
| 15 |
"strconv" |
| 16 |
"sync" |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
"github.com/FuturFusion/vsock" |
| 21 |
"github.com/FuturFusion/vsock/internal/vsutil" |
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
"golang.org/x/net/nettest" |
| 24 |
"golang.org/x/sync/errgroup" |
| 25 |
"golang.org/x/sys/unix" |
| 26 |
) |
| 27 |
|
| 28 |
func TestIntegrationContextIDGuest(t *testing.T) { |
| 29 |
if vsutil.IsHypervisor(t) { |
| 30 |
t.Skip("skipping, machine is not a guest") |
| 31 |
} |
| 32 |
|
| 33 |
cid, err := vsock.ContextID() |
| 34 |
if err != nil { |
| 35 |
t.Fatalf("failed to retrieve guest's context ID: %v", err) |
| 36 |
} |
| 37 |
|
| 38 |
t.Logf("guest context ID: %d", cid) |
| 39 |
|
| 40 |
// Guests should always have a context ID of 3 or more, since |
| 41 |
// 0-2 are invalid or reserved. |
| 42 |
if cid < 3 { |
| 43 |
t.Fatalf("unexpected guest context ID: %d", cid) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestIntegrationContextIDHost(t *testing.T) { |
| 48 |
if !vsutil.IsHypervisor(t) { |
| 49 |
t.Skip("skipping, machine is not a hypervisor") |
| 50 |
} |
| 51 |
|
| 52 |
cid, err := vsock.ContextID() |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("failed to retrieve host's context ID: %v", err) |
| 55 |
} |
| 56 |
|
| 57 |
t.Logf("host context ID: %d", cid) |
| 58 |
|
| 59 |
if want, got := uint32(vsock.Host), cid; want != got { |
| 60 |
t.Fatalf("unexpected host context ID:\n- want: %d\n- got: %d", |
| 61 |
want, got) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestIntegrationListenerUnblockAcceptTimeout(t *testing.T) { |
| 66 |
l, done := newListener(t) |
| 67 |
defer done() |
| 68 |
|
| 69 |
if err := l.SetDeadline(time.Now().Add(100 * time.Millisecond)); err != nil { |
| 70 |
t.Fatalf("failed to set listener deadline: %v", err) |
| 71 |
} |
| 72 |
|
| 73 |
_, err := l.Accept() |
| 74 |
if err == nil { |
| 75 |
t.Fatal("expected an error, but none occurred") |
| 76 |
} |
| 77 |
|
| 78 |
if nerr, ok := err.(net.Error); !ok || (ok && !nerr.Timeout()) { |
| 79 |
t.Errorf("expected timeout network error, but got: %#v", err) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestIntegrationConnShutdown(t *testing.T) { |
| 84 |
vsutil.SkipHostIntegration(t) |
| 85 |
|
| 86 |
// This functionality is proposed for inclusion in x/net/nettest, and should |
| 87 |
// be removed from here if the proposal is accepted. See: |
| 88 |
// https://github.com/golang/go/issues/31033. |
| 89 |
|
| 90 |
timer := time.AfterFunc(10*time.Second, func() { |
| 91 |
panic("test took too long") |
| 92 |
}) |
| 93 |
defer timer.Stop() |
| 94 |
|
| 95 |
mp := makeVSockPipe() |
| 96 |
|
| 97 |
c1, c2, stop, err := mp() |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("failed to make pipe: %v", err) |
| 100 |
} |
| 101 |
defer stop() |
| 102 |
|
| 103 |
vc1, vc2 := c1.(*vsock.Conn), c2.(*vsock.Conn) |
| 104 |
|
| 105 |
var wg sync.WaitGroup |
| 106 |
wg.Add(1) |
| 107 |
defer wg.Wait() |
| 108 |
|
| 109 |
// Perform CloseRead/CloseWrite in lock-step. |
| 110 |
var ( |
| 111 |
readClosed = make(chan struct{}) |
| 112 |
writeClosed = make(chan struct{}) |
| 113 |
) |
| 114 |
|
| 115 |
go func() { |
| 116 |
defer wg.Done() |
| 117 |
|
| 118 |
b := make([]byte, 8) |
| 119 |
if _, err := io.ReadFull(vc2, b); err != nil { |
| 120 |
panicf("failed to vc2.Read: %v", err) |
| 121 |
} |
| 122 |
|
| 123 |
if err := vc2.CloseRead(); err != nil { |
| 124 |
panicf("failed to vc2.CloseRead: %v", err) |
| 125 |
} |
| 126 |
close(readClosed) |
| 127 |
|
| 128 |
// Any further read should return io.EOF. |
| 129 |
<-writeClosed |
| 130 |
if _, err := io.ReadFull(vc2, b); err != io.EOF { |
| 131 |
panicf("expected vc2.Read EOF, but got: %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
if err := vc2.CloseWrite(); err != nil { |
| 135 |
panicf("failed to vc2.CloseWrite: %v", err) |
| 136 |
} |
| 137 |
}() |
| 138 |
|
| 139 |
b := make([]byte, 8) |
| 140 |
if _, err := vc1.Write(b); err != nil { |
| 141 |
t.Fatalf("failed to vc1.Write: %v", err) |
| 142 |
} |
| 143 |
|
| 144 |
// Any write to a read-closed connection should return EPIPE. |
| 145 |
// |
| 146 |
// TODO(mdlayher): this test was flappy until err != nil check was added; |
| 147 |
// sometimes it returns EPIPE and sometimes it does not. Check this. |
| 148 |
<-readClosed |
| 149 |
if _, err := vc1.Write(b); err != nil && !isBrokenPipe(err) { |
| 150 |
t.Fatalf("expected vc1.Write broken pipe, but got: %v", err) |
| 151 |
} |
| 152 |
|
| 153 |
if err := vc1.CloseWrite(); err != nil { |
| 154 |
t.Fatalf("failed to vc1.CloseWrite: %v", err) |
| 155 |
} |
| 156 |
close(writeClosed) |
| 157 |
|
| 158 |
// Any further read should return io.EOF after the other end write-closes. |
| 159 |
if _, err := io.ReadFull(vc1, b); err != io.EOF { |
| 160 |
panicf("expected vc1.Read EOF, but got: %v", err) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestIntegrationConnSyscallConn(t *testing.T) { |
| 165 |
vsutil.SkipHostIntegration(t) |
| 166 |
|
| 167 |
mp := makeVSockPipe() |
| 168 |
|
| 169 |
c, _, stop, err := mp() |
| 170 |
if err != nil { |
| 171 |
t.Fatalf("failed to make pipe: %v", err) |
| 172 |
} |
| 173 |
defer stop() |
| 174 |
|
| 175 |
rc, err := c.(*vsock.Conn).SyscallConn() |
| 176 |
if err != nil { |
| 177 |
t.Fatalf("failed to syscallconn: %v", err) |
| 178 |
} |
| 179 |
|
| 180 |
// Greatly reduce the size of the socket buffer. |
| 181 |
const ( |
| 182 |
size uint64 = 64 |
| 183 |
name = unix.SO_VM_SOCKETS_BUFFER_MAX_SIZE |
| 184 |
) |
| 185 |
|
| 186 |
err = rc.Control(func(fd uintptr) { |
| 187 |
err := unix.SetsockoptUint64(int(fd), unix.AF_VSOCK, name, size) |
| 188 |
if err != nil { |
| 189 |
t.Fatalf("failed to setsockopt: %v", err) |
| 190 |
} |
| 191 |
|
| 192 |
out, err := unix.GetsockoptUint64(int(fd), unix.AF_VSOCK, name) |
| 193 |
if err != nil { |
| 194 |
t.Fatalf("failed to getsockopt: %v", err) |
| 195 |
} |
| 196 |
|
| 197 |
if diff := cmp.Diff(size, out); diff != "" { |
| 198 |
t.Fatalf("unexpected socket buffer size (-want +got):\n%s", diff) |
| 199 |
} |
| 200 |
}) |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("failed to control: %v", err) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestIntegrationConnDialNoListener(t *testing.T) { |
| 207 |
// Dial out to vsock listeners which do not exist, and expect an immediate |
| 208 |
// error rather than hanging. This mostly relies on changes to the |
| 209 |
// underlying socket library, but we test it anyway to lock things in. |
| 210 |
// |
| 211 |
// See: https://github.com/mdlayher/vsock/issues/47. |
| 212 |
const max = math.MaxUint32 |
| 213 |
for _, port := range []uint32{max - 2, max - 1, max} { |
| 214 |
_, err := vsock.Dial(vsock.Local, port, nil) |
| 215 |
if err == nil { |
| 216 |
t.Fatal("dial succeeded, but should not have") |
| 217 |
} |
| 218 |
|
| 219 |
got, ok := err.(*net.OpError) |
| 220 |
if !ok { |
| 221 |
t.Fatalf("expected *net.OpError, but got %T", err) |
| 222 |
} |
| 223 |
|
| 224 |
// Expect one of ECONNRESET, ENODEV, ENETUNREACH, ETIMEDOUT depending on the kernel. |
| 225 |
switch { |
| 226 |
case errors.Is(got.Err, unix.ECONNRESET), errors.Is(got.Err, unix.ENODEV), |
| 227 |
errors.Is(got.Err, unix.ENETUNREACH), errors.Is(got.Err, unix.ETIMEDOUT): |
| 228 |
// OK. |
| 229 |
default: |
| 230 |
t.Fatalf("unexpected syscall error: %v", got.Err) |
| 231 |
} |
| 232 |
|
| 233 |
// Zero out the error comparison. |
| 234 |
got.Err = nil |
| 235 |
|
| 236 |
want := &net.OpError{ |
| 237 |
Op: "dial", |
| 238 |
Net: "vsock", |
| 239 |
Addr: &vsock.Addr{ContextID: vsock.Local, Port: port}, |
| 240 |
} |
| 241 |
|
| 242 |
if diff := cmp.Diff(want, err); diff != "" { |
| 243 |
t.Errorf("unexpected error (-want +got):\n%s", diff) |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestIntegrationFileListenerOK(t *testing.T) { |
| 249 |
// Use raw system calls to set up the socket for FileListener. Although the |
| 250 |
// socket library does the heavy lifting, we want to verify that this also |
| 251 |
// works specifically for AF_VSOCK. |
| 252 |
fd, err := unix.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0) |
| 253 |
if err != nil { |
| 254 |
t.Fatalf("failed to open socket: %v", err) |
| 255 |
} |
| 256 |
|
| 257 |
// Bind to local, any available port. |
| 258 |
err = unix.Bind(fd, &unix.SockaddrVM{ |
| 259 |
CID: unix.VMADDR_CID_LOCAL, |
| 260 |
Port: unix.VMADDR_PORT_ANY, |
| 261 |
}) |
| 262 |
if err != nil { |
| 263 |
// Same problem with GitHub actions kernel, investigate. |
| 264 |
switch err { |
| 265 |
case unix.EADDRNOTAVAIL: |
| 266 |
skipOldKernel(t) |
| 267 |
default: |
| 268 |
t.Fatalf("failed to bind: %v", err) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if err := unix.Listen(fd, unix.SOMAXCONN); err != nil { |
| 273 |
t.Fatalf("failed to listen: %v", err) |
| 274 |
} |
| 275 |
|
| 276 |
// The socket should be ready, create a blocking file which is ready to be |
| 277 |
// passed into FileListener. |
| 278 |
f := os.NewFile(uintptr(fd), "vsock-listener") |
| 279 |
defer f.Close() |
| 280 |
|
| 281 |
l, err := vsock.FileListener(f) |
| 282 |
if err != nil { |
| 283 |
t.Fatalf("failed to open file listener: %v", err) |
| 284 |
} |
| 285 |
defer l.Close() |
| 286 |
|
| 287 |
// To exercise the listener, attempt to accept and then immediately close a |
| 288 |
// single vsock connection. Dial to the listener from the main goroutine and |
| 289 |
// wait for everything to finish. |
| 290 |
var eg errgroup.Group |
| 291 |
eg.Go(func() error { |
| 292 |
c, err := l.Accept() |
| 293 |
if err != nil { |
| 294 |
return fmt.Errorf("failed to accept: %v", err) |
| 295 |
} |
| 296 |
|
| 297 |
_ = c.Close() |
| 298 |
return nil |
| 299 |
}) |
| 300 |
|
| 301 |
addr := l.Addr().(*vsock.Addr) |
| 302 |
c, err := vsock.Dial(addr.ContextID, addr.Port, nil) |
| 303 |
if err != nil { |
| 304 |
t.Fatalf("failed to dial listener: %v", err) |
| 305 |
} |
| 306 |
_ = c.Close() |
| 307 |
|
| 308 |
if err := eg.Wait(); err != nil { |
| 309 |
t.Fatalf("failed to wait for listener goroutine: %v", err) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
func TestIntegrationFileListenerInvalid(t *testing.T) { |
| 314 |
// Same idea as the previous test, but intentionally create a TCP socket |
| 315 |
// instead of a vsock so we can verify the library rejects the socket. |
| 316 |
fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, 0) |
| 317 |
if err != nil { |
| 318 |
t.Fatalf("failed to open socket: %v", err) |
| 319 |
} |
| 320 |
|
| 321 |
// Bind to any address. |
| 322 |
if err := unix.Bind(fd, &unix.SockaddrInet6{}); err != nil { |
| 323 |
t.Fatalf("failed to bind: %v", err) |
| 324 |
} |
| 325 |
|
| 326 |
if err := unix.Listen(fd, unix.SOMAXCONN); err != nil { |
| 327 |
t.Fatalf("failed to listen: %v", err) |
| 328 |
} |
| 329 |
|
| 330 |
// The library should reject this file for having the wrong address family. |
| 331 |
f := os.NewFile(uintptr(fd), "tcpv6-listener") |
| 332 |
defer f.Close() |
| 333 |
|
| 334 |
_, got := vsock.FileListener(f) |
| 335 |
|
| 336 |
want := &net.OpError{ |
| 337 |
Op: "listen", |
| 338 |
Net: "vsock", |
| 339 |
Err: os.NewSyscallError("listen", unix.EINVAL), |
| 340 |
} |
| 341 |
|
| 342 |
if diff := cmp.Diff(want, got); diff != "" { |
| 343 |
t.Fatalf("unexpected error (-want +got):\n%s", diff) |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
func isBrokenPipe(err error) bool { |
| 348 |
if err == nil { |
| 349 |
return false |
| 350 |
} |
| 351 |
|
| 352 |
nerr, ok := err.(*net.OpError) |
| 353 |
if !ok { |
| 354 |
return false |
| 355 |
} |
| 356 |
|
| 357 |
return nerr.Err == unix.EPIPE |
| 358 |
} |
| 359 |
|
| 360 |
func TestIntegrationNettestTestConn(t *testing.T) { |
| 361 |
vsutil.SkipHostIntegration(t) |
| 362 |
|
| 363 |
nettest.TestConn(t, makeVSockPipe()) |
| 364 |
} |
| 365 |
|
| 366 |
var cidRe = regexp.MustCompile(`\S+\((\d+)\)`) |
| 367 |
|
| 368 |
func TestIntegrationNettestTestListener(t *testing.T) { |
| 369 |
vsutil.SkipHostIntegration(t) |
| 370 |
|
| 371 |
// This test uses the nettest.TestListener API which is being built in: |
| 372 |
// https://go-review.googlesource.com/c/net/+/123056. |
| 373 |
// |
| 374 |
// TODO(mdlayher): stop skipping this test once that CL lands. |
| 375 |
|
| 376 |
mos := func() (ln net.Listener, dial func(string, string) (net.Conn, error), stop func(), err error) { |
| 377 |
l, err := vsock.ListenContextID(vsock.Local, 0, nil) |
| 378 |
if err != nil { |
| 379 |
return nil, nil, nil, err |
| 380 |
} |
| 381 |
|
| 382 |
stop = func() { |
| 383 |
// TODO(mdlayher): cancel context if we use vsock.DialContext. |
| 384 |
_ = l.Close() |
| 385 |
} |
| 386 |
|
| 387 |
dial = func(_, addr string) (net.Conn, error) { |
| 388 |
host, sport, err := net.SplitHostPort(addr) |
| 389 |
if err != nil { |
| 390 |
return nil, err |
| 391 |
} |
| 392 |
|
| 393 |
// Extract the CID value from the surrounding text. |
| 394 |
scid := cidRe.FindStringSubmatch(host) |
| 395 |
cid, err := strconv.Atoi(scid[1]) |
| 396 |
if err != nil { |
| 397 |
return nil, err |
| 398 |
} |
| 399 |
|
| 400 |
port, err := strconv.Atoi(sport) |
| 401 |
if err != nil { |
| 402 |
return nil, err |
| 403 |
} |
| 404 |
|
| 405 |
return vsock.Dial(uint32(cid), uint32(port), nil) |
| 406 |
} |
| 407 |
|
| 408 |
return l, dial, stop, nil |
| 409 |
} |
| 410 |
|
| 411 |
_ = mos |
| 412 |
t.Skip("skipping, enable once https://go-review.googlesource.com/c/net/+/123056 is merged") |
| 413 |
// nettest.TestListener(t, mos) |
| 414 |
} |
| 415 |
|
| 416 |
func newListener(t *testing.T) (*vsock.Listener, func()) { |
| 417 |
t.Helper() |
| 418 |
|
| 419 |
timer := time.AfterFunc(10*time.Second, func() { |
| 420 |
panic("test took too long") |
| 421 |
}) |
| 422 |
|
| 423 |
// Bind to Local for all integration tests to avoid the need to run a |
| 424 |
// hypervisor and VM setup. |
| 425 |
l, err := vsock.ListenContextID(vsock.Local, 0, nil) |
| 426 |
if err != nil { |
| 427 |
vsutil.SkipDeviceError(t, err) |
| 428 |
|
| 429 |
// Unwrap net.OpError + os.SyscallError if needed. |
| 430 |
// TODO(mdlayher): errors.Unwrap in Go 1.13. |
| 431 |
nerr, ok := err.(*net.OpError) |
| 432 |
if !ok { |
| 433 |
t.Fatalf("failed to create vsock listener: %v", err) |
| 434 |
} |
| 435 |
serr, ok := nerr.Err.(*os.SyscallError) |
| 436 |
if !ok { |
| 437 |
t.Fatalf("unexpected inner error for *net.OpError: %#v", nerr.Err) |
| 438 |
} |
| 439 |
|
| 440 |
switch serr.Err { |
| 441 |
case unix.EADDRNOTAVAIL: |
| 442 |
skipOldKernel(t) |
| 443 |
default: |
| 444 |
t.Fatalf("unexpected vsock listener system call error: %v", err) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
return l, func() { |
| 449 |
// Clean up the timer and this listener. |
| 450 |
timer.Stop() |
| 451 |
_ = l.Close() |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
func skipOldKernel(t *testing.T) { |
| 456 |
t.Helper() |
| 457 |
|
| 458 |
// The kernel in use is to old to support Local binds, so this |
| 459 |
// test must be skipped. Print an informative message. |
| 460 |
var utsname unix.Utsname |
| 461 |
if err := unix.Uname(&utsname); err != nil { |
| 462 |
t.Fatalf("failed to get uname: %v", err) |
| 463 |
} |
| 464 |
|
| 465 |
t.Skipf("skipping, kernel %s is too old to support AF_VSOCK local binds", |
| 466 |
string(bytes.TrimRight(utsname.Release[:], "\x00"))) |
| 467 |
} |
| 468 |
|
| 469 |
func makeVSockPipe() nettest.MakePipe { |
| 470 |
return makeLocalPipe( |
| 471 |
func() (net.Listener, error) { return vsock.ListenContextID(vsock.Local, 0, nil) }, |
| 472 |
func(addr net.Addr) (net.Conn, error) { |
| 473 |
// ContextID will always be Local. |
| 474 |
a := addr.(*vsock.Addr) |
| 475 |
return vsock.Dial(a.ContextID, a.Port, nil) |
| 476 |
}, |
| 477 |
) |
| 478 |
} |
| 479 |
|
| 480 |
// makeLocalPipe produces a nettest.MakePipe function using the input functions |
| 481 |
// to configure a net.Listener and point a net.Conn at the listener. |
| 482 |
// |
| 483 |
// This function is proposed for inclusion in x/net/nettest, and should be |
| 484 |
// removed from here if the proposal is accepted. See: |
| 485 |
// https://github.com/golang/go/issues/30984. |
| 486 |
func makeLocalPipe( |
| 487 |
listen func() (net.Listener, error), |
| 488 |
dial func(addr net.Addr) (net.Conn, error), |
| 489 |
) nettest.MakePipe { |
| 490 |
if listen == nil { |
| 491 |
panic("nil listen function passed to makeLocalPipe") |
| 492 |
} |
| 493 |
|
| 494 |
if dial == nil { |
| 495 |
dial = func(addr net.Addr) (net.Conn, error) { |
| 496 |
return net.Dial(addr.Network(), addr.String()) |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
// The majority of this code is taken from golang.org/x/net/nettest: |
| 501 |
// https://go.googlesource.com/net/+/9e4ed9723b84cb6661bb04e4104f7bfb3ff5d016/nettest/conntest_test.go. |
| 502 |
// |
| 503 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 504 |
|
| 505 |
return func() (c1, c2 net.Conn, stop func(), err error) { |
| 506 |
ln, err := listen() |
| 507 |
if err != nil { |
| 508 |
return nil, nil, nil, fmt.Errorf("failed to create local listener: %v", err) |
| 509 |
} |
| 510 |
|
| 511 |
// Start a connection between two endpoints. |
| 512 |
var err1, err2 error |
| 513 |
done := make(chan bool) |
| 514 |
go func() { |
| 515 |
c2, err2 = ln.Accept() |
| 516 |
close(done) |
| 517 |
}() |
| 518 |
c1, err1 = dial(ln.Addr()) |
| 519 |
<-done |
| 520 |
|
| 521 |
stop = func() { |
| 522 |
if err1 == nil { |
| 523 |
c1.Close() |
| 524 |
} |
| 525 |
if err2 == nil { |
| 526 |
c2.Close() |
| 527 |
} |
| 528 |
ln.Close() |
| 529 |
switch ln.Addr().Network() { |
| 530 |
case "unix", "unixpacket": |
| 531 |
os.Remove(ln.Addr().String()) |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
switch { |
| 536 |
case err1 != nil: |
| 537 |
stop() |
| 538 |
return nil, nil, nil, err1 |
| 539 |
case err2 != nil: |
| 540 |
stop() |
| 541 |
return nil, nil, nil, err2 |
| 542 |
default: |
| 543 |
return c1, c2, stop, nil |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
func panicf(format string, a ...interface{}) { |
| 549 |
panic(fmt.Sprintf(format, a...)) |
| 550 |
} |