Skip to content

Commit

Permalink
net: use case-insensitive host string comparison in TestLookup*
Browse files Browse the repository at this point in the history
Some nameservers alter the case of records as they return, e.g
.google.COM or .Google.com.
However according to RFC4343, DNS name should be treated in case insensitive fashion.
This CL will fix case sensitive testcases.

Fixes #34781

Change-Id: I5f9f6a41ddc1c61993e8d1f934ef0febddc3adc1
Reviewed-on: https://go-review.googlesource.com/c/go/+/200277
Reviewed-by: Andrei Tudor Călin <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
mengzhuo authored and bradfitz committed Oct 12, 2019
1 parent 3c56eb4 commit 592d304
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/net/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"time"
)

func hasSuffixFold(s, suffix string) bool {
return strings.HasSuffix(strings.ToLower(s), strings.ToLower(suffix))
}

func lookupLocalhost(ctx context.Context, fn func(context.Context, string, string) ([]IPAddr, error), network, host string) ([]IPAddr, error) {
switch host {
case "localhost":
Expand Down Expand Up @@ -97,11 +101,11 @@ func TestLookupGoogleSRV(t *testing.T) {
if len(srvs) == 0 {
t.Error("got no record")
}
if !strings.HasSuffix(cname, tt.cname) {
if !hasSuffixFold(cname, tt.cname) {
t.Errorf("got %s; want %s", cname, tt.cname)
}
for _, srv := range srvs {
if !strings.HasSuffix(srv.Target, tt.target) {
if !hasSuffixFold(srv.Target, tt.target) {
t.Errorf("got %v; want a record containing %s", srv, tt.target)
}
}
Expand Down Expand Up @@ -147,7 +151,7 @@ func TestLookupGmailMX(t *testing.T) {
t.Error("got no record")
}
for _, mx := range mxs {
if !strings.HasSuffix(mx.Host, tt.host) {
if !hasSuffixFold(mx.Host, tt.host) {
t.Errorf("got %v; want a record containing %s", mx, tt.host)
}
}
Expand Down Expand Up @@ -193,9 +197,7 @@ func TestLookupGmailNS(t *testing.T) {
t.Error("got no record")
}
for _, ns := range nss {
// Some nameservers alter the case of NS records. See #34446.
host := strings.ToLower(ns.Host)
if !strings.HasSuffix(host, tt.host) {
if !hasSuffixFold(ns.Host, tt.host) {
t.Errorf("got %v; want a record containing %s", ns, tt.host)
}
}
Expand Down Expand Up @@ -281,7 +283,7 @@ func TestLookupGooglePublicDNSAddr(t *testing.T) {
t.Error("got no record")
}
for _, name := range names {
if !strings.HasSuffix(name, ".google.com.") && !strings.HasSuffix(name, ".google.") {
if !hasSuffixFold(name, ".google.com.") && !hasSuffixFold(name, ".google.") {
t.Errorf("got %q; want a record ending in .google.com. or .google.", name)
}
}
Expand Down Expand Up @@ -373,7 +375,7 @@ func TestLookupCNAME(t *testing.T) {
}
t.Fatal(err)
}
if !strings.HasSuffix(cname, tt.cname) {
if !hasSuffixFold(cname, tt.cname) {
t.Errorf("got %s; want a record containing %s", cname, tt.cname)
}
}
Expand Down Expand Up @@ -658,7 +660,7 @@ func testDots(t *testing.T, mode string) {
t.Errorf("LookupAddr(8.8.8.8): %v (mode=%v)", err, mode)
} else {
for _, name := range names {
if !strings.HasSuffix(name, ".google.com.") && !strings.HasSuffix(name, ".google.") {
if !hasSuffixFold(name, ".google.com.") && !hasSuffixFold(name, ".google.") {
t.Errorf("LookupAddr(8.8.8.8) = %v, want names ending in .google.com or .google with trailing dot (mode=%v)", names, mode)
break
}
Expand All @@ -679,7 +681,7 @@ func testDots(t *testing.T, mode string) {
t.Errorf("LookupMX(google.com): %v (mode=%v)", err, mode)
} else {
for _, mx := range mxs {
if !strings.HasSuffix(mx.Host, ".google.com.") {
if !hasSuffixFold(mx.Host, ".google.com.") {
t.Errorf("LookupMX(google.com) = %v, want names ending in .google.com. with trailing dot (mode=%v)", mxString(mxs), mode)
break
}
Expand All @@ -692,7 +694,7 @@ func testDots(t *testing.T, mode string) {
t.Errorf("LookupNS(google.com): %v (mode=%v)", err, mode)
} else {
for _, ns := range nss {
if !strings.HasSuffix(ns.Host, ".google.com.") {
if !hasSuffixFold(ns.Host, ".google.com.") {
t.Errorf("LookupNS(google.com) = %v, want names ending in .google.com. with trailing dot (mode=%v)", nsString(nss), mode)
break
}
Expand All @@ -704,11 +706,11 @@ func testDots(t *testing.T, mode string) {
testenv.SkipFlakyNet(t)
t.Errorf("LookupSRV(xmpp-server, tcp, google.com): %v (mode=%v)", err, mode)
} else {
if !strings.HasSuffix(cname, ".google.com.") {
if !hasSuffixFold(cname, ".google.com.") {
t.Errorf("LookupSRV(xmpp-server, tcp, google.com) returned cname=%v, want name ending in .google.com. with trailing dot (mode=%v)", cname, mode)
}
for _, srv := range srvs {
if !strings.HasSuffix(srv.Target, ".google.com.") {
if !hasSuffixFold(srv.Target, ".google.com.") {
t.Errorf("LookupSRV(xmpp-server, tcp, google.com) returned addrs=%v, want names ending in .google.com. with trailing dot (mode=%v)", srvString(srvs), mode)
break
}
Expand Down

0 comments on commit 592d304

Please sign in to comment.