From 7dce883097972eccdf19c027bad7766975a5644c Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Thu, 12 Oct 2023 16:07:25 +0200 Subject: [PATCH] simplify dns.MockResolver, changing MockReq to just a string representing the request similar to Authentic/Inauthentic --- dkim/dkim_test.go | 4 ++-- dmarc/dmarc_test.go | 12 ++++++------ dns/mock.go | 40 +++++++++++++++++++++------------------ iprev/iprev_test.go | 10 +++++----- mtasts/mtasts_test.go | 6 +++--- mtastsdb/db_test.go | 4 ++-- smtpclient/gather_test.go | 18 +++++++++--------- spf/spf_test.go | 9 ++++----- tlsrpt/lookup_test.go | 4 ++-- updates/updates_test.go | 4 ++-- 10 files changed, 57 insertions(+), 54 deletions(-) diff --git a/dkim/dkim_test.go b/dkim/dkim_test.go index f16e479..6331f63 100644 --- a/dkim/dkim_test.go +++ b/dkim/dkim_test.go @@ -460,8 +460,8 @@ test }) // DNS request is failing temporarily. test(nil, StatusTemperror, ErrDNS, func() { - resolver.Fail = map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "test._domainkey.mox.example."}: {}, + resolver.Fail = []string{ + "txt test._domainkey.mox.example.", } }) // Claims to be DKIM through v=, but cannot be parsed. ../rfc/6376:2621 diff --git a/dmarc/dmarc_test.go b/dmarc/dmarc_test.go index 623d484..019ee94 100644 --- a/dmarc/dmarc_test.go +++ b/dmarc/dmarc_test.go @@ -21,8 +21,8 @@ func TestLookup(t *testing.T) { "_dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"}, "_dmarc.example.com.": {"v=DMARC1; p=none;"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_dmarc.temperror.example."}: {}, + Fail: []string{ + "txt _dmarc.temperror.example.", }, } @@ -60,8 +60,8 @@ func TestLookupExternalReportsAccepted(t *testing.T) { "example.com._report._dmarc.multiple.example.": {"v=DMARC1; p=none;", "v=DMARC1"}, "example.com._report._dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "example.com._report._dmarc.temperror.example."}: {}, + Fail: []string{ + "txt example.com._report._dmarc.temperror.example.", }, } @@ -100,8 +100,8 @@ func TestVerify(t *testing.T) { "_dmarc.malformed.example.": {"v=DMARC1; p=none; bogus"}, "_dmarc.example.com.": {"v=DMARC1; p=reject"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_dmarc.temperror.example."}: {}, + Fail: []string{ + "txt _dmarc.temperror.example.", }, } diff --git a/dns/mock.go b/dns/mock.go index 022a2c9..fa0f456 100644 --- a/dns/mock.go +++ b/dns/mock.go @@ -20,20 +20,24 @@ type MockResolver struct { MX map[string][]*net.MX TLSA map[string][]adns.TLSA // Keys are e.g. _25._tcp.. CNAME map[string]string - Fail map[Mockreq]struct{} + Fail []string // Records of the form "type name", e.g. "cname localhost." that will return a servfail. AllAuthentic bool // Default value for authentic in responses. Overridden with Authentic and Inauthentic - Authentic []string // Records of the form "type name", e.g. "cname localhost." - Inauthentic []string + Authentic []string // Like Fail, but records that cause the response to be authentic. + Inauthentic []string // Like Authentic, but making response inauthentic. } -type Mockreq struct { +type mockReq struct { Type string // E.g. "cname", "txt", "mx", "ptr", etc. Name string // Name of request. For TLSA, the full requested DNS name, e.g. _25._tcp.. } +func (mr mockReq) String() string { + return mr.Type + " " + mr.Name +} + var _ Resolver = MockResolver{} -func (r MockResolver) result(ctx context.Context, mr Mockreq) (string, adns.Result, error) { +func (r MockResolver) result(ctx context.Context, mr mockReq) (string, adns.Result, error) { result := adns.Result{Authentic: r.AllAuthentic} if err := ctx.Err(); err != nil { @@ -50,14 +54,14 @@ func (r MockResolver) result(ctx context.Context, mr Mockreq) (string, adns.Resu } for { - if _, ok := r.Fail[mr]; ok { - updateAuthentic(mr.Type + " " + mr.Name) + if slices.Contains(r.Fail, mr.String()) { + updateAuthentic(mr.String()) return mr.Name, adns.Result{}, r.servfail(mr.Name) } cname, ok := r.CNAME[mr.Name] if !ok { - updateAuthentic(mr.Type + " " + mr.Name) + updateAuthentic(mr.String()) break } updateAuthentic("cname " + mr.Name) @@ -95,7 +99,7 @@ func (r MockResolver) LookupPort(ctx context.Context, network, service string) ( } func (r MockResolver) LookupCNAME(ctx context.Context, name string) (string, adns.Result, error) { - mr := Mockreq{"cname", name} + mr := mockReq{"cname", name} name, result, err := r.result(ctx, mr) if err != nil { return name, result, err @@ -108,7 +112,7 @@ func (r MockResolver) LookupCNAME(ctx context.Context, name string) (string, adn } func (r MockResolver) LookupAddr(ctx context.Context, ip string) ([]string, adns.Result, error) { - mr := Mockreq{"ptr", ip} + mr := mockReq{"ptr", ip} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -121,7 +125,7 @@ func (r MockResolver) LookupAddr(ctx context.Context, ip string) ([]string, adns } func (r MockResolver) LookupNS(ctx context.Context, name string) ([]*net.NS, adns.Result, error) { - mr := Mockreq{"ns", name} + mr := mockReq{"ns", name} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -131,7 +135,7 @@ func (r MockResolver) LookupNS(ctx context.Context, name string) ([]*net.NS, adn func (r MockResolver) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, adns.Result, error) { xname := fmt.Sprintf("_%s._%s.%s", service, proto, name) - mr := Mockreq{"srv", xname} + mr := mockReq{"srv", xname} name, result, err := r.result(ctx, mr) if err != nil { return name, nil, result, err @@ -141,7 +145,7 @@ func (r MockResolver) LookupSRV(ctx context.Context, service, proto, name string func (r MockResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, adns.Result, error) { // todo: make closer to resolver, doing a & aaaa lookups, including their error/(in)secure status. - mr := Mockreq{"ipaddr", host} + mr := mockReq{"ipaddr", host} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -164,7 +168,7 @@ func (r MockResolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAd func (r MockResolver) LookupHost(ctx context.Context, host string) ([]string, adns.Result, error) { // todo: make closer to resolver, doing a & aaaa lookups, including their error/(in)secure status. - mr := Mockreq{"host", host} + mr := mockReq{"host", host} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -179,7 +183,7 @@ func (r MockResolver) LookupHost(ctx context.Context, host string) ([]string, ad } func (r MockResolver) LookupIP(ctx context.Context, network, host string) ([]net.IP, adns.Result, error) { - mr := Mockreq{"ip", host} + mr := mockReq{"ip", host} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -204,7 +208,7 @@ func (r MockResolver) LookupIP(ctx context.Context, network, host string) ([]net } func (r MockResolver) LookupMX(ctx context.Context, name string) ([]*net.MX, adns.Result, error) { - mr := Mockreq{"mx", name} + mr := mockReq{"mx", name} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -217,7 +221,7 @@ func (r MockResolver) LookupMX(ctx context.Context, name string) ([]*net.MX, adn } func (r MockResolver) LookupTXT(ctx context.Context, name string) ([]string, adns.Result, error) { - mr := Mockreq{"txt", name} + mr := mockReq{"txt", name} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err @@ -236,7 +240,7 @@ func (r MockResolver) LookupTLSA(ctx context.Context, port int, protocol string, } else { name = fmt.Sprintf("_%d._%s.%s", port, protocol, host) } - mr := Mockreq{"tlsa", name} + mr := mockReq{"tlsa", name} _, result, err := r.result(ctx, mr) if err != nil { return nil, result, err diff --git a/iprev/iprev_test.go b/iprev/iprev_test.go index ae65a4f..d1e2301 100644 --- a/iprev/iprev_test.go +++ b/iprev/iprev_test.go @@ -33,11 +33,11 @@ func TestIPRev(t *testing.T) { AAAA: map[string][]string{ "basic6.example.": {"2001:db8::1"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "ptr", Name: "10.0.0.3"}: {}, - {Type: "ptr", Name: "2001:db8::3"}: {}, - {Type: "ip", Name: "temperror.example."}: {}, - {Type: "ip", Name: "temperror2.example."}: {}, + Fail: []string{ + "ptr 10.0.0.3", + "ptr 2001:db8::3", + "ip temperror.example.", + "ip temperror2.example.", }, Authentic: []string{ "ptr 10.0.0.1", diff --git a/mtasts/mtasts_test.go b/mtasts/mtasts_test.go index 24a6851..cca1b1f 100644 --- a/mtasts/mtasts_test.go +++ b/mtasts/mtasts_test.go @@ -39,9 +39,9 @@ func TestLookup(t *testing.T) { "_mta-sts.b.cnames.example.": "_mta-sts.c.cnames.example.", "_mta-sts.followtemperror.example.": "_mta-sts.cnametemperror.example.", }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_mta-sts.temperror.example."}: {}, - {Type: "cname", Name: "_mta-sts.cnametemperror.example."}: {}, + Fail: []string{ + "txt _mta-sts.temperror.example.", + "cname _mta-sts.cnametemperror.example.", }, } diff --git a/mtastsdb/db_test.go b/mtastsdb/db_test.go index e3af0ee..70f048f 100644 --- a/mtastsdb/db_test.go +++ b/mtastsdb/db_test.go @@ -117,8 +117,8 @@ func TestDB(t *testing.T) { "_mta-sts.other.example.com.": {"v=STSv1; id=1"}, "_mta-sts.temperror.example.com.": {""}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_mta-sts.temperror.example.com."}: {}, + Fail: []string{ + "txt _mta-sts.temperror.example.com.", }, } diff --git a/smtpclient/gather_test.go b/smtpclient/gather_test.go index 043a129..85b0686 100644 --- a/smtpclient/gather_test.go +++ b/smtpclient/gather_test.go @@ -73,10 +73,10 @@ func TestGatherDestinations(t *testing.T) { "danglingcname.example.": "absent.example.", // Points to missing name. "temperror-cname.example.": "absent.example.", }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "mx", Name: "temperror-mx.example."}: {}, - {Type: "host", Name: "temperror-a.example."}: {}, - {Type: "cname", Name: "temperror-cname.example."}: {}, + Fail: []string{ + "mx temperror-mx.example.", + "host temperror-a.example.", + "cname temperror-cname.example.", }, Inauthentic: []string{"cname cnameinauthentic.example."}, } @@ -154,9 +154,9 @@ func TestGatherIPs(t *testing.T) { "danglingcname.example.": "absent.example.", // Points to missing name. "temperror-cname.example.": "absent.example.", }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "host", Name: "temperror-a.example."}: {}, - {Type: "cname", Name: "temperror-cname.example."}: {}, + Fail: []string{ + "host temperror-a.example.", + "cname temperror-cname.example.", }, Inauthentic: []string{"cname cnameinauthentic.example."}, } @@ -241,8 +241,8 @@ func TestGatherTLSA(t *testing.T) { "_25._tcp.cnameinauthentic.example.": "_25._tcp.host1.example.", "_25._tcp.danglingcname.example.": "_25._tcp.absent.example.", // Points to missing name. }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "cname", Name: "_25._tcp.temperror-cname.example."}: {}, + Fail: []string{ + "cname _25._tcp.temperror-cname.example.", }, Inauthentic: []string{ "cname _25._tcp.cnameinauthentic.example.", diff --git a/spf/spf_test.go b/spf/spf_test.go index 6f69237..2b320ec 100644 --- a/spf/spf_test.go +++ b/spf/spf_test.go @@ -22,8 +22,8 @@ func TestLookup(t *testing.T) { "nonspf.example.": {"something else"}, "ok.example.": {"v=spf1"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "temperror.example."}: {}, + Fail: []string{ + "txt temperror.example.", }, } @@ -245,7 +245,6 @@ func TestVerify(t *testing.T) { {Host: "mail-c.example.org.", Pref: 10}, }, }, - Fail: map[dns.Mockreq]struct{}{}, } ctx := context.Background() @@ -441,8 +440,8 @@ func TestVerifyScenarios(t *testing.T) { "2001:db8::1": {"mail.mox.example."}, "10.0.1.1": {"mx1.many-mx.example.", "mx2.many-mx.example.", "mx3.many-mx.example.", "mx4.many-mx.example.", "mx5.many-mx.example.", "mx6.many-mx.example.", "mx7.many-mx.example.", "mx8.many-mx.example.", "mx9.many-mx.example.", "mx10.many-mx.example.", "mx11.many-mx.example."}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "temperror.example."}: {}, + Fail: []string{ + "txt temperror.example.", }, } diff --git a/tlsrpt/lookup_test.go b/tlsrpt/lookup_test.go index 8d669da..6e162ef 100644 --- a/tlsrpt/lookup_test.go +++ b/tlsrpt/lookup_test.go @@ -18,8 +18,8 @@ func TestLookup(t *testing.T) { "_smtp._tls.malformed.example.": {"v=TLSRPTv1; bad"}, "_smtp._tls.other.example.": {"other"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_smtp._tls.temperror.example."}: {}, + Fail: []string{ + "txt _smtp._tls.temperror.example.", }, } diff --git a/updates/updates_test.go b/updates/updates_test.go index 9b8fa8c..4e4b644 100644 --- a/updates/updates_test.go +++ b/updates/updates_test.go @@ -28,8 +28,8 @@ func TestUpdates(t *testing.T) { "_updates.temperror.example.": {"v=UPDATES0; l=v0.0.1"}, "_updates.unknown.example.": {"v=UPDATES0; l=v0.0.1; unknown=ok"}, }, - Fail: map[dns.Mockreq]struct{}{ - {Type: "txt", Name: "_updates.temperror.example."}: {}, + Fail: []string{ + "txt _updates.temperror.example.", }, }