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 (
|
|
|
|
"regexp";
|
|
|
|
"testing";
|
|
|
|
)
|
|
|
|
|
|
|
|
type DialErrorTest struct {
|
|
|
|
Net string;
|
|
|
|
Laddr string;
|
|
|
|
Raddr string;
|
|
|
|
Pattern string;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dialErrorTests = []DialErrorTest {
|
|
|
|
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-10-03 11:35:49 -06:00
|
|
|
"dial tcp no-such-name.google.com.:80: lookup no-such-name.google.com.( on .*)?: no such host",
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "no-such-name.no-such-top-level-domain.:80",
|
2009-10-03 11:35:49 -06:00
|
|
|
"dial tcp no-such-name.no-such-top-level-domain.:80: lookup no-such-name.no-such-top-level-domain.( on .*)?: no such host",
|
2009-06-25 21:24:55 -06:00
|
|
|
},
|
|
|
|
DialErrorTest{
|
|
|
|
"tcp", "", "no-such-name:80",
|
2009-10-03 11:35:49 -06:00
|
|
|
`dial tcp no-such-name:80: lookup no-such-name\..*\.( on .*)?: no such host`,
|
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/",
|
|
|
|
"dial unix /etc/: (permission denied|socket operation on non-socket)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialError(t *testing.T) {
|
|
|
|
for i, tt := range dialErrorTests {
|
2009-08-12 14:19:17 -06:00
|
|
|
c, e := Dial(tt.Net, tt.Laddr, tt.Raddr);
|
2009-06-25 21:24:55 -06:00
|
|
|
if c != nil {
|
|
|
|
c.Close();
|
|
|
|
}
|
|
|
|
if e == nil {
|
|
|
|
t.Errorf("#%d: nil error, want match for %#q", i, tt.Pattern);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
s := e.String();
|
2009-09-15 10:41:59 -06:00
|
|
|
match, _ := regexp.MatchString(tt.Pattern, s);
|
2009-06-25 21:24:55 -06:00
|
|
|
if !match {
|
|
|
|
t.Errorf("#%d: %q, want match for %#q", i, s, tt.Pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|