2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
// +build !js
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2017-10-07 00:16:47 +00:00
|
|
|
"bufio"
|
|
|
|
"context"
|
2018-12-28 15:30:48 +00:00
|
|
|
"internal/poll"
|
2017-04-10 11:32:00 +00:00
|
|
|
"internal/testenv"
|
2014-09-21 17:33:12 +00:00
|
|
|
"io"
|
2018-12-28 15:30:48 +00:00
|
|
|
"os"
|
2012-03-27 23:13:14 +00:00
|
|
|
"runtime"
|
2014-09-21 17:33:12 +00:00
|
|
|
"sync"
|
2012-03-27 23:13:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
var prohibitionaryDialArgTests = []struct {
|
|
|
|
network string
|
|
|
|
address string
|
|
|
|
}{
|
|
|
|
{"tcp6", "127.0.0.1"},
|
|
|
|
{"tcp6", "::ffff:127.0.0.1"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProhibitionaryDialArg(t *testing.T) {
|
2017-10-07 00:16:47 +00:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4map() {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skip("mapping ipv4 address inside ipv6 address not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := Listen("tcp", "[::]:0")
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
_, port, err := SplitHostPort(ln.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
for i, tt := range prohibitionaryDialArgTests {
|
|
|
|
c, err := Dial(tt.network, JoinHostPort(tt.address, port))
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func TestDialLocal(t *testing.T) {
|
|
|
|
ln, err := newLocalListener("tcp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
defer ln.Close()
|
|
|
|
_, port, err := SplitHostPort(ln.Addr().String())
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
c, err := Dial("tcp", JoinHostPort("", port))
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestDialerDualStackFDLeak(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
t.Skipf("%s does not have full support of socktest", runtime.GOOS)
|
|
|
|
case "windows":
|
|
|
|
t.Skipf("not implemented a way to cancel dial racers in TCP SYN-SENT state on %s", runtime.GOOS)
|
2017-10-07 00:16:47 +00:00
|
|
|
case "openbsd":
|
|
|
|
testenv.SkipFlaky(t, 15157)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
before := sw.Sockets()
|
2017-04-10 11:32:00 +00:00
|
|
|
origTestHookLookupIP := testHookLookupIP
|
|
|
|
defer func() { testHookLookupIP = origTestHookLookupIP }()
|
|
|
|
testHookLookupIP = lookupLocalhost
|
|
|
|
handler := func(dss *dualStackServer, ln Listener) {
|
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
c.Close()
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
dss, err := newDualStackServer()
|
2017-04-10 11:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := dss.buildup(handler); err != nil {
|
2017-10-07 00:16:47 +00:00
|
|
|
dss.teardown()
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const N = 10
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(N)
|
2018-12-28 15:30:48 +00:00
|
|
|
d := &Dialer{DualStack: true, Timeout: 5 * time.Second}
|
2017-04-10 11:32:00 +00:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
c, err := d.Dial("tcp", JoinHostPort("localhost", dss.port))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2017-10-07 00:16:47 +00:00
|
|
|
dss.teardown()
|
2017-04-10 11:32:00 +00:00
|
|
|
after := sw.Sockets()
|
|
|
|
if len(after) != len(before) {
|
|
|
|
t.Errorf("got %d; want %d", len(after), len(before))
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// Define a pair of blackholed (IPv4, IPv6) addresses, for which dialTCP is
|
|
|
|
// expected to hang until the timeout elapses. These addresses are reserved
|
|
|
|
// for benchmarking by RFC 6890.
|
|
|
|
const (
|
2017-10-07 00:16:47 +00:00
|
|
|
slowDst4 = "198.18.0.254"
|
|
|
|
slowDst6 = "2001:2::254"
|
2017-04-10 11:32:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// In some environments, the slow IPs may be explicitly unreachable, and fail
|
|
|
|
// more quickly than expected. This test hook prevents dialTCP from returning
|
|
|
|
// before the deadline.
|
2019-06-02 15:48:37 +00:00
|
|
|
func slowDialTCP(ctx context.Context, network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
sd := &sysDialer{network: network, address: raddr.String()}
|
|
|
|
c, err := sd.doDialTCP(ctx, laddr, raddr)
|
2017-04-10 11:32:00 +00:00
|
|
|
if ParseIP(slowDst4).Equal(raddr.IP) || ParseIP(slowDst6).Equal(raddr.IP) {
|
2017-10-07 00:16:47 +00:00
|
|
|
// Wait for the deadline, or indefinitely if none exists.
|
|
|
|
<-ctx.Done()
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
return c, err
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func dialClosedPort() (actual, expected time.Duration) {
|
|
|
|
// Estimate the expected time for this platform.
|
|
|
|
// On Windows, dialing a closed port takes roughly 1 second,
|
|
|
|
// but other platforms should be instantaneous.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = 1500 * time.Millisecond
|
2018-12-28 15:30:48 +00:00
|
|
|
} else if runtime.GOOS == "darwin" {
|
|
|
|
expected = 150 * time.Millisecond
|
2017-04-10 11:32:00 +00:00
|
|
|
} else {
|
|
|
|
expected = 95 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
return 999 * time.Hour, expected
|
|
|
|
}
|
|
|
|
addr := l.Addr().String()
|
|
|
|
l.Close()
|
|
|
|
// On OpenBSD, interference from TestSelfConnect is mysteriously
|
|
|
|
// causing the first attempt to hang for a few seconds, so we throw
|
|
|
|
// away the first result and keep the second.
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
startTime := time.Now()
|
|
|
|
c, err := Dial("tcp", addr)
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
elapsed := time.Now().Sub(startTime)
|
|
|
|
if i == 2 {
|
|
|
|
return elapsed, expected
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestDialParallel(t *testing.T) {
|
2017-10-07 00:16:47 +00:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
closedPortDelay, expectClosedPortDelay := dialClosedPort()
|
|
|
|
if closedPortDelay > expectClosedPortDelay {
|
|
|
|
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
const instant time.Duration = 0
|
|
|
|
const fallbackDelay = 200 * time.Millisecond
|
|
|
|
|
|
|
|
// Some cases will run quickly when "connection refused" is fast,
|
2017-10-07 00:16:47 +00:00
|
|
|
// or trigger the fallbackDelay on Windows. This value holds the
|
2017-04-10 11:32:00 +00:00
|
|
|
// lesser of the two delays.
|
|
|
|
var closedPortOrFallbackDelay time.Duration
|
|
|
|
if closedPortDelay < fallbackDelay {
|
|
|
|
closedPortOrFallbackDelay = closedPortDelay
|
|
|
|
} else {
|
|
|
|
closedPortOrFallbackDelay = fallbackDelay
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
origTestHookDialTCP := testHookDialTCP
|
|
|
|
defer func() { testHookDialTCP = origTestHookDialTCP }()
|
|
|
|
testHookDialTCP = slowDialTCP
|
|
|
|
|
|
|
|
nCopies := func(s string, n int) []string {
|
|
|
|
out := make([]string, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
out[i] = s
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
primaries []string
|
|
|
|
fallbacks []string
|
|
|
|
teardownNetwork string
|
|
|
|
expectOk bool
|
|
|
|
expectElapsed time.Duration
|
|
|
|
}{
|
|
|
|
// These should just work on the first try.
|
|
|
|
{[]string{"127.0.0.1"}, []string{}, "", true, instant},
|
|
|
|
{[]string{"::1"}, []string{}, "", true, instant},
|
|
|
|
{[]string{"127.0.0.1", "::1"}, []string{slowDst6}, "tcp6", true, instant},
|
|
|
|
{[]string{"::1", "127.0.0.1"}, []string{slowDst4}, "tcp4", true, instant},
|
|
|
|
// Primary is slow; fallback should kick in.
|
|
|
|
{[]string{slowDst4}, []string{"::1"}, "", true, fallbackDelay},
|
|
|
|
// Skip a "connection refused" in the primary thread.
|
|
|
|
{[]string{"127.0.0.1", "::1"}, []string{}, "tcp4", true, closedPortDelay},
|
|
|
|
{[]string{"::1", "127.0.0.1"}, []string{}, "tcp6", true, closedPortDelay},
|
|
|
|
// Skip a "connection refused" in the fallback thread.
|
|
|
|
{[]string{slowDst4, slowDst6}, []string{"::1", "127.0.0.1"}, "tcp6", true, fallbackDelay + closedPortDelay},
|
|
|
|
// Primary refused, fallback without delay.
|
|
|
|
{[]string{"127.0.0.1"}, []string{"::1"}, "tcp4", true, closedPortOrFallbackDelay},
|
|
|
|
{[]string{"::1"}, []string{"127.0.0.1"}, "tcp6", true, closedPortOrFallbackDelay},
|
|
|
|
// Everything is refused.
|
|
|
|
{[]string{"127.0.0.1"}, []string{}, "tcp4", false, closedPortDelay},
|
|
|
|
// Nothing to do; fail instantly.
|
|
|
|
{[]string{}, []string{}, "", false, instant},
|
|
|
|
// Connecting to tons of addresses should not trip the deadline.
|
|
|
|
{nCopies("::1", 1000), []string{}, "", true, instant},
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := func(dss *dualStackServer, ln Listener) {
|
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Close()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
// Convert a list of IP strings into TCPAddrs.
|
|
|
|
makeAddrs := func(ips []string, port string) addrList {
|
|
|
|
var out addrList
|
|
|
|
for _, ip := range ips {
|
|
|
|
addr, err := ResolveTCPAddr("tcp", JoinHostPort(ip, port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
out = append(out, addr)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
return out
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
for i, tt := range testCases {
|
2017-10-07 00:16:47 +00:00
|
|
|
dss, err := newDualStackServer()
|
2017-04-10 11:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dss.teardown()
|
|
|
|
if err := dss.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tt.teardownNetwork != "" {
|
|
|
|
// Destroy one of the listening sockets, creating an unreachable port.
|
|
|
|
dss.teardownNetwork(tt.teardownNetwork)
|
|
|
|
}
|
|
|
|
|
|
|
|
primaries := makeAddrs(tt.primaries, dss.port)
|
|
|
|
fallbacks := makeAddrs(tt.fallbacks, dss.port)
|
|
|
|
d := Dialer{
|
|
|
|
FallbackDelay: fallbackDelay,
|
|
|
|
}
|
|
|
|
startTime := time.Now()
|
2019-06-02 15:48:37 +00:00
|
|
|
sd := &sysDialer{
|
2017-10-07 00:16:47 +00:00
|
|
|
Dialer: d,
|
|
|
|
network: "tcp",
|
|
|
|
address: "?",
|
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
c, err := sd.dialParallel(context.Background(), primaries, fallbacks)
|
2017-10-07 00:16:47 +00:00
|
|
|
elapsed := time.Since(startTime)
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.expectOk && err != nil {
|
|
|
|
t.Errorf("#%d: got %v; want nil", i, err)
|
|
|
|
} else if !tt.expectOk && err == nil {
|
|
|
|
t.Errorf("#%d: got nil; want non-nil", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectElapsedMin := tt.expectElapsed - 95*time.Millisecond
|
|
|
|
expectElapsedMax := tt.expectElapsed + 95*time.Millisecond
|
2019-06-02 15:48:37 +00:00
|
|
|
if elapsed < expectElapsedMin {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Errorf("#%d: got %v; want >= %v", i, elapsed, expectElapsedMin)
|
2019-06-02 15:48:37 +00:00
|
|
|
} else if elapsed > expectElapsedMax {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Errorf("#%d: got %v; want <= %v", i, elapsed, expectElapsedMax)
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
// Repeat each case, ensuring that it can be canceled quickly.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
startTime = time.Now()
|
2019-06-02 15:48:37 +00:00
|
|
|
c, err = sd.dialParallel(ctx, primaries, fallbacks)
|
2017-10-07 00:16:47 +00:00
|
|
|
if c != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
elapsed = time.Now().Sub(startTime)
|
|
|
|
if elapsed > 100*time.Millisecond {
|
|
|
|
t.Errorf("#%d (cancel): got %v; want <= 100ms", i, elapsed)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func lookupSlowFast(ctx context.Context, fn func(context.Context, string, string) ([]IPAddr, error), network, host string) ([]IPAddr, error) {
|
2017-04-10 11:32:00 +00:00
|
|
|
switch host {
|
|
|
|
case "slow6loopback4":
|
|
|
|
// Returns a slow IPv6 address, and a local IPv4 address.
|
|
|
|
return []IPAddr{
|
|
|
|
{IP: ParseIP(slowDst6)},
|
|
|
|
{IP: ParseIP("127.0.0.1")},
|
|
|
|
}, nil
|
|
|
|
default:
|
2019-06-02 15:48:37 +00:00
|
|
|
return fn(ctx, network, host)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialerFallbackDelay(t *testing.T) {
|
2017-10-07 00:16:47 +00:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
origTestHookLookupIP := testHookLookupIP
|
|
|
|
defer func() { testHookLookupIP = origTestHookLookupIP }()
|
|
|
|
testHookLookupIP = lookupSlowFast
|
2015-08-28 15:33:40 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
origTestHookDialTCP := testHookDialTCP
|
|
|
|
defer func() { testHookDialTCP = origTestHookDialTCP }()
|
|
|
|
testHookDialTCP = slowDialTCP
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
dualstack bool
|
|
|
|
delay time.Duration
|
|
|
|
expectElapsed time.Duration
|
|
|
|
}{
|
|
|
|
// Use a very brief delay, which should fallback immediately.
|
|
|
|
{true, 1 * time.Nanosecond, 0},
|
|
|
|
// Use a 200ms explicit timeout.
|
|
|
|
{true, 200 * time.Millisecond, 200 * time.Millisecond},
|
|
|
|
// The default is 300ms.
|
|
|
|
{true, 0, 300 * time.Millisecond},
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
handler := func(dss *dualStackServer, ln Listener) {
|
2014-09-21 17:33:12 +00:00
|
|
|
for {
|
2017-04-10 11:32:00 +00:00
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
2014-09-21 17:33:12 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
c.Close()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
dss, err := newDualStackServer()
|
2014-09-21 17:33:12 +00:00
|
|
|
if err != nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Fatal(err)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
defer dss.teardown()
|
2017-04-10 11:32:00 +00:00
|
|
|
if err := dss.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
for i, tt := range testCases {
|
2017-10-07 00:16:47 +00:00
|
|
|
d := &Dialer{DualStack: tt.dualstack, FallbackDelay: tt.delay}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
c, err := d.Dial("tcp", JoinHostPort("slow6loopback4", dss.port))
|
|
|
|
elapsed := time.Now().Sub(startTime)
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
} else if tt.dualstack {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
expectMin := tt.expectElapsed - 1*time.Millisecond
|
|
|
|
expectMax := tt.expectElapsed + 95*time.Millisecond
|
2019-06-02 15:48:37 +00:00
|
|
|
if elapsed < expectMin {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Errorf("#%d: got %v; want >= %v", i, elapsed, expectMin)
|
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
if elapsed > expectMax {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Errorf("#%d: got %v; want <= %v", i, elapsed, expectMax)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func TestDialParallelSpuriousConnection(t *testing.T) {
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-10-07 00:16:47 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
handler := func(dss *dualStackServer, ln Listener) {
|
|
|
|
// Accept one connection per address.
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// The client should close itself, without sending data.
|
|
|
|
c.SetReadDeadline(time.Now().Add(1 * time.Second))
|
|
|
|
var b [1]byte
|
|
|
|
if _, err := c.Read(b[:]); err != io.EOF {
|
|
|
|
t.Errorf("got %v; want %v", err, io.EOF)
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
dss, err := newDualStackServer()
|
2014-09-21 17:33:12 +00:00
|
|
|
if err != nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Fatal(err)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
defer dss.teardown()
|
|
|
|
if err := dss.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fallbackDelay = 100 * time.Millisecond
|
|
|
|
|
|
|
|
origTestHookDialTCP := testHookDialTCP
|
|
|
|
defer func() { testHookDialTCP = origTestHookDialTCP }()
|
|
|
|
testHookDialTCP = func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
// Sleep long enough for Happy Eyeballs to kick in, and inhibit cancelation.
|
|
|
|
// This forces dialParallel to juggle two successful connections.
|
|
|
|
time.Sleep(fallbackDelay * 2)
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
// Now ignore the provided context (which will be canceled) and use a
|
|
|
|
// different one to make sure this completes with a valid connection,
|
|
|
|
// which we hope to be closed below:
|
2019-06-02 15:48:37 +00:00
|
|
|
sd := &sysDialer{network: net, address: raddr.String()}
|
|
|
|
return sd.doDialTCP(context.Background(), laddr, raddr)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
d := Dialer{
|
|
|
|
FallbackDelay: fallbackDelay,
|
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
sd := &sysDialer{
|
2017-10-07 00:16:47 +00:00
|
|
|
Dialer: d,
|
|
|
|
network: "tcp",
|
|
|
|
address: "?",
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
makeAddr := func(ip string) addrList {
|
|
|
|
addr, err := ResolveTCPAddr("tcp", JoinHostPort(ip, dss.port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return addrList{addr}
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
// dialParallel returns one connection (and closes the other.)
|
2019-06-02 15:48:37 +00:00
|
|
|
c, err := sd.dialParallel(context.Background(), makeAddr("127.0.0.1"), makeAddr("::1"))
|
2014-09-21 17:33:12 +00:00
|
|
|
if err != nil {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Fatal(err)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
c.Close()
|
2017-04-10 11:32:00 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
// The server should've seen both connections.
|
|
|
|
wg.Wait()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestDialerPartialDeadline(t *testing.T) {
|
|
|
|
now := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
var testCases = []struct {
|
|
|
|
now time.Time
|
|
|
|
deadline time.Time
|
|
|
|
addrs int
|
|
|
|
expectDeadline time.Time
|
|
|
|
expectErr error
|
|
|
|
}{
|
|
|
|
// Regular division.
|
|
|
|
{now, now.Add(12 * time.Second), 1, now.Add(12 * time.Second), nil},
|
|
|
|
{now, now.Add(12 * time.Second), 2, now.Add(6 * time.Second), nil},
|
|
|
|
{now, now.Add(12 * time.Second), 3, now.Add(4 * time.Second), nil},
|
|
|
|
// Bump against the 2-second sane minimum.
|
|
|
|
{now, now.Add(12 * time.Second), 999, now.Add(2 * time.Second), nil},
|
|
|
|
// Total available is now below the sane minimum.
|
|
|
|
{now, now.Add(1900 * time.Millisecond), 999, now.Add(1900 * time.Millisecond), nil},
|
|
|
|
// Null deadline.
|
|
|
|
{now, noDeadline, 1, noDeadline, nil},
|
|
|
|
// Step the clock forward and cross the deadline.
|
|
|
|
{now.Add(-1 * time.Millisecond), now, 1, now, nil},
|
2018-12-28 15:30:48 +00:00
|
|
|
{now.Add(0 * time.Millisecond), now, 1, noDeadline, poll.ErrTimeout},
|
|
|
|
{now.Add(1 * time.Millisecond), now, 1, noDeadline, poll.ErrTimeout},
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
for i, tt := range testCases {
|
|
|
|
deadline, err := partialDeadline(tt.now, tt.deadline, tt.addrs)
|
|
|
|
if err != tt.expectErr {
|
|
|
|
t.Errorf("#%d: got %v; want %v", i, err, tt.expectErr)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
if !deadline.Equal(tt.expectDeadline) {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Errorf("#%d: got %v; want %v", i, deadline, tt.expectDeadline)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestDialerLocalAddr(t *testing.T) {
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-10-07 00:16:47 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
type test struct {
|
|
|
|
network, raddr string
|
|
|
|
laddr Addr
|
|
|
|
error
|
|
|
|
}
|
|
|
|
var tests = []test{
|
|
|
|
{"tcp4", "127.0.0.1", nil, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{}, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: ParseIP("0.0.0.0")}, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: ParseIP("0.0.0.0").To4()}, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: ParseIP("::")}, &AddrError{Err: "some error"}},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: ParseIP("127.0.0.1").To4()}, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: ParseIP("127.0.0.1").To16()}, nil},
|
|
|
|
{"tcp4", "127.0.0.1", &TCPAddr{IP: IPv6loopback}, errNoSuitableAddress},
|
|
|
|
{"tcp4", "127.0.0.1", &UDPAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
{"tcp4", "127.0.0.1", &UnixAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
|
|
|
|
{"tcp6", "::1", nil, nil},
|
|
|
|
{"tcp6", "::1", &TCPAddr{}, nil},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: ParseIP("0.0.0.0")}, nil},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: ParseIP("0.0.0.0").To4()}, nil},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: ParseIP("::")}, nil},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: ParseIP("127.0.0.1").To4()}, errNoSuitableAddress},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: ParseIP("127.0.0.1").To16()}, errNoSuitableAddress},
|
|
|
|
{"tcp6", "::1", &TCPAddr{IP: IPv6loopback}, nil},
|
|
|
|
{"tcp6", "::1", &UDPAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
{"tcp6", "::1", &UnixAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
|
|
|
|
{"tcp", "127.0.0.1", nil, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{}, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("0.0.0.0")}, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("0.0.0.0").To4()}, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("127.0.0.1").To4()}, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("127.0.0.1").To16()}, nil},
|
|
|
|
{"tcp", "127.0.0.1", &TCPAddr{IP: IPv6loopback}, errNoSuitableAddress},
|
|
|
|
{"tcp", "127.0.0.1", &UDPAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
{"tcp", "127.0.0.1", &UnixAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
|
|
|
|
{"tcp", "::1", nil, nil},
|
|
|
|
{"tcp", "::1", &TCPAddr{}, nil},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: ParseIP("0.0.0.0")}, nil},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: ParseIP("0.0.0.0").To4()}, nil},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: ParseIP("::")}, nil},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: ParseIP("127.0.0.1").To4()}, errNoSuitableAddress},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: ParseIP("127.0.0.1").To16()}, errNoSuitableAddress},
|
|
|
|
{"tcp", "::1", &TCPAddr{IP: IPv6loopback}, nil},
|
|
|
|
{"tcp", "::1", &UDPAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
{"tcp", "::1", &UnixAddr{}, &AddrError{Err: "some error"}},
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:30:48 +00:00
|
|
|
if supportsIPv4map() {
|
2017-10-07 00:16:47 +00:00
|
|
|
tests = append(tests, test{
|
|
|
|
"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("::")}, nil,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
tests = append(tests, test{
|
|
|
|
"tcp", "127.0.0.1", &TCPAddr{IP: ParseIP("::")}, &AddrError{Err: "some error"},
|
|
|
|
})
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
origTestHookLookupIP := testHookLookupIP
|
|
|
|
defer func() { testHookLookupIP = origTestHookLookupIP }()
|
|
|
|
testHookLookupIP = lookupLocalhost
|
|
|
|
handler := func(ls *localServer, ln Listener) {
|
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
var err error
|
|
|
|
var lss [2]*localServer
|
|
|
|
for i, network := range []string{"tcp4", "tcp6"} {
|
|
|
|
lss[i], err = newLocalServer(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer lss[i].teardown()
|
|
|
|
if err := lss[i].buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
d := &Dialer{LocalAddr: tt.laddr}
|
|
|
|
var addr string
|
|
|
|
ip := ParseIP(tt.raddr)
|
|
|
|
if ip.To4() != nil {
|
|
|
|
addr = lss[0].Listener.Addr().String()
|
|
|
|
}
|
|
|
|
if ip.To16() != nil && ip.To4() == nil {
|
|
|
|
addr = lss[1].Listener.Addr().String()
|
|
|
|
}
|
|
|
|
c, err := d.Dial(tt.network, addr)
|
|
|
|
if err == nil && tt.error != nil || err != nil && tt.error == nil {
|
2018-12-28 15:30:48 +00:00
|
|
|
// On Darwin this occasionally times out.
|
|
|
|
// We don't know why. Issue #22019.
|
|
|
|
if runtime.GOOS == "darwin" && tt.error == nil && os.IsTimeout(err) {
|
|
|
|
t.Logf("ignoring timeout error on Darwin; see https://golang.org/issue/22019")
|
|
|
|
} else {
|
|
|
|
t.Errorf("%s %v->%s: got %v; want %v", tt.network, tt.laddr, tt.raddr, err, tt.error)
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.Close()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestDialerDualStack(t *testing.T) {
|
2018-12-28 15:30:48 +00:00
|
|
|
testenv.SkipFlaky(t, 13324)
|
|
|
|
|
|
|
|
if !supportsIPv4() || !supportsIPv6() {
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skip("both IPv4 and IPv6 are required")
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
closedPortDelay, expectClosedPortDelay := dialClosedPort()
|
|
|
|
if closedPortDelay > expectClosedPortDelay {
|
|
|
|
t.Errorf("got %v; want <= %v", closedPortDelay, expectClosedPortDelay)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
origTestHookLookupIP := testHookLookupIP
|
|
|
|
defer func() { testHookLookupIP = origTestHookLookupIP }()
|
|
|
|
testHookLookupIP = lookupLocalhost
|
|
|
|
handler := func(dss *dualStackServer, ln Listener) {
|
2014-09-21 17:33:12 +00:00
|
|
|
for {
|
2017-04-10 11:32:00 +00:00
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
2014-09-21 17:33:12 +00:00
|
|
|
return
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
c.Close()
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
var timeout = 150*time.Millisecond + closedPortDelay
|
|
|
|
for _, dualstack := range []bool{false, true} {
|
2017-10-07 00:16:47 +00:00
|
|
|
dss, err := newDualStackServer()
|
2017-04-10 11:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dss.teardown()
|
|
|
|
if err := dss.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &Dialer{DualStack: dualstack, Timeout: timeout}
|
|
|
|
for range dss.lns {
|
|
|
|
c, err := d.Dial("tcp", JoinHostPort("localhost", dss.port))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch addr := c.LocalAddr().(*TCPAddr); {
|
|
|
|
case addr.IP.To4() != nil:
|
2014-09-21 17:33:12 +00:00
|
|
|
dss.teardownNetwork("tcp4")
|
2017-04-10 11:32:00 +00:00
|
|
|
case addr.IP.To16() != nil && addr.IP.To4() == nil:
|
2014-09-21 17:33:12 +00:00
|
|
|
dss.teardownNetwork("tcp6")
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
|
|
|
|
func TestDialerKeepAlive(t *testing.T) {
|
2017-04-10 11:32:00 +00:00
|
|
|
handler := func(ls *localServer, ln Listener) {
|
2015-08-28 15:33:40 +00:00
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
ls, err := newLocalServer("tcp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ls.teardown()
|
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
defer func() { testHookSetKeepAlive = func(time.Duration) {} }()
|
2017-04-10 11:32:00 +00:00
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
tests := []struct {
|
|
|
|
ka time.Duration
|
|
|
|
expected time.Duration
|
|
|
|
}{
|
|
|
|
{-1, -1},
|
|
|
|
{0, 15 * time.Second},
|
|
|
|
{5 * time.Second, 5 * time.Second},
|
|
|
|
{30 * time.Second, 30 * time.Second},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
var got time.Duration = -1
|
|
|
|
testHookSetKeepAlive = func(d time.Duration) { got = d }
|
|
|
|
d := Dialer{KeepAlive: test.ka}
|
2017-04-10 11:32:00 +00:00
|
|
|
c, err := d.Dial("tcp", ls.Listener.Addr().String())
|
2015-08-28 15:33:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c.Close()
|
2019-06-02 15:48:37 +00:00
|
|
|
if got != test.expected {
|
|
|
|
t.Errorf("Dialer.KeepAlive = %v: SetKeepAlive set to %v, want %v", d.KeepAlive, got, test.expected)
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
func TestDialCancel(t *testing.T) {
|
2017-10-07 00:16:47 +00:00
|
|
|
switch testenv.Builder() {
|
|
|
|
case "linux-arm64-buildlet":
|
|
|
|
t.Skip("skipping on linux-arm64-buildlet; incompatible network config? issue 15191")
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
mustHaveExternalNetwork(t)
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "nacl" {
|
|
|
|
// nacl doesn't have external network access.
|
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blackholeIPPort := JoinHostPort(slowDst4, "1234")
|
2018-12-28 15:30:48 +00:00
|
|
|
if !supportsIPv4() {
|
2017-04-10 11:32:00 +00:00
|
|
|
blackholeIPPort = JoinHostPort(slowDst6, "1234")
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
const cancelTick = 5 // the timer tick we cancel the dial at
|
|
|
|
const timeoutTick = 100
|
|
|
|
|
|
|
|
var d Dialer
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
d.Cancel = cancel
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
connc := make(chan Conn, 1)
|
|
|
|
go func() {
|
|
|
|
if c, err := d.Dial("tcp", blackholeIPPort); err != nil {
|
|
|
|
errc <- err
|
|
|
|
} else {
|
|
|
|
connc <- c
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
ticks := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
ticks++
|
|
|
|
if ticks == cancelTick {
|
|
|
|
close(cancel)
|
|
|
|
}
|
|
|
|
if ticks == timeoutTick {
|
|
|
|
t.Fatal("timeout waiting for dial to fail")
|
|
|
|
}
|
|
|
|
case c := <-connc:
|
|
|
|
c.Close()
|
|
|
|
t.Fatal("unexpected successful connection")
|
|
|
|
case err := <-errc:
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
if ticks < cancelTick {
|
|
|
|
t.Fatalf("dial error after %d ticks (%d before cancel sent): %v",
|
|
|
|
ticks, cancelTick-ticks, err)
|
|
|
|
}
|
|
|
|
if oe, ok := err.(*OpError); !ok || oe.Err != errCanceled {
|
|
|
|
t.Fatalf("dial error = %v (%T); want OpError with Err == errCanceled", err, err)
|
|
|
|
}
|
|
|
|
return // success.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 00:16:47 +00:00
|
|
|
|
|
|
|
func TestCancelAfterDial(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("avoiding time.Sleep")
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := newLocalListener("tcp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
defer func() {
|
|
|
|
ln.Close()
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Echo back the first line of each incoming connection.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rb := bufio.NewReader(c)
|
|
|
|
line, err := rb.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
c.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, err := c.Write([]byte(line)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
try := func() {
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
d := &Dialer{Cancel: cancel}
|
|
|
|
c, err := d.Dial("tcp", ln.Addr().String())
|
|
|
|
|
|
|
|
// Immediately after dialing, request cancelation and sleep.
|
|
|
|
// Before Issue 15078 was fixed, this would cause subsequent operations
|
|
|
|
// to fail with an i/o timeout roughly 50% of the time.
|
|
|
|
close(cancel)
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
// Send some data to confirm that the connection is still alive.
|
|
|
|
const message = "echo!\n"
|
|
|
|
if _, err := c.Write([]byte(message)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The server should echo the line, and close the connection.
|
|
|
|
rb := bufio.NewReader(c)
|
|
|
|
line, err := rb.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if line != message {
|
|
|
|
t.Errorf("got %q; want %q", line, message)
|
|
|
|
}
|
|
|
|
if _, err := rb.ReadByte(); err != io.EOF {
|
|
|
|
t.Errorf("got %v; want %v", err, io.EOF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This bug manifested about 50% of the time, so try it a few times.
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
try()
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 15:30:48 +00:00
|
|
|
|
|
|
|
// Issue 18806: it should always be possible to net.Dial a
|
|
|
|
// net.Listener().Addr().String when the listen address was ":n", even
|
|
|
|
// if the machine has halfway configured IPv6 such that it can bind on
|
|
|
|
// "::" not connect back to that same address.
|
|
|
|
func TestDialListenerAddr(t *testing.T) {
|
2019-06-02 15:48:37 +00:00
|
|
|
mustHaveExternalNetwork(t)
|
2018-12-28 15:30:48 +00:00
|
|
|
ln, err := Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
addr := ln.Addr().String()
|
|
|
|
c, err := Dial("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("for addr %q, dial error: %v", addr, err)
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
2019-06-02 15:48:37 +00:00
|
|
|
|
|
|
|
func TestDialerControl(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("StreamDial", func(t *testing.T) {
|
|
|
|
for _, network := range []string{"tcp", "tcp4", "tcp6", "unix", "unixpacket"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ln, err := newLocalListener(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
d := Dialer{Control: controlOnConnSetup}
|
|
|
|
c, err := d.Dial(network, ln.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("PacketDial", func(t *testing.T) {
|
|
|
|
for _, network := range []string{"udp", "udp4", "udp6", "unixgram"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c1, err := newLocalPacketListener(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if network == "unixgram" {
|
|
|
|
defer os.Remove(c1.LocalAddr().String())
|
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
d := Dialer{Control: controlOnConnSetup}
|
|
|
|
c2, err := d.Dial(network, c1.LocalAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c2.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustHaveExternalNetwork is like testenv.MustHaveExternalNetwork
|
|
|
|
// except that it won't skip testing on non-iOS builders.
|
|
|
|
func mustHaveExternalNetwork(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
ios := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
|
|
|
|
if testenv.Builder() == "" || ios {
|
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
}
|
|
|
|
}
|