| 1 |
//go:build linux |
| 2 |
|
| 3 |
package socket_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"context" |
| 8 |
"encoding/binary" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"net" |
| 13 |
"os" |
| 14 |
"runtime" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/google/go-cmp/cmp" |
| 18 |
"github.com/mdlayher/socket" |
| 19 |
"github.com/mdlayher/socket/internal/sockettest" |
| 20 |
"golang.org/x/sync/errgroup" |
| 21 |
"golang.org/x/sys/unix" |
| 22 |
) |
| 23 |
|
| 24 |
func TestLinuxConnBuffers(t *testing.T) { |
| 25 |
t.Parallel() |
| 26 |
|
| 27 |
// This test isn't necessarily Linux-specific but it's easiest to verify on |
| 28 |
// Linux because we can rely on the kernel's documented buffer size |
| 29 |
// manipulation behavior. |
| 30 |
c, err := socket.Socket(unix.AF_INET, unix.SOCK_STREAM, 0, "tcpv4", nil) |
| 31 |
if err != nil { |
| 32 |
t.Fatalf("failed to open socket: %v", err) |
| 33 |
} |
| 34 |
defer c.Close() |
| 35 |
|
| 36 |
const ( |
| 37 |
set = 8192 |
| 38 |
|
| 39 |
// Per socket(7): |
| 40 |
// |
| 41 |
// "The kernel doubles this value (to allow space for |
| 42 |
// bookâkeeping overhead) when it is set using setsockopt(2), |
| 43 |
// and this doubled value is returned by getsockopt(2)."" |
| 44 |
want = set * 2 |
| 45 |
) |
| 46 |
|
| 47 |
if err := c.SetReadBuffer(set); err != nil { |
| 48 |
t.Fatalf("failed to set read buffer size: %v", err) |
| 49 |
} |
| 50 |
|
| 51 |
if err := c.SetWriteBuffer(set); err != nil { |
| 52 |
t.Fatalf("failed to set write buffer size: %v", err) |
| 53 |
} |
| 54 |
|
| 55 |
// Now that we've set the buffers, we can check the size by asking the |
| 56 |
// kernel using SyscallConn and getsockopt. |
| 57 |
|
| 58 |
rcv, err := c.ReadBuffer() |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("failed to get read buffer size: %v", err) |
| 61 |
} |
| 62 |
|
| 63 |
snd, err := c.WriteBuffer() |
| 64 |
if err != nil { |
| 65 |
t.Fatalf("failed to get write buffer size: %v", err) |
| 66 |
} |
| 67 |
|
| 68 |
if diff := cmp.Diff(want, rcv); diff != "" { |
| 69 |
t.Fatalf("unexpected read buffer size (-want +got):\n%s", diff) |
| 70 |
} |
| 71 |
if diff := cmp.Diff(want, snd); diff != "" { |
| 72 |
t.Fatalf("unexpected write buffer size (-want +got):\n%s", diff) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestLinuxNetworkNamespaces(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
|
| 79 |
l, err := sockettest.Listen(0, nil) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("failed to create listener: %v", err) |
| 82 |
} |
| 83 |
defer l.Close() |
| 84 |
|
| 85 |
addrC := make(chan net.Addr, 1) |
| 86 |
|
| 87 |
var eg errgroup.Group |
| 88 |
eg.Go(func() error { |
| 89 |
// We are poisoning this thread by creating a new anonymous network |
| 90 |
// namespace. Do not unlock the OS thread so that the runtime will kill |
| 91 |
// this thread when the goroutine exits. |
| 92 |
runtime.LockOSThread() |
| 93 |
|
| 94 |
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil { |
| 95 |
// Explicit wrap to check for permission denied. |
| 96 |
return fmt.Errorf("failed to unshare network namespace: %w", err) |
| 97 |
} |
| 98 |
|
| 99 |
ns, err := socket.ThreadNetNS() |
| 100 |
if err != nil { |
| 101 |
return fmt.Errorf("failed to get listener thread's network namespace: %v", err) |
| 102 |
} |
| 103 |
|
| 104 |
// This OS thread has been moved to a different network namespace and |
| 105 |
// thus we should also be able to start a listener on the same port. |
| 106 |
l, err := sockettest.Listen( |
| 107 |
l.Addr().(*net.TCPAddr).Port, |
| 108 |
&socket.Config{NetNS: ns.FD()}, |
| 109 |
) |
| 110 |
if err != nil { |
| 111 |
return fmt.Errorf("failed to create listener in network namespace: %v", err) |
| 112 |
} |
| 113 |
defer l.Close() |
| 114 |
|
| 115 |
addrC <- l.Addr() |
| 116 |
return nil |
| 117 |
}) |
| 118 |
|
| 119 |
if err := eg.Wait(); err != nil { |
| 120 |
if errors.Is(err, os.ErrPermission) { |
| 121 |
t.Skipf("skipping, permission denied: %v", err) |
| 122 |
} |
| 123 |
|
| 124 |
t.Fatalf("failed to run listener thread: %v", err) |
| 125 |
} |
| 126 |
|
| 127 |
select { |
| 128 |
case addr := <-addrC: |
| 129 |
if diff := cmp.Diff(l.Addr(), addr); diff != "" { |
| 130 |
t.Fatalf("unexpected network address (-want +got):\n%s", diff) |
| 131 |
} |
| 132 |
default: |
| 133 |
t.Fatal("listener thread did not return its local address") |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestLinuxDialVsockNoListener(t *testing.T) { |
| 138 |
t.Parallel() |
| 139 |
|
| 140 |
// See https://github.com/mdlayher/vsock/issues/47 and |
| 141 |
// https://github.com/lxc/lxd/pull/9894 for context on this test. |
| 142 |
c, err := socket.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0, "vsock", nil) |
| •143 |
if err != nil { |
| 144 |
// The buildds seem not to have the vsock kernel module loaded, so skip |
| 145 |
// the test if we can't open the socket |
| 146 |
t.Skipf("failed to open socket: %v", err) |
| 147 |
} |
| 148 |
defer c.Close() |
| 149 |
|
| 150 |
// Given a (hopefully) non-existent listener on localhost, expect |
| 151 |
// ECONNRESET. |
| 152 |
_, err = c.Connect(context.Background(), &unix.SockaddrVM{ |
| 153 |
CID: unix.VMADDR_CID_LOCAL, |
| 154 |
Port: math.MaxUint32, |
| 155 |
}) |
| 156 |
if err == nil { |
| 157 |
// See https://github.com/mdlayher/socket/issues/4. |
| 158 |
t.Skipf("skipping, expected error but vsock successfully connected to local service") |
| 159 |
} |
| 160 |
|
| 161 |
want := os.NewSyscallError("connect", unix.ECONNRESET) |
| •162 |
if diff := cmp.Diff(want, err); diff != "" { |
| 163 |
t.Skipf("unexpected connect error (-want +got):\n%s", diff) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestLinuxOpenPIDFD(t *testing.T) { |
| 168 |
// Verify we can use regular files with socket by properly handling |
| 169 |
// ENOTSOCK, as is the case with pidfds. |
| 170 |
fd, err := unix.PidfdOpen(1, unix.PIDFD_NONBLOCK) |
| •171 |
if err != nil { |
| 172 |
if errors.Is(err, unix.EINVAL) { |
| 173 |
t.Skipf("skipping, PIDFD_NONBLOCK is not implemented: %v", err) |
| 174 |
} |
| 175 |
t.Fatalf("failed to open pidfd for init: %v", err) |
| 176 |
} |
| 177 |
|
| 178 |
c, err := socket.New(fd, "pidfd") |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("failed to open Conn for pidfd: %v", err) |
| 181 |
} |
| 182 |
_ = c.Close() |
| 183 |
} |
| 184 |
|
| 185 |
func TestLinuxBindToDevice(t *testing.T) { |
| 186 |
t.Parallel() |
| 187 |
|
| 188 |
c, err := socket.Socket(unix.AF_INET, unix.SOCK_STREAM, 0, "tcpv4", nil) |
| 189 |
if err != nil { |
| 190 |
t.Fatalf("failed to open socket: %v", err) |
| 191 |
} |
| 192 |
defer c.Close() |
| 193 |
|
| 194 |
// Assumes the loopback interface is always the first device on Linux |
| 195 |
// machines. |
| 196 |
const ( |
| 197 |
name = "lo" |
| 198 |
index = 1 |
| 199 |
) |
| 200 |
|
| •201 |
if err := c.SetsockoptString(unix.SOL_SOCKET, unix.SO_BINDTODEVICE, name); err != nil { |
| 202 |
t.Skipf("failed to bind to device: %v", err) |
| 203 |
} |
| 204 |
|
| 205 |
gotName, err := c.GetsockoptString(unix.SOL_SOCKET, unix.SO_BINDTODEVICE) |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("failed to get bound interface name: %v", err) |
| 208 |
} |
| 209 |
if diff := cmp.Diff(name, gotName); diff != "" { |
| 210 |
t.Fatalf("unexpected interface name (-want +got):\n%s", diff) |
| 211 |
} |
| 212 |
|
| 213 |
gotIndex, err := c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_BINDTOIFINDEX) |
| 214 |
if err != nil { |
| 215 |
t.Fatalf("failed to get bound interface index: %v", err) |
| 216 |
} |
| 217 |
if diff := cmp.Diff(index, gotIndex); diff != "" { |
| 218 |
t.Fatalf("unexpected interface index (-want +got):\n%s", diff) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func TestLinuxConnSockoptBytesRoundTrip(t *testing.T) { |
| 223 |
t.Parallel() |
| 224 |
|
| 225 |
c, err := socket.Socket(unix.AF_INET, unix.SOCK_STREAM, 0, "tcpv4", nil) |
| 226 |
if err != nil { |
| 227 |
t.Fatalf("failed to open socket: %v", err) |
| 228 |
} |
| 229 |
defer c.Close() |
| 230 |
|
| 231 |
// Set SO_RCVBUF as a raw 4-byte native-endian integer, then read it back |
| 232 |
// both as raw bytes and as an integer and verify they agree. Per |
| 233 |
// socket(7) the kernel doubles the value that was set. |
| 234 |
const set = 8192 |
| 235 |
in := binary.NativeEndian.AppendUint32(nil, set) |
| 236 |
|
| 237 |
if err := c.SetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, in); err != nil { |
| 238 |
t.Fatalf("failed to set SO_RCVBUF bytes: %v", err) |
| 239 |
} |
| 240 |
|
| 241 |
want, err := c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_RCVBUF) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("failed to get SO_RCVBUF int: %v", err) |
| 244 |
} |
| 245 |
if diff := cmp.Diff(set*2, want); diff != "" { |
| 246 |
t.Fatalf("unexpected SO_RCVBUF int (-want +got):\n%s", diff) |
| 247 |
} |
| 248 |
|
| 249 |
out := make([]byte, 4) |
| 250 |
n, err := c.GetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, out) |
| 251 |
if err != nil { |
| 252 |
t.Fatalf("failed to get SO_RCVBUF bytes: %v", err) |
| 253 |
} |
| 254 |
if diff := cmp.Diff(4, n); diff != "" { |
| 255 |
t.Fatalf("unexpected SO_RCVBUF optlen (-want +got):\n%s", diff) |
| 256 |
} |
| 257 |
|
| 258 |
got := int(binary.NativeEndian.Uint32(out)) |
| 259 |
if diff := cmp.Diff(want, got); diff != "" { |
| 260 |
t.Fatalf("unexpected SO_RCVBUF bytes value (-want +got):\n%s", diff) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
func TestLinuxConnGetsockoptBytesShortOptlen(t *testing.T) { |
| 265 |
t.Parallel() |
| 266 |
|
| 267 |
c, err := socket.Socket(unix.AF_INET, unix.SOCK_STREAM, 0, "tcpv4", nil) |
| 268 |
if err != nil { |
| 269 |
t.Fatalf("failed to open socket: %v", err) |
| 270 |
} |
| 271 |
defer c.Close() |
| 272 |
|
| 273 |
want, err := c.GetsockoptInt(unix.SOL_SOCKET, unix.SO_SNDBUF) |
| 274 |
if err != nil { |
| 275 |
t.Fatalf("failed to get SO_SNDBUF int: %v", err) |
| 276 |
} |
| 277 |
|
| 278 |
// Pass a buffer larger than the option and fill the trailing bytes with a |
| 279 |
// sentinel value: the kernel must report the true optlen and leave the |
| 280 |
// bytes beyond it untouched. |
| 281 |
out := bytes.Repeat([]byte{0xff}, 16) |
| 282 |
n, err := c.GetsockoptBytes(unix.SOL_SOCKET, unix.SO_SNDBUF, out) |
| 283 |
if err != nil { |
| 284 |
t.Fatalf("failed to get SO_SNDBUF bytes: %v", err) |
| 285 |
} |
| 286 |
if diff := cmp.Diff(4, n); diff != "" { |
| 287 |
t.Fatalf("unexpected SO_SNDBUF optlen (-want +got):\n%s", diff) |
| 288 |
} |
| 289 |
|
| 290 |
got := int(binary.NativeEndian.Uint32(out[:n])) |
| 291 |
if diff := cmp.Diff(want, got); diff != "" { |
| 292 |
t.Fatalf("unexpected SO_SNDBUF bytes value (-want +got):\n%s", diff) |
| 293 |
} |
| 294 |
|
| 295 |
if diff := cmp.Diff(bytes.Repeat([]byte{0xff}, 12), out[n:]); diff != "" { |
| 296 |
t.Fatalf("unexpected trailing bytes (-want +got):\n%s", diff) |
| 297 |
} |
| 298 |
|
| 299 |
// An unbound socket reports SO_BINDTODEVICE with optlen 0 and does not |
| 300 |
// touch the buffer. |
| 301 |
name := bytes.Repeat([]byte{0xff}, unix.IFNAMSIZ) |
| 302 |
n, err = c.GetsockoptBytes(unix.SOL_SOCKET, unix.SO_BINDTODEVICE, name) |
| 303 |
if err != nil { |
| 304 |
t.Fatalf("failed to get SO_BINDTODEVICE bytes: %v", err) |
| 305 |
} |
| 306 |
if diff := cmp.Diff(0, n); diff != "" { |
| 307 |
t.Fatalf("unexpected SO_BINDTODEVICE optlen (-want +got):\n%s", diff) |
| 308 |
} |
| 309 |
if diff := cmp.Diff(bytes.Repeat([]byte{0xff}, unix.IFNAMSIZ), name); diff != "" { |
| 310 |
t.Fatalf("unexpected SO_BINDTODEVICE bytes (-want +got):\n%s", diff) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
func TestLinuxConnSockoptBytesEmpty(t *testing.T) { |
| 315 |
t.Parallel() |
| 316 |
|
| 317 |
c, err := socket.Socket(unix.AF_INET, unix.SOCK_STREAM, 0, "tcpv4", nil) |
| 318 |
if err != nil { |
| 319 |
t.Fatalf("failed to open socket: %v", err) |
| 320 |
} |
| 321 |
defer c.Close() |
| 322 |
|
| 323 |
for _, b := range [][]byte{nil, {}} { |
| 324 |
// Empty buffers must pass a nil pointer with optlen 0 rather than |
| 325 |
// panicking. The kernel rejects an integer option that is too short. |
| 326 |
err := c.SetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, b) |
| 327 |
if !errors.Is(err, unix.EINVAL) { |
| 328 |
t.Fatalf("expected EINVAL from empty setsockopt, but got: %v", err) |
| 329 |
} |
| 330 |
|
| 331 |
// The kernel clamps optlen to the buffer size, so getsockopt with an |
| 332 |
// empty buffer succeeds and reports optlen 0. |
| 333 |
n, err := c.GetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, b) |
| 334 |
if err != nil { |
| 335 |
t.Fatalf("failed to get SO_RCVBUF with empty buffer: %v", err) |
| 336 |
} |
| 337 |
if diff := cmp.Diff(0, n); diff != "" { |
| 338 |
t.Fatalf("unexpected empty getsockopt optlen (-want +got):\n%s", diff) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// Closed connections must report EBADF. |
| 343 |
if err := c.Close(); err != nil { |
| 344 |
t.Fatalf("failed to close: %v", err) |
| 345 |
} |
| 346 |
|
| 347 |
if err := c.SetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, []byte{0, 0, 0, 0}); !errors.Is(err, unix.EBADF) { |
| 348 |
t.Fatalf("expected EBADF from closed setsockopt, but got: %v", err) |
| 349 |
} |
| 350 |
if _, err := c.GetsockoptBytes(unix.SOL_SOCKET, unix.SO_RCVBUF, make([]byte, 4)); !errors.Is(err, unix.EBADF) { |
| 351 |
t.Fatalf("expected EBADF from closed getsockopt, but got: %v", err) |
| 352 |
} |
| 353 |
} |