2023-01-30 16:27:06 +03:00
|
|
|
package tlsrpt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mjl-/mox/dns"
|
2023-12-05 15:35:58 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLookup(t *testing.T) {
|
2023-12-05 15:35:58 +03:00
|
|
|
log := mlog.New("tlsrpt", nil)
|
2023-01-30 16:27:06 +03:00
|
|
|
resolver := dns.MockResolver{
|
|
|
|
TXT: map[string][]string{
|
|
|
|
"_smtp._tls.basic.example.": {"v=TLSRPTv1; rua=mailto:tlsrpt@basic.example"},
|
|
|
|
"_smtp._tls.one.example.": {"v=TLSRPTv1; rua=mailto:tlsrpt@basic.example", "other"},
|
|
|
|
"_smtp._tls.multiple.example.": {"v=TLSRPTv1; rua=mailto:tlsrpt@basic.example", "v=TLSRPTv1; rua=mailto:tlsrpt@basic.example"},
|
|
|
|
"_smtp._tls.malformed.example.": {"v=TLSRPTv1; bad"},
|
|
|
|
"_smtp._tls.other.example.": {"other"},
|
|
|
|
},
|
2023-10-12 17:07:25 +03:00
|
|
|
Fail: []string{
|
|
|
|
"txt _smtp._tls.temperror.example.",
|
2023-01-30 16:27:06 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
test := func(domain string, expRecord *Record, expErr error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
d := dns.Domain{ASCII: domain}
|
2023-12-05 15:35:58 +03:00
|
|
|
record, _, err := Lookup(context.Background(), log.Logger, resolver, d)
|
2023-01-30 16:27:06 +03:00
|
|
|
if (err == nil) != (expErr == nil) || err != nil && !errors.Is(err, expErr) {
|
|
|
|
t.Fatalf("lookup, got err %#v, expected %#v", err, expErr)
|
|
|
|
}
|
|
|
|
if err == nil && !reflect.DeepEqual(record, expRecord) {
|
|
|
|
t.Fatalf("lookup, got %#v, expected %#v", record, expRecord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 22:25:06 +03:00
|
|
|
test("basic.example", &Record{Version: "TLSRPTv1", RUAs: [][]RUA{{"mailto:tlsrpt@basic.example"}}}, nil)
|
|
|
|
test("one.example", &Record{Version: "TLSRPTv1", RUAs: [][]RUA{{"mailto:tlsrpt@basic.example"}}}, nil)
|
2023-01-30 16:27:06 +03:00
|
|
|
test("multiple.example", nil, ErrMultipleRecords)
|
|
|
|
test("absent.example", nil, ErrNoRecord)
|
|
|
|
test("other.example", nil, ErrNoRecord)
|
|
|
|
test("malformed.example", nil, ErrRecordSyntax)
|
|
|
|
test("temperror.example", nil, ErrDNS)
|
|
|
|
}
|