File: spf_test.go

package info (click to toggle)
golang-blitiri-go-spf 0.0%2Bgit20170821.0.33aa985-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 68 kB
  • ctags: 51
  • sloc: makefile: 2
file content (182 lines) | stat: -rw-r--r-- 4,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package spf

import (
	"flag"
	"fmt"
	"net"
	"os"
	"testing"
)

var txtResults = map[string][]string{}
var txtErrors = map[string]error{}

func LookupTXT(domain string) (txts []string, err error) {
	return txtResults[domain], txtErrors[domain]
}

var mxResults = map[string][]*net.MX{}
var mxErrors = map[string]error{}

func LookupMX(domain string) (mxs []*net.MX, err error) {
	return mxResults[domain], mxErrors[domain]
}

var ipResults = map[string][]net.IP{}
var ipErrors = map[string]error{}

func LookupIP(host string) (ips []net.IP, err error) {
	return ipResults[host], ipErrors[host]
}

var addrResults = map[string][]string{}
var addrErrors = map[string]error{}

func LookupAddr(host string) (addrs []string, err error) {
	return addrResults[host], addrErrors[host]
}

func TestMain(m *testing.M) {
	lookupTXT = LookupTXT
	lookupMX = LookupMX
	lookupIP = LookupIP
	lookupAddr = LookupAddr

	flag.Parse()
	os.Exit(m.Run())
}

var ip1110 = net.ParseIP("1.1.1.0")
var ip1111 = net.ParseIP("1.1.1.1")
var ip6666 = net.ParseIP("2001:db8::68")
var ip6660 = net.ParseIP("2001:db8::0")

func TestBasic(t *testing.T) {
	cases := []struct {
		txt string
		res Result
	}{
		{"", None},
		{"blah", None},
		{"v=spf1", Neutral},
		{"v=spf1 ", Neutral},
		{"v=spf1 -", PermError},
		{"v=spf1 all", Pass},
		{"v=spf1  +all", Pass},
		{"v=spf1 -all ", Fail},
		{"v=spf1 ~all", SoftFail},
		{"v=spf1 ?all", Neutral},
		{"v=spf1 a ~all", SoftFail},
		{"v=spf1 a/24", Neutral},
		{"v=spf1 a:d1110/24", Pass},
		{"v=spf1 a:d1110", Neutral},
		{"v=spf1 a:d1111", Pass},
		{"v=spf1 a:nothing/24", Neutral},
		{"v=spf1 mx", Neutral},
		{"v=spf1 mx/24", Neutral},
		{"v=spf1 mx:a/montoto ~all", PermError},
		{"v=spf1 mx:d1110/24 ~all", Pass},
		{"v=spf1 ip4:1.2.3.4 ~all", SoftFail},
		{"v=spf1 ip6:12 ~all", PermError},
		{"v=spf1 ip4:1.1.1.1 -all", Pass},
		{"v=spf1 ptr -all", Pass},
		{"v=spf1 ptr:d1111 -all", Pass},
		{"v=spf1 ptr:lalala -all", Pass},
		{"v=spf1 blah", PermError},
	}

	ipResults["d1111"] = []net.IP{ip1111}
	ipResults["d1110"] = []net.IP{ip1110}
	mxResults["d1110"] = []*net.MX{{"d1110", 5}, {"nothing", 10}}
	addrResults["1.1.1.1"] = []string{"lalala.", "domain.", "d1111."}

	for _, c := range cases {
		txtResults["domain"] = []string{c.txt}
		res, err := CheckHost(ip1111, "domain")
		if (res == TempError || res == PermError) && (err == nil) {
			t.Errorf("%q: expected error, got nil", c.txt)
		}
		if res != c.res {
			t.Errorf("%q: expected %q, got %q", c.txt, c.res, res)
			t.Logf("%q:   error: %v", c.txt, err)
		}
	}
}

func TestIPv6(t *testing.T) {
	cases := []struct {
		txt string
		res Result
	}{
		{"v=spf1 all", Pass},
		{"v=spf1 a ~all", SoftFail},
		{"v=spf1 a/24", Neutral},
		{"v=spf1 a:d6660/24", Pass},
		{"v=spf1 a:d6660", Neutral},
		{"v=spf1 a:d6666", Pass},
		{"v=spf1 a:nothing/24", Neutral},
		{"v=spf1 mx:d6660/24 ~all", Pass},
		{"v=spf1 ip6:2001:db8::68 ~all", Pass},
		{"v=spf1 ip6:2001:db8::1/24 ~all", Pass},
		{"v=spf1 ip6:2001:db8::1/100 ~all", Pass},
		{"v=spf1 ptr -all", Pass},
		{"v=spf1 ptr:d6666 -all", Pass},
		{"v=spf1 ptr:sonlas6 -all", Pass},
	}

	ipResults["d6666"] = []net.IP{ip6666}
	ipResults["d6660"] = []net.IP{ip6660}
	mxResults["d6660"] = []*net.MX{{"d6660", 5}, {"nothing", 10}}
	addrResults["2001:db8::68"] = []string{"sonlas6.", "domain.", "d6666."}

	for _, c := range cases {
		txtResults["domain"] = []string{c.txt}
		res, err := CheckHost(ip6666, "domain")
		if (res == TempError || res == PermError) && (err == nil) {
			t.Errorf("%q: expected error, got nil", c.txt)
		}
		if res != c.res {
			t.Errorf("%q: expected %q, got %q", c.txt, c.res, res)
			t.Logf("%q:   error: %v", c.txt, err)
		}
	}
}

func TestNotSupported(t *testing.T) {
	cases := []string{
		"v=spf1 exists:blah -all",
		"v=spf1 exp=blah -all",
		"v=spf1 a:%{o} -all",
	}

	for _, txt := range cases {
		txtResults["domain"] = []string{txt}
		res, err := CheckHost(ip1111, "domain")
		if res != Neutral {
			t.Errorf("%q: expected neutral, got %v", txt, res)
			t.Logf("%q:   error: %v", txt, err)
		}
	}
}

func TestRecursion(t *testing.T) {
	txtResults["domain"] = []string{"v=spf1 include:domain ~all"}

	res, err := CheckHost(ip1111, "domain")
	if res != PermError {
		t.Errorf("expected permerror, got %v (%v)", res, err)
	}
}

func TestNoRecord(t *testing.T) {
	txtResults["d1"] = []string{""}
	txtResults["d2"] = []string{"loco", "v=spf2"}
	txtErrors["nospf"] = fmt.Errorf("no such domain")

	for _, domain := range []string{"d1", "d2", "d3", "nospf"} {
		res, err := CheckHost(ip1111, domain)
		if res != None {
			t.Errorf("expected none, got %v (%v)", res, err)
		}
	}
}