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 os.Remove(mox.DataDirPath("dmarcrpt.db"))
24 err := Init()
25 tcheckf(t, err, "init")
26 defer func() {
27 err := Close()
28 tcheckf(t, err, "close")
29 }()
30
31 feedback := &dmarcrpt.Feedback{
32 ReportMetadata: dmarcrpt.ReportMetadata{
33 OrgName: "google.com",
34 Email: "noreply-dmarc-support@google.com",
35 ExtraContactInfo: "https://support.google.com/a/answer/2466580",
36 ReportID: "10051505501689795560",
37 DateRange: dmarcrpt.DateRange{
38 Begin: 1596412800,
39 End: 1596499199,
40 },
41 },
42 PolicyPublished: dmarcrpt.PolicyPublished{
43 Domain: "example.org",
44 ADKIM: "r",
45 ASPF: "r",
46 Policy: "reject",
47 SubdomainPolicy: "reject",
48 Percentage: 100,
49 },
50 Records: []dmarcrpt.ReportRecord{
51 {
52 Row: dmarcrpt.Row{
53 SourceIP: "127.0.0.1",
54 Count: 1,
55 PolicyEvaluated: dmarcrpt.PolicyEvaluated{
56 Disposition: dmarcrpt.DispositionNone,
57 DKIM: dmarcrpt.DMARCPass,
58 SPF: dmarcrpt.DMARCPass,
59 },
60 },
61 Identifiers: dmarcrpt.Identifiers{
62 HeaderFrom: "example.org",
63 },
64 AuthResults: dmarcrpt.AuthResults{
65 DKIM: []dmarcrpt.DKIMAuthResult{
66 {
67 Domain: "example.org",
68 Result: dmarcrpt.DKIMPass,
69 Selector: "example",
70 },
71 },
72 SPF: []dmarcrpt.SPFAuthResult{
73 {
74 Domain: "example.org",
75 Result: dmarcrpt.SPFPass,
76 },
77 },
78 },
79 },
80 },
81 }
82 if err := AddReport(ctxbg, feedback, dns.Domain{ASCII: "google.com"}); err != nil {
83 t.Fatalf("adding report: %s", err)
84 }
85
86 records, err := Records(ctxbg)
87 if err != nil || len(records) != 1 || !reflect.DeepEqual(&records[0].Feedback, feedback) {
88 t.Fatalf("records: got err %v, records %#v, expected no error, single record with feedback %#v", err, records, feedback)
89 }
90
91 record, err := RecordID(ctxbg, records[0].ID)
92 if err != nil || !reflect.DeepEqual(&record.Feedback, feedback) {
93 t.Fatalf("record id: got err %v, record %#v, expected feedback %#v", err, record, feedback)
94 }
95
96 start := time.Unix(1596412800, 0)
97 end := time.Unix(1596499199, 0)
98 records, err = RecordsPeriodDomain(ctxbg, start, end, "example.org")
99 if err != nil || len(records) != 1 || !reflect.DeepEqual(&records[0].Feedback, feedback) {
100 t.Fatalf("records: got err %v, records %#v, expected no error, single record with feedback %#v", err, records, feedback)
101 }
102
103 records, err = RecordsPeriodDomain(ctxbg, end, end, "example.org")
104 if err != nil || len(records) != 0 {
105 t.Fatalf("records: got err %v, records %#v, expected no error and no records", err, records)
106 }
107 records, err = RecordsPeriodDomain(ctxbg, start, end, "other.example")
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}
112