Skip to content

Commit

Permalink
simplify dns.MockResolver, changing MockReq to just a string represen…
Browse files Browse the repository at this point in the history
…ting the request

similar to Authentic/Inauthentic
  • Loading branch information
mjl- committed Oct 12, 2023
1 parent c095f3f commit 7dce883
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions dkim/dkim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions dmarc/dmarc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down Expand Up @@ -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.",
},
}

Expand Down Expand Up @@ -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.",
},
}

Expand Down
40 changes: 22 additions & 18 deletions dns/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ type MockResolver struct {
MX map[string][]*net.MX
TLSA map[string][]adns.TLSA // Keys are e.g. _25._tcp.<host>.
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.<host>.
}

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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions iprev/iprev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions mtasts/mtasts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down
4 changes: 2 additions & 2 deletions mtastsdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down
18 changes: 9 additions & 9 deletions smtpclient/gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."},
}
Expand Down Expand Up @@ -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."},
}
Expand Down Expand Up @@ -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.",
Expand Down
9 changes: 4 additions & 5 deletions spf/spf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down Expand Up @@ -245,7 +245,6 @@ func TestVerify(t *testing.T) {
{Host: "mail-c.example.org.", Pref: 10},
},
},
Fail: map[dns.Mockreq]struct{}{},
}

ctx := context.Background()
Expand Down Expand Up @@ -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.",
},
}

Expand Down
4 changes: 2 additions & 2 deletions tlsrpt/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down
4 changes: 2 additions & 2 deletions updates/updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
}

Expand Down

0 comments on commit 7dce883

Please sign in to comment.