Repository navigation
Expand file tree
/
Copy pathrules_test.go
More file actions
195 lines (182 loc) · 4.91 KB
/
Copy pathrules_test.go
File metadata and controls
195 lines (182 loc) · 4.91 KB
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
//
// Right now this merely tests some support functions in rules.go and
// does not attempt to test the main Decide() function. That one is
// complicated, especially if we want to test the full logic; we'd
// have to construct some rules and then drive an entire conversation
// through Decide().
package main
import (
"testing"
)
//
// Test address and host matches, since so much depends on them.
var aMatches = []struct {
a, pat string
}{
{"abc@def", "abc@def"},
{"", "<>"},
{"abc@def", "abc@"},
{"abc@def", "@def"},
{"abc", "abc@"},
{"abc@def.ghi", "@.ghi"},
{"abc@def.ghi", "@.def.ghi"},
{"anything@anything", "@"},
// verify that upper-cased addresses match (lower-cased) patterns
{"ABC@DEF", "abc@"},
{"ABC@DEF", "@def"},
}
var nMatches = []struct {
a, pat string
}{
{"abc@def", "not"},
{"abc@def", "@ghi"},
{"abc@def", "def@"},
{"anything", "<>"},
{"noat", "@"},
{"abc@zamdef.ghi", "@.def.ghi"},
{"", "@"},
{"@route", "@"},
{"broken@", "@"},
}
func TestAddrMatches(t *testing.T) {
for _, inp := range aMatches {
if !matchAddress(inp.a, inp.pat) {
t.Errorf("did not match '%s' with pattern '%s'", inp.a, inp.pat)
}
}
for _, inp := range nMatches {
if matchAddress(inp.a, inp.pat) {
t.Errorf("did match '%s' with pattern '%s'", inp.a, inp.pat)
}
}
}
// Hostname matching tests
var ahMatches = []struct {
h, pat string
}{
{"abc", "abc"},
{"abc", ".abc"},
{"abc.def", ".def"},
{"abc.def.ghi", ".ghi"},
// This is questionable, but let's go for it.
{".", "."},
}
var nhMatches = []struct {
h, pat string
}{
{"abc", "not"},
{"abc", ".not"},
{"prefabc", ".abc"},
{".", "not"},
{".", ".not"},
}
func TestHostMatches(t *testing.T) {
for _, inp := range ahMatches {
if !matchHost(inp.h, inp.pat) {
t.Errorf("did not match '%s' with pattern '%s'", inp.h, inp.pat)
}
}
for _, inp := range nhMatches {
if matchHost(inp.h, inp.pat) {
t.Errorf("did match '%s' with pattern '%s'", inp.h, inp.pat)
}
}
}
// Tests for thing -> options set for it functions in rules.go.
// Note that rparse_test.py also does a certain amount of implicit
// testing as part of its general match testing.
// Test whether various addresses yield various address options that they
// should. This is not an exhaustive test of all possibilities, especially
// of all of the various garbage addresses.
var aOpts = []struct {
addr string
opt Option
}{
{"", oZero},
{"noat", oNoat},
{"\"fred\"@jones", oQuoted | oUnqualified},
{"jim@jones", oUnqualified},
{"@jones:user@jim.bob", oRoute},
{"@j:user@jim", oRoute | oUnqualified},
{"@garbage", oGarbage | oUnqualified},
{"garbage@", oGarbage | oUnqualified},
{"<job@jim.bob", oGarbage},
{"joe..@jim.bob", oGarbage},
{"joe@@jim.bob", oGarbage},
{"joe@jim.bob\"", oGarbage},
{"joe@jim.bob>", oGarbage},
// Ideally this wouldn't happen, but oh well
{"\"joe..bob\"@jim.bob", oQuoted | oGarbage},
}
func TestAddrOpts(t *testing.T) {
c := setupContext(t)
for _, opt := range aOpts {
o := getAddrOpts(opt.addr, c)
if o != opt.opt {
t.Errorf("address '%s' evaluated to: %v instead of %v\n", opt.addr, o, opt.opt)
}
}
}
// This kind of duplicates TestHeloHas in rparse_test.go, but it turns
// out that I want to directly test what certain EHLO names generate,
// not do so indirectly by probing with helo-has rule matches.
//
// Options here don't include oEhlo; that's automatically added.
var hOpts = []struct {
host string
opt Option
}{
{"", oNone | oNodots},
{"abc.def", oZero},
{"abcdef", oNodots},
{".", oBogus | oNodots},
{"127.0.0.1", oBareip | oMyip},
{"[127.0.0.1]", oProperip | oMyip},
{"[127.100.100.100]", oProperip | oOtherip},
{"[192.168.10.3]", oProperip | oRemip},
{"1::", oBareip | oOtherip},
}
func TestHostOpts(t *testing.T) {
c := setupContext(t)
for _, opt := range hOpts {
c.heloname = opt.host
o := heloGetter(c)
if o != opt.opt|oEhlo {
t.Errorf("host '%s' evaluated to: %v instead of %v\n", opt.host, o, opt.opt|oEhlo)
}
}
}
// Test generation of DNS options for a given set of DNS results.
// We simply fake the contents of context.trans.rdns because we
// know that dnsGetter() just looks at list length.
// We borrow setupContext() from rparse_test.go.
var dOpts = []struct {
ver, nofw, inc bool
opt Option
}{
{true, false, false, oGood | oExists},
{false, true, false, oNofwd | oNodns},
{false, false, true, oInconsist | oNodns},
{false, false, false, oNodns},
{true, true, true, oExists | oNofwd | oInconsist},
{false, true, true, oNodns | oNofwd | oInconsist},
}
func TestDnsOpts(t *testing.T) {
var s = []string{"a.c"}
var choose = func(v bool) []string {
if v {
return s
}
return []string{}
}
c := setupContext(t)
for _, opt := range dOpts {
c.trans.rdns.verified = choose(opt.ver)
c.trans.rdns.nofwd = choose(opt.nofw)
c.trans.rdns.inconsist = choose(opt.inc)
o := dnsGetter(c)
if o != opt.opt {
t.Errorf("dns %v/%v/%v evaluated to: %v instead of %v\n", opt.ver, opt.nofw, opt.inc, o, opt.opt)
}
}
}