forked from zaccone/spf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer_test.go
213 lines (200 loc) · 6.57 KB
/
lexer_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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package spf
import (
"reflect"
"testing"
)
func TestLexerNext(t *testing.T) {
spfRecord := "a:127.0.0.1"
l := &lexer{0, 0, 0, len(spfRecord), spfRecord}
for i, char := range spfRecord {
if i != l.pos {
t.Error("At position ", i, " lexer.pos is ", l.pos)
}
lexChar, _ := l.next()
if char != lexChar {
t.Error("Expected character ", char, " got ", lexChar)
}
}
if !l.eof() {
t.Error("Expected lexer to indicate EOF (didn't happen).")
}
if l.start != 0 {
t.Error("For record ", spfRecord, " lexer.start should be equal to 0")
}
}
func TestLexerScanIdent(t *testing.T) {
tests := []struct {
query string
want *token
}{
{"v=spf1", &token{mechanism: tVersion, qualifier: qPlus, value: "spf1", key: "v"}},
{"v=spf1 ", &token{mechanism: tVersion, qualifier: qPlus, value: "spf1", key: "v"}},
{"A:127.0.0.1", &token{mechanism: tA, qualifier: qPlus, value: "127.0.0.1", key: "A"}},
{"a:127.0.0.1", &token{mechanism: tA, qualifier: qPlus, value: "127.0.0.1", key: "a"}},
{"a", &token{mechanism: tA, qualifier: qPlus, value: ""}},
{"A", &token{mechanism: tA, qualifier: qPlus, value: ""}},
{"a:127.0.0.1 ", &token{mechanism: tA, qualifier: qPlus, value: "127.0.0.1", key: "a"}},
{"?a:127.0.0.1 ", &token{mechanism: tA, qualifier: qQuestionMark, value: "127.0.0.1", key: "a"}},
{"?ip6:2001::43 ", &token{mechanism: tIP6, qualifier: qQuestionMark, value: "2001::43", key: "ip6"}},
{"+ip6:::1", &token{mechanism: tIP6, qualifier: qPlus, value: "::1", key: "ip6"}},
{"^ip6:2001::4", &token{mechanism: tErr, qualifier: qErr, value: "^ip6:2001::4", key: "^ip6"}},
{"-all", &token{mechanism: tAll, qualifier: qMinus, value: ""}},
{"-all ", &token{mechanism: tAll, qualifier: qMinus, value: ""}},
{"-mx:localhost", &token{mechanism: tMX, qualifier: qMinus, value: "localhost", key: "mx"}},
{"mx", &token{mechanism: tMX, qualifier: qPlus, value: ""}},
{"a:", &token{mechanism: tErr, qualifier: qErr, value: "a:", key: "a"}},
{"?mx:localhost", &token{mechanism: tMX, qualifier: qQuestionMark, value: "localhost", key: "mx"}},
{"?random:localhost", &token{mechanism: tErr, qualifier: qErr, value: "?random:localhost", key: "random"}},
{"-:localhost", &token{mechanism: tErr, qualifier: qErr, value: "-:localhost"}},
{"", &token{mechanism: tErr, qualifier: qErr, value: ""}},
{"qowie", &token{mechanism: tErr, qualifier: qErr, value: "qowie"}},
{"~+all", &token{mechanism: tErr, qualifier: qErr, value: "~+all"}},
{"-~all", &token{mechanism: tErr, qualifier: qErr, value: "-~all"}},
{"mx", &token{mechanism: tMX, qualifier: qPlus, value: ""}},
{"mx/24", &token{mechanism: tMX, qualifier: qPlus, value: "/24", key: "mx"}},
{"~mx/24", &token{mechanism: tMX, qualifier: qTilde, value: "/24", key: "mx"}},
{"a", &token{mechanism: tA, qualifier: qPlus, value: ""}},
{"a/24", &token{mechanism: tA, qualifier: qPlus, value: "/24", key: "a"}},
{"~a/24", &token{mechanism: tA, qualifier: qTilde, value: "/24", key: "a"}},
{"xss=<script>alert('SPF-XSS')</script>", &token{mechanism: tUnknownModifier, qualifier: qPlus, value: "<script>alert('SPF-XSS')</script>", key: "xss"}},
}
for _, test := range tests {
t.Run(test.query, func(t *testing.T) {
l := &lexer{0, len(test.query), len(test.query) - 1, len(test.query), test.query}
got := l.scanIdent()
if !reflect.DeepEqual(test.want, got) {
t.Errorf("want %#v, got %#v", test.want, got)
}
})
}
}
func TestIsValidName(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"John", true},
{"john.doe", true},
{"john-doe_123", true},
{"123john", false},
{".john", false},
{"", false},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
actual := reNameRFC7208.MatchString(test.input)
if actual != test.expected {
t.Errorf("Expected %v, got %v", test.expected, actual)
}
})
}
}
func TestIsValidMacroString(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"%{s}", true},
{"Hello", true},
{"%{d10r.}", true},
{"%%", true},
{"%_", true},
{"%-", true},
{"%", false},
{"", true},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
actual := reMacroStringRFC7208.MatchString(test.input)
if actual != test.expected {
t.Errorf("Expected %v, got %v", test.expected, actual)
}
})
}
}
func TestLexFunc(t *testing.T) {
type TestPair struct {
Record string
Tokens []*token
}
versionToken := &token{mechanism: tVersion, qualifier: qPlus, value: "spf1", key: "v"}
testpairs := []TestPair{
{
"v=spf1 a:127.0.0.1",
[]*token{
versionToken,
{mechanism: tA, qualifier: qPlus, value: "127.0.0.1", key: "a"},
},
},
{
"v=spf1 ip4:127.0.0.1 -all",
[]*token{
versionToken,
{mechanism: tIP4, qualifier: qPlus, value: "127.0.0.1", key: "ip4"},
{mechanism: tAll, qualifier: qMinus, value: ""},
},
},
{
"v=spf1 -ptr:arpa.1.0.0.127 -all ",
[]*token{
versionToken,
{mechanism: tPTR, qualifier: qMinus, value: "arpa.1.0.0.127", key: "ptr"},
{mechanism: tAll, qualifier: qMinus, value: "", key: ""},
},
},
{
"v=spf1 ~ip6:2001:db8::cd30 ?all ",
[]*token{
versionToken,
{mechanism: tIP6, qualifier: qTilde, value: "2001:db8::cd30", key: "ip6"},
{mechanism: tAll, qualifier: qQuestionMark, value: ""},
},
},
{
"v=spf1 include:example.org -all ",
[]*token{
versionToken,
{mechanism: tInclude, qualifier: qPlus, value: "example.org", key: "include"},
{mechanism: tAll, qualifier: qMinus, value: ""},
},
},
{
"v=spf1 include=example.org -all ",
[]*token{
versionToken,
{mechanism: tUnknownModifier, qualifier: qPlus, value: "example.org", key: "include"},
{mechanism: tAll, qualifier: qMinus, value: ""},
},
},
{
"v=spf1 exists:%{ir}.%{l1r+-}._spf.%{d} +all",
[]*token{
versionToken,
{mechanism: tExists, qualifier: qPlus, value: "%{ir}.%{l1r+-}._spf.%{d}", key: "exists"},
{mechanism: tAll, qualifier: qPlus, value: ""},
},
},
{
"v=spf1 redirect=_spf.example.org",
[]*token{
versionToken,
{mechanism: tRedirect, qualifier: qPlus, value: "_spf.example.org", key: "redirect"},
},
},
{
"v=spf1 mx -all exp=explain._spf.%{d}",
[]*token{
versionToken,
{mechanism: tMX, qualifier: qPlus, value: ""},
{mechanism: tAll, qualifier: qMinus, value: ""},
{mechanism: tExp, qualifier: qPlus, value: "explain._spf.%{d}", key: "exp"},
},
},
}
for _, testpair := range testpairs {
ltok := lex(testpair.Record)
if !reflect.DeepEqual(testpair.Tokens, ltok) {
t.Error("Expected tokens ", testpair.Tokens, " got ", ltok)
}
}
}