git » spf » commit 8bc316d

Add tests for DNS errors

author Alberto Bertogli
2018-03-18 13:08:46 UTC
committer Alberto Bertogli
2018-03-18 13:16:26 UTC
parent e0516859cd94ab1733adfd2bee1e5616b503cbf3

Add tests for DNS errors

This patch extends the tests to cover DNS errors, both temporary and
permanent (which unfortunately have different semantics).

spf_test.go +68 -0

diff --git a/spf_test.go b/spf_test.go
index b385eec..76ca65c 100644
--- a/spf_test.go
+++ b/spf_test.go
@@ -270,3 +270,71 @@ func TestNoRecord(t *testing.T) {
 		}
 	}
 }
+
+func TestDNSTemporaryErrors(t *testing.T) {
+	dnsError := &net.DNSError{
+		Err:         "temporary error for testing",
+		IsTemporary: true,
+	}
+
+	// Domain "tmperr" will fail resolution with a temporary error.
+	txtErrors["tmperr"] = dnsError
+	ipErrors["tmperr"] = dnsError
+	mxErrors["tmperr"] = dnsError
+	mxResults["tmpmx"] = []*net.MX{{"tmperr", 10}}
+	addrErrors["1.1.1.1"] = dnsError
+
+	cases := []struct {
+		txt string
+		res Result
+	}{
+		{"v=spf1 include:tmperr", TempError},
+		{"v=spf1 a:tmperr", TempError},
+		{"v=spf1 mx:tmperr", TempError},
+		{"v=spf1 ptr:tmperr", TempError},
+		{"v=spf1 mx:tmpmx", TempError},
+	}
+
+	for _, c := range cases {
+		txtResults["domain"] = []string{c.txt}
+		res, err := CheckHost(ip1111, "domain")
+		if res != c.res {
+			t.Errorf("%q: expected %v, got %v (%v)",
+				c.txt, c.res, res, err)
+		}
+	}
+}
+
+func TestDNSPermanentErrors(t *testing.T) {
+	dnsError := &net.DNSError{
+		Err:         "permanent error for testing",
+		IsTemporary: false,
+	}
+
+	// Domain "tmperr" will fail resolution with a temporary error.
+	txtErrors["tmperr"] = dnsError
+	ipErrors["tmperr"] = dnsError
+	mxErrors["tmperr"] = dnsError
+	mxResults["tmpmx"] = []*net.MX{{"tmperr", 10}}
+	addrErrors["1.1.1.1"] = dnsError
+
+	cases := []struct {
+		txt string
+		res Result
+	}{
+		{"v=spf1 include:tmperr", PermError},
+		{"v=spf1 a:tmperr", Neutral},
+		{"v=spf1 mx:tmperr", Neutral},
+		{"v=spf1 ptr:tmperr", Neutral},
+		{"v=spf1 mx:tmpmx", Neutral},
+	}
+
+	for _, c := range cases {
+		txtResults["domain"] = []string{c.txt}
+		res, err := CheckHost(ip1111, "domain")
+		if res != c.res {
+			t.Errorf("%q: expected %v, got %v (%v)",
+				c.txt, c.res, res, err)
+		}
+	}
+}