Skip to content

Commit 541f2d0

Browse files
committed
net: add Resolver.PreferTCP test + GODEBUG=netdns=go,tcp doc
1 parent cd77ba3 commit 541f2d0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/net/dnsclient_unix_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,3 +1621,30 @@ func TestTXTRecordTwoStrings(t *testing.T) {
16211621
t.Errorf("txt[1], got %q, want %q", txt[1], want)
16221622
}
16231623
}
1624+
1625+
// Issue 29358. Add configuration knob to force TCP-only DNS requests in the pure Go resolver.
1626+
func TestPreferTCP(t *testing.T) {
1627+
fake := fakeDNSServer{
1628+
rh: func(n, _ string, q dnsmessage.Message, _ time.Time) (dnsmessage.Message, error) {
1629+
r := dnsmessage.Message{
1630+
Header: dnsmessage.Header{
1631+
ID: q.Header.ID,
1632+
Response: true,
1633+
RCode: dnsmessage.RCodeSuccess,
1634+
},
1635+
Questions: q.Questions,
1636+
}
1637+
if n == "udp" {
1638+
t.Fatal("udp protocol was used instead of tcp")
1639+
}
1640+
return r, nil
1641+
},
1642+
}
1643+
r := Resolver{PreferGo: true, PreferTCP: true, Dial: fake.DialContext}
1644+
ctx, cancel := context.WithCancel(context.Background())
1645+
defer cancel()
1646+
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second)
1647+
if err != nil {
1648+
t.Fatal("exchange failed:", err)
1649+
}
1650+
}

src/net/net.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ GODEBUG environment variable (see package runtime) to go or cgo, as in:
6666
The decision can also be forced while building the Go source tree
6767
by setting the netgo or netcgo build tag.
6868
69+
The pure Go resolver can be configured to use only TCP to communicate
70+
by using the tcp option, as in GODEBUG=netdns=go,tcp.
71+
6972
A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver
7073
to print debugging information about its decisions.
7174
To force a particular resolver while also printing debugging information,

0 commit comments

Comments
 (0)