Commit ceeb52d8 authored by Mikio Hara's avatar Mikio Hara Committed by Russ Cox

net: LookupAddr("127.0.0.1") is "localhost" not "localhost." on Plan 9 and Windows

This change applies the fix for #13564 to Plan 9 and Windows.
Also enables Lookup API test cases on builders.

Updates #13564.

Change-Id: I863f03c7cb6fbe58b3a55223bfa0ac5f9bf9c3df
Reviewed-on: https://go-review.googlesource.com/18559
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 771da539
...@@ -190,17 +190,6 @@ func lookupPort(network, service string) (port int, err error) { ...@@ -190,17 +190,6 @@ func lookupPort(network, service string) (port int, err error) {
return 0, unknownPortError return 0, unknownPortError
} }
// ensureEndDot adds '.' at the end of name unless it is already there.
func ensureEndDot(name string) string {
if name == "" {
return "."
}
if name[len(name)-1] == '.' {
return name
}
return name + "."
}
func lookupCNAME(name string) (cname string, err error) { func lookupCNAME(name string) (cname string, err error) {
lines, err := queryDNS(name, "cname") lines, err := queryDNS(name, "cname")
if err != nil { if err != nil {
...@@ -236,8 +225,8 @@ func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err ...@@ -236,8 +225,8 @@ func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
if !(portOk && priorityOk && weightOk) { if !(portOk && priorityOk && weightOk) {
continue continue
} }
addrs = append(addrs, &SRV{ensureEndDot(f[5]), uint16(port), uint16(priority), uint16(weight)}) addrs = append(addrs, &SRV{absDomainName([]byte(f[5])), uint16(port), uint16(priority), uint16(weight)})
cname = ensureEndDot(f[0]) cname = absDomainName([]byte(f[0]))
} }
byPriorityWeight(addrs).sort() byPriorityWeight(addrs).sort()
return return
...@@ -254,7 +243,7 @@ func lookupMX(name string) (mx []*MX, err error) { ...@@ -254,7 +243,7 @@ func lookupMX(name string) (mx []*MX, err error) {
continue continue
} }
if pref, _, ok := dtoi(f[2], 0); ok { if pref, _, ok := dtoi(f[2], 0); ok {
mx = append(mx, &MX{ensureEndDot(f[3]), uint16(pref)}) mx = append(mx, &MX{absDomainName([]byte(f[3])), uint16(pref)})
} }
} }
byPref(mx).sort() byPref(mx).sort()
...@@ -271,7 +260,7 @@ func lookupNS(name string) (ns []*NS, err error) { ...@@ -271,7 +260,7 @@ func lookupNS(name string) (ns []*NS, err error) {
if len(f) < 3 { if len(f) < 3 {
continue continue
} }
ns = append(ns, &NS{ensureEndDot(f[2])}) ns = append(ns, &NS{absDomainName([]byte(f[2]))})
} }
return return
} }
...@@ -283,7 +272,7 @@ func lookupTXT(name string) (txt []string, err error) { ...@@ -283,7 +272,7 @@ func lookupTXT(name string) (txt []string, err error) {
} }
for _, line := range lines { for _, line := range lines {
if i := byteIndex(line, '\t'); i >= 0 { if i := byteIndex(line, '\t'); i >= 0 {
txt = append(txt, ensureEndDot(line[i+1:])) txt = append(txt, absDomainName([]byte(line[i+1:])))
} }
} }
return return
...@@ -303,7 +292,7 @@ func lookupAddr(addr string) (name []string, err error) { ...@@ -303,7 +292,7 @@ func lookupAddr(addr string) (name []string, err error) {
if len(f) < 3 { if len(f) < 3 {
continue continue
} }
name = append(name, ensureEndDot(f[2])) name = append(name, absDomainName([]byte(f[2])))
} }
return return
} }
...@@ -7,6 +7,7 @@ package net ...@@ -7,6 +7,7 @@ package net
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"internal/testenv"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
...@@ -57,7 +58,7 @@ var lookupGoogleSRVTests = []struct { ...@@ -57,7 +58,7 @@ var lookupGoogleSRVTests = []struct {
} }
func TestLookupGoogleSRV(t *testing.T) { func TestLookupGoogleSRV(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -91,7 +92,7 @@ var lookupGmailMXTests = []struct { ...@@ -91,7 +92,7 @@ var lookupGmailMXTests = []struct {
} }
func TestLookupGmailMX(t *testing.T) { func TestLookupGmailMX(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -122,7 +123,7 @@ var lookupGmailNSTests = []struct { ...@@ -122,7 +123,7 @@ var lookupGmailNSTests = []struct {
} }
func TestLookupGmailNS(t *testing.T) { func TestLookupGmailNS(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -153,7 +154,7 @@ var lookupGmailTXTTests = []struct { ...@@ -153,7 +154,7 @@ var lookupGmailTXTTests = []struct {
} }
func TestLookupGmailTXT(t *testing.T) { func TestLookupGmailTXT(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -187,7 +188,7 @@ var lookupGooglePublicDNSAddrTests = []struct { ...@@ -187,7 +188,7 @@ var lookupGooglePublicDNSAddrTests = []struct {
} }
func TestLookupGooglePublicDNSAddr(t *testing.T) { func TestLookupGooglePublicDNSAddr(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 { if !supportsIPv4 || !supportsIPv6 || !*testIPv4 || !*testIPv6 {
...@@ -211,7 +212,7 @@ func TestLookupGooglePublicDNSAddr(t *testing.T) { ...@@ -211,7 +212,7 @@ func TestLookupGooglePublicDNSAddr(t *testing.T) {
} }
func TestLookupIPv6LinkLocalAddr(t *testing.T) { func TestLookupIPv6LinkLocalAddr(t *testing.T) {
if !supportsIPv6 { if !supportsIPv6 || !*testIPv6 {
t.Skip("IPv6 is required") t.Skip("IPv6 is required")
} }
...@@ -242,7 +243,7 @@ var lookupIANACNAMETests = []struct { ...@@ -242,7 +243,7 @@ var lookupIANACNAMETests = []struct {
} }
func TestLookupIANACNAME(t *testing.T) { func TestLookupIANACNAME(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -268,7 +269,7 @@ var lookupGoogleHostTests = []struct { ...@@ -268,7 +269,7 @@ var lookupGoogleHostTests = []struct {
} }
func TestLookupGoogleHost(t *testing.T) { func TestLookupGoogleHost(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -299,7 +300,7 @@ var lookupGoogleIPTests = []struct { ...@@ -299,7 +300,7 @@ var lookupGoogleIPTests = []struct {
} }
func TestLookupGoogleIP(t *testing.T) { func TestLookupGoogleIP(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skip("avoid external network") t.Skip("avoid external network")
} }
if !supportsIPv4 || !*testIPv4 { if !supportsIPv4 || !*testIPv4 {
...@@ -421,7 +422,7 @@ func TestLookupIPDeadline(t *testing.T) { ...@@ -421,7 +422,7 @@ func TestLookupIPDeadline(t *testing.T) {
} }
func TestLookupDotsWithLocalSource(t *testing.T) { func TestLookupDotsWithLocalSource(t *testing.T) {
if !supportsIPv4 { if !supportsIPv4 || !*testIPv4 {
t.Skip("IPv4 is required") t.Skip("IPv4 is required")
} }
...@@ -433,7 +434,7 @@ func TestLookupDotsWithLocalSource(t *testing.T) { ...@@ -433,7 +434,7 @@ func TestLookupDotsWithLocalSource(t *testing.T) {
names, err := LookupAddr("127.0.0.1") names, err := LookupAddr("127.0.0.1")
fixup() fixup()
if err != nil { if err != nil {
t.Errorf("#%d: %v", i, err) t.Logf("#%d: %v", i, err)
continue continue
} }
mode := "netgo" mode := "netgo"
...@@ -451,8 +452,11 @@ func TestLookupDotsWithLocalSource(t *testing.T) { ...@@ -451,8 +452,11 @@ func TestLookupDotsWithLocalSource(t *testing.T) {
} }
func TestLookupDotsWithRemoteSource(t *testing.T) { func TestLookupDotsWithRemoteSource(t *testing.T) {
if testing.Short() || !*testExternal { if testing.Short() && testenv.Builder() == "" || !*testExternal {
t.Skipf("skipping external network test") t.Skip("avoid external network")
}
if !supportsIPv4 || *testIPv4 {
t.Skip("IPv4 is required")
} }
if fixup := forceGoDNS(); fixup != nil { if fixup := forceGoDNS(); fixup != nil {
......
...@@ -213,17 +213,6 @@ func newLookupPort(network, service string) (int, error) { ...@@ -213,17 +213,6 @@ func newLookupPort(network, service string) (int, error) {
return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service} return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
} }
// ensureEndDot adds '.' at the end of name unless it is already there.
func ensureEndDot(name string) string {
if name == "" {
return "."
}
if name[len(name)-1] == '.' {
return name
}
return name + "."
}
func lookupCNAME(name string) (string, error) { func lookupCNAME(name string) (string, error) {
acquireThread() acquireThread()
defer releaseThread() defer releaseThread()
...@@ -232,7 +221,7 @@ func lookupCNAME(name string) (string, error) { ...@@ -232,7 +221,7 @@ func lookupCNAME(name string) (string, error) {
// windows returns DNS_INFO_NO_RECORDS if there are no CNAME-s // windows returns DNS_INFO_NO_RECORDS if there are no CNAME-s
if errno, ok := e.(syscall.Errno); ok && errno == syscall.DNS_INFO_NO_RECORDS { if errno, ok := e.(syscall.Errno); ok && errno == syscall.DNS_INFO_NO_RECORDS {
// if there are no aliases, the canonical name is the input name // if there are no aliases, the canonical name is the input name
return ensureEndDot(name), nil return absDomainName([]byte(name)), nil
} }
if e != nil { if e != nil {
return "", &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name} return "", &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name}
...@@ -241,7 +230,7 @@ func lookupCNAME(name string) (string, error) { ...@@ -241,7 +230,7 @@ func lookupCNAME(name string) (string, error) {
resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), r) resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), r)
cname := syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(resolved))[:]) cname := syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(resolved))[:])
return ensureEndDot(cname), nil return absDomainName([]byte(cname)), nil
} }
func lookupSRV(service, proto, name string) (string, []*SRV, error) { func lookupSRV(service, proto, name string) (string, []*SRV, error) {
...@@ -263,10 +252,10 @@ func lookupSRV(service, proto, name string) (string, []*SRV, error) { ...@@ -263,10 +252,10 @@ func lookupSRV(service, proto, name string) (string, []*SRV, error) {
srvs := make([]*SRV, 0, 10) srvs := make([]*SRV, 0, 10)
for _, p := range validRecs(r, syscall.DNS_TYPE_SRV, target) { for _, p := range validRecs(r, syscall.DNS_TYPE_SRV, target) {
v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0])) v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0]))
srvs = append(srvs, &SRV{ensureEndDot(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:])), v.Port, v.Priority, v.Weight}) srvs = append(srvs, &SRV{absDomainName([]byte(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:]))), v.Port, v.Priority, v.Weight})
} }
byPriorityWeight(srvs).sort() byPriorityWeight(srvs).sort()
return ensureEndDot(target), srvs, nil return absDomainName([]byte(target)), srvs, nil
} }
func lookupMX(name string) ([]*MX, error) { func lookupMX(name string) ([]*MX, error) {
...@@ -282,7 +271,7 @@ func lookupMX(name string) ([]*MX, error) { ...@@ -282,7 +271,7 @@ func lookupMX(name string) ([]*MX, error) {
mxs := make([]*MX, 0, 10) mxs := make([]*MX, 0, 10)
for _, p := range validRecs(r, syscall.DNS_TYPE_MX, name) { for _, p := range validRecs(r, syscall.DNS_TYPE_MX, name) {
v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0])) v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0]))
mxs = append(mxs, &MX{ensureEndDot(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:])), v.Preference}) mxs = append(mxs, &MX{absDomainName([]byte(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:]))), v.Preference})
} }
byPref(mxs).sort() byPref(mxs).sort()
return mxs, nil return mxs, nil
...@@ -301,7 +290,7 @@ func lookupNS(name string) ([]*NS, error) { ...@@ -301,7 +290,7 @@ func lookupNS(name string) ([]*NS, error) {
nss := make([]*NS, 0, 10) nss := make([]*NS, 0, 10)
for _, p := range validRecs(r, syscall.DNS_TYPE_NS, name) { for _, p := range validRecs(r, syscall.DNS_TYPE_NS, name) {
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0])) v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
nss = append(nss, &NS{ensureEndDot(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))}) nss = append(nss, &NS{absDomainName([]byte(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:])))})
} }
return nss, nil return nss, nil
} }
...@@ -344,7 +333,7 @@ func lookupAddr(addr string) ([]string, error) { ...@@ -344,7 +333,7 @@ func lookupAddr(addr string) ([]string, error) {
ptrs := make([]string, 0, 10) ptrs := make([]string, 0, 10)
for _, p := range validRecs(r, syscall.DNS_TYPE_PTR, arpa) { for _, p := range validRecs(r, syscall.DNS_TYPE_PTR, arpa) {
v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0])) v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
ptrs = append(ptrs, ensureEndDot(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))) ptrs = append(ptrs, absDomainName([]byte(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))))
} }
return ptrs, nil return ptrs, nil
} }
......
...@@ -177,14 +177,14 @@ func nslookupMX(name string) (mx []*MX, err error) { ...@@ -177,14 +177,14 @@ func nslookupMX(name string) (mx []*MX, err error) {
rx := regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+mail exchanger\s*=\s*([0-9]+)\s*([a-z0-9.\-]+)$`) rx := regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+mail exchanger\s*=\s*([0-9]+)\s*([a-z0-9.\-]+)$`)
for _, ans := range rx.FindAllStringSubmatch(r, -1) { for _, ans := range rx.FindAllStringSubmatch(r, -1) {
pref, _, _ := dtoi(ans[2], 0) pref, _, _ := dtoi(ans[2], 0)
mx = append(mx, &MX{ensureEndDot(ans[3]), uint16(pref)}) mx = append(mx, &MX{absDomainName([]byte(ans[3])), uint16(pref)})
} }
// windows nslookup syntax // windows nslookup syntax
// gmail.com MX preference = 30, mail exchanger = alt3.gmail-smtp-in.l.google.com // gmail.com MX preference = 30, mail exchanger = alt3.gmail-smtp-in.l.google.com
rx = regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+MX preference\s*=\s*([0-9]+)\s*,\s*mail exchanger\s*=\s*([a-z0-9.\-]+)$`) rx = regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+MX preference\s*=\s*([0-9]+)\s*,\s*mail exchanger\s*=\s*([a-z0-9.\-]+)$`)
for _, ans := range rx.FindAllStringSubmatch(r, -1) { for _, ans := range rx.FindAllStringSubmatch(r, -1) {
pref, _, _ := dtoi(ans[2], 0) pref, _, _ := dtoi(ans[2], 0)
mx = append(mx, &MX{ensureEndDot(ans[3]), uint16(pref)}) mx = append(mx, &MX{absDomainName([]byte(ans[3])), uint16(pref)})
} }
return return
} }
...@@ -198,7 +198,7 @@ func nslookupNS(name string) (ns []*NS, err error) { ...@@ -198,7 +198,7 @@ func nslookupNS(name string) (ns []*NS, err error) {
// golang.org nameserver = ns1.google.com. // golang.org nameserver = ns1.google.com.
rx := regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+nameserver\s*=\s*([a-z0-9.\-]+)$`) rx := regexp.MustCompile(`(?m)^([a-z0-9.\-]+)\s+nameserver\s*=\s*([a-z0-9.\-]+)$`)
for _, ans := range rx.FindAllStringSubmatch(r, -1) { for _, ans := range rx.FindAllStringSubmatch(r, -1) {
ns = append(ns, &NS{ensureEndDot(ans[2])}) ns = append(ns, &NS{absDomainName([]byte(ans[2]))})
} }
return return
} }
...@@ -215,7 +215,7 @@ func nslookupCNAME(name string) (cname string, err error) { ...@@ -215,7 +215,7 @@ func nslookupCNAME(name string) (cname string, err error) {
for _, ans := range rx.FindAllStringSubmatch(r, -1) { for _, ans := range rx.FindAllStringSubmatch(r, -1) {
last = ans[2] last = ans[2]
} }
return ensureEndDot(last), nil return absDomainName([]byte(last)), nil
} }
func nslookupTXT(name string) (txt []string, err error) { func nslookupTXT(name string) (txt []string, err error) {
......
...@@ -6,6 +6,17 @@ ...@@ -6,6 +6,17 @@
package net package net
import "runtime"
// See unix_test.go for what these (don't) do.
func forceGoDNS() func() {
switch runtime.GOOS {
case "plan9", "windows":
return func() {}
default:
return nil
}
}
// See unix_test.go for what these (don't) do. // See unix_test.go for what these (don't) do.
func forceGoDNS() func() { return nil }
func forceCgoDNS() func() { return nil } func forceCgoDNS() func() { return nil }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment