git » spf » commit 79d5715

test: Test void limit lookup check on empty answers

author Alberto Bertogli
2022-08-01 20:35:30 UTC
committer Alberto Bertogli
2022-08-02 23:51:01 UTC
parent c4a8b797d0fd6d88880a41d0ad855dc933dc0ac3

test: Test void limit lookup check on empty answers

The current tests for void lookup limits only cover NXDOMAIN answers,
this patch extends that to empty answers, to make sure we treat both
cases well.

spf_test.go +34 -20

diff --git a/spf_test.go b/spf_test.go
index d1c8ae4..c2391c8 100644
--- a/spf_test.go
+++ b/spf_test.go
@@ -538,30 +538,44 @@ func TestOverrideVoidLookupLimit(t *testing.T) {
 	dns.Txt["domain2"] = []string{"v=spf1 exists:%{i}.two include:domain3"}
 	dns.Txt["domain3"] = []string{"v=spf1 exists:%{i}.three include:domain4"}
 	dns.Txt["domain4"] = []string{"v=spf1 +all"}
-	dns.Errors["1.1.1.1.one"] = nxDomainErr
-	dns.Errors["1.1.1.1.two"] = nxDomainErr
-	dns.Errors["1.1.1.1.three"] = nxDomainErr
 
-	// The default of 2
-	res, err := CheckHostWithSender(ip1111, "helo", "user@domain1")
-	if res != PermError {
-		t.Errorf("expected permerror, got %q / %q", res, err)
-	}
+	checkLimits := func() {
+		// The default of 2
+		res, err := CheckHostWithSender(ip1111, "helo", "user@domain1")
+		if res != PermError {
+			t.Errorf("expected permerror, got %q / %q", res, err)
+		}
 
-	// Set the limit to 10, which is excessive.
-	res, err = CheckHostWithSender(ip1111, "helo", "user@domain1",
-		OverrideVoidLookupLimit(10))
-	if res != Pass {
-		t.Errorf("expected pass, got %q / %q", res, err)
-	}
+		// Set the limit to 10, which is excessive.
+		res, err = CheckHostWithSender(ip1111, "helo", "user@domain1",
+			OverrideVoidLookupLimit(10))
+		if res != Pass {
+			t.Errorf("expected pass, got %q / %q", res, err)
+		}
 
-	// Set the limit to 1, which is not enough.
-	res, err = CheckHostWithSender(ip1111, "helo", "user@domain1",
-		OverrideVoidLookupLimit(1))
-	if res != PermError || err != ErrVoidLookupLimitReached {
-		t.Errorf("expected permerror/void lookup limit reached, got %q / %q",
-			res, err)
+		// Set the limit to 1, which is not enough.
+		res, err = CheckHostWithSender(ip1111, "helo", "user@domain1",
+			OverrideVoidLookupLimit(1))
+		if res != PermError || err != ErrVoidLookupLimitReached {
+			t.Errorf("expected permerror/void lookup limit reached, got %q / %q",
+				res, err)
+		}
 	}
+
+	// First, check for NXDOMAIN.
+	dns.Errors["1.1.1.1.one"] = nxDomainErr
+	dns.Errors["1.1.1.1.two"] = nxDomainErr
+	dns.Errors["1.1.1.1.three"] = nxDomainErr
+	checkLimits()
+
+	// Then, check for empty answers (after clearing the errors).
+	delete(dns.Errors, "1.1.1.1.one")
+	delete(dns.Errors, "1.1.1.1.two")
+	delete(dns.Errors, "1.1.1.1.three")
+	dns.Ip["1.1.1.1.one"] = nil
+	dns.Ip["1.1.1.1.two"] = nil
+	dns.Ip["1.1.1.1.three"] = nil
+	checkLimits()
 }
 
 func TestWithContext(t *testing.T) {