11 "github.com/mjl-/mox/dmarcrpt"
12 "github.com/mjl-/mox/dns"
13 "github.com/mjl-/mox/mox-"
16var ctxbg = context.Background()
18func TestDMARCDB(t *testing.T) {
20 mox.ConfigStaticPath = filepath.FromSlash("../testdata/dmarcdb/mox.conf")
21 mox.MustLoadConfig(true, false)
23 dbpath := mox.DataDirPath("dmarcrpt.db")
24 os.MkdirAll(filepath.Dir(dbpath), 0770)
26 if err := Init(); err != nil {
27 t.Fatalf("init database: %s", err)
29 defer os.Remove(dbpath)
35 feedback := &dmarcrpt.Feedback{
36 ReportMetadata: dmarcrpt.ReportMetadata{
37 OrgName: "google.com",
38 Email: "noreply-dmarc-support@google.com",
39 ExtraContactInfo: "https://support.google.com/a/answer/2466580",
40 ReportID: "10051505501689795560",
41 DateRange: dmarcrpt.DateRange{
46 PolicyPublished: dmarcrpt.PolicyPublished{
47 Domain: "example.org",
51 SubdomainPolicy: "reject",
54 Records: []dmarcrpt.ReportRecord{
57 SourceIP: "127.0.0.1",
59 PolicyEvaluated: dmarcrpt.PolicyEvaluated{
60 Disposition: dmarcrpt.DispositionNone,
61 DKIM: dmarcrpt.DMARCPass,
62 SPF: dmarcrpt.DMARCPass,
65 Identifiers: dmarcrpt.Identifiers{
66 HeaderFrom: "example.org",
68 AuthResults: dmarcrpt.AuthResults{
69 DKIM: []dmarcrpt.DKIMAuthResult{
71 Domain: "example.org",
72 Result: dmarcrpt.DKIMPass,
76 SPF: []dmarcrpt.SPFAuthResult{
78 Domain: "example.org",
79 Result: dmarcrpt.SPFPass,
86 if err := AddReport(ctxbg, feedback, dns.Domain{ASCII: "google.com"}); err != nil {
87 t.Fatalf("adding report: %s", err)
90 records, err := Records(ctxbg)
91 if err != nil || len(records) != 1 || !reflect.DeepEqual(&records[0].Feedback, feedback) {
92 t.Fatalf("records: got err %v, records %#v, expected no error, single record with feedback %#v", err, records, feedback)
95 record, err := RecordID(ctxbg, records[0].ID)
96 if err != nil || !reflect.DeepEqual(&record.Feedback, feedback) {
97 t.Fatalf("record id: got err %v, record %#v, expected feedback %#v", err, record, feedback)
100 start := time.Unix(1596412800, 0)
101 end := time.Unix(1596499199, 0)
102 records, err = RecordsPeriodDomain(ctxbg, start, end, "example.org")
103 if err != nil || len(records) != 1 || !reflect.DeepEqual(&records[0].Feedback, feedback) {
104 t.Fatalf("records: got err %v, records %#v, expected no error, single record with feedback %#v", err, records, feedback)
107 records, err = RecordsPeriodDomain(ctxbg, end, end, "example.org")
108 if err != nil || len(records) != 0 {
109 t.Fatalf("records: got err %v, records %#v, expected no error and no records", err, records)
111 records, err = RecordsPeriodDomain(ctxbg, start, end, "other.example")
112 if err != nil || len(records) != 0 {
113 t.Fatalf("records: got err %v, records %#v, expected no error and no records", err, records)