-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmtp_test.go
249 lines (211 loc) · 5.98 KB
/
smtp_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
// mailpopbox
// Copyright 2020 Blue Static <https://www.bluestatic.org>
// This program is free software licensed under the GNU General Public License,
// version 3.0. The full text of the license can be found in LICENSE.txt.
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/mail"
"os"
"path/filepath"
"strings"
"testing"
"go.uber.org/zap"
"src.bluestatic.org/mailpopbox/smtp"
)
func TestVerifyAddress(t *testing.T) {
dir, err := ioutil.TempDir("", "maildrop")
if err != nil {
t.Errorf("Failed to create temp dir: %v", err)
return
}
defer os.RemoveAll(dir)
s := smtpServer{
config: Config{
Hostname: "mx.example.com",
Servers: []Server{
{
Domain: "example.com",
MaildropPath: dir,
BlockedAddresses: []string{
},
},
},
},
log: zap.NewNop(),
}
if s.VerifyAddress(mail.Address{Address: "[email protected]"}) != smtp.ReplyOK {
t.Errorf("Valid mailbox is not reported to be valid")
}
if s.VerifyAddress(mail.Address{Address: "[email protected]"}) != smtp.ReplyOK {
t.Errorf("Valid mailbox is not reported to be valid")
}
if s.VerifyAddress(mail.Address{Address: "[email protected]"}) == smtp.ReplyOK {
t.Errorf("Invalid mailbox reports to be valid")
}
if s.VerifyAddress(mail.Address{Address: "[email protected]"}) == smtp.ReplyOK {
t.Errorf("Invalid mailbox reports to be valid")
}
if s.VerifyAddress(mail.Address{Address: "unknown"}) == smtp.ReplyOK {
t.Errorf("Invalid mailbox reports to be valid")
}
if s.VerifyAddress(mail.Address{Address: "[email protected]"}) == smtp.ReplyOK {
t.Errorf("Blocked mailbox reports to be valid")
}
}
func TestMessageDelivery(t *testing.T) {
dir, err := ioutil.TempDir("", "maildrop")
if err != nil {
t.Errorf("Failed to create temp dir: %v", err)
return
}
defer os.RemoveAll(dir)
s := smtpServer{
config: Config{
Hostname: "mx.example.com",
Servers: []Server{
{
Domain: "example.com",
MaildropPath: dir,
},
},
},
log: zap.NewNop(),
}
env := smtp.Envelope{
MailFrom: mail.Address{Address: "[email protected]"},
RcptTo: []mail.Address{{Address: "[email protected]"}},
Data: []byte("Hello, world"),
ID: "msgid",
}
if rl := s.DeliverMessage(env); rl != nil {
t.Errorf("Failed to deliver message: %v", rl)
}
f, err := os.Open(filepath.Join(dir, "msgid.msg"))
if err != nil {
t.Errorf("Failed to open delivered message: %v", err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("Failed to read message: %v", err)
}
if !bytes.Contains(data, env.Data) {
t.Errorf("Could not find expected data in message")
}
}
func TestAuthenticate(t *testing.T) {
server := smtpServer{
config: Config{
Servers: []Server{
Server{
Domain: "domain1.net",
MailboxPassword: "d1",
},
Server{
Domain: "domain2.xyz",
MailboxPassword: "d2",
},
},
},
}
authTests := []struct {
authz, authc, passwd string
ok bool
}{
{"[email protected]", "[email protected]", "d1", true},
{"", "[email protected]", "d1", true},
{"[email protected]", "[email protected]", "d1", false},
{"[email protected]", "[email protected]", "d2", false},
{"[email protected]", "[email protected]", "d2", true},
{"invalid", "[email protected]", "d2", false},
{"", "[email protected]", "d2", true},
{"", "", "", false},
}
for i, test := range authTests {
actual := server.Authenticate(test.authz, test.authc, test.passwd)
if actual != test.ok {
t.Errorf("Test %d, got %v, expected %v", i, actual, test.ok)
}
}
}
type testMTA struct {
relayed chan smtp.Envelope
}
func (m *testMTA) RelayMessage(en smtp.Envelope) {
m.relayed <- en
}
func newTestMTA() *testMTA {
return &testMTA{
relayed: make(chan smtp.Envelope),
}
}
func TestBasicRelay(t *testing.T) {
mta := newTestMTA()
server := smtpServer{
mta: mta,
log: zap.NewNop(),
}
buf := new(bytes.Buffer)
fmt.Fprintln(buf, "From: <[email protected]>\r")
fmt.Fprintln(buf, "To: <[email protected]>\r")
fmt.Fprintf(buf, "Subject: Basic relay\n\n")
fmt.Fprintln(buf, "This is a basic relay message")
en := smtp.Envelope{
MailFrom: mail.Address{Address: "[email protected]"},
RcptTo: []mail.Address{{Address: "[email protected]"}},
Data: buf.Bytes(),
ID: "id1",
}
server.RelayMessage(en, en.MailFrom.Address)
relayed := <-mta.relayed
if !bytes.Equal(relayed.Data, en.Data) {
t.Errorf("Relayed message data does not match")
}
}
func TestSendAsRelay(t *testing.T) {
mta := newTestMTA()
server := smtpServer{
mta: mta,
log: zap.NewNop(),
}
buf := new(bytes.Buffer)
fmt.Fprintln(buf, "Received: msg from wherever")
fmt.Fprintln(buf, "From: <[email protected]>")
fmt.Fprintln(buf, "To: <[email protected]>")
fmt.Fprintf(buf, "Subject: Send-as relay [sendas:source]\n\n")
fmt.Fprintln(buf, "We've switched the senders!")
en := smtp.Envelope{
MailFrom: mail.Address{Address: "[email protected]"},
RcptTo: []mail.Address{{Address: "[email protected]"}},
Data: buf.Bytes(),
ID: "id1",
}
server.RelayMessage(en, en.MailFrom.Address)
relayed := <-mta.relayed
replaced := "[email protected]"
original := "[email protected]"
if want, got := replaced, relayed.MailFrom.Address; want != got {
t.Errorf("Want mail to be from %q, got %q", want, got)
}
if want, got := 1, len(relayed.RcptTo); want != got {
t.Errorf("Want %d recipient, got %d", want, got)
}
if want, got := "[email protected]", relayed.RcptTo[0].Address; want != got {
t.Errorf("Unexpected RcptTo %q", got)
}
msg := string(relayed.Data)
if strings.Index(msg, original) != -1 {
t.Errorf("Should not find %q in message %q", original, msg)
}
if strings.Index(msg, "\nFrom: <[email protected]>\n") == -1 {
t.Errorf("Could not find From: header in message %q", msg)
}
if strings.Index(msg, "\nSubject: Send-as relay \n") == -1 {
t.Errorf("Could not find modified Subject: header in message %q", msg)
}
}