1
0
mirror of https://github.com/golang/go synced 2024-11-23 02:10:03 -07:00

net: don't rely on system hosts in TestCVE202133195

Also don't unnecessarily deref the error return.

Fixes #46504

Change-Id: I22d14ac76776f8988fa0774bdcb5fcd801ce0185
Reviewed-on: https://go-review.googlesource.com/c/go/+/324190
Trust: David Chase <drchase@google.com>
Trust: Damien Neil <dneil@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Roland Shoemaker 2021-06-02 09:20:22 -07:00 committed by Damien Neil
parent 4f572d7076
commit dd7ba3ba2c

View File

@ -1898,61 +1898,62 @@ func TestCVE202133195(t *testing.T) {
// Change the default resolver to match our manipulated resolver // Change the default resolver to match our manipulated resolver
originalDefault := DefaultResolver originalDefault := DefaultResolver
DefaultResolver = &r DefaultResolver = &r
defer func() { defer func() { DefaultResolver = originalDefault }()
DefaultResolver = originalDefault // Redirect host file lookups.
}() defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
testHookHostsPath = "testdata/hosts"
_, err := r.LookupCNAME(context.Background(), "golang.org") _, err := r.LookupCNAME(context.Background(), "golang.org")
if expected := "lookup golang.org: CNAME target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: CNAME target is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupCNAME returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupCNAME returned unexpected error, got %q, want %q", err, expected)
} }
_, err = LookupCNAME("golang.org") _, err = LookupCNAME("golang.org")
if expected := "lookup golang.org: CNAME target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: CNAME target is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupCNAME returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupCNAME returned unexpected error, got %q, want %q", err, expected)
} }
_, _, err = r.LookupSRV(context.Background(), "target", "tcp", "golang.org") _, _, err = r.LookupSRV(context.Background(), "target", "tcp", "golang.org")
if expected := "lookup golang.org: SRV target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: SRV target is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupSRV returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupSRV returned unexpected error, got %q, want %q", err, expected)
} }
_, _, err = LookupSRV("target", "tcp", "golang.org") _, _, err = LookupSRV("target", "tcp", "golang.org")
if expected := "lookup golang.org: SRV target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: SRV target is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupSRV returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupSRV returned unexpected error, got %q, want %q", err, expected)
} }
_, _, err = r.LookupSRV(context.Background(), "hdr", "tcp", "golang.org") _, _, err = r.LookupSRV(context.Background(), "hdr", "tcp", "golang.org")
if expected := "lookup golang.org: SRV header name is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: SRV header name is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupSRV returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupSRV returned unexpected error, got %q, want %q", err, expected)
} }
_, _, err = LookupSRV("hdr", "tcp", "golang.org") _, _, err = LookupSRV("hdr", "tcp", "golang.org")
if expected := "lookup golang.org: SRV header name is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: SRV header name is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupSRV returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupSRV returned unexpected error, got %q, want %q", err, expected)
} }
_, err = r.LookupMX(context.Background(), "golang.org") _, err = r.LookupMX(context.Background(), "golang.org")
if expected := "lookup golang.org: MX target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: MX target is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupMX returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupMX returned unexpected error, got %q, want %q", err, expected)
} }
_, err = LookupMX("golang.org") _, err = LookupMX("golang.org")
if expected := "lookup golang.org: MX target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: MX target is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupMX returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupMX returned unexpected error, got %q, want %q", err, expected)
} }
_, err = r.LookupNS(context.Background(), "golang.org") _, err = r.LookupNS(context.Background(), "golang.org")
if expected := "lookup golang.org: NS target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: NS target is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupNS returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupNS returned unexpected error, got %q, want %q", err, expected)
} }
_, err = LookupNS("golang.org") _, err = LookupNS("golang.org")
if expected := "lookup golang.org: NS target is invalid"; err == nil || err.Error() != expected { if expected := "lookup golang.org: NS target is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupNS returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupNS returned unexpected error, got %q, want %q", err, expected)
} }
_, err = r.LookupAddr(context.Background(), "1.2.3.4") _, err = r.LookupAddr(context.Background(), "192.0.2.42")
if expected := "lookup 1.2.3.4: PTR target is invalid"; err == nil || err.Error() != expected { if expected := "lookup 192.0.2.42: PTR target is invalid"; err == nil || err.Error() != expected {
t.Errorf("Resolver.LookupAddr returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("Resolver.LookupAddr returned unexpected error, got %q, want %q", err, expected)
} }
_, err = LookupAddr("1.2.3.4") _, err = LookupAddr("192.0.2.42")
if expected := "lookup 1.2.3.4: PTR target is invalid"; err == nil || err.Error() != expected { if expected := "lookup 192.0.2.42: PTR target is invalid"; err == nil || err.Error() != expected {
t.Errorf("LookupAddr returned unexpected error, got %q, want %q", err.Error(), expected) t.Errorf("LookupAddr returned unexpected error, got %q, want %q", err, expected)
} }
} }