forked from grafana/smtprelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_test.go
254 lines (221 loc) · 6.21 KB
/
relay_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"bufio"
"bytes"
"context"
"errors"
"log/slog"
"net/textproto"
"strings"
"testing"
"github.com/grafana/smtprelay/v2/internal/smtpd"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_RecepientsCheck(t *testing.T) {
registry := prometheus.NewRegistry()
err := registerMetrics(registry)
require.NoError(t, err)
tc := []struct {
name string
allowed string
denied string
expected error
emails []string
}{
{
name: "without any list, all emails are allowed",
},
{
name: "with emails not in the allow list",
emails: []string{"[email protected]"},
allowed: "(.+@example.(org|com)|[email protected])",
expected: smtpd.ErrRecipientInvalid,
},
{
name: "with emails that are denied",
denied: "(.+@example.(org|com)|[email protected])",
expected: smtpd.ErrRecipientDenied,
},
{
name: "with valid email that are not denied",
emails: []string{"[email protected]", "[email protected]"},
denied: "(.+@example.(org|com)|[email protected])",
},
{
name: "with valid email that are allowed",
emails: []string{"[email protected]", "[email protected]"},
allowed: "(.+@example.(org|com)|[email protected])",
},
{
name: "with valid email that complies with both the allowed and denied list",
emails: []string{"[email protected]", "[email protected]"},
denied: "(.+@example.(org|com)|[email protected])",
allowed: "[email protected]",
},
{
name: "with an email that is not in any of the lists",
emails: []string{"[email protected]"},
denied: "(.+@example.(org|com)|[email protected])",
allowed: "[email protected]",
expected: smtpd.ErrRecipientInvalid,
},
}
ctx := context.Background()
r := &relay{}
for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
checker := r.recipientChecker(tt.allowed, tt.denied)
for _, e := range tt.emails {
if err := checker(ctx, smtpd.Peer{}, e); err != nil {
if !errors.Is(err, tt.expected) {
t.Errorf("got %d, want %d for the email %s", err, tt.expected, e)
}
}
}
})
}
}
func TestAddLogHeaderFields(t *testing.T) {
out := &bytes.Buffer{}
logger := slog.New(slog.NewTextHandler(out, &slog.HandlerOptions{
// remove time, level, and msg for simpler testing
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
switch a.Key {
case "time", "level", "msg":
return slog.Attr{}
}
return a
},
}))
data := []byte(`Subject: test
Message-ID: 9a7f8b9c-6d1d-4b9a-8c0a-9e4b9c6d1d4b
DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=dkim; c=relaxed/relaxed;
q=dns/txt;
Date: Thu, 01 Jan 1970 00:00:00 +0000
From: Alice <[email protected]>
To: Bob <[email protected]>
This is a test message.
`)
treader := textproto.NewReader(bufio.NewReader(bytes.NewBuffer(data)))
hdr, err := treader.ReadMIMEHeader()
require.NoError(t, err)
t.Run("no logHeaders", func(t *testing.T) {
log := addLogHeaderFields(nil, logger, textproto.MIMEHeader{})
out.Reset()
log.Info("")
assert.Empty(t, strings.TrimSpace(out.String()))
})
t.Run("with logHeaders", func(t *testing.T) {
hdrs := map[string]string{"header1": "field1", "header2": "field2"}
log := addLogHeaderFields(hdrs, logger, textproto.MIMEHeader{})
out.Reset()
log.Info("")
assert.Empty(t, strings.TrimSpace(out.String()))
})
t.Run("with simple data, logHeaders not present", func(t *testing.T) {
hdrs := map[string]string{"header1": "field1", "header2": "field2"}
log := addLogHeaderFields(hdrs, logger, hdr)
out.Reset()
log.Info("")
assert.Empty(t, strings.TrimSpace(out.String()))
})
t.Run("with simple data, logHeaders found", func(t *testing.T) {
hdrs := map[string]string{"subject": "Subject", "msgid": "Message-ID"}
log := addLogHeaderFields(hdrs, logger, hdr)
out.Reset()
log.Info("")
// we can't do a straight string compare because the order of the fields
// will change
pairs := strings.Split(
strings.TrimSpace(out.String()),
" ",
)
assert.Equal(t, 2, len(pairs))
assert.Contains(t, pairs, "subject=test")
assert.Contains(t, pairs, "msgid=9a7f8b9c-6d1d-4b9a-8c0a-9e4b9c6d1d4b")
})
}
func TestAddrAllowedNoDomain(t *testing.T) {
allowedAddrs := []string{"[email protected]"}
if addrAllowed("bob.com", allowedAddrs) {
t.FailNow()
}
}
func TestAddrAllowedSingle(t *testing.T) {
allowedAddrs := []string{"[email protected]"}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
}
func TestAddrAllowedDifferentCase(t *testing.T) {
allowedAddrs := []string{"[email protected]"}
testAddrs := []string{
}
for _, addr := range testAddrs {
if !addrAllowed(addr, allowedAddrs) {
t.Errorf("Address %v not allowed, but should be", addr)
}
}
}
func TestAddrAllowedLocal(t *testing.T) {
allowedAddrs := []string{"joe"}
if !addrAllowed("joe", allowedAddrs) {
t.FailNow()
}
if addrAllowed("bob", allowedAddrs) {
t.FailNow()
}
}
func TestAddrAllowedMulti(t *testing.T) {
allowedAddrs := []string{"[email protected]", "[email protected]"}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
}
func TestAddrAllowedSingleDomain(t *testing.T) {
allowedAddrs := []string{"@abc.com"}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
}
func TestAddrAllowedMixed(t *testing.T) {
allowedAddrs := []string{"app", "[email protected]", "@appsrv.example.com"}
if !addrAllowed("app", allowedAddrs) {
t.FailNow()
}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if !addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
if addrAllowed("[email protected]", allowedAddrs) {
t.FailNow()
}
}