summaryrefslogtreecommitdiff
path: root/src/net/conf_test.go
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2023-05-07 16:46:42 +0000
committerGopher Robot <gobot@golang.org>2023-05-11 16:17:04 +0000
commit472f623482cdd7b89691d7dfe1c9e3d1c21ea538 (patch)
tree5edf33524ac5df400cb7bd8ac871a5df70228669 /src/net/conf_test.go
parent2e7d864f43b957673f50e648b27c46074ed43404 (diff)
downloadgo-git-472f623482cdd7b89691d7dfe1c9e3d1c21ea538.tar.gz
net: force cgo for myhostname and mdns nss modules for LookupAddr on unix
Currently there is a small bug in the LookupAddr for unix systems that causes the use of go resolver instead of the cgo one. Example for nss myhostname: func main() { fmt.Println(net.LookupAddr(os.Args[1])) } root@arch:~# cat /etc/nsswitch.conf | grep host hosts: myhostname dns root@arch:~# GODEBUG=netdns=+3 go run main.go 192.168.1.200 go package net: confVal.netCgo = false netGo = false go package net: dynamic selection of DNS resolver go package net: hostLookupOrder() = dns [] lookup 200.1.168.192.in-addr.arpa. on 8.8.8.8:53: no such host root@arch:~# GODEBUG=netdns=go+3 go run main.go 192.168.1.200 go package net: confVal.netCgo = false netGo = true go package net: GODEBUG setting forcing use of Go's resolver go package net: hostLookupOrder() = dns [] lookup 200.1.168.192.in-addr.arpa. on 8.8.8.8:53: no such host root@arch:~# GODEBUG=netdns=cgo+3 go run main.go 192.168.1.200 go package net: confVal.netCgo = true netGo = false go package net: using cgo DNS resolver go package net: hostLookupOrder() = cgo [arch] <nil> The problem come from that we are only checking for hostnames that the myhostname can resolve, but not for the addrs that it can also. man nss-myhostname: Please keep in mind that nss-myhostname (and nss-resolve) also resolve in the other direction — from locally attached IP addresses to hostnames. Change-Id: Ic18a9f99a2214b2938463e9a95f7f3ca5db1c01b GitHub-Last-Rev: ade40fd3e3057de418b9b6a79f79fb9a53fb6c09 GitHub-Pull-Request: golang/go#59921 Reviewed-on: https://go-review.googlesource.com/c/go/+/491235 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
Diffstat (limited to 'src/net/conf_test.go')
-rw-r--r--src/net/conf_test.go37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/net/conf_test.go b/src/net/conf_test.go
index 08d774bfe2..d2cdac8083 100644
--- a/src/net/conf_test.go
+++ b/src/net/conf_test.go
@@ -304,7 +304,6 @@ func TestConfHostLookupOrder(t *testing.T) {
{"anything.localhost.localdomain", "myhostname", hostLookupCgo},
{"Anything.Localhost.Localdomain", "myhostname", hostLookupCgo},
{"somehostname", "myhostname", hostLookupFilesDNS},
- {"", "myhostname", hostLookupFilesDNS}, // Issue 13623
},
},
{
@@ -392,6 +391,42 @@ func TestConfHostLookupOrder(t *testing.T) {
}
}
+func TestAddrLookupOrder(t *testing.T) {
+ // This test is written for a system with cgo available,
+ // without using the netgo tag.
+ if netGoBuildTag {
+ t.Skip("skipping test because net package built with netgo tag")
+ }
+ if !cgoAvailable {
+ t.Skip("skipping test because cgo resolver not available")
+ }
+
+ defer setSystemNSS(getSystemNSS(), 0)
+ c, err := newResolvConfTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer c.teardown()
+
+ if !c.forceUpdateConf(defaultResolvConf, time.Now().Add(time.Hour)) {
+ t.Fatal("failed to change resolv config")
+ }
+
+ setSystemNSS(nssStr(t, "hosts: files myhostname dns"), time.Hour)
+ cnf := &conf{}
+ order, _ := cnf.addrLookupOrder(nil, "192.0.2.1")
+ if order != hostLookupCgo {
+ t.Errorf("addrLookupOrder returned: %v, want cgo", order)
+ }
+
+ setSystemNSS(nssStr(t, "hosts: files mdns4 dns"), time.Hour)
+ order, _ = cnf.addrLookupOrder(nil, "192.0.2.1")
+ if order != hostLookupCgo {
+ t.Errorf("addrLookupOrder returned: %v, want cgo", order)
+ }
+
+}
+
func setSystemNSS(nss *nssConf, addDur time.Duration) {
nssConfig.mu.Lock()
nssConfig.nssConf = nss