2009-06-25 21:24:55 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"flag"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
2009-06-25 21:24:55 -06:00
|
|
|
)
|
|
|
|
|
2009-11-10 18:39:01 -07:00
|
|
|
var runErrorTest = flag.Bool("run_error_test", false, "let TestDialError check for dns errors")
|
|
|
|
|
2009-06-25 21:24:55 -06:00
|
|
|
type DialErrorTest struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
Net string
|
|
|
|
Laddr string
|
|
|
|
Raddr string
|
|
|
|
Pattern string
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
|
2009-11-05 00:16:46 -07:00
|
|
|
var dialErrorTests = []DialErrorTest{
|
2009-06-25 21:24:55 -06:00
|
|
|
DialErrorTest{
|
|
|
|
"datakit", "", "mh/astro/r70",
|
|
|
|
"dial datakit mh/astro/r70: unknown network datakit",
|
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "127.0.0.1:☺",
|
|
|
|
"dial tcp 127.0.0.1:☺: unknown port tcp/☺",
|
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "no-such-name.google.com.:80",
|
2009-11-04 09:34:31 -07:00
|
|
|
"dial tcp no-such-name.google.com.:80: lookup no-such-name.google.com.( on .*)?: no (.*)",
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "no-such-name.no-such-top-level-domain.:80",
|
2009-11-04 09:30:53 -07:00
|
|
|
"dial tcp no-such-name.no-such-top-level-domain.:80: lookup no-such-name.no-such-top-level-domain.( on .*)?: no (.*)",
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "no-such-name:80",
|
2009-11-10 18:09:33 -07:00
|
|
|
`dial tcp no-such-name:80: lookup no-such-name\.(.*\.)?( on .*)?: no (.*)`,
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "mh/astro/r70:http",
|
|
|
|
"dial tcp mh/astro/r70:http: lookup mh/astro/r70: invalid domain name",
|
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"unix", "", "/etc/file-not-found",
|
|
|
|
"dial unix /etc/file-not-found: no such file or directory",
|
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"unix", "", "/etc/",
|
2009-11-10 19:27:26 -07:00
|
|
|
"dial unix /etc/: (permission denied|socket operation on non-socket|connection refused)",
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialError(t *testing.T) {
|
2009-11-10 18:39:01 -07:00
|
|
|
if !*runErrorTest {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Logf("test disabled; use --run_error_test to enable")
|
|
|
|
return
|
2009-11-10 18:39:01 -07:00
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
for i, tt := range dialErrorTests {
|
2009-12-15 16:35:38 -07:00
|
|
|
c, e := Dial(tt.Net, tt.Laddr, tt.Raddr)
|
2009-06-25 21:24:55 -06:00
|
|
|
if c != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
c.Close()
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
if e == nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern)
|
|
|
|
continue
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s := e.String()
|
|
|
|
match, _ := regexp.MatchString(tt.Pattern, s)
|
2009-06-25 21:24:55 -06:00
|
|
|
if !match {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern)
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|