60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package mailstore
|
|
|
|
import "testing"
|
|
|
|
func TestParseDMARCRecord(t *testing.T) {
|
|
pol := parseDMARCRecord("v=DMARC1; p=reject; sp=quarantine; pct=50")
|
|
if pol.P != "reject" || pol.SP != "quarantine" || pol.Pct != 50 {
|
|
t.Fatalf("got %+v", pol)
|
|
}
|
|
}
|
|
|
|
func TestParseDMARCRecordDefaultsSPToP(t *testing.T) {
|
|
pol := parseDMARCRecord("v=DMARC1; p=quarantine")
|
|
if pol.SP != "quarantine" {
|
|
t.Fatalf("sp should default to p, got %q", pol.SP)
|
|
}
|
|
if pol.Pct != 100 {
|
|
t.Fatalf("pct should default to 100, got %d", pol.Pct)
|
|
}
|
|
}
|
|
|
|
func TestParseDMARCRecordIgnoresOutOfRangePct(t *testing.T) {
|
|
pol := parseDMARCRecord("v=DMARC1; p=reject; pct=150")
|
|
if pol.Pct != 100 {
|
|
t.Fatalf("out-of-range pct should be ignored, keeping default 100, got %d", pol.Pct)
|
|
}
|
|
}
|
|
|
|
func TestEffectivePolicyUsesPForOrgDomain(t *testing.T) {
|
|
pol := &DMARCPolicy{P: "reject", SP: "quarantine"}
|
|
if got := pol.EffectivePolicy("example.com", "example.com"); got != "reject" {
|
|
t.Fatalf("got %q, want p applied for the organizational domain itself", got)
|
|
}
|
|
}
|
|
|
|
func TestEffectivePolicyUsesSPForSubdomain(t *testing.T) {
|
|
pol := &DMARCPolicy{P: "reject", SP: "quarantine"}
|
|
if got := pol.EffectivePolicy("mail.example.com", "example.com"); got != "quarantine" {
|
|
t.Fatalf("got %q, want sp applied for a strict subdomain", got)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationalDomainStripsSubdomain(t *testing.T) {
|
|
if got := OrganizationalDomain("mail.example.com"); got != "example.com" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationalDomainHandlesMultiPartTLD(t *testing.T) {
|
|
if got := OrganizationalDomain("mail.example.co.uk"); got != "example.co.uk" {
|
|
t.Fatalf("got %q, want the public-suffix-aware organizational domain", got)
|
|
}
|
|
}
|
|
|
|
func TestLookupDMARCPolicyEmptyDomain(t *testing.T) {
|
|
if pol := LookupDMARCPolicy(""); pol != nil {
|
|
t.Fatalf("expected nil policy for empty domain, got %+v", pol)
|
|
}
|
|
}
|