1package dmarcdb
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "reflect"
8 "testing"
9 "time"
10
11 "github.com/mjl-/mox/dmarcrpt"
12 "github.com/mjl-/mox/dns"
13 "github.com/mjl-/mox/mox-"
14)
15
16var ctxbg = context.Background()
17
18func TestDMARCDB(t *testing.T) {
19 mox.Shutdown = ctxbg
20 mox.ConfigStaticPath = filepath.FromSlash("../testdata/dmarcdb/mox.conf")
21 mox.MustLoadConfig(true, false)
22
23 dbpath := mox.DataDirPath("dmarcrpt.db")
24 os.MkdirAll(filepath.Dir(dbpath), 0770)
25
26 if err := Init(); err != nil {
27 t.Fatalf("init database: %s", err)
28 }
29 defer os.Remove(dbpath)
30 defer func() {
31 ReportsDB.Close()
32 ReportsDB = nil
33 }()
34
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{
42 Begin: 1596412800,
43 End: 1596499199,
44 },
45 },
46 PolicyPublished: dmarcrpt.PolicyPublished{
47 Domain: "example.org",
48 ADKIM: "r",
49 ASPF: "r",
50 Policy: "reject",
51 SubdomainPolicy: "reject",
52 Percentage: 100,
53 },
54 Records: []dmarcrpt.ReportRecord{
55 {
56 Row: dmarcrpt.Row{
57 SourceIP: "127.0.0.1",
58 Count: 1,
59 PolicyEvaluated: dmarcrpt.PolicyEvaluated{
60 Disposition: dmarcrpt.DispositionNone,
61 DKIM: dmarcrpt.DMARCPass,
62 SPF: dmarcrpt.DMARCPass,
63 },
64 },
65 Identifiers: dmarcrpt.Identifiers{
66 HeaderFrom: "example.org",
67 },
68 AuthResults: dmarcrpt.AuthResults{
69 DKIM: []dmarcrpt.DKIMAuthResult{
70 {
71 Domain: "example.org",
72 Result: dmarcrpt.DKIMPass,
73 Selector: "example",
74 },
75 },
76 SPF: []dmarcrpt.SPFAuthResult{
77 {
78 Domain: "example.org",
79 Result: dmarcrpt.SPFPass,
80 },
81 },
82 },
83 },
84 },
85 }
86 if err := AddReport(ctxbg, feedback, dns.Domain{ASCII: "google.com"}); err != nil {
87 t.Fatalf("adding report: %s", err)
88 }
89
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)
93 }
94
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)
98 }
99
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)
105 }
106
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)
110 }
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)
114 }
115}
116