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

Subversion Repositories openrisc

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

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
        "regexp"
11
        "runtime"
12
        "testing"
13
)
14
 
15
var runErrorTest = flag.Bool("run_error_test", false, "let TestDialError check for dns errors")
16
 
17
type DialErrorTest struct {
18
        Net     string
19
        Raddr   string
20
        Pattern string
21
}
22
 
23
var dialErrorTests = []DialErrorTest{
24
        {
25
                "datakit", "mh/astro/r70",
26
                "dial datakit mh/astro/r70: unknown network datakit",
27
        },
28
        {
29
                "tcp", "127.0.0.1:☺",
30
                "dial tcp 127.0.0.1:☺: unknown port tcp/☺",
31
        },
32
        {
33
                "tcp", "no-such-name.google.com.:80",
34
                "dial tcp no-such-name.google.com.:80: lookup no-such-name.google.com.( on .*)?: no (.*)",
35
        },
36
        {
37
                "tcp", "no-such-name.no-such-top-level-domain.:80",
38
                "dial tcp no-such-name.no-such-top-level-domain.:80: lookup no-such-name.no-such-top-level-domain.( on .*)?: no (.*)",
39
        },
40
        {
41
                "tcp", "no-such-name:80",
42
                `dial tcp no-such-name:80: lookup no-such-name\.(.*\.)?( on .*)?: no (.*)`,
43
        },
44
        {
45
                "tcp", "mh/astro/r70:http",
46
                "dial tcp mh/astro/r70:http: lookup mh/astro/r70: invalid domain name",
47
        },
48
        {
49
                "unix", "/etc/file-not-found",
50
                "dial unix /etc/file-not-found: [nN]o such file or directory",
51
        },
52
        {
53
                "unix", "/etc/",
54
                "dial unix /etc/: ([pP]ermission denied|socket operation on non-socket|connection refused)",
55
        },
56
        {
57
                "unixpacket", "/etc/file-not-found",
58
                "dial unixpacket /etc/file-not-found: no such file or directory",
59
        },
60
        {
61
                "unixpacket", "/etc/",
62
                "dial unixpacket /etc/: (permission denied|socket operation on non-socket|connection refused)",
63
        },
64
}
65
 
66
var duplicateErrorPattern = `dial (.*) dial (.*)`
67
 
68
func TestDialError(t *testing.T) {
69
        if !*runErrorTest {
70
                t.Logf("test disabled; use --run_error_test to enable")
71
                return
72
        }
73
        for i, tt := range dialErrorTests {
74
                c, err := Dial(tt.Net, tt.Raddr)
75
                if c != nil {
76
                        c.Close()
77
                }
78
                if err == nil {
79
                        t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern)
80
                        continue
81
                }
82
                s := err.Error()
83
                match, _ := regexp.MatchString(tt.Pattern, s)
84
                if !match {
85
                        t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern)
86
                }
87
                match, _ = regexp.MatchString(duplicateErrorPattern, s)
88
                if match {
89
                        t.Errorf("#%d: %q, duplicate error return from Dial", i, s)
90
                }
91
        }
92
}
93
 
94
var revAddrTests = []struct {
95
        Addr      string
96
        Reverse   string
97
        ErrPrefix string
98
}{
99
        {"1.2.3.4", "4.3.2.1.in-addr.arpa.", ""},
100
        {"245.110.36.114", "114.36.110.245.in-addr.arpa.", ""},
101
        {"::ffff:12.34.56.78", "78.56.34.12.in-addr.arpa.", ""},
102
        {"::1", "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.", ""},
103
        {"1::", "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.ip6.arpa.", ""},
104
        {"1234:567::89a:bcde", "e.d.c.b.a.9.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
105
        {"1234:567:fefe:bcbc:adad:9e4a:89a:bcde", "e.d.c.b.a.9.8.0.a.4.e.9.d.a.d.a.c.b.c.b.e.f.e.f.7.6.5.0.4.3.2.1.ip6.arpa.", ""},
106
        {"1.2.3", "", "unrecognized address"},
107
        {"1.2.3.4.5", "", "unrecognized address"},
108
        {"1234:567:bcbca::89a:bcde", "", "unrecognized address"},
109
        {"1234:567::bcbc:adad::89a:bcde", "", "unrecognized address"},
110
}
111
 
112
func TestReverseAddress(t *testing.T) {
113
        for i, tt := range revAddrTests {
114
                a, err := reverseaddr(tt.Addr)
115
                if len(tt.ErrPrefix) > 0 && err == nil {
116
                        t.Errorf("#%d: expected %q, got  (error)", i, tt.ErrPrefix)
117
                        continue
118
                }
119
                if len(tt.ErrPrefix) == 0 && err != nil {
120
                        t.Errorf("#%d: expected , got %q (error)", i, err)
121
                }
122
                if err != nil && err.(*DNSError).Err != tt.ErrPrefix {
123
                        t.Errorf("#%d: expected %q, got %q (mismatched error)", i, tt.ErrPrefix, err.(*DNSError).Err)
124
                }
125
                if a != tt.Reverse {
126
                        t.Errorf("#%d: expected %q, got %q (reverse address)", i, tt.Reverse, a)
127
                }
128
        }
129
}
130
 
131
func TestShutdown(t *testing.T) {
132
        if runtime.GOOS == "plan9" {
133
                return
134
        }
135
        l, err := Listen("tcp", "127.0.0.1:0")
136
        if err != nil {
137
                if l, err = Listen("tcp6", "[::1]:0"); err != nil {
138
                        t.Fatalf("ListenTCP on :0: %v", err)
139
                }
140
        }
141
 
142
        go func() {
143
                c, err := l.Accept()
144
                if err != nil {
145
                        t.Fatalf("Accept: %v", err)
146
                }
147
                var buf [10]byte
148
                n, err := c.Read(buf[:])
149
                if n != 0 || err != io.EOF {
150
                        t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err)
151
                }
152
                c.Write([]byte("response"))
153
                c.Close()
154
        }()
155
 
156
        c, err := Dial("tcp", l.Addr().String())
157
        if err != nil {
158
                t.Fatalf("Dial: %v", err)
159
        }
160
        defer c.Close()
161
 
162
        err = c.(*TCPConn).CloseWrite()
163
        if err != nil {
164
                t.Fatalf("CloseWrite: %v", err)
165
        }
166
        var buf [10]byte
167
        n, err := c.Read(buf[:])
168
        if err != nil {
169
                t.Fatalf("client Read: %d, %v", n, err)
170
        }
171
        got := string(buf[:n])
172
        if got != "response" {
173
                t.Errorf("read = %q, want \"response\"", got)
174
        }
175
}

powered by: WebSVN 2.1.0

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