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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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
        "os"
9
        "reflect"
10
        "runtime"
11
        "testing"
12
)
13
 
14
type listenerFile interface {
15
        Listener
16
        File() (f *os.File, err error)
17
}
18
 
19
type packetConnFile interface {
20
        PacketConn
21
        File() (f *os.File, err error)
22
}
23
 
24
type connFile interface {
25
        Conn
26
        File() (f *os.File, err error)
27
}
28
 
29
func testFileListener(t *testing.T, net, laddr string) {
30
        if net == "tcp" {
31
                laddr += ":0" // any available port
32
        }
33
        l, err := Listen(net, laddr)
34
        if err != nil {
35
                t.Fatalf("Listen failed: %v", err)
36
        }
37
        defer l.Close()
38
        lf := l.(listenerFile)
39
        f, err := lf.File()
40
        if err != nil {
41
                t.Fatalf("File failed: %v", err)
42
        }
43
        c, err := FileListener(f)
44
        if err != nil {
45
                t.Fatalf("FileListener failed: %v", err)
46
        }
47
        if !reflect.DeepEqual(l.Addr(), c.Addr()) {
48
                t.Fatalf("Addrs not equal: %#v != %#v", l.Addr(), c.Addr())
49
        }
50
        if err := c.Close(); err != nil {
51
                t.Fatalf("Close failed: %v", err)
52
        }
53
        if err := f.Close(); err != nil {
54
                t.Fatalf("Close failed: %v", err)
55
        }
56
}
57
 
58
func TestFileListener(t *testing.T) {
59
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
60
                return
61
        }
62
        testFileListener(t, "tcp", "127.0.0.1")
63
        testFileListener(t, "tcp", "127.0.0.1")
64
        if supportsIPv6 && supportsIPv4map {
65
                testFileListener(t, "tcp", "[::ffff:127.0.0.1]")
66
                testFileListener(t, "tcp", "127.0.0.1")
67
                testFileListener(t, "tcp", "[::ffff:127.0.0.1]")
68
        }
69
        if runtime.GOOS == "linux" {
70
                testFileListener(t, "unix", "@gotest/net")
71
                testFileListener(t, "unixpacket", "@gotest/net")
72
        }
73
}
74
 
75
func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
76
        f, err := pcf.File()
77
        if err != nil {
78
                t.Fatalf("File failed: %v", err)
79
        }
80
        c, err := FilePacketConn(f)
81
        if err != nil {
82
                t.Fatalf("FilePacketConn failed: %v", err)
83
        }
84
        if !reflect.DeepEqual(pcf.LocalAddr(), c.LocalAddr()) {
85
                t.Fatalf("LocalAddrs not equal: %#v != %#v", pcf.LocalAddr(), c.LocalAddr())
86
        }
87
        if listen {
88
                if _, err := c.WriteTo([]byte{}, c.LocalAddr()); err != nil {
89
                        t.Fatalf("WriteTo failed: %v", err)
90
                }
91
        }
92
        if err := c.Close(); err != nil {
93
                t.Fatalf("Close failed: %v", err)
94
        }
95
        if err := f.Close(); err != nil {
96
                t.Fatalf("Close failed: %v", err)
97
        }
98
}
99
 
100
func testFilePacketConnListen(t *testing.T, net, laddr string) {
101
        l, err := ListenPacket(net, laddr)
102
        if err != nil {
103
                t.Fatalf("Listen failed: %v", err)
104
        }
105
        testFilePacketConn(t, l.(packetConnFile), true)
106
        if err := l.Close(); err != nil {
107
                t.Fatalf("Close failed: %v", err)
108
        }
109
}
110
 
111
func testFilePacketConnDial(t *testing.T, net, raddr string) {
112
        c, err := Dial(net, raddr)
113
        if err != nil {
114
                t.Fatalf("Dial failed: %v", err)
115
        }
116
        testFilePacketConn(t, c.(packetConnFile), false)
117
        if err := c.Close(); err != nil {
118
                t.Fatalf("Close failed: %v", err)
119
        }
120
}
121
 
122
func TestFilePacketConn(t *testing.T) {
123
        if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
124
                return
125
        }
126
        testFilePacketConnListen(t, "udp", "127.0.0.1:0")
127
        testFilePacketConnDial(t, "udp", "127.0.0.1:12345")
128
        if supportsIPv6 {
129
                testFilePacketConnListen(t, "udp", "[::1]:0")
130
        }
131
        if supportsIPv6 && supportsIPv4map {
132
                testFilePacketConnDial(t, "udp", "[::ffff:127.0.0.1]:12345")
133
        }
134
        if runtime.GOOS == "linux" {
135
                testFilePacketConnListen(t, "unixgram", "@gotest1/net")
136
        }
137
}

powered by: WebSVN 2.1.0

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