| 1 |
package socket_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"math" |
| 10 |
"net" |
| 11 |
"net/netip" |
| 12 |
"os" |
| 13 |
"runtime" |
| 14 |
"sync" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/google/go-cmp/cmp" |
| 19 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 20 |
"github.com/mdlayher/socket/internal/sockettest" |
| 21 |
"golang.org/x/net/nettest" |
| 22 |
"golang.org/x/sync/errgroup" |
| 23 |
"golang.org/x/sys/unix" |
| 24 |
) |
| 25 |
|
| 26 |
func TestConn(t *testing.T) { |
| 27 |
t.Parallel() |
| 28 |
|
| 29 |
tests := []struct { |
| 30 |
name string |
| 31 |
pipe nettest.MakePipe |
| 32 |
}{ |
| 33 |
// Standard library plumbing. |
| 34 |
{ |
| 35 |
name: "basic", |
| 36 |
pipe: makePipe( |
| 37 |
func() (net.Listener, error) { |
| 38 |
return sockettest.Listen(0, nil) |
| 39 |
}, |
| 40 |
func(addr net.Addr) (net.Conn, error) { |
| 41 |
return sockettest.Dial(context.Background(), addr, nil) |
| 42 |
}, |
| 43 |
), |
| 44 |
}, |
| 45 |
// Our own implementations which have context cancelation support. |
| 46 |
{ |
| 47 |
name: "context", |
| 48 |
pipe: makePipe( |
| 49 |
func() (net.Listener, error) { |
| 50 |
l, err := sockettest.Listen(0, nil) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
|
| 55 |
return l.Context(context.Background()), nil |
| 56 |
}, |
| 57 |
func(addr net.Addr) (net.Conn, error) { |
| 58 |
ctx := context.Background() |
| 59 |
|
| 60 |
c, err := sockettest.Dial(ctx, addr, nil) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
|
| 65 |
return c.Context(ctx), nil |
| 66 |
}, |
| 67 |
), |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
for _, tt := range tests { |
| 72 |
t.Run(tt.name, func(t *testing.T) { |
| 73 |
t.Parallel() |
| 74 |
|
| 75 |
nettest.TestConn(t, tt.pipe) |
| 76 |
|
| 77 |
// Our own extensions to TestConn. |
| 78 |
t.Run("CloseReadWrite", func(t *testing.T) { timeoutWrapper(t, tt.pipe, testCloseReadWrite) }) |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestDialTCPNoListener(t *testing.T) { |
| 84 |
t.Parallel() |
| 85 |
|
| 86 |
// See https://github.com/mdlayher/vsock/issues/47 and |
| 87 |
// https://github.com/lxc/lxd/pull/9894 for context on this test. |
| 88 |
// |
| 89 |
// |
| 90 |
// Given a (hopefully) non-existent listener on localhost, expect |
| 91 |
// ECONNREFUSED. |
| 92 |
_, err := sockettest.Dial(context.Background(), &net.TCPAddr{ |
| 93 |
IP: net.IPv6loopback, |
| 94 |
Port: math.MaxUint16, |
| 95 |
}, nil) |
| 96 |
|
| 97 |
want := os.NewSyscallError("connect", unix.ECONNREFUSED) |
| 98 |
if diff := cmp.Diff(want, err); diff != "" { |
| 99 |
t.Fatalf("unexpected connect error (-want +got):\n%s", diff) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestDialTCPContextCanceledBefore(t *testing.T) { |
| 104 |
t.Parallel() |
| 105 |
|
| 106 |
// Context is canceled before any dialing can take place. |
| 107 |
ctx, cancel := context.WithCancel(context.Background()) |
| 108 |
cancel() |
| 109 |
|
| 110 |
_, err := sockettest.Dial(ctx, &net.TCPAddr{ |
| 111 |
IP: net.IPv6loopback, |
| 112 |
Port: math.MaxUint16, |
| 113 |
}, nil) |
| 114 |
|
| 115 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 116 |
t.Fatalf("unexpected connect error (-want +got):\n%s", diff) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
var ipTests = []struct { |
| 121 |
name string |
| 122 |
ip netip.Addr |
| 123 |
}{ |
| 124 |
// It appears we can dial addresses in the documentation range and |
| 125 |
// connect will hang, which is perfect for this test case. |
| 126 |
{ |
| 127 |
name: "IPv4", |
| 128 |
ip: netip.MustParseAddr("192.0.2.1"), |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "IPv6", |
| 132 |
ip: netip.MustParseAddr("2001:db8::1"), |
| 133 |
}, |
| 134 |
} |
| 135 |
|
| 136 |
func TestDialTCPContextCanceledDuring(t *testing.T) { |
| 137 |
t.Parallel() |
| 138 |
|
| 139 |
for _, tt := range ipTests { |
| 140 |
t.Run(tt.name, func(t *testing.T) { |
| 141 |
t.Parallel() |
| 142 |
|
| 143 |
// Context is canceled during a blocking operation but without an |
| 144 |
// explicit deadline passed on the context. |
| 145 |
ctx, cancel := context.WithCancel(context.Background()) |
| 146 |
defer cancel() |
| 147 |
|
| 148 |
go func() { |
| 149 |
time.Sleep(1 * time.Second) |
| 150 |
cancel() |
| 151 |
}() |
| 152 |
|
| 153 |
_, err := sockettest.Dial(ctx, &net.TCPAddr{ |
| 154 |
IP: tt.ip.AsSlice(), |
| 155 |
Port: math.MaxUint16, |
| 156 |
}, nil) |
| 157 |
if errors.Is(err, unix.ENETUNREACH) || errors.Is(err, unix.EHOSTUNREACH) { |
| 158 |
t.Skipf("skipping, no outbound %s connectivity: %v", tt.name, err) |
| 159 |
} |
| 160 |
|
| 161 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 162 |
t.Fatalf("unexpected connect error (-want +got):\n%s", diff) |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestDialTCPContextDeadlineExceeded(t *testing.T) { |
| 169 |
t.Parallel() |
| 170 |
|
| 171 |
for _, tt := range ipTests { |
| 172 |
t.Run(tt.name, func(t *testing.T) { |
| 173 |
t.Parallel() |
| 174 |
|
| 175 |
// Dialing is canceled after the deadline passes. |
| 176 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 177 |
defer cancel() |
| 178 |
|
| 179 |
_, err := sockettest.Dial(ctx, &net.TCPAddr{ |
| 180 |
IP: tt.ip.AsSlice(), |
| 181 |
Port: math.MaxUint16, |
| 182 |
}, nil) |
| 183 |
if errors.Is(err, unix.ENETUNREACH) || errors.Is(err, unix.EHOSTUNREACH) { |
| 184 |
t.Skipf("skipping, no outbound %s connectivity: %v", tt.name, err) |
| 185 |
} |
| 186 |
|
| 187 |
if diff := cmp.Diff(context.DeadlineExceeded, err, cmpopts.EquateErrors()); diff != "" { |
| 188 |
t.Fatalf("unexpected connect error (-want +got):\n%s", diff) |
| 189 |
} |
| 190 |
}) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestListenerAcceptTCPContextCanceledBefore(t *testing.T) { |
| 195 |
t.Parallel() |
| 196 |
|
| 197 |
l, err := sockettest.Listen(0, nil) |
| 198 |
if err != nil { |
| 199 |
t.Fatalf("failed to listen: %v", err) |
| 200 |
} |
| 201 |
defer l.Close() |
| 202 |
|
| 203 |
// Context is canceled before accept can take place. |
| 204 |
ctx, cancel := context.WithCancel(context.Background()) |
| 205 |
cancel() |
| 206 |
|
| 207 |
_, err = l.Context(ctx).Accept() |
| 208 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 209 |
t.Fatalf("unexpected accept error (-want +got):\n%s", diff) |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
func TestListenerAcceptTCPContextCanceledDuring(t *testing.T) { |
| 214 |
t.Parallel() |
| 215 |
|
| 216 |
l, err := sockettest.Listen(0, nil) |
| 217 |
if err != nil { |
| 218 |
t.Fatalf("failed to listen: %v", err) |
| 219 |
} |
| 220 |
defer l.Close() |
| 221 |
|
| 222 |
// Context is canceled during a blocking operation but without an |
| 223 |
// explicit deadline passed on the context. |
| 224 |
ctx, cancel := context.WithCancel(context.Background()) |
| 225 |
defer cancel() |
| 226 |
|
| 227 |
go func() { |
| 228 |
time.Sleep(1 * time.Second) |
| 229 |
cancel() |
| 230 |
}() |
| 231 |
|
| 232 |
_, err = l.Context(ctx).Accept() |
| 233 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 234 |
t.Fatalf("unexpected accept error (-want +got):\n%s", diff) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestListenerAcceptTCPContextCanceledDuringWithDeadline(t *testing.T) { |
| 239 |
t.Parallel() |
| 240 |
|
| 241 |
l, err := sockettest.Listen(0, nil) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("failed to listen: %v", err) |
| 244 |
} |
| 245 |
defer l.Close() |
| 246 |
|
| 247 |
// Context carries both a distant deadline and is canceled early during a |
| 248 |
// blocking operation. Cancelation must be honored immediately rather than |
| 249 |
// waiting for the deadline to expire. |
| 250 |
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 251 |
defer cancel() |
| 252 |
|
| 253 |
go func() { |
| 254 |
time.Sleep(100 * time.Millisecond) |
| 255 |
cancel() |
| 256 |
}() |
| 257 |
|
| 258 |
start := time.Now() |
| 259 |
_, err = l.Context(ctx).Accept() |
| 260 |
elapsed := time.Since(start) |
| 261 |
|
| 262 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 263 |
t.Fatalf("unexpected accept error (-want +got):\n%s", diff) |
| 264 |
} |
| 265 |
if elapsed > 5*time.Second { |
| 266 |
t.Fatalf("accept took %v to observe cancelation, expected immediate return", elapsed) |
| 267 |
} |
| 268 |
|
| 269 |
// The forced wakeup must not leave a stale deadline armed on the socket. |
| 270 |
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) |
| 271 |
defer cancel() |
| 272 |
|
| 273 |
_, err = l.Context(ctx).Accept() |
| 274 |
if diff := cmp.Diff(context.DeadlineExceeded, err, cmpopts.EquateErrors()); diff != "" { |
| 275 |
t.Fatalf("unexpected second accept error (-want +got):\n%s", diff) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func TestListenerAcceptTCPContextBackground(t *testing.T) { |
| 280 |
t.Parallel() |
| 281 |
|
| 282 |
l, err := sockettest.Listen(0, nil) |
| 283 |
if err != nil { |
| 284 |
t.Fatalf("failed to listen: %v", err) |
| 285 |
} |
| 286 |
defer l.Close() |
| 287 |
|
| 288 |
// A context which can never be canceled must still allow the operation |
| 289 |
// to complete normally. |
| 290 |
var eg errgroup.Group |
| 291 |
eg.Go(func() error { |
| 292 |
c, err := l.Context(context.Background()).Accept() |
| 293 |
if err != nil { |
| 294 |
return fmt.Errorf("failed to accept: %v", err) |
| 295 |
} |
| 296 |
return c.Close() |
| 297 |
}) |
| 298 |
|
| 299 |
c, err := net.Dial(l.Addr().Network(), l.Addr().String()) |
| 300 |
if err != nil { |
| 301 |
t.Fatalf("failed to dial listener: %v", err) |
| 302 |
} |
| 303 |
defer c.Close() |
| 304 |
|
| 305 |
if err := eg.Wait(); err != nil { |
| 306 |
t.Fatalf("failed to accept with background context: %v", err) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestListenerAcceptTCPContextDeadlineExceeded(t *testing.T) { |
| 311 |
t.Parallel() |
| 312 |
|
| 313 |
l, err := sockettest.Listen(0, nil) |
| 314 |
if err != nil { |
| 315 |
t.Fatalf("failed to listen: %v", err) |
| 316 |
} |
| 317 |
defer l.Close() |
| 318 |
|
| 319 |
// Accept is canceled after the deadline passes. |
| 320 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 321 |
defer cancel() |
| 322 |
|
| 323 |
_, err = l.Context(ctx).Accept() |
| 324 |
if diff := cmp.Diff(context.DeadlineExceeded, err, cmpopts.EquateErrors()); diff != "" { |
| 325 |
t.Fatalf("unexpected accept error (-want +got):\n%s", diff) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func TestListenerConnTCPContextCanceled(t *testing.T) { |
| 330 |
t.Parallel() |
| 331 |
|
| 332 |
l, err := sockettest.Listen(0, nil) |
| 333 |
if err != nil { |
| 334 |
t.Fatalf("failed to open listener: %v", err) |
| 335 |
} |
| 336 |
defer l.Close() |
| 337 |
|
| 338 |
// Accept a single connection. |
| 339 |
var eg errgroup.Group |
| 340 |
eg.Go(func() error { |
| 341 |
c, err := l.Accept() |
| 342 |
if err != nil { |
| 343 |
return fmt.Errorf("failed to accept: %v", err) |
| 344 |
} |
| 345 |
defer c.Close() |
| 346 |
|
| 347 |
// Context is canceled during recvfrom. |
| 348 |
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 349 |
defer cancel() |
| 350 |
|
| 351 |
b := make([]byte, 1024) |
| 352 |
_, _, err = c.(*sockettest.Conn).Conn.Recvfrom(ctx, b, 0) |
| 353 |
return err |
| 354 |
}) |
| 355 |
|
| 356 |
c, err := net.Dial(l.Addr().Network(), l.Addr().String()) |
| 357 |
if err != nil { |
| 358 |
t.Fatalf("failed to dial listener: %v", err) |
| 359 |
} |
| 360 |
defer c.Close() |
| 361 |
|
| 362 |
// Client never sends data, so we wait until ctx cancel and errgroup return. |
| 363 |
if diff := cmp.Diff(context.DeadlineExceeded, eg.Wait(), cmpopts.EquateErrors()); diff != "" { |
| 364 |
t.Fatalf("unexpected recvfrom error (-want +got):\n%s", diff) |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
func TestListenerConnTCPContextDeadlineExceeded(t *testing.T) { |
| 369 |
t.Parallel() |
| 370 |
|
| 371 |
l, err := sockettest.Listen(0, nil) |
| 372 |
if err != nil { |
| 373 |
t.Fatalf("failed to open listener: %v", err) |
| 374 |
} |
| 375 |
defer l.Close() |
| 376 |
|
| 377 |
// Accept a single connection. |
| 378 |
var eg errgroup.Group |
| 379 |
eg.Go(func() error { |
| 380 |
c, err := l.Accept() |
| 381 |
if err != nil { |
| 382 |
return fmt.Errorf("failed to accept: %v", err) |
| 383 |
} |
| 384 |
defer c.Close() |
| 385 |
|
| 386 |
// Context is canceled before recvfrom can take place. |
| 387 |
ctx, cancel := context.WithCancel(context.Background()) |
| 388 |
cancel() |
| 389 |
|
| 390 |
b := make([]byte, 1024) |
| 391 |
_, _, err = c.(*sockettest.Conn).Conn.Recvfrom(ctx, b, 0) |
| 392 |
return err |
| 393 |
}) |
| 394 |
|
| 395 |
c, err := net.Dial(l.Addr().Network(), l.Addr().String()) |
| 396 |
if err != nil { |
| 397 |
t.Fatalf("failed to dial listener: %v", err) |
| 398 |
} |
| 399 |
defer c.Close() |
| 400 |
|
| 401 |
// Client never sends data, so we wait until ctx cancel and errgroup return. |
| 402 |
if diff := cmp.Diff(context.Canceled, eg.Wait(), cmpopts.EquateErrors()); diff != "" { |
| 403 |
t.Fatalf("unexpected recvfrom error (-want +got):\n%s", diff) |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
func TestListenerConnTCPContextCanceledDuringWithDeadline(t *testing.T) { |
| 408 |
t.Parallel() |
| 409 |
|
| 410 |
l, err := sockettest.Listen(0, nil) |
| 411 |
if err != nil { |
| 412 |
t.Fatalf("failed to open listener: %v", err) |
| 413 |
} |
| 414 |
defer l.Close() |
| 415 |
|
| 416 |
// Accept a single connection. |
| 417 |
var eg errgroup.Group |
| 418 |
eg.Go(func() error { |
| 419 |
c, err := l.Accept() |
| 420 |
if err != nil { |
| 421 |
return fmt.Errorf("failed to accept: %v", err) |
| 422 |
} |
| 423 |
defer c.Close() |
| 424 |
|
| 425 |
// Context carries both a distant deadline and is canceled early |
| 426 |
// during recvmsg. Cancelation must be honored immediately rather than |
| 427 |
// waiting for the deadline to expire. |
| 428 |
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 429 |
defer cancel() |
| 430 |
|
| 431 |
go func() { |
| 432 |
time.Sleep(100 * time.Millisecond) |
| 433 |
cancel() |
| 434 |
}() |
| 435 |
|
| 436 |
start := time.Now() |
| 437 |
b := make([]byte, 1024) |
| 438 |
_, _, _, _, err = c.(*sockettest.Conn).Conn.Recvmsg(ctx, b, nil, 0) |
| 439 |
elapsed := time.Since(start) |
| 440 |
|
| 441 |
if diff := cmp.Diff(context.Canceled, err, cmpopts.EquateErrors()); diff != "" { |
| 442 |
return fmt.Errorf("unexpected recvmsg error (-want +got):\n%s", diff) |
| 443 |
} |
| 444 |
if elapsed > 5*time.Second { |
| 445 |
return fmt.Errorf("recvmsg took %v to observe cancelation, expected immediate return", elapsed) |
| 446 |
} |
| 447 |
|
| 448 |
// The forced wakeup must not leave a stale deadline armed on the |
| 449 |
// socket. |
| 450 |
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) |
| 451 |
defer cancel() |
| 452 |
|
| 453 |
_, _, _, _, err = c.(*sockettest.Conn).Conn.Recvmsg(ctx, b, nil, 0) |
| 454 |
if diff := cmp.Diff(context.DeadlineExceeded, err, cmpopts.EquateErrors()); diff != "" { |
| 455 |
return fmt.Errorf("unexpected second recvmsg error (-want +got):\n%s", diff) |
| 456 |
} |
| 457 |
|
| 458 |
return nil |
| 459 |
}) |
| 460 |
|
| 461 |
c, err := net.Dial(l.Addr().Network(), l.Addr().String()) |
| 462 |
if err != nil { |
| 463 |
t.Fatalf("failed to dial listener: %v", err) |
| 464 |
} |
| 465 |
defer c.Close() |
| 466 |
|
| 467 |
// Client never sends data, so we wait until ctx cancel and errgroup return. |
| 468 |
if err := eg.Wait(); err != nil { |
| 469 |
t.Fatal(err) |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
func TestFileConn(t *testing.T) { |
| 474 |
t.Parallel() |
| 475 |
|
| 476 |
// Use raw system calls to set up the socket since we assume anything being |
| 477 |
// passed into a FileConn is set up by another system, such as systemd's |
| 478 |
// socket activation. |
| 479 |
fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, 0) |
| 480 |
if err != nil { |
| 481 |
t.Fatalf("failed to open socket: %v", err) |
| 482 |
} |
| 483 |
|
| 484 |
// Bind to loopback, any available port. |
| 485 |
sa := &unix.SockaddrInet6{Addr: [16]byte{15: 0x01}} |
| 486 |
if err := unix.Bind(fd, sa); err != nil { |
| 487 |
t.Fatalf("failed to bind: %v", err) |
| 488 |
} |
| 489 |
|
| 490 |
if err := unix.Listen(fd, unix.SOMAXCONN); err != nil { |
| 491 |
t.Fatalf("failed to listen: %v", err) |
| 492 |
} |
| 493 |
|
| 494 |
// The socket should be ready, create a blocking file which is ready to be |
| 495 |
// passed into FileConn via the FileListener helper. |
| 496 |
f := os.NewFile(uintptr(fd), "tcpv6-listener") |
| 497 |
defer f.Close() |
| 498 |
|
| 499 |
l, err := sockettest.FileListener(f) |
| 500 |
if err != nil { |
| 501 |
t.Fatalf("failed to open file listener: %v", err) |
| 502 |
} |
| 503 |
defer l.Close() |
| 504 |
|
| 505 |
// To exercise the listener, attempt to accept and then immediately close a |
| 506 |
// single TCPv6 connection. Dial to the listener from the main goroutine and |
| 507 |
// wait for everything to finish. |
| 508 |
var eg errgroup.Group |
| 509 |
eg.Go(func() error { |
| 510 |
c, err := l.Accept() |
| 511 |
if err != nil { |
| 512 |
return fmt.Errorf("failed to accept: %v", err) |
| 513 |
} |
| 514 |
|
| 515 |
_ = c.Close() |
| 516 |
return nil |
| 517 |
}) |
| 518 |
|
| 519 |
c, err := net.Dial(l.Addr().Network(), l.Addr().String()) |
| 520 |
if err != nil { |
| 521 |
t.Fatalf("failed to dial listener: %v", err) |
| 522 |
} |
| 523 |
_ = c.Close() |
| 524 |
|
| 525 |
if err := eg.Wait(); err != nil { |
| 526 |
t.Fatalf("failed to wait for listener goroutine: %v", err) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// Use our TCP net.Listener and net.Conn implementations backed by *socket.Conn |
| 531 |
// and run compliance tests with nettest.TestConn. |
| 532 |
// |
| 533 |
// This nettest.MakePipe function is adapted from nettest's own tests: |
| 534 |
// https://github.com/golang/net/blob/master/nettest/conntest_test.go |
| 535 |
// |
| 536 |
// Copyright 2016 The Go Authors. All rights reserved. Use of this source |
| 537 |
// code is governed by a BSD-style license that can be found in the LICENSE |
| 538 |
// file. |
| 539 |
func makePipe( |
| 540 |
listen func() (net.Listener, error), |
| 541 |
dial func(addr net.Addr) (net.Conn, error), |
| 542 |
) nettest.MakePipe { |
| 543 |
return func() (c1, c2 net.Conn, stop func(), err error) { |
| 544 |
ln, err := listen() |
| 545 |
if err != nil { |
| 546 |
return nil, nil, nil, err |
| 547 |
} |
| 548 |
|
| 549 |
// Start a connection between two endpoints. |
| 550 |
var err1, err2 error |
| 551 |
done := make(chan bool) |
| 552 |
go func() { |
| 553 |
c2, err2 = ln.Accept() |
| 554 |
close(done) |
| 555 |
}() |
| 556 |
c1, err1 = dial(ln.Addr()) |
| 557 |
<-done |
| 558 |
|
| 559 |
stop = func() { |
| 560 |
if err1 == nil { |
| 561 |
c1.Close() |
| 562 |
} |
| 563 |
if err2 == nil { |
| 564 |
c2.Close() |
| 565 |
} |
| 566 |
ln.Close() |
| 567 |
} |
| 568 |
|
| 569 |
switch { |
| 570 |
case err1 != nil: |
| 571 |
stop() |
| 572 |
return nil, nil, nil, err1 |
| 573 |
case err2 != nil: |
| 574 |
stop() |
| 575 |
return nil, nil, nil, err2 |
| 576 |
default: |
| 577 |
return c1, c2, stop, nil |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
// Copied from x/net/nettest, pending acceptance of: |
| 583 |
// https://go-review.googlesource.com/c/net/+/372815 |
| 584 |
type connTester func(t *testing.T, c1, c2 net.Conn) |
| 585 |
|
| 586 |
func timeoutWrapper(t *testing.T, mp nettest.MakePipe, f connTester) { |
| 587 |
t.Helper() |
| 588 |
c1, c2, stop, err := mp() |
| 589 |
if err != nil { |
| 590 |
t.Fatalf("unable to make pipe: %v", err) |
| 591 |
} |
| 592 |
var once sync.Once |
| 593 |
defer once.Do(func() { stop() }) |
| 594 |
timer := time.AfterFunc(time.Minute, func() { |
| 595 |
once.Do(func() { |
| 596 |
t.Error("test timed out; terminating pipe") |
| 597 |
stop() |
| 598 |
}) |
| 599 |
}) |
| 600 |
defer timer.Stop() |
| 601 |
f(t, c1, c2) |
| 602 |
} |
| 603 |
|
| 604 |
// testCloseReadWrite tests that net.Conns which also implement the optional |
| 605 |
// CloseRead and CloseWrite methods can be half-closed correctly. |
| 606 |
func testCloseReadWrite(t *testing.T, c1, c2 net.Conn) { |
| 607 |
// TODO(mdlayher): investigate why Mac/Windows errors are so different. |
| 608 |
if runtime.GOOS != "linux" { |
| 609 |
t.Skip("skipping, not supported on non-Linux platforms") |
| 610 |
} |
| 611 |
|
| 612 |
type closerConn interface { |
| 613 |
net.Conn |
| 614 |
CloseRead() error |
| 615 |
CloseWrite() error |
| 616 |
} |
| 617 |
|
| 618 |
cc1, ok1 := c1.(closerConn) |
| 619 |
cc2, ok2 := c2.(closerConn) |
| 620 |
if !ok1 || !ok2 { |
| 621 |
// Both c1 and c2 must implement closerConn to proceed. |
| 622 |
return |
| 623 |
} |
| 624 |
|
| 625 |
var wg sync.WaitGroup |
| 626 |
wg.Add(2) |
| 627 |
defer wg.Wait() |
| 628 |
|
| 629 |
go func() { |
| 630 |
defer wg.Done() |
| 631 |
|
| 632 |
// Writing succeeds at first but should result in a permanent "broken |
| 633 |
// pipe" error after closing the write side of the net.Conn. |
| 634 |
b := make([]byte, 64) |
| 635 |
if err := chunkedCopy(cc1, bytes.NewReader(b)); err != nil { |
| 636 |
t.Errorf("unexpected initial cc1.Write error: %v", err) |
| 637 |
} |
| 638 |
if err := cc1.CloseWrite(); err != nil { |
| 639 |
t.Errorf("unexpected cc1.CloseWrite error: %v", err) |
| 640 |
} |
| 641 |
_, err := cc1.Write(b) |
| 642 |
if nerr, ok := err.(net.Error); !ok || nerr.Timeout() { |
| 643 |
t.Errorf("unexpected final cc1.Write error: %v", err) |
| 644 |
} |
| 645 |
}() |
| 646 |
|
| 647 |
go func() { |
| 648 |
defer wg.Done() |
| 649 |
|
| 650 |
// Reading succeeds at first but should result in an EOF error after |
| 651 |
// closing the read side of the net.Conn. |
| 652 |
if err := chunkedCopy(io.Discard, cc2); err != nil { |
| 653 |
t.Errorf("unexpected initial cc2.Read error: %v", err) |
| 654 |
} |
| 655 |
if err := cc2.CloseRead(); err != nil { |
| 656 |
t.Errorf("unexpected cc2.CloseRead error: %v", err) |
| 657 |
} |
| 658 |
if _, err := cc2.Read(make([]byte, 64)); err != io.EOF { |
| 659 |
t.Errorf("unexpected final cc2.Read error: %v", err) |
| 660 |
} |
| 661 |
}() |
| 662 |
} |
| 663 |
|
| 664 |
// chunkedCopy copies from r to w in fixed-width chunks to avoid |
| 665 |
// causing a Write that exceeds the maximum packet size for packet-based |
| 666 |
// connections like "unixpacket". |
| 667 |
// We assume that the maximum packet size is at least 1024. |
| 668 |
func chunkedCopy(w io.Writer, r io.Reader) error { |
| 669 |
b := make([]byte, 1024) |
| 670 |
_, err := io.CopyBuffer(struct{ io.Writer }{w}, struct{ io.Reader }{r}, b) |
| 671 |
return err |
| 672 |
} |