Skip to content

net: use fake DNS dialer for /etc/hosts aliases tests #61734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/net/dnsclient_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"errors"
"fmt"
"internal/testenv"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -2200,9 +2199,6 @@ var goLookupIPCNAMEOrderDNSFilesModeTests = []struct {
}

func TestGoLookupIPCNAMEOrderHostsAliasesDNSFilesMode(t *testing.T) {
if testenv.Builder() == "" {
t.Skip("Makes assumptions about local networks and (re)naming that aren't always true")
}
defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/aliases"
mode := hostLookupDNSFiles
Expand All @@ -2213,9 +2209,29 @@ func TestGoLookupIPCNAMEOrderHostsAliasesDNSFilesMode(t *testing.T) {
}

func testGoLookupIPCNAMEOrderHostsAliases(t *testing.T, mode hostLookupOrder, lookup, lookupRes string) {
fake := fakeDNSServer{
rh: func(_, _ string, q dnsmessage.Message, _ time.Time) (dnsmessage.Message, error) {
var answers []dnsmessage.Resource

if mode != hostLookupDNSFiles {
t.Fatal("received unexpected DNS query")
}

return dnsmessage.Message{
Header: dnsmessage.Header{
ID: q.Header.ID,
Response: true,
},
Questions: []dnsmessage.Question{q.Questions[0]},
Answers: answers,
}, nil
},
}

r := Resolver{PreferGo: true, Dial: fake.DialContext}
ins := []string{lookup, absDomainName(lookup), strings.ToLower(lookup), strings.ToUpper(lookup)}
for _, in := range ins {
_, res, err := goResolver.goLookupIPCNAMEOrder(context.Background(), "ip", in, mode, nil)
_, res, err := r.goLookupIPCNAMEOrder(context.Background(), "ip", in, mode, nil)
if err != nil {
t.Errorf("expected err == nil, but got error: %v", err)
}
Expand Down