OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [dialgoogle_test.go] - Blame information for rev 858

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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.
4
 
5
package net
6
 
7
import (
8
        "flag"
9
        "fmt"
10
        "io"
11
        "strings"
12
        "syscall"
13
        "testing"
14
)
15
 
16
// If an IPv6 tunnel is running, we can try dialing a real IPv6 address.
17
var ipv6 = flag.Bool("ipv6", false, "assume ipv6 tunnel is present")
18
 
19
// fd is already connected to the destination, port 80.
20
// Run an HTTP request to fetch the appropriate page.
21
func fetchGoogle(t *testing.T, fd Conn, network, addr string) {
22
        req := []byte("GET /robots.txt HTTP/1.0\r\nHost: www.google.com\r\n\r\n")
23
        n, err := fd.Write(req)
24
 
25
        buf := make([]byte, 1000)
26
        n, err = io.ReadFull(fd, buf)
27
 
28
        if n < 1000 {
29
                t.Errorf("fetchGoogle: short HTTP read from %s %s - %v", network, addr, err)
30
                return
31
        }
32
}
33
 
34
func doDial(t *testing.T, network, addr string) {
35
        fd, err := Dial(network, addr)
36
        if err != nil {
37
                t.Errorf("Dial(%q, %q, %q) = _, %v", network, "", addr, err)
38
                return
39
        }
40
        fetchGoogle(t, fd, network, addr)
41
        fd.Close()
42
}
43
 
44
func TestLookupCNAME(t *testing.T) {
45
        if testing.Short() {
46
                // Don't use external network.
47
                t.Logf("skipping external network test during -short")
48
                return
49
        }
50
        cname, err := LookupCNAME("www.google.com")
51
        if !strings.HasSuffix(cname, ".l.google.com.") || err != nil {
52
                t.Errorf(`LookupCNAME("www.google.com.") = %q, %v, want "*.l.google.com.", nil`, cname, err)
53
        }
54
}
55
 
56
var googleaddrsipv4 = []string{
57
        "%d.%d.%d.%d:80",
58
        "www.google.com:80",
59
        "%d.%d.%d.%d:http",
60
        "www.google.com:http",
61
        "%03d.%03d.%03d.%03d:0080",
62
        "[::ffff:%d.%d.%d.%d]:80",
63
        "[::ffff:%02x%02x:%02x%02x]:80",
64
        "[0:0:0:0:0000:ffff:%d.%d.%d.%d]:80",
65
        "[0:0:0:0:000000:ffff:%d.%d.%d.%d]:80",
66
        "[0:0:0:0:0:ffff::%d.%d.%d.%d]:80",
67
}
68
 
69
func TestDialGoogleIPv4(t *testing.T) {
70
        if testing.Short() {
71
                // Don't use external network.
72
                t.Logf("skipping external network test during -short")
73
                return
74
        }
75
 
76
        // Insert an actual IPv4 address for google.com
77
        // into the table.
78
        addrs, err := LookupIP("www.google.com")
79
        if err != nil {
80
                t.Fatalf("lookup www.google.com: %v", err)
81
        }
82
        var ip IP
83
        for _, addr := range addrs {
84
                if x := addr.To4(); x != nil {
85
                        ip = x
86
                        break
87
                }
88
        }
89
        if ip == nil {
90
                t.Fatalf("no IPv4 addresses for www.google.com")
91
        }
92
 
93
        for i, s := range googleaddrsipv4 {
94
                if strings.Contains(s, "%") {
95
                        googleaddrsipv4[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3])
96
                }
97
        }
98
 
99
        for i := 0; i < len(googleaddrsipv4); i++ {
100
                addr := googleaddrsipv4[i]
101
                if addr == "" {
102
                        continue
103
                }
104
                t.Logf("-- %s --", addr)
105
                doDial(t, "tcp", addr)
106
                if addr[0] != '[' {
107
                        doDial(t, "tcp4", addr)
108
                        if supportsIPv6 {
109
                                // make sure syscall.SocketDisableIPv6 flag works.
110
                                syscall.SocketDisableIPv6 = true
111
                                doDial(t, "tcp", addr)
112
                                doDial(t, "tcp4", addr)
113
                                syscall.SocketDisableIPv6 = false
114
                        }
115
                }
116
        }
117
}
118
 
119
var googleaddrsipv6 = []string{
120
        "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:80",
121
        "ipv6.google.com:80",
122
        "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:http",
123
        "ipv6.google.com:http",
124
}
125
 
126
func TestDialGoogleIPv6(t *testing.T) {
127
        if testing.Short() {
128
                // Don't use external network.
129
                t.Logf("skipping external network test during -short")
130
                return
131
        }
132
        // Only run tcp6 if the kernel will take it.
133
        if !*ipv6 || !supportsIPv6 {
134
                return
135
        }
136
 
137
        // Insert an actual IPv6 address for ipv6.google.com
138
        // into the table.
139
        addrs, err := LookupIP("ipv6.google.com")
140
        if err != nil {
141
                t.Fatalf("lookup ipv6.google.com: %v", err)
142
        }
143
        var ip IP
144
        for _, addr := range addrs {
145
                if x := addr.To16(); x != nil {
146
                        ip = x
147
                        break
148
                }
149
        }
150
        if ip == nil {
151
                t.Fatalf("no IPv6 addresses for ipv6.google.com")
152
        }
153
 
154
        for i, s := range googleaddrsipv6 {
155
                if strings.Contains(s, "%") {
156
                        googleaddrsipv6[i] = fmt.Sprintf(s, ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15])
157
                }
158
        }
159
 
160
        for i := 0; i < len(googleaddrsipv6); i++ {
161
                addr := googleaddrsipv6[i]
162
                if addr == "" {
163
                        continue
164
                }
165
                t.Logf("-- %s --", addr)
166
                doDial(t, "tcp", addr)
167
                doDial(t, "tcp6", addr)
168
        }
169
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.