Fixes #57757.
Change-Id: I896dae8e5905ae98539ab83c9379fd1c9886d44a
Reviewed-on: https://go-review.googlesource.com/c/go/+/467335
Reviewed-by: Mateusz Poliwczak <mpoliwczak34@gmail.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Joedian Reid <joedian@golang.org>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
}
//sys NetUserGetLocalGroups(serverName *uint16, userName *uint16, level uint32, flags uint32, buf **byte, prefMaxLen uint32, entriesRead *uint32, totalEntries *uint32) (neterr error) = netapi32.NetUserGetLocalGroups
+
+// GetSystemDirectory retrieves the path to current location of the system
+// directory, which is typically, though not always, `C:\Windows\System32`.
+//
+//go:linkname GetSystemDirectory
+func GetSystemDirectory() string // Implemented in runtime package.
// Go resolver was explicitly requested
// or cgo resolver is not available.
// Figure out the order below.
- switch c.goos {
- case "windows":
- // TODO(bradfitz): implement files-based
- // lookup on Windows too? I guess /etc/hosts
- // kinda exists on Windows. But for now, only
- // do DNS.
- fallbackOrder = hostLookupDNS
- default:
- fallbackOrder = hostLookupFilesDNS
- }
+ fallbackOrder = hostLookupFilesDNS
canUseCgo = false
} else if c.netCgo {
// Cgo resolver was explicitly requested.
t.Fatal(err)
}
// Redirect host file lookups.
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
- testHookHostsPath = "testdata/hosts"
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
+ hostsFilePath = "testdata/hosts"
for _, order := range []hostLookupOrder{hostLookupFilesDNS, hostLookupDNSFiles} {
name := fmt.Sprintf("order %v", order)
DefaultResolver = &r
defer func() { DefaultResolver = originalDefault }()
// Redirect host file lookups.
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
- testHookHostsPath = "testdata/hosts"
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
+ hostsFilePath = "testdata/hosts"
tests := []struct {
name string
}
func TestGoLookupIPCNAMEOrderHostsAliasesFilesOnlyMode(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
- testHookHostsPath = "testdata/aliases"
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
+ hostsFilePath = "testdata/aliases"
mode := hostLookupFiles
for _, v := range lookupStaticHostAliasesTest {
}
func TestGoLookupIPCNAMEOrderHostsAliasesFilesDNSMode(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
- testHookHostsPath = "testdata/aliases"
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
+ hostsFilePath = "testdata/aliases"
mode := hostLookupFilesDNS
for _, v := range lookupStaticHostAliasesTest {
}
func TestGoLookupIPCNAMEOrderHostsAliasesDNSFilesMode(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
- testHookHostsPath = "testdata/aliases"
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
+ hostsFilePath = "testdata/aliases"
mode := hostLookupDNSFiles
for _, v := range goLookupIPCNAMEOrderDNSFilesModeTests {
}
func TestLookupOrderFilesNoSuchHost(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
if runtime.GOOS != "openbsd" {
defer setSystemNSS(getSystemNSS(), 0)
setSystemNSS(nssStr(t, "hosts: files"), time.Hour)
if err := os.WriteFile(tmpFile, []byte{}, 0660); err != nil {
t.Fatal(err)
}
- testHookHostsPath = tmpFile
+ hostsFilePath = tmpFile
const testName = "test.invalid"
// if non-nil, overrides dialTCP.
testHookDialTCP func(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error)
- testHookHostsPath = "/etc/hosts"
- testHookLookupIP = func(
+ testHookLookupIP = func(
ctx context.Context,
fn func(context.Context, string, string) ([]IPAddr, error),
network string,
--- /dev/null
+// Copyright 2015 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
+
+var (
+ hostsFilePath = "/etc/hosts"
+)
var (
testHookCanceledDial = func() {} // for golang.org/issue/16523
+ hostsFilePath = "/etc/hosts"
+
// Placeholders for socket system calls.
socketFunc func(int, int, int) (int, error) = syscall.Socket
connectFunc func(int, syscall.Sockaddr) error = syscall.Connect
)
var (
+ hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
+
// Placeholders for socket system calls.
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
func readHosts() {
now := time.Now()
- hp := testHookHostsPath
+ hp := hostsFilePath
if now.Before(hosts.expire) && hosts.path == hp && len(hosts.byName) > 0 {
return
}
func TestLookupStaticHost(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
for _, tt := range lookupStaticHostTests {
- testHookHostsPath = tt.name
+ hostsFilePath = tt.name
for _, ent := range tt.ents {
testStaticHost(t, tt.name, ent)
}
}
func TestLookupStaticAddr(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
for _, tt := range lookupStaticAddrTests {
- testHookHostsPath = tt.name
+ hostsFilePath = tt.name
for _, ent := range tt.ents {
testStaticAddr(t, tt.name, ent)
}
func TestHostCacheModification(t *testing.T) {
// Ensure that programs can't modify the internals of the host cache.
// See https://golang.org/issues/14212.
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
- testHookHostsPath = "testdata/ipv4-hosts"
+ hostsFilePath = "testdata/ipv4-hosts"
ent := staticHostEntry{"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}}
- testStaticHost(t, testHookHostsPath, ent)
+ testStaticHost(t, hostsFilePath, ent)
// Modify the addresses return by lookupStaticHost.
addrs, _ := lookupStaticHost(ent.in)
for i := range addrs {
addrs[i] += "junk"
}
- testStaticHost(t, testHookHostsPath, ent)
+ testStaticHost(t, hostsFilePath, ent)
- testHookHostsPath = "testdata/ipv6-hosts"
+ hostsFilePath = "testdata/ipv6-hosts"
ent = staticHostEntry{"::1", []string{"localhost"}}
- testStaticAddr(t, testHookHostsPath, ent)
+ testStaticAddr(t, hostsFilePath, ent)
// Modify the hosts return by lookupStaticAddr.
hosts := lookupStaticAddr(ent.in)
for i := range hosts {
hosts[i] += "junk"
}
- testStaticAddr(t, testHookHostsPath, ent)
+ testStaticAddr(t, hostsFilePath, ent)
}
var lookupStaticHostAliasesTest = []struct {
}
func TestLookupStaticHostAliases(t *testing.T) {
- defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+ defer func(orig string) { hostsFilePath = orig }(hostsFilePath)
- testHookHostsPath = "testdata/aliases"
+ hostsFilePath = "testdata/aliases"
for _, ent := range lookupStaticHostAliasesTest {
testLookupStaticHostAliases(t, ent.lookup, absDomainName(ent.res))
}
sysDirectoryLen = l + 1
}
+//go:linkname windows_GetSystemDirectory internal/syscall/windows.GetSystemDirectory
+func windows_GetSystemDirectory() string {
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
+}
+
func windowsLoadSystemLib(name []uint16) uintptr {
return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
}