simplify dns.MockResolver, changing MockReq to just a string representing the request

similar to Authentic/Inauthentic
This commit is contained in:
Mechiel Lukkien 2023-10-12 16:07:25 +02:00
parent c095f3f39c
commit 7dce883097
No known key found for this signature in database
10 changed files with 57 additions and 54 deletions

View file

@ -460,8 +460,8 @@ test
}) })
// DNS request is failing temporarily. // DNS request is failing temporarily.
test(nil, StatusTemperror, ErrDNS, func() { test(nil, StatusTemperror, ErrDNS, func() {
resolver.Fail = map[dns.Mockreq]struct{}{ resolver.Fail = []string{
{Type: "txt", Name: "test._domainkey.mox.example."}: {}, "txt test._domainkey.mox.example.",
} }
}) })
// Claims to be DKIM through v=, but cannot be parsed. ../rfc/6376:2621 // Claims to be DKIM through v=, but cannot be parsed. ../rfc/6376:2621

View file

@ -21,8 +21,8 @@ func TestLookup(t *testing.T) {
"_dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"}, "_dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"},
"_dmarc.example.com.": {"v=DMARC1; p=none;"}, "_dmarc.example.com.": {"v=DMARC1; p=none;"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_dmarc.temperror.example."}: {}, "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.multiple.example.": {"v=DMARC1; p=none;", "v=DMARC1"},
"example.com._report._dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"}, "example.com._report._dmarc.malformed.example.": {"v=DMARC1; p=none; bogus;"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "example.com._report._dmarc.temperror.example."}: {}, "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.malformed.example.": {"v=DMARC1; p=none; bogus"},
"_dmarc.example.com.": {"v=DMARC1; p=reject"}, "_dmarc.example.com.": {"v=DMARC1; p=reject"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_dmarc.temperror.example."}: {}, "txt _dmarc.temperror.example.",
}, },
} }

View file

@ -20,20 +20,24 @@ type MockResolver struct {
MX map[string][]*net.MX MX map[string][]*net.MX
TLSA map[string][]adns.TLSA // Keys are e.g. _25._tcp.<host>. TLSA map[string][]adns.TLSA // Keys are e.g. _25._tcp.<host>.
CNAME map[string]string 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 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." Authentic []string // Like Fail, but records that cause the response to be authentic.
Inauthentic []string Inauthentic []string // Like Authentic, but making response inauthentic.
} }
type Mockreq struct { type mockReq struct {
Type string // E.g. "cname", "txt", "mx", "ptr", etc. 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.<host>. Name string // Name of request. For TLSA, the full requested DNS name, e.g. _25._tcp.<host>.
} }
func (mr mockReq) String() string {
return mr.Type + " " + mr.Name
}
var _ Resolver = MockResolver{} 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} result := adns.Result{Authentic: r.AllAuthentic}
if err := ctx.Err(); err != nil { if err := ctx.Err(); err != nil {
@ -50,14 +54,14 @@ func (r MockResolver) result(ctx context.Context, mr Mockreq) (string, adns.Resu
} }
for { for {
if _, ok := r.Fail[mr]; ok { if slices.Contains(r.Fail, mr.String()) {
updateAuthentic(mr.Type + " " + mr.Name) updateAuthentic(mr.String())
return mr.Name, adns.Result{}, r.servfail(mr.Name) return mr.Name, adns.Result{}, r.servfail(mr.Name)
} }
cname, ok := r.CNAME[mr.Name] cname, ok := r.CNAME[mr.Name]
if !ok { if !ok {
updateAuthentic(mr.Type + " " + mr.Name) updateAuthentic(mr.String())
break break
} }
updateAuthentic("cname " + mr.Name) 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) { 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) name, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return name, result, err 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) { 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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) xname := fmt.Sprintf("_%s._%s.%s", service, proto, name)
mr := Mockreq{"srv", xname} mr := mockReq{"srv", xname}
name, result, err := r.result(ctx, mr) name, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return name, nil, result, err 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) { 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. // 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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. // 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err 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) { 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) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err return nil, result, err
@ -236,7 +240,7 @@ func (r MockResolver) LookupTLSA(ctx context.Context, port int, protocol string,
} else { } else {
name = fmt.Sprintf("_%d._%s.%s", port, protocol, host) name = fmt.Sprintf("_%d._%s.%s", port, protocol, host)
} }
mr := Mockreq{"tlsa", name} mr := mockReq{"tlsa", name}
_, result, err := r.result(ctx, mr) _, result, err := r.result(ctx, mr)
if err != nil { if err != nil {
return nil, result, err return nil, result, err

View file

@ -33,11 +33,11 @@ func TestIPRev(t *testing.T) {
AAAA: map[string][]string{ AAAA: map[string][]string{
"basic6.example.": {"2001:db8::1"}, "basic6.example.": {"2001:db8::1"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "ptr", Name: "10.0.0.3"}: {}, "ptr 10.0.0.3",
{Type: "ptr", Name: "2001:db8::3"}: {}, "ptr 2001:db8::3",
{Type: "ip", Name: "temperror.example."}: {}, "ip temperror.example.",
{Type: "ip", Name: "temperror2.example."}: {}, "ip temperror2.example.",
}, },
Authentic: []string{ Authentic: []string{
"ptr 10.0.0.1", "ptr 10.0.0.1",

View file

@ -39,9 +39,9 @@ func TestLookup(t *testing.T) {
"_mta-sts.b.cnames.example.": "_mta-sts.c.cnames.example.", "_mta-sts.b.cnames.example.": "_mta-sts.c.cnames.example.",
"_mta-sts.followtemperror.example.": "_mta-sts.cnametemperror.example.", "_mta-sts.followtemperror.example.": "_mta-sts.cnametemperror.example.",
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_mta-sts.temperror.example."}: {}, "txt _mta-sts.temperror.example.",
{Type: "cname", Name: "_mta-sts.cnametemperror.example."}: {}, "cname _mta-sts.cnametemperror.example.",
}, },
} }

View file

@ -117,8 +117,8 @@ func TestDB(t *testing.T) {
"_mta-sts.other.example.com.": {"v=STSv1; id=1"}, "_mta-sts.other.example.com.": {"v=STSv1; id=1"},
"_mta-sts.temperror.example.com.": {""}, "_mta-sts.temperror.example.com.": {""},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_mta-sts.temperror.example.com."}: {}, "txt _mta-sts.temperror.example.com.",
}, },
} }

View file

@ -73,10 +73,10 @@ func TestGatherDestinations(t *testing.T) {
"danglingcname.example.": "absent.example.", // Points to missing name. "danglingcname.example.": "absent.example.", // Points to missing name.
"temperror-cname.example.": "absent.example.", "temperror-cname.example.": "absent.example.",
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "mx", Name: "temperror-mx.example."}: {}, "mx temperror-mx.example.",
{Type: "host", Name: "temperror-a.example."}: {}, "host temperror-a.example.",
{Type: "cname", Name: "temperror-cname.example."}: {}, "cname temperror-cname.example.",
}, },
Inauthentic: []string{"cname cnameinauthentic.example."}, Inauthentic: []string{"cname cnameinauthentic.example."},
} }
@ -154,9 +154,9 @@ func TestGatherIPs(t *testing.T) {
"danglingcname.example.": "absent.example.", // Points to missing name. "danglingcname.example.": "absent.example.", // Points to missing name.
"temperror-cname.example.": "absent.example.", "temperror-cname.example.": "absent.example.",
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "host", Name: "temperror-a.example."}: {}, "host temperror-a.example.",
{Type: "cname", Name: "temperror-cname.example."}: {}, "cname temperror-cname.example.",
}, },
Inauthentic: []string{"cname cnameinauthentic.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.cnameinauthentic.example.": "_25._tcp.host1.example.",
"_25._tcp.danglingcname.example.": "_25._tcp.absent.example.", // Points to missing name. "_25._tcp.danglingcname.example.": "_25._tcp.absent.example.", // Points to missing name.
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "cname", Name: "_25._tcp.temperror-cname.example."}: {}, "cname _25._tcp.temperror-cname.example.",
}, },
Inauthentic: []string{ Inauthentic: []string{
"cname _25._tcp.cnameinauthentic.example.", "cname _25._tcp.cnameinauthentic.example.",

View file

@ -22,8 +22,8 @@ func TestLookup(t *testing.T) {
"nonspf.example.": {"something else"}, "nonspf.example.": {"something else"},
"ok.example.": {"v=spf1"}, "ok.example.": {"v=spf1"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "temperror.example."}: {}, "txt temperror.example.",
}, },
} }
@ -245,7 +245,6 @@ func TestVerify(t *testing.T) {
{Host: "mail-c.example.org.", Pref: 10}, {Host: "mail-c.example.org.", Pref: 10},
}, },
}, },
Fail: map[dns.Mockreq]struct{}{},
} }
ctx := context.Background() ctx := context.Background()
@ -441,8 +440,8 @@ func TestVerifyScenarios(t *testing.T) {
"2001:db8::1": {"mail.mox.example."}, "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."}, "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{}{ Fail: []string{
{Type: "txt", Name: "temperror.example."}: {}, "txt temperror.example.",
}, },
} }

View file

@ -18,8 +18,8 @@ func TestLookup(t *testing.T) {
"_smtp._tls.malformed.example.": {"v=TLSRPTv1; bad"}, "_smtp._tls.malformed.example.": {"v=TLSRPTv1; bad"},
"_smtp._tls.other.example.": {"other"}, "_smtp._tls.other.example.": {"other"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_smtp._tls.temperror.example."}: {}, "txt _smtp._tls.temperror.example.",
}, },
} }

View file

@ -28,8 +28,8 @@ func TestUpdates(t *testing.T) {
"_updates.temperror.example.": {"v=UPDATES0; l=v0.0.1"}, "_updates.temperror.example.": {"v=UPDATES0; l=v0.0.1"},
"_updates.unknown.example.": {"v=UPDATES0; l=v0.0.1; unknown=ok"}, "_updates.unknown.example.": {"v=UPDATES0; l=v0.0.1; unknown=ok"},
}, },
Fail: map[dns.Mockreq]struct{}{ Fail: []string{
{Type: "txt", Name: "_updates.temperror.example."}: {}, "txt _updates.temperror.example.",
}, },
} }