mirror of
https://github.com/golang/go
synced 2024-09-29 12:14:28 -06:00
net: return errNoSuchHost when no entry found in /etc/hosts and order is hostLookupFiles
When /etc/nsswitch.conf lists: "hosts: files" then LookupHost returns two nils when no entry inside /etc/hosts is found.
Change-Id: I96d68a079dfe009655c84cf0e697ce19a5bb6698
GitHub-Last-Rev: 894f066bbc
GitHub-Pull-Request: golang/go#56747
Reviewed-on: https://go-review.googlesource.com/c/go/+/450875
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
1f4394a0c9
commit
3e5c2c1556
@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
||||
//go:build unix
|
||||
|
||||
package net
|
||||
|
||||
|
@ -551,9 +551,13 @@ func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hos
|
||||
if order == hostLookupFilesDNS || order == hostLookupFiles {
|
||||
// Use entries from /etc/hosts if they match.
|
||||
addrs, _ = lookupStaticHost(name)
|
||||
if len(addrs) > 0 || order == hostLookupFiles {
|
||||
if len(addrs) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if order == hostLookupFiles {
|
||||
return nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, IsNotFound: true}
|
||||
}
|
||||
}
|
||||
ips, _, err := r.goLookupIPCNAMEOrder(ctx, "ip", name, order, conf)
|
||||
if err != nil {
|
||||
|
@ -12,7 +12,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@ -2503,3 +2505,92 @@ func TestDNSConfigNoReload(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupOrderFilesNoSuchHost(t *testing.T) {
|
||||
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
|
||||
if runtime.GOOS != "openbsd" {
|
||||
defer setSystemNSS(getSystemNSS(), 0)
|
||||
setSystemNSS(nssStr(t, "hosts: files"), time.Hour)
|
||||
}
|
||||
|
||||
conf, err := newResolvConfTest()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conf.teardown()
|
||||
|
||||
resolvConf := dnsConfig{servers: defaultNS}
|
||||
if runtime.GOOS == "openbsd" {
|
||||
// Set error to ErrNotExist, so that the hostLookupOrder
|
||||
// returns hostLookupFiles for openbsd.
|
||||
resolvConf.err = os.ErrNotExist
|
||||
}
|
||||
|
||||
if !conf.forceUpdateConf(&resolvConf, time.Now().Add(time.Hour)) {
|
||||
t.Fatal("failed to update resolv config")
|
||||
}
|
||||
|
||||
tmpFile := filepath.Join(t.TempDir(), "hosts")
|
||||
if err := os.WriteFile(tmpFile, []byte{}, 0660); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
testHookHostsPath = tmpFile
|
||||
|
||||
const testName = "test.invalid"
|
||||
|
||||
order, _ := systemConf().hostLookupOrder(DefaultResolver, testName)
|
||||
if order != hostLookupFiles {
|
||||
// skip test for systems which do not return hostLookupFiles
|
||||
t.Skipf("hostLookupOrder did not return hostLookupFiles")
|
||||
}
|
||||
|
||||
var lookupTests = []struct {
|
||||
name string
|
||||
lookup func(name string) error
|
||||
}{
|
||||
{
|
||||
name: "Host",
|
||||
lookup: func(name string) error {
|
||||
_, err = DefaultResolver.LookupHost(context.Background(), name)
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IP",
|
||||
lookup: func(name string) error {
|
||||
_, err = DefaultResolver.LookupIP(context.Background(), "ip", name)
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IPAddr",
|
||||
lookup: func(name string) error {
|
||||
_, err = DefaultResolver.LookupIPAddr(context.Background(), name)
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "NetIP",
|
||||
lookup: func(name string) error {
|
||||
_, err = DefaultResolver.LookupNetIP(context.Background(), "ip", name)
|
||||
return err
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, v := range lookupTests {
|
||||
err := v.lookup(testName)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Lookup%v: unexpected success", v.name)
|
||||
continue
|
||||
}
|
||||
|
||||
expectedErr := DNSError{Err: errNoSuchHost.Error(), Name: testName, IsNotFound: true}
|
||||
var dnsErr *DNSError
|
||||
errors.As(err, &dnsErr)
|
||||
if dnsErr == nil || *dnsErr != expectedErr {
|
||||
t.Errorf("Lookup%v: unexpected error: %v", v.name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user