}
}
}
+
+func TestLookupBadName(t *testing.T) {
+ // Check that we get the same error for invalid names regardless of lookup algorithm.
+
+ mode := ""
+ check := func(fn string, err error) {
+ if e, ok := err.(*DNSError); !ok || e.Err != "invalid domain name" {
+ t.Errorf("%s: %s(\"!!!.local\") = %T(%v), want DNSError(invalid domain name)", mode, fn, err, err)
+ }
+ }
+
+ for i, fn := range []func() func(){forceGoDNS, forceCgoDNS} {
+ fixup := fn()
+ if fixup == nil {
+ continue
+ }
+ mode = "netgo"
+ if i == 1 {
+ mode = "netcgo"
+ }
+
+ _, err := LookupHost("!!!.local")
+ check("LookupHost", err)
+
+ _, err = LookupIP("!!!.local")
+ check("LookupIP", err)
+
+ _, err = LookupCNAME("!!!.local")
+ check("LookupCNAME", err)
+
+ _, _, err = LookupSRV("x", "tcp", "!!!.local")
+ check("LookupSRV", err)
+
+ _, err = LookupMX("!!!.local")
+ check("LookupMX", err)
+
+ _, err = LookupNS("!!!.local")
+ check("LookupNS", err)
+
+ _, err = LookupTXT("!!!.local")
+ check("LookupTXT", err)
+
+ fixup()
+ }
+}
if ip := ParseIP(host); ip != nil {
return []string{host}, nil
}
+ if !isDomainName(host) {
+ return nil, &DNSError{Err: "invalid domain name", Name: host}
+ }
return lookupHost(host)
}
if ip := ParseIP(host); ip != nil {
return []IP{ip}, nil
}
+ if !isDomainName(host) {
+ return nil, &DNSError{Err: "invalid domain name", Name: host}
+ }
addrs, err := lookupIPMerge(host)
if err != nil {
return
// LookupHost or LookupIP directly; both take care of resolving
// the canonical name as part of the lookup.
func LookupCNAME(name string) (cname string, err error) {
+ if !isDomainName(name) {
+ return "", &DNSError{Err: "invalid domain name", Name: name}
+ }
return lookupCNAME(name)
}
// publishing SRV records under non-standard names, if both service
// and proto are empty strings, LookupSRV looks up name directly.
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
+ if !isDomainName(name) {
+ return "", nil, &DNSError{Err: "invalid domain name", Name: name}
+ }
return lookupSRV(service, proto, name)
}
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
func LookupMX(name string) (mxs []*MX, err error) {
+ if !isDomainName(name) {
+ return nil, &DNSError{Err: "invalid domain name", Name: name}
+ }
return lookupMX(name)
}
// LookupNS returns the DNS NS records for the given domain name.
func LookupNS(name string) (nss []*NS, err error) {
+ if !isDomainName(name) {
+ return nil, &DNSError{Err: "invalid domain name", Name: name}
+ }
return lookupNS(name)
}
// LookupTXT returns the DNS TXT records for the given domain name.
func LookupTXT(name string) (txts []string, err error) {
+ if !isDomainName(name) {
+ return nil, &DNSError{Err: "invalid domain name", Name: name}
+ }
return lookupTXT(name)
}