-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathspf_test.go
93 lines (79 loc) · 2.03 KB
/
spf_test.go
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
package spf
import (
"testing"
)
const domain = "google.com"
type spferror struct {
domain string
raw string
}
type spftest struct {
server string
email string
result Result
}
type spfstr struct {
raw string
expected string
}
func TestNewSPF(t *testing.T) {
errorTests := []spferror{
spferror{"google.com", "somestring"},
spferror{"google.com", "v=spf1 include:_spf.google.com ~all -none"},
spferror{"google.com", "v=spf1 include:google.com"},
}
for _, expected := range errorTests {
_, err := NewSPF(expected.domain, expected.raw, 0)
if err == nil {
t.Log("Analyzing:", expected.raw)
t.Error("Expected error got nil")
}
}
}
func TestSPFTest(t *testing.T) {
tests := []spftest{
spftest{"127.0.0.1", "[email protected]", SoftFail},
spftest{"74.125.141.26", "[email protected]", Pass},
spftest{"35.190.247.0", "[email protected]", Pass},
spftest{"172.217.0.0", "info@_netblocks3.google.com", Pass},
spftest{"172.217.0.0", "[email protected]", Pass},
spftest{"1.1.1.1", "[email protected]", PermError},
}
for _, expected := range tests {
actual, err := SPFTest(expected.server, expected.email)
if err != nil {
t.Error(err)
}
if actual != expected.result {
t.Error("For", expected.server, "at", expected.email, "Expected", expected.result, "got", actual)
}
}
}
func TestSPFString(t *testing.T) {
tests := []spfstr{
spfstr{
"v=spf1 ip4:45.55.100.54 ip4:192.241.161.190 ip4:188.226.145.26 ~all",
"v=spf1 ip4:45.55.100.54 ip4:192.241.161.190 ip4:188.226.145.26 ~all",
},
spfstr{
"v=spf1 ip4:127.0.0.0/8 -ip4:127.0.0.1 ?ip4:127.0.0.2 -all",
"v=spf1 ip4:127.0.0.0/8 -ip4:127.0.0.1 ?ip4:127.0.0.2 -all",
},
spfstr{
"v=spf1 redirect=_spf.sample.invalid",
"v=spf1 redirect=_spf.sample.invalid",
},
}
for _, tcase := range tests {
s, err := NewSPF("domain", tcase.raw, 0)
if err != nil {
t.Log("Analyzing", tcase.raw)
t.Error(err)
}
r := s.SPFString()
if r != tcase.expected {
t.Log("Analyzing", tcase.raw)
t.Error("Expected", tcase.expected, "got", r)
}
}
}