1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 "net/internal/socktest"
22 // uninstallTestHooks runs just before a run of benchmarks.
23 testHookUninstaller sync
.Once
27 testTCPBig
= flag
.Bool("tcpbig", false, "whether to test massive size of data per read or write call on TCP connection")
29 testDNSFlood
= flag
.Bool("dnsflood", false, "whether to test DNS query flooding")
31 // If external IPv4 connectivity exists, we can try dialing
32 // non-node/interface local scope IPv4 addresses.
33 // On Windows, Lookup APIs may not return IPv4-related
34 // resource records when a node has no external IPv4
36 testIPv4
= flag
.Bool("ipv4", true, "assume external IPv4 connectivity exists")
38 // If external IPv6 connectivity exists, we can try dialing
39 // non-node/interface local scope IPv6 addresses.
40 // On Windows, Lookup APIs may not return IPv6-related
41 // resource records when a node has no external IPv6
43 testIPv6
= flag
.Bool("ipv6", false, "assume external IPv6 connectivity exists")
46 func TestMain(m
*testing
.M
) {
52 testHookUninstaller
.Do(uninstallTestHooks
)
53 if testing
.Verbose() {
54 printRunningGoroutines()
55 printInflightSockets()
62 type ipv6LinkLocalUnicastTest
struct {
63 network
, address
string
68 ipv6LinkLocalUnicastTCPTests
[]ipv6LinkLocalUnicastTest
69 ipv6LinkLocalUnicastUDPTests
[]ipv6LinkLocalUnicastTest
72 func setupTestData() {
74 resolveTCPAddrTests
= append(resolveTCPAddrTests
, []resolveTCPAddrTest
{
75 {"tcp", "localhost:1", &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 1}, nil},
76 {"tcp4", "localhost:2", &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 2}, nil},
78 resolveUDPAddrTests
= append(resolveUDPAddrTests
, []resolveUDPAddrTest
{
79 {"udp", "localhost:1", &UDPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 1}, nil},
80 {"udp4", "localhost:2", &UDPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 2}, nil},
82 resolveIPAddrTests
= append(resolveIPAddrTests
, []resolveIPAddrTest
{
83 {"ip", "localhost", &IPAddr
{IP
: IPv4(127, 0, 0, 1)}, nil},
84 {"ip4", "localhost", &IPAddr
{IP
: IPv4(127, 0, 0, 1)}, nil},
89 resolveTCPAddrTests
= append(resolveTCPAddrTests
, resolveTCPAddrTest
{"tcp6", "localhost:3", &TCPAddr
{IP
: IPv6loopback
, Port
: 3}, nil})
90 resolveUDPAddrTests
= append(resolveUDPAddrTests
, resolveUDPAddrTest
{"udp6", "localhost:3", &UDPAddr
{IP
: IPv6loopback
, Port
: 3}, nil})
91 resolveIPAddrTests
= append(resolveIPAddrTests
, resolveIPAddrTest
{"ip6", "localhost", &IPAddr
{IP
: IPv6loopback
}, nil})
93 // Issue 20911: don't return IPv4 addresses for
94 // Resolve*Addr calls of the IPv6 unspecified address.
95 resolveTCPAddrTests
= append(resolveTCPAddrTests
, resolveTCPAddrTest
{"tcp", "[::]:4", &TCPAddr
{IP
: IPv6unspecified
, Port
: 4}, nil})
96 resolveUDPAddrTests
= append(resolveUDPAddrTests
, resolveUDPAddrTest
{"udp", "[::]:4", &UDPAddr
{IP
: IPv6unspecified
, Port
: 4}, nil})
97 resolveIPAddrTests
= append(resolveIPAddrTests
, resolveIPAddrTest
{"ip", "::", &IPAddr
{IP
: IPv6unspecified
}, nil})
100 ifi
:= loopbackInterface()
102 index
:= fmt
.Sprintf("%v", ifi
.Index
)
103 resolveTCPAddrTests
= append(resolveTCPAddrTests
, []resolveTCPAddrTest
{
104 {"tcp6", "[fe80::1%" + ifi
.Name
+ "]:1", &TCPAddr
{IP
: ParseIP("fe80::1"), Port
: 1, Zone
: zoneCache
.name(ifi
.Index
)}, nil},
105 {"tcp6", "[fe80::1%" + index
+ "]:2", &TCPAddr
{IP
: ParseIP("fe80::1"), Port
: 2, Zone
: index
}, nil},
107 resolveUDPAddrTests
= append(resolveUDPAddrTests
, []resolveUDPAddrTest
{
108 {"udp6", "[fe80::1%" + ifi
.Name
+ "]:1", &UDPAddr
{IP
: ParseIP("fe80::1"), Port
: 1, Zone
: zoneCache
.name(ifi
.Index
)}, nil},
109 {"udp6", "[fe80::1%" + index
+ "]:2", &UDPAddr
{IP
: ParseIP("fe80::1"), Port
: 2, Zone
: index
}, nil},
111 resolveIPAddrTests
= append(resolveIPAddrTests
, []resolveIPAddrTest
{
112 {"ip6", "fe80::1%" + ifi
.Name
, &IPAddr
{IP
: ParseIP("fe80::1"), Zone
: zoneCache
.name(ifi
.Index
)}, nil},
113 {"ip6", "fe80::1%" + index
, &IPAddr
{IP
: ParseIP("fe80::1"), Zone
: index
}, nil},
117 addr
:= ipv6LinkLocalUnicastAddr(ifi
)
119 if runtime
.GOOS
!= "dragonfly" {
120 ipv6LinkLocalUnicastTCPTests
= append(ipv6LinkLocalUnicastTCPTests
, []ipv6LinkLocalUnicastTest
{
121 {"tcp", "[" + addr
+ "%" + ifi
.Name
+ "]:0", false},
123 ipv6LinkLocalUnicastUDPTests
= append(ipv6LinkLocalUnicastUDPTests
, []ipv6LinkLocalUnicastTest
{
124 {"udp", "[" + addr
+ "%" + ifi
.Name
+ "]:0", false},
127 ipv6LinkLocalUnicastTCPTests
= append(ipv6LinkLocalUnicastTCPTests
, []ipv6LinkLocalUnicastTest
{
128 {"tcp6", "[" + addr
+ "%" + ifi
.Name
+ "]:0", false},
130 ipv6LinkLocalUnicastUDPTests
= append(ipv6LinkLocalUnicastUDPTests
, []ipv6LinkLocalUnicastTest
{
131 {"udp6", "[" + addr
+ "%" + ifi
.Name
+ "]:0", false},
133 switch runtime
.GOOS
{
134 case "darwin", "dragonfly", "freebsd", "openbsd", "netbsd":
135 ipv6LinkLocalUnicastTCPTests
= append(ipv6LinkLocalUnicastTCPTests
, []ipv6LinkLocalUnicastTest
{
136 {"tcp", "[localhost%" + ifi
.Name
+ "]:0", true},
137 {"tcp6", "[localhost%" + ifi
.Name
+ "]:0", true},
139 ipv6LinkLocalUnicastUDPTests
= append(ipv6LinkLocalUnicastUDPTests
, []ipv6LinkLocalUnicastTest
{
140 {"udp", "[localhost%" + ifi
.Name
+ "]:0", true},
141 {"udp6", "[localhost%" + ifi
.Name
+ "]:0", true},
144 ipv6LinkLocalUnicastTCPTests
= append(ipv6LinkLocalUnicastTCPTests
, []ipv6LinkLocalUnicastTest
{
145 {"tcp", "[ip6-localhost%" + ifi
.Name
+ "]:0", true},
146 {"tcp6", "[ip6-localhost%" + ifi
.Name
+ "]:0", true},
148 ipv6LinkLocalUnicastUDPTests
= append(ipv6LinkLocalUnicastUDPTests
, []ipv6LinkLocalUnicastTest
{
149 {"udp", "[ip6-localhost%" + ifi
.Name
+ "]:0", true},
150 {"udp6", "[ip6-localhost%" + ifi
.Name
+ "]:0", true},
156 func printRunningGoroutines() {
157 gss
:= runningGoroutines()
161 fmt
.Fprintf(os
.Stderr
, "Running goroutines:\n")
162 for _
, gs
:= range gss
{
163 fmt
.Fprintf(os
.Stderr
, "%v\n", gs
)
165 fmt
.Fprintf(os
.Stderr
, "\n")
168 // runningGoroutines returns a list of remaining goroutines.
169 func runningGoroutines() []string {
171 b
:= make([]byte, 2<<20)
172 b
= b
[:runtime
.Stack(b
, true)]
173 for _
, s
:= range strings
.Split(string(b
), "\n\n") {
174 ss
:= strings
.SplitN(s
, "\n", 2)
178 stack
:= strings
.TrimSpace(ss
[1])
179 if !strings
.Contains(stack
, "created by net") {
182 gss
= append(gss
, stack
)
188 func printInflightSockets() {
193 fmt
.Fprintf(os
.Stderr
, "Inflight sockets:\n")
194 for s
, so
:= range sos
{
195 fmt
.Fprintf(os
.Stderr
, "%v: %v\n", s
, so
)
197 fmt
.Fprintf(os
.Stderr
, "\n")
200 func printSocketStats() {
205 fmt
.Fprintf(os
.Stderr
, "Socket statistical information:\n")
206 for _
, st
:= range sts
{
207 fmt
.Fprintf(os
.Stderr
, "%v\n", st
)
209 fmt
.Fprintf(os
.Stderr
, "\n")