Skip to content

Commit

Permalink
chore: rename structs and add expected counts to test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
p5 committed Jul 19, 2024
1 parent 15afb9b commit 1cd641a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
12 changes: 6 additions & 6 deletions pkg/dmarc/rua.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"net/netip"
)

func (f *Feedback) Parse(data []byte) error {
func (f *RUA) ParseXML(data []byte) error {
return xml.Unmarshal(data, f)
}

// Feedback represents the top-level structure of a DMARC feedback report
type Feedback struct {
// RUA represents the top-level structure of a DMARC RUA report
type RUA struct {
ReportMetadata ReportMetadata `xml:"report_metadata"`
PolicyPublished PolicyPublished `xml:"policy_published"`
Records []Record `xml:"record"`
Expand All @@ -25,10 +25,10 @@ type ReportMetadata struct {
DateRange DateRange `xml:"date_range"`
}

// DateRange represents the date range for the DMARC report
// DateRange represents the date range for the DMARC report (in Unix time)
type DateRange struct {
Begin int `xml:"begin"`
End int `xml:"end"`
Begin int64 `xml:"begin"`
Enc int64 `xml:"end"`
}

// PolicyPublished contains information about the DMARC policy published by the domain owner
Expand Down
63 changes: 52 additions & 11 deletions pkg/dmarc/rua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ import (
)

type TestCases struct {
FileName string
Valid bool
FileName string
Valid bool
PassCount int
QuarantineCount int
RejectCount int
}

func TestUnmarshalXml(t *testing.T) {
func TestParseXML(t *testing.T) {
testCases := []TestCases{
{
FileName: "./testdata/00-empty-valid.xml",
Valid: true,
FileName: "./testdata/00-empty-valid.xml",
Valid: true,
PassCount: 0,
QuarantineCount: 0,
RejectCount: 0,
},
{
FileName: "./testdata/01-multiple-valid.xml",
Valid: true,
FileName: "./testdata/01-multiple-valid.xml",
Valid: true,
PassCount: 2,
QuarantineCount: 1,
RejectCount: 4,
},
{
FileName: "./testdata/02-single-valid.xml",
Valid: true,
FileName: "./testdata/02-single-valid.xml",
Valid: true,
PassCount: 1,
QuarantineCount: 0,
RejectCount: 0,
},
{
FileName: "./testdata/03-invalid.xml",
Expand All @@ -39,16 +51,45 @@ func TestUnmarshalXml(t *testing.T) {
}

// Create a Feedback object
var feedback Feedback
var feedback RUA

// Parse the XML
err = feedback.Parse(data)
err = feedback.ParseXML(data)

if tc.Valid && err != nil {
t.Errorf("expected file %s to be valid, but got error: %v", tc.FileName, err)
} else if !tc.Valid && err == nil {
t.Errorf("expected file %s to be invalid, but parsing succeeded", tc.FileName)
}

if tc.Valid {
// Get the total count of emails for each disposition
passCount := 0
quarantineCount := 0
rejectCount := 0
for _, record := range feedback.Records {
switch record.Row.PolicyEvaluated.Disposition {
case "none":
passCount += record.Row.Count
case "quarantine":
quarantineCount += record.Row.Count
case "reject":
rejectCount += record.Row.Count
}
}

if passCount != tc.PassCount {
t.Errorf("expected pass count to be %d, got %d", tc.PassCount, passCount)
}

if quarantineCount != tc.QuarantineCount {
t.Errorf("expected quarantine count to be %d, got %d", tc.QuarantineCount, quarantineCount)
}

if rejectCount != tc.RejectCount {
t.Errorf("expected reject count to be %d, got %d", tc.RejectCount, rejectCount)
}
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/dmarc/testdata/01-multiple-valid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<source_ip>5.42.104.137</source_ip>
<count>1</count>
<policy_evaluated>
<disposition>reject</disposition>
<disposition>quarantine</disposition>
<dkim>fail</dkim>
<spf>fail</spf>
</policy_evaluated>
Expand Down

0 comments on commit 1cd641a

Please sign in to comment.