-
Notifications
You must be signed in to change notification settings - Fork 6
/
macro_test.go
156 lines (139 loc) · 3.86 KB
/
macro_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
package spf
import (
"net"
"testing"
)
const (
domain = "matching.com"
sender = "sender@domain.com"
)
var (
ip4 = net.IP{10, 11, 12, 13}
tkn = &token{mechanism: tExp, qualifier: qMinus, value: ""}
)
type MacroTest struct {
Input string
Output string
}
func TestMacroIteration(t *testing.T) {
testCases := []*MacroTest{
{"matching.com", "matching.com"},
{"%%matching.com", "%matching.com"},
{"%%matching%_%%.com", "%matching %.com"},
{"matching%-.com", "matching%20.com"},
{"%%%%%_%-", "%% %20"},
{"Please email to %{s} end",
"Please email to sender@domain.com end"},
{"Please email to %{l} end",
"Please email to sender end"},
{"Please email to %{o} end",
"Please email to domain.com end"},
{"Domain %{d} end",
"Domain matching.com end"},
{"Address IP %{i} end",
"Address IP 10.11.12.13 end"},
{"Address IP %{i1} end",
"Address IP 13 end"},
{"Address IP %{i100} end",
"Address IP 10.11.12.13 end"},
{"Address IP %{ir} end",
"Address IP 13.12.11.10 end"},
{"Address IP %{i2r} end",
"Address IP 11.10 end"},
{"Address IP %{i500r} end",
"Address IP 13.12.11.10 end"},
}
parser := newParser(sender, domain, ip4, stub, testResolver)
for _, test := range testCases {
tkn.value = test.Input
result, err := parseMacroToken(parser, tkn)
if err != nil {
t.Errorf("Macro %s evaluation failed due to returned error: %v\n",
test.Input, err)
}
if result != test.Output {
t.Errorf("Macro '%s', evaluation failed, got: '%s',\nexpected '%s'\n",
test.Input, result, test.Output)
}
}
}
// TestMacroExpansionRFCExamples will execute examples from RFC 7208, section
// 7.4
func TestMacroExpansionRFCExamples(t *testing.T) {
testCases := []*MacroTest{
{"", ""},
{"%{s}", "strong-bad@email.example.com"},
{"%{o}", "email.example.com"},
{"%{d}", "email.example.com"},
{"%{d4}", "email.example.com"},
{"%{d3}", "email.example.com"},
{"%{d2}", "example.com"},
{"%{d1}", "com"},
{"%{dr}", "com.example.email"},
{"%{d2r}", "example.email"},
{"%{l}", "strong-bad"},
{"%{l-}", "strong.bad"},
{"%{lr}", "strong-bad"},
{"%{lr-}", "bad.strong"},
{"%{l1r-}", "strong"},
{"%{ir}.%{v}._spf.%{d2}",
"3.2.0.192.in-addr._spf.example.com"},
{"%{lr-}.lp._spf.%{d2}", "bad.strong.lp._spf.example.com"},
{"%{lr-}.lp.%{ir}.%{v}._spf.%{d2}",
"bad.strong.lp.3.2.0.192.in-addr._spf.example.com"},
{"%{ir}.%{v}.%{l1r-}.lp._spf.%{d2}",
"3.2.0.192.in-addr.strong.lp._spf.example.com"},
{"%{d2}.trusted-domains.example.net",
"example.com.trusted-domains.example.net"},
}
parser := newParser("strong-bad@email.example.com",
"email.example.com", net.IP{192, 0, 2, 3}, stub, testResolver)
for _, test := range testCases {
tkn.value = test.Input
result, err := parseMacroToken(parser, tkn)
if err != nil {
t.Errorf("Macro %s evaluation failed due to returned error: %v\n",
test.Input, err)
}
if result != test.Output {
t.Errorf("Macro '%s', evaluation failed, got: '%s',\nexpected '%s'\n",
test.Input, result, test.Output)
}
}
}
// TODO(zaccone): Fill epected error messages and compare with those returned.
func TestParsingErrors(t *testing.T) {
testcases := []*MacroTest{
{"%", ""},
{"%{?", ""},
{"%}", ""},
{"%a", ""},
{"%", ""},
{"%{}", ""},
{"%{", ""},
{"%{234", ""},
{"%{2a3}", ""},
{"%{i2", ""},
{"%{s2a3}", ""},
{"%{s2i3}", ""},
{"%{s2ir-3}", ""},
{"%{l2a3}", ""},
{"%{i2a3}", ""},
{"%{o2a3}", ""},
{"%{d2a3}", ""},
{"%{i-2}", ""},
}
parser := newParser(sender, domain, ip4, stub, testResolver)
for _, test := range testcases {
tkn.value = test.Input
result, err := parseMacroToken(parser, tkn)
if result != "" {
t.Errorf("For input '%s' expected empty result, got '%s' instead\n",
test.Input, result)
}
if err == nil {
t.Errorf("For input '%s', expected non-empty err, got nil instead and result '%s'\n",
test.Input, result)
}
}
}