net: add IsNotFound field to DNSError

This adds the ability to determine if a lookup error was
due to a non-existent hostname. Previously users needed
to do string matching on the DNSError.Err value.

Fixes #28635

Change-Id: If4bd3ad32cbc2db5614f2c6b72e0a9161d813efa
Reviewed-on: https://go-review.googlesource.com/c/go/+/168597
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Shubham Sharma 2019-03-21 21:10:12 +05:30 committed by Brad Fitzpatrick
parent 7e08c7f43d
commit fac3b5d05e
7 changed files with 68 additions and 19 deletions

View file

@ -666,7 +666,7 @@ func TestErrorForOriginalNameWhenSearching(t *testing.T) {
wantErr *DNSError
}{
{true, &DNSError{Name: fqdn, Err: "server misbehaving", IsTemporary: true}},
{false, &DNSError{Name: fqdn, Err: errNoSuchHost.Error()}},
{false, &DNSError{Name: fqdn, Err: errNoSuchHost.Error(), IsNotFound: true}},
}
for _, tt := range cases {
r := Resolver{PreferGo: true, StrictErrors: tt.strictErrors, Dial: fake.DialContext}
@ -1138,9 +1138,10 @@ func TestStrictErrorsLookupIP(t *testing.T) {
}
makeNxDomain := func() error {
return &DNSError{
Err: errNoSuchHost.Error(),
Name: name,
Server: server,
Err: errNoSuchHost.Error(),
Name: name,
Server: server,
IsNotFound: true,
}
}
@ -1472,6 +1473,32 @@ func TestIssue8434(t *testing.T) {
}
}
func TestIssueNoSuchHostExists(t *testing.T) {
err := lookupWithFake(fakeDNSServer{
rh: func(n, _ string, q dnsmessage.Message, _ time.Time) (dnsmessage.Message, error) {
return dnsmessage.Message{
Header: dnsmessage.Header{
ID: q.ID,
Response: true,
RCode: dnsmessage.RCodeNameError,
},
Questions: q.Questions,
}, nil
},
}, "golang.org.", dnsmessage.TypeALL)
if err == nil {
t.Fatal("expected an error")
}
if _, ok := err.(Error); !ok {
t.Fatalf("err = %#v; wanted something supporting net.Error", err)
}
if de, ok := err.(*DNSError); !ok {
t.Fatalf("err = %#v; wanted a *net.DNSError", err)
} else if !de.IsNotFound {
t.Fatalf("IsNotFound = false for err = %#v; want IsNotFound == true", err)
}
}
// TestNoSuchHost verifies that tryOneName works correctly when the domain does
// not exist.
//
@ -1541,6 +1568,9 @@ func TestNoSuchHost(t *testing.T) {
if de.Err != errNoSuchHost.Error() {
t.Fatalf("Err = %#v; wanted %q", de.Err, errNoSuchHost.Error())
}
if !de.IsNotFound {
t.Fatalf("IsNotFound = %v wanted true", de.IsNotFound)
}
})
}
}