Source file
src/net/external_test.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "fmt"
11 "internal/testenv"
12 "io"
13 "strings"
14 "testing"
15 )
16
17 func TestResolveGoogle(t *testing.T) {
18 testenv.MustHaveExternalNetwork(t)
19
20 if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 {
21 t.Skip("both IPv4 and IPv6 are required")
22 }
23
24 for _, network := range []string{"tcp", "tcp4", "tcp6"} {
25 addr, err := ResolveTCPAddr(network, "www.google.com:http")
26 if err != nil {
27 t.Error(err)
28 continue
29 }
30 switch {
31 case network == "tcp" && addr.IP.To4() == nil:
32 fallthrough
33 case network == "tcp4" && addr.IP.To4() == nil:
34 t.Errorf("got %v; want an IPv4 address on %s", addr, network)
35 case network == "tcp6" && (addr.IP.To16() == nil || addr.IP.To4() != nil):
36 t.Errorf("got %v; want an IPv6 address on %s", addr, network)
37 }
38 }
39 }
40
41 var dialGoogleTests = []struct {
42 dial func(string, string) (Conn, error)
43 unreachableNetwork string
44 networks []string
45 addrs []string
46 }{
47 {
48 dial: (&Dialer{DualStack: true}).Dial,
49 networks: []string{"tcp", "tcp4", "tcp6"},
50 addrs: []string{"www.google.com:http"},
51 },
52 {
53 dial: Dial,
54 unreachableNetwork: "tcp6",
55 networks: []string{"tcp", "tcp4"},
56 },
57 {
58 dial: Dial,
59 unreachableNetwork: "tcp4",
60 networks: []string{"tcp", "tcp6"},
61 },
62 }
63
64 func TestDialGoogle(t *testing.T) {
65 testenv.MustHaveExternalNetwork(t)
66
67 if !supportsIPv4() || !supportsIPv6() || !*testIPv4 || !*testIPv6 {
68 t.Skip("both IPv4 and IPv6 are required")
69 }
70
71 var err error
72 dialGoogleTests[1].addrs, dialGoogleTests[2].addrs, err = googleLiteralAddrs()
73 if err != nil {
74 t.Error(err)
75 }
76 for _, tt := range dialGoogleTests {
77 for _, network := range tt.networks {
78 disableSocketConnect(tt.unreachableNetwork)
79 for _, addr := range tt.addrs {
80 if err := fetchGoogle(tt.dial, network, addr); err != nil {
81 t.Error(err)
82 }
83 }
84 enableSocketConnect()
85 }
86 }
87 }
88
89 var (
90 literalAddrs4 = [...]string{
91 "%d.%d.%d.%d:80",
92 "www.google.com:80",
93 "%d.%d.%d.%d:http",
94 "www.google.com:http",
95 "%03d.%03d.%03d.%03d:0080",
96 "[::ffff:%d.%d.%d.%d]:80",
97 "[::ffff:%02x%02x:%02x%02x]:80",
98 "[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
99 "[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
100 "[0:0:0:0::ffff:%d.%d.%d.%d]:80",
101 }
102 literalAddrs6 = [...]string{
103 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
104 "ipv6.google.com:80",
105 "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
106 "ipv6.google.com:http",
107 }
108 )
109
110 func googleLiteralAddrs() (lits4, lits6 []string, err error) {
111 ips, err := LookupIP("www.google.com")
112 if err != nil {
113 return nil, nil, err
114 }
115 if len(ips) == 0 {
116 return nil, nil, nil
117 }
118 var ip4, ip6 IP
119 for _, ip := range ips {
120 if ip4 == nil && ip.To4() != nil {
121 ip4 = ip.To4()
122 }
123 if ip6 == nil && ip.To16() != nil && ip.To4() == nil {
124 ip6 = ip.To16()
125 }
126 if ip4 != nil && ip6 != nil {
127 break
128 }
129 }
130 if ip4 != nil {
131 for i, lit4 := range literalAddrs4 {
132 if strings.Contains(lit4, "%") {
133 literalAddrs4[i] = fmt.Sprintf(lit4, ip4[0], ip4[1], ip4[2], ip4[3])
134 }
135 }
136 lits4 = literalAddrs4[:]
137 }
138 if ip6 != nil {
139 for i, lit6 := range literalAddrs6 {
140 if strings.Contains(lit6, "%") {
141 literalAddrs6[i] = fmt.Sprintf(lit6, ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7], ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15])
142 }
143 }
144 lits6 = literalAddrs6[:]
145 }
146 return
147 }
148
149 func fetchGoogle(dial func(string, string) (Conn, error), network, address string) error {
150 c, err := dial(network, address)
151 if err != nil {
152 return err
153 }
154 defer c.Close()
155 req := []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
156 if _, err := c.Write(req); err != nil {
157 return err
158 }
159 b := make([]byte, 1000)
160 n, err := io.ReadFull(c, b)
161 if err != nil {
162 return err
163 }
164 if n < 1000 {
165 return fmt.Errorf("short read from %s:%s->%s", network, c.RemoteAddr(), c.LocalAddr())
166 }
167 return nil
168 }
169
View as plain text