git » spf » commit f2bd062

tests: Add tests for some corner cases

author Alberto Bertogli
2020-07-24 18:14:55 UTC
committer Alberto Bertogli
2020-07-24 18:16:08 UTC
parent d38264e7398dda890ed638c55933224e156c307e

tests: Add tests for some corner cases

During coverage analysis, some small gaps in test coverage were
identified.

While they are not problematic today, some are worth testing explicitly
to ensure there are no regressions in the future.

spf_test.go +49 -0
testdata/blitirispf-tests.yml +32 -0

diff --git a/spf_test.go b/spf_test.go
index e7ab6d8..69d4a8b 100644
--- a/spf_test.go
+++ b/spf_test.go
@@ -401,3 +401,52 @@ func TestMacrosV4(t *testing.T) {
 func mx(host string, pref uint16) *net.MX {
 	return &net.MX{Host: host, Pref: pref}
 }
+
+func TestIPMatchHelper(t *testing.T) {
+	cases := []struct {
+		ip      net.IP
+		tomatch net.IP
+		masks   dualMasks
+		ok      bool
+		err     error
+	}{
+		{ip1111, ip1110, dualMasks{24, -1}, true, nil},
+		{ip1111, ip1111, dualMasks{-1, -1}, true, nil},
+		{ip1111, ip1110, dualMasks{-1, -1}, false, nil},
+		{ip1111, ip1110, dualMasks{32, -1}, false, nil},
+		{ip1111, ip1110, dualMasks{99, -1}, false, errInvalidMask},
+
+		{ip6666, ip6660, dualMasks{-1, 100}, true, nil},
+		{ip6666, ip6666, dualMasks{-1, -1}, true, nil},
+		{ip6666, ip6660, dualMasks{-1, -1}, false, nil},
+		{ip6666, ip6660, dualMasks{-1, 128}, false, nil},
+		{ip6666, ip6660, dualMasks{-1, 200}, false, errInvalidMask},
+	}
+	for _, c := range cases {
+		ok, err := ipMatch(c.ip, c.tomatch, c.masks)
+		if ok != c.ok || err != c.err {
+			t.Errorf("[%s %s/%v]: expected %v/%v, got %v/%v",
+				c.ip, c.tomatch, c.masks, c.ok, c.err, ok, err)
+		}
+	}
+}
+
+func TestInvalidMacro(t *testing.T) {
+	// Test that the macro expansion detects some invalid macros.
+	macros := []string{
+		"%{x}", "%{z}", "%{c}", "%{r}", "%{t}",
+	}
+	for _, macro := range macros {
+		r := resolution{
+			ip:     ip1111,
+			count:  0,
+			sender: "sender.com",
+		}
+
+		out, err := r.expandMacros(macro, "sender.com")
+		if out != "" || err != errInvalidMacro {
+			t.Errorf(`[%s]:expected ""/%v, got %q/%v`,
+				macro, errInvalidMacro, out, err)
+		}
+	}
+}
diff --git a/testdata/blitirispf-tests.yml b/testdata/blitirispf-tests.yml
index 46241e4..24c9926 100644
--- a/testdata/blitirispf-tests.yml
+++ b/testdata/blitirispf-tests.yml
@@ -82,3 +82,35 @@ zonedata:
     - PTR: ptr:lalala.com.
   ptr:lalala.com:
     - A: 1.2.3.4
+---
+description: Error on PTR forward resolution
+tests:
+  broken-ptr-forward:
+    description: |
+      Check that if during 'ptr' forward resolution we get an error, we skip
+      the domain (and consequently fail the check).
+    mailfrom: "foo@domain.net"
+    host: 1.2.3.4
+    result: softfail
+zonedata:
+  domain.net:
+    - SPF: v=spf1 ptr:lalala.com ~all
+  4.3.2.1.in-addr.arpa:
+    - PTR: lalala.com
+  lalala.com:
+    - TIMEOUT: true
+---
+description: Permanent error on 'exists' resolution
+tests:
+  exists-perm-error:
+    description: |
+      Check that if, during an 'exists' forward resolution we get an error, we
+      fail the check.
+    mailfrom: "foo@domain.net"
+    host: 1.2.3.4
+    result: softfail
+zonedata:
+  domain.net:
+    - SPF: v=spf1 exists:lalala.com ~all
+  lalala.com:
+    - SERVFAIL: true