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

Subversion Repositories openrisc

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

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
        "io"
10
        "os"
11
        "runtime"
12
        "strings"
13
        "testing"
14
        "time"
15
)
16
 
17
// Do not test empty datagrams by default.
18
// It causes unexplained timeouts on some systems,
19
// including Snow Leopard.  I think that the kernel
20
// doesn't quite expect them.
21
var testUDP = flag.Bool("udp", false, "whether to test UDP datagrams")
22
 
23
func runEcho(fd io.ReadWriter, done chan<- int) {
24
        var buf [1024]byte
25
 
26
        for {
27
                n, err := fd.Read(buf[0:])
28
                if err != nil || n == 0 || string(buf[:n]) == "END" {
29
                        break
30
                }
31
                fd.Write(buf[0:n])
32
        }
33
        done <- 1
34
}
35
 
36
func runServe(t *testing.T, network, addr string, listening chan<- string, done chan<- int) {
37
        l, err := Listen(network, addr)
38
        if err != nil {
39
                t.Fatalf("net.Listen(%q, %q) = _, %v", network, addr, err)
40
        }
41
        listening <- l.Addr().String()
42
 
43
        for {
44
                fd, err := l.Accept()
45
                if err != nil {
46
                        break
47
                }
48
                echodone := make(chan int)
49
                go runEcho(fd, echodone)
50
                <-echodone // make sure Echo stops
51
                l.Close()
52
        }
53
        done <- 1
54
}
55
 
56
func connect(t *testing.T, network, addr string, isEmpty bool) {
57
        var fd Conn
58
        var err error
59
        if network == "unixgram" {
60
                fd, err = DialUnix(network, &UnixAddr{addr + ".local", network}, &UnixAddr{addr, network})
61
        } else {
62
                fd, err = Dial(network, addr)
63
        }
64
        if err != nil {
65
                t.Fatalf("net.Dial(%q, %q) = _, %v", network, addr, err)
66
        }
67
        fd.SetReadDeadline(time.Now().Add(1 * time.Second))
68
 
69
        var b []byte
70
        if !isEmpty {
71
                b = []byte("hello, world\n")
72
        }
73
        var b1 [100]byte
74
 
75
        n, err1 := fd.Write(b)
76
        if n != len(b) {
77
                t.Fatalf("fd.Write(%q) = %d, %v", b, n, err1)
78
        }
79
 
80
        n, err1 = fd.Read(b1[0:])
81
        if n != len(b) || err1 != nil {
82
                t.Fatalf("fd.Read() = %d, %v (want %d, nil)", n, err1, len(b))
83
        }
84
 
85
        // Send explicit ending for unixpacket.
86
        // Older Linux kernels do stop reads on close.
87
        if network == "unixpacket" {
88
                fd.Write([]byte("END"))
89
        }
90
 
91
        fd.Close()
92
}
93
 
94
func doTest(t *testing.T, network, listenaddr, dialaddr string) {
95
        t.Logf("Test %q %q %q", network, listenaddr, dialaddr)
96
        switch listenaddr {
97
        case "", "0.0.0.0", "[::]", "[::ffff:0.0.0.0]":
98
                if testing.Short() || avoidMacFirewall {
99
                        t.Logf("skip wildcard listen during short test")
100
                        return
101
                }
102
        }
103
        listening := make(chan string)
104
        done := make(chan int)
105
        if network == "tcp" || network == "tcp4" || network == "tcp6" {
106
                listenaddr += ":0" // any available port
107
        }
108
        go runServe(t, network, listenaddr, listening, done)
109
        addr := <-listening // wait for server to start
110
        if network == "tcp" || network == "tcp4" || network == "tcp6" {
111
                dialaddr += addr[strings.LastIndex(addr, ":"):]
112
        }
113
        connect(t, network, dialaddr, false)
114
        <-done // make sure server stopped
115
}
116
 
117
func TestTCPServer(t *testing.T) {
118
        if runtime.GOOS != "openbsd" {
119
                doTest(t, "tcp", "", "127.0.0.1")
120
        }
121
        doTest(t, "tcp", "0.0.0.0", "127.0.0.1")
122
        doTest(t, "tcp", "127.0.0.1", "127.0.0.1")
123
        doTest(t, "tcp4", "", "127.0.0.1")
124
        doTest(t, "tcp4", "0.0.0.0", "127.0.0.1")
125
        doTest(t, "tcp4", "127.0.0.1", "127.0.0.1")
126
        if supportsIPv6 {
127
                doTest(t, "tcp", "", "[::1]")
128
                doTest(t, "tcp", "[::]", "[::1]")
129
                doTest(t, "tcp", "[::1]", "[::1]")
130
                doTest(t, "tcp6", "", "[::1]")
131
                doTest(t, "tcp6", "[::]", "[::1]")
132
                doTest(t, "tcp6", "[::1]", "[::1]")
133
        }
134
        if supportsIPv6 && supportsIPv4map {
135
                doTest(t, "tcp", "[::ffff:0.0.0.0]", "127.0.0.1")
136
                doTest(t, "tcp", "[::]", "127.0.0.1")
137
                doTest(t, "tcp4", "[::ffff:0.0.0.0]", "127.0.0.1")
138
                doTest(t, "tcp6", "", "127.0.0.1")
139
                doTest(t, "tcp6", "[::ffff:0.0.0.0]", "127.0.0.1")
140
                doTest(t, "tcp6", "[::]", "127.0.0.1")
141
                doTest(t, "tcp", "127.0.0.1", "[::ffff:127.0.0.1]")
142
                doTest(t, "tcp", "[::ffff:127.0.0.1]", "127.0.0.1")
143
                doTest(t, "tcp4", "127.0.0.1", "[::ffff:127.0.0.1]")
144
                doTest(t, "tcp4", "[::ffff:127.0.0.1]", "127.0.0.1")
145
                doTest(t, "tcp6", "127.0.0.1", "[::ffff:127.0.0.1]")
146
                doTest(t, "tcp6", "[::ffff:127.0.0.1]", "127.0.0.1")
147
        }
148
}
149
 
150
func TestUnixServer(t *testing.T) {
151
        // "unix" sockets are not supported on windows and Plan 9.
152
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
153
                return
154
        }
155
        os.Remove("/tmp/gotest.net")
156
        doTest(t, "unix", "/tmp/gotest.net", "/tmp/gotest.net")
157
        os.Remove("/tmp/gotest.net")
158
        if runtime.GOOS == "linux" {
159
                doTest(t, "unixpacket", "/tmp/gotest.net", "/tmp/gotest.net")
160
                os.Remove("/tmp/gotest.net")
161
                // Test abstract unix domain socket, a Linux-ism
162
                doTest(t, "unix", "@gotest/net", "@gotest/net")
163
                doTest(t, "unixpacket", "@gotest/net", "@gotest/net")
164
        }
165
}
166
 
167
func runPacket(t *testing.T, network, addr string, listening chan<- string, done chan<- int) {
168
        c, err := ListenPacket(network, addr)
169
        if err != nil {
170
                t.Fatalf("net.ListenPacket(%q, %q) = _, %v", network, addr, err)
171
        }
172
        listening <- c.LocalAddr().String()
173
        var buf [1000]byte
174
Run:
175
        for {
176
                c.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
177
                n, addr, err := c.ReadFrom(buf[0:])
178
                if e, ok := err.(Error); ok && e.Timeout() {
179
                        select {
180
                        case done <- 1:
181
                                break Run
182
                        default:
183
                                continue Run
184
                        }
185
                }
186
                if err != nil {
187
                        break
188
                }
189
                if _, err = c.WriteTo(buf[0:n], addr); err != nil {
190
                        t.Fatalf("WriteTo %v: %v", addr, err)
191
                }
192
        }
193
        c.Close()
194
        done <- 1
195
}
196
 
197
func doTestPacket(t *testing.T, network, listenaddr, dialaddr string, isEmpty bool) {
198
        t.Logf("TestPacket %q %q %q", network, listenaddr, dialaddr)
199
        listening := make(chan string)
200
        done := make(chan int)
201
        if network == "udp" {
202
                listenaddr += ":0" // any available port
203
        }
204
        go runPacket(t, network, listenaddr, listening, done)
205
        addr := <-listening // wait for server to start
206
        if network == "udp" {
207
                dialaddr += addr[strings.LastIndex(addr, ":"):]
208
        }
209
        connect(t, network, dialaddr, isEmpty)
210
        <-done // tell server to stop
211
        <-done // wait for stop
212
}
213
 
214
func TestUDPServer(t *testing.T) {
215
        if !*testUDP {
216
                return
217
        }
218
        for _, isEmpty := range []bool{false, true} {
219
                doTestPacket(t, "udp", "0.0.0.0", "127.0.0.1", isEmpty)
220
                doTestPacket(t, "udp", "", "127.0.0.1", isEmpty)
221
                if supportsIPv6 && supportsIPv4map {
222
                        doTestPacket(t, "udp", "[::]", "[::ffff:127.0.0.1]", isEmpty)
223
                        doTestPacket(t, "udp", "[::]", "127.0.0.1", isEmpty)
224
                        doTestPacket(t, "udp", "0.0.0.0", "[::ffff:127.0.0.1]", isEmpty)
225
                }
226
        }
227
}
228
 
229
func TestUnixDatagramServer(t *testing.T) {
230
        // "unix" sockets are not supported on windows and Plan 9.
231
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
232
                return
233
        }
234
        for _, isEmpty := range []bool{false} {
235
                os.Remove("/tmp/gotest1.net")
236
                os.Remove("/tmp/gotest1.net.local")
237
                doTestPacket(t, "unixgram", "/tmp/gotest1.net", "/tmp/gotest1.net", isEmpty)
238
                os.Remove("/tmp/gotest1.net")
239
                os.Remove("/tmp/gotest1.net.local")
240
                if runtime.GOOS == "linux" {
241
                        // Test abstract unix domain socket, a Linux-ism
242
                        doTestPacket(t, "unixgram", "@gotest1/net", "@gotest1/net", isEmpty)
243
                }
244
        }
245
}

powered by: WebSVN 2.1.0

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