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
|
//+build linux
package vsock_test
import (
"fmt"
"net"
"os"
"regexp"
"strconv"
"testing"
"time"
"github.com/mdlayher/vsock"
"github.com/mdlayher/vsock/internal/vsutil"
"golang.org/x/net/nettest"
)
func TestIntegrationContextIDGuest(t *testing.T) {
if vsutil.IsHypervisor(t) {
t.Skip("skipping, machine is not a guest")
}
cid, err := vsock.ContextID()
if err != nil {
t.Fatalf("failed to retrieve guest's context ID: %v", err)
}
t.Logf("guest context ID: %d", cid)
// Guests should always have a context ID of 3 or more, since
// 0-2 are invalid or reserved.
if cid < 3 {
t.Fatalf("unexpected guest context ID: %d", cid)
}
}
func TestIntegrationContextIDHost(t *testing.T) {
if !vsutil.IsHypervisor(t) {
t.Skip("skipping, machine is not a hypervisor")
}
cid, err := vsock.ContextID()
if err != nil {
t.Fatalf("failed to retrieve host's context ID: %v", err)
}
t.Logf("host context ID: %d", cid)
if want, got := uint32(vsock.Host), cid; want != got {
t.Fatalf("unexpected host context ID:\n- want: %d\n- got: %d",
want, got)
}
}
func TestIntegrationNettestTestConn(t *testing.T) {
vsutil.SkipHostIntegration(t)
nettest.TestConn(t, makeVSockPipe())
}
var cidRe = regexp.MustCompile(`\S+\((\d+)\)`)
func TestIntegrationNettestTestListener(t *testing.T) {
vsutil.SkipHostIntegration(t)
// This test uses the nettest.TestListener API which is being built in:
// https://go-review.googlesource.com/c/net/+/123056.
//
// TODO(mdlayher): stop skipping this test once that CL lands.
mos := func() (ln net.Listener, dial func(string, string) (net.Conn, error), stop func(), err error) {
l, err := vsock.Listen(0)
if err != nil {
return nil, nil, nil, err
}
stop = func() {
// TODO(mdlayher): cancel context if we use vsock.DialContext.
_ = l.Close()
}
dial = func(_, addr string) (net.Conn, error) {
host, sport, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
// Extract the CID value from the surrounding text.
scid := cidRe.FindStringSubmatch(host)
cid, err := strconv.Atoi(scid[1])
if err != nil {
return nil, err
}
port, err := strconv.Atoi(sport)
if err != nil {
return nil, err
}
return vsock.Dial(uint32(cid), uint32(port))
}
return l, dial, stop, nil
}
_ = mos
t.Skip("skipping, enable once https://go-review.googlesource.com/c/net/+/123056 is merged")
// nettest.TestListener(t, mos)
}
func newListener(t *testing.T) (*vsock.Listener, func()) {
t.Helper()
timer := time.AfterFunc(10*time.Second, func() {
panic("test took too long")
})
l, err := vsock.Listen(0)
if err != nil {
vsutil.SkipDeviceError(t, err)
t.Fatalf("failed to create vsock listener: %v", err)
}
return l, func() {
// Clean up the timer and this listener.
timer.Stop()
_ = l.Close()
}
}
func makeVSockPipe() nettest.MakePipe {
return makeLocalPipe(
func() (net.Listener, error) { return vsock.Listen(0) },
func(addr net.Addr) (net.Conn, error) {
a := addr.(*vsock.Addr)
return vsock.Dial(a.ContextID, a.Port)
},
)
}
// makeLocalPipe produces a nettest.MakePipe function using the input functions
// to configure a net.Listener and point a net.Conn at the listener.
//
// This function is proposed for inclusion in x/net/nettest, and should be
// removed from here if the proposal is accepted. See:
// https://github.com/golang/go/issues/30984.
func makeLocalPipe(
listen func() (net.Listener, error),
dial func(addr net.Addr) (net.Conn, error),
) nettest.MakePipe {
if listen == nil {
panic("nil listen function passed to makeLocalPipe")
}
if dial == nil {
dial = func(addr net.Addr) (net.Conn, error) {
return net.Dial(addr.Network(), addr.String())
}
}
// The majority of this code is taken from golang.org/x/net/nettest:
// https://go.googlesource.com/net/+/9e4ed9723b84cb6661bb04e4104f7bfb3ff5d016/nettest/conntest_test.go.
//
// Copyright 2016 The Go Authors. All rights reserved.
return func() (c1, c2 net.Conn, stop func(), err error) {
ln, err := listen()
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create local listener: %v", err)
}
// Start a connection between two endpoints.
var err1, err2 error
done := make(chan bool)
go func() {
c2, err2 = ln.Accept()
close(done)
}()
c1, err1 = dial(ln.Addr())
<-done
stop = func() {
if err1 == nil {
c1.Close()
}
if err2 == nil {
c2.Close()
}
ln.Close()
switch ln.Addr().Network() {
case "unix", "unixpacket":
os.Remove(ln.Addr().String())
}
}
switch {
case err1 != nil:
stop()
return nil, nil, nil, err1
case err2 != nil:
stop()
return nil, nil, nil, err2
default:
return c1, c2, stop, nil
}
}
}
|