forked from zaccone/spf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspf_filter_test.go
140 lines (133 loc) · 2.72 KB
/
spf_filter_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
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
package spf_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/redsift/spf/v2"
)
func TestIsSPFCandidate(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "simple valid case",
input: "v=spf1",
expected: true,
},
{
name: "valid with colon separator",
input: "v:spf1",
expected: true,
},
{
name: "valid with whitespace before v",
input: " v=spf1",
expected: true,
},
{
name: "valid with whitespace after v",
input: "v =spf1",
expected: true,
},
{
name: "valid with whitespace around separator",
input: "v = spf1",
expected: true,
},
{
name: "valid uppercase SPF",
input: "v=SPF1",
expected: true,
},
{
name: "valid uppercase V",
input: "V=spf1",
expected: true,
},
{
name: "valid with mixed case",
input: "V=sPf1",
expected: true,
},
{
name: "valid with text before pattern",
input: "text v=spf1",
expected: true,
},
{
name: "valid with text after pattern",
input: "v=spf1 additional text",
expected: true,
},
{
name: "empty string",
input: "",
expected: false,
},
{
name: "only whitespace",
input: " ",
expected: false,
},
{
name: "missing v",
input: "=spf1",
expected: true,
},
{
name: "missing separator",
input: "vspf1",
expected: false,
},
{
name: "missing spf",
input: "v=",
expected: false,
},
{
name: "wrong separator",
input: "v-spf1",
expected: false,
},
{
name: "only v",
input: "v",
expected: false,
},
{
name: "only spf",
input: "spf",
expected: false,
},
{
name: "complex valid case with multiple parts",
input: "header v=spf1 include:_spf.example.com ~all",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := spf.IsSPFCandidate(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
func BenchmarkIsSPFCandidate(b *testing.B) {
testCases := map[string]string{
"Empty": "",
"Simple": "v=spf1",
"Complex": "v=SPF1 include:_spf.example.com ~all",
"NoSPF": "This is a long string without any SPF information in it at all",
"EmailHeader": "header.from=example.org; spf=pass (google.com: domain of [email protected] designates 12.34.56.78 as permitted sender) [email protected]",
"ExcessWhitespace": " v = spf1 ",
}
for name, tc := range testCases {
b.Run(name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
spf.IsSPFCandidate(tc)
}
})
}
}