Skip to content

Commit

Permalink
Merge pull request #37 from tierpod/issue_36
Browse files Browse the repository at this point in the history
Fix issue 36
  • Loading branch information
tierpod authored Mar 9, 2024
2 parents 4c37294 + 142f009 commit 7317393
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cmd/dmarc-report-converter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"time"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (o *Output) isStdout() bool {
func loadConfig(path string) (*config, error) {
var c config

data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -88,7 +88,7 @@ func loadConfig(path string) (*config, error) {
if c.Output.ExternalTemplate == "" {
return nil, fmt.Errorf("'output.external_template' must be configured to use external_template output")
}
data, err := ioutil.ReadFile(c.Output.ExternalTemplate)
data, err := os.ReadFile(c.Output.ExternalTemplate)
if err != nil {
return nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/dmarc/dmarc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ func (r *Report) CalculateStats() {
}
}
s.Failed = s.All - s.Passed
s.PassedPercent = math.Round((float64(s.Passed) / float64(s.All)) * 100)
if s.All == 0 {
s.PassedPercent = 0
} else {
s.PassedPercent = math.Round((float64(s.Passed) / float64(s.All)) * 100)
}

r.MessagesStats = *s
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/dmarc/dmarc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,65 @@ func TestReadParse(t *testing.T) {
}
}
}

// Test empty data
var xmlEmptyReport = Report{
XMLName: xml.Name{
Space: "",
Local: "feedback",
},
ReportMetadata: xmlReportMetadata,
PolicyPublished: xmlPolicyPublished,
Records: []Record{
{
Row: Row{
SourceIP: "",
Count: 0,
PolicyEvaluated: PolicyEvaluated{
Disposition: "",
DKIM: "",
SPF: "",
},
},
Identifiers: Identifiers{
HeaderFrom: "",
EnvelopeFrom: "",
},
AuthResults: AuthResults{
DKIM: DKIMAuthResult{
Domain: "",
Result: "",
Selector: "",
},
SPF: SPFAuthResult{
Domain: "",
Result: "",
Scope: "",
},
},
},
},
MessagesStats: MessagesStats{
All: 0,
Failed: 0,
Passed: 0,
PassedPercent: 0,
},
}

func TestReadParse_Empty(t *testing.T) {
testFile := "testdata/test_empty.xml"
f, err := os.Open(testFile)
if err != nil {
t.Fatalf("ReadParse(%v): %v", testFile, err)
}
defer f.Close()
out, err := ReadParse(f, false)
if err != nil {
t.Fatalf("ReadParse(%v): %v", testFile, err)
}

if !reflect.DeepEqual(out, xmlEmptyReport) {
t.Errorf("ReadParse(%v): parsed structs are invalid: %+v", testFile, out)
}
}
9 changes: 4 additions & 5 deletions pkg/dmarc/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path/filepath"
Expand All @@ -25,7 +24,7 @@ const (
// ReadParseXML reads xml data from r and parses it to Report struct. If lookupAddr is
// true, performs a reverse lookups for feedback>record>row>source_ip
func ReadParseXML(r io.Reader, lookupAddr bool) (Report, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return Report{}, err
}
Expand All @@ -46,7 +45,7 @@ func ReadParseGZIP(r io.Reader, lookupAddr bool) (Report, error) {
// unpack and check mimetype again
buf := bytes.NewBuffer(nil)
teer := io.TeeReader(gr, buf)
data, err := ioutil.ReadAll(teer)
data, err := io.ReadAll(teer)
if err != nil {
return Report{}, err
}
Expand All @@ -65,7 +64,7 @@ func ReadParseGZIP(r io.Reader, lookupAddr bool) (Report, error) {
// ReadParseZIP reads zipped xml data from r and parses it to Report struct. If lookupAddr is
// true, performs a reverse lookups for feedback>record>row>source_ip
func ReadParseZIP(r io.Reader, lookupAddr bool) (Report, error) {
zipBytes, err := ioutil.ReadAll(r)
zipBytes, err := io.ReadAll(r)
if err != nil {
return Report{}, err
}
Expand Down Expand Up @@ -103,7 +102,7 @@ func ReadParseZIP(r io.Reader, lookupAddr bool) (Report, error) {
// Report struct (if mimetype is supported).
// If lookupAddr is true, performs reverse lookups for feedback>record>row>source_ip
func ReadParse(r io.Reader, lookupAddr bool) (Report, error) {
data, err := ioutil.ReadAll(r)
data, err := io.ReadAll(r)
if err != nil {
return Report{}, err
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/dmarc/testdata/test_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<feedback>
<report_metadata>
<org_name>Test Inc.</org_name>
<email>postmaster@test</email>
<report_id>1.id.0</report_id>
<extra_contact_info>http://test/help</extra_contact_info>
<date_range>
<begin>1524182400</begin>
<end>1524268799 </end>
</date_range>
</report_metadata>
<policy_published>
<domain>test.net</domain>
<adkim>r</adkim>
<aspf>r</aspf>
<p>none</p>
<pct>100</pct>
</policy_published>
<record>
<row>
<source_ip/>
<count>0</count>
<policy_evaluated>
<disposition/>
<dkim/>
<spf/>
</policy_evaluated>
</row>
<identifiers>
<header_from/>
</identifiers>
<auth_results>
<spf>
<domain/>
<result/>
</spf>
</auth_results>
</record>
</feedback>

0 comments on commit 7317393

Please sign in to comment.