forked from letsencrypt/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer_test.go
431 lines (371 loc) · 12.3 KB
/
mailer_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package mail
import (
"bufio"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/mail"
"net/textproto"
"strconv"
"strings"
"testing"
"time"
"github.com/jmhodges/clock"
blog "github.com/letsencrypt/boulder/log"
"github.com/letsencrypt/boulder/metrics"
"github.com/letsencrypt/boulder/test"
)
type fakeSource struct{}
func (f fakeSource) generate() *big.Int {
return big.NewInt(1991)
}
func TestGenerateMessage(t *testing.T) {
fc := clock.NewFake()
fromAddress, _ := mail.ParseAddress("happy sender <[email protected]>")
log := blog.UseMock()
m := New("", "", "", "", nil, *fromAddress, log, metrics.NoopRegisterer, 0, 0)
m.clk = fc
m.csprgSource = fakeSource{}
messageBytes, err := m.generateMessage([]string{"[email protected]"}, "test subject", "this is the body\n")
test.AssertNotError(t, err, "Failed to generate email body")
message := string(messageBytes)
fields := strings.Split(message, "\r\n")
test.AssertEquals(t, len(fields), 12)
fmt.Println(message)
test.AssertEquals(t, fields[0], "To: \"[email protected]\"")
test.AssertEquals(t, fields[1], "From: \"happy sender\" <[email protected]>")
test.AssertEquals(t, fields[2], "Subject: test subject")
test.AssertEquals(t, fields[3], "Date: 01 Jan 70 00:00 UTC")
test.AssertEquals(t, fields[4], "Message-Id: <[email protected]>")
test.AssertEquals(t, fields[5], "MIME-Version: 1.0")
test.AssertEquals(t, fields[6], "Content-Type: text/plain; charset=UTF-8")
test.AssertEquals(t, fields[7], "Content-Transfer-Encoding: quoted-printable")
test.AssertEquals(t, fields[8], "")
test.AssertEquals(t, fields[9], "this is the body")
}
func TestFailNonASCIIAddress(t *testing.T) {
log := blog.UseMock()
fromAddress, _ := mail.ParseAddress("[email protected]")
m := New("", "", "", "", nil, *fromAddress, log, metrics.NoopRegisterer, 0, 0)
_, err := m.generateMessage([]string{"遗憾@email.com"}, "test subject", "this is the body\n")
test.AssertError(t, err, "Allowed a non-ASCII to address incorrectly")
}
func expect(t *testing.T, buf *bufio.Reader, expected string) error {
line, _, err := buf.ReadLine()
if err != nil {
t.Errorf("readline: %s expected: %s\n", err, expected)
return err
}
if string(line) != expected {
t.Errorf("Expected %s, got %s", expected, line)
return fmt.Errorf("Expected %s, got %s", expected, line)
}
return nil
}
type connHandler func(int, *testing.T, net.Conn)
func listenForever(l net.Listener, t *testing.T, handler connHandler) {
connID := 0
for {
conn, err := l.Accept()
if err != nil {
return
}
connID++
go handler(connID, t, conn)
}
}
func authenticateClient(t *testing.T, conn net.Conn) {
buf := bufio.NewReader(conn)
// we can ignore write errors because any
// failures will be caught on the connecting
// side
_, _ = conn.Write([]byte("220 smtp.example.com ESMTP\n"))
if err := expect(t, buf, "EHLO localhost"); err != nil {
return
}
_, _ = conn.Write([]byte("250-PIPELINING\n"))
_, _ = conn.Write([]byte("250-AUTH PLAIN LOGIN\n"))
_, _ = conn.Write([]byte("250 8BITMIME\n"))
// Base64 encoding of "\[email protected]\0passwd"
if err := expect(t, buf, "AUTH PLAIN AHVzZXJAZXhhbXBsZS5jb20AcGFzc3dk"); err != nil {
return
}
_, _ = conn.Write([]byte("235 2.7.0 Authentication successful\n"))
}
// The normal handler authenticates the client and then disconnects without
// further command processing. It is sufficient for TestConnect()
func normalHandler(connID int, t *testing.T, conn net.Conn) {
defer func() {
err := conn.Close()
if err != nil {
t.Errorf("conn.Close: %s", err)
}
}()
authenticateClient(t, conn)
}
// The disconnectHandler authenticates the client like the normalHandler but
// additionally processes an email flow (e.g. MAIL, RCPT and DATA commands).
// When the `connID` is <= `closeFirst` the connection is closed immediately
// after the MAIL command is received and prior to issuing a 250 response. If
// a `goodbyeMsg` is provided, it is written to the client immediately before
// closing. In this way the first `closeFirst` connections will not complete
// normally and can be tested for reconnection logic.
func disconnectHandler(closeFirst int, goodbyeMsg string) connHandler {
return func(connID int, t *testing.T, conn net.Conn) {
defer func() {
err := conn.Close()
if err != nil {
t.Errorf("conn.Close: %s", err)
}
}()
authenticateClient(t, conn)
buf := bufio.NewReader(conn)
if err := expect(t, buf, "MAIL FROM:<<[email protected]>> BODY=8BITMIME"); err != nil {
return
}
if connID <= closeFirst {
// If there was a `goodbyeMsg` specified, write it to the client before
// closing the connection. This is a good way to deliver a SMTP error
// before closing
if goodbyeMsg != "" {
_, _ = fmt.Fprintf(conn, "%s\r\n", goodbyeMsg)
fmt.Printf("Wrote goodbye msg: %s\n", goodbyeMsg)
}
fmt.Printf("Cutting off client early\n")
return
}
_, _ = conn.Write([]byte("250 Sure. Go on. \r\n"))
if err := expect(t, buf, "RCPT TO:<[email protected]>"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Tell Me More \r\n"))
if err := expect(t, buf, "DATA"); err != nil {
return
}
_, _ = conn.Write([]byte("354 Cool Data\r\n"))
_, _ = conn.Write([]byte("250 Peace Out\r\n"))
}
}
func badEmailHandler(messagesToProcess int) connHandler {
return func(_ int, t *testing.T, conn net.Conn) {
defer func() {
err := conn.Close()
if err != nil {
t.Errorf("conn.Close: %s", err)
}
}()
authenticateClient(t, conn)
buf := bufio.NewReader(conn)
if err := expect(t, buf, "MAIL FROM:<<[email protected]>> BODY=8BITMIME"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Sure. Go on. \r\n"))
if err := expect(t, buf, "RCPT TO:<[email protected]>"); err != nil {
return
}
_, _ = conn.Write([]byte("401 4.1.3 Bad recipient address syntax\r\n"))
if err := expect(t, buf, "RSET"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Ok yr rset now\r\n"))
}
}
func setup(t *testing.T) (*MailerImpl, net.Listener, func()) {
fromAddress, _ := mail.ParseAddress("[email protected]")
log := blog.UseMock()
keyPair, err := tls.LoadX509KeyPair("../test/mail-test-srv/localhost/cert.pem", "../test/mail-test-srv/localhost/key.pem")
if err != nil {
t.Fatalf("loading keypair: %s", err)
}
// Listen on port 0 to get any free available port
l, err := tls.Listen("tcp", ":0", &tls.Config{
Certificates: []tls.Certificate{keyPair},
})
if err != nil {
t.Fatalf("listen: %s", err)
}
cleanUp := func() {
err := l.Close()
if err != nil {
t.Errorf("listen.Close: %s", err)
}
}
pem, err := ioutil.ReadFile("../test/mail-test-srv/minica.pem")
if err != nil {
t.Fatalf("loading smtp root: %s", err)
}
smtpRoots := x509.NewCertPool()
ok := smtpRoots.AppendCertsFromPEM(pem)
if !ok {
t.Fatal("failed parsing SMTP root")
}
// We can look at the listener Addr() to figure out which free port was
// assigned by the operating system
addr := l.Addr().(*net.TCPAddr)
port := addr.Port
m := New(
"localhost",
strconv.Itoa(port),
"passwd",
smtpRoots,
*fromAddress,
log,
metrics.NoopRegisterer,
time.Second*2, time.Second*10)
return m, l, cleanUp
}
func TestConnect(t *testing.T) {
m, l, cleanUp := setup(t)
defer cleanUp()
go listenForever(l, t, normalHandler)
err := m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.Close()
if err != nil {
t.Errorf("Failed to clean up: %s", err)
}
}
func TestReconnectSuccess(t *testing.T) {
m, l, cleanUp := setup(t)
defer cleanUp()
const closedConns = 5
// Configure a test server that will disconnect the first `closedConns`
// connections after the MAIL cmd
go listenForever(l, t, disconnectHandler(closedConns, ""))
// With a mailer client that has a max attempt > `closedConns` we expect no
// error. The message should be delivered after `closedConns` reconnect
// attempts.
err := m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.SendMail([]string{"[email protected]"}, "You are already a winner!", "Just kidding")
if err != nil {
t.Errorf("Expected SendMail() to not fail. Got err: %s", err)
}
}
func TestBadEmailError(t *testing.T) {
m, l, cleanUp := setup(t)
defer cleanUp()
const messages = 3
go listenForever(l, t, badEmailHandler(messages))
err := m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.SendMail([]string{"[email protected]"}, "You are already a winner!", "Just kidding")
// We expect there to be an error
if err == nil {
t.Errorf("Expected SendMail() to return an RecoverableSMTPError, got nil")
}
expected := "401: 4.1.3 Bad recipient address syntax"
if rcptErr, ok := err.(RecoverableSMTPError); !ok {
t.Errorf("Expected SendMail() to return an RecoverableSMTPError, got a %T error: %v", err, err)
} else if rcptErr.Message != expected {
t.Errorf("SendMail() returned RecoverableSMTPError with wrong message. Got %q, expected %q\n",
rcptErr.Message, expected)
}
}
func TestReconnectSMTP421(t *testing.T) {
m, l, cleanUp := setup(t)
defer cleanUp()
const closedConns = 5
// A SMTP 421 can be generated when the server times out an idle connection.
// For more information see https://github.com/letsencrypt/boulder/issues/2249
smtp421 := "421 1.2.3 green.eggs.and.spam Error: timeout exceeded"
// Configure a test server that will disconnect the first `closedConns`
// connections after the MAIL cmd with a SMTP 421 error
go listenForever(l, t, disconnectHandler(closedConns, smtp421))
// With a mailer client that has a max attempt > `closedConns` we expect no
// error. The message should be delivered after `closedConns` reconnect
// attempts.
err := m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.SendMail([]string{"[email protected]"}, "You are already a winner!", "Just kidding")
if err != nil {
t.Errorf("Expected SendMail() to not fail. Got err: %s", err)
}
}
func TestOtherError(t *testing.T) {
m, l, cleanUp := setup(t)
defer cleanUp()
const messages = 3
go listenForever(l, t, func(_ int, t *testing.T, conn net.Conn) {
defer func() {
err := conn.Close()
if err != nil {
t.Errorf("conn.Close: %s", err)
}
}()
authenticateClient(t, conn)
buf := bufio.NewReader(conn)
if err := expect(t, buf, "MAIL FROM:<<[email protected]>> BODY=8BITMIME"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Sure. Go on. \r\n"))
if err := expect(t, buf, "RCPT TO:<[email protected]>"); err != nil {
return
}
_, _ = conn.Write([]byte("999 1.1.1 This would probably be bad?\r\n"))
if err := expect(t, buf, "RSET"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Ok yr rset now\r\n"))
})
err := m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.SendMail([]string{"[email protected]"}, "You are already a winner!", "Just kidding")
// We expect there to be an error
if err == nil {
t.Errorf("Expected SendMail() to return an error, got nil")
}
expected := "999 1.1.1 This would probably be bad?"
if rcptErr, ok := err.(*textproto.Error); !ok {
t.Errorf("Expected SendMail() to return an textproto.Error, got a %T error: %v", err, err)
} else if rcptErr.Error() != expected {
t.Errorf("SendMail() returned textproto.Error with wrong message. Got %q, expected %q\n",
rcptErr.Error(), expected)
}
m, l, cleanUp = setup(t)
defer cleanUp()
go listenForever(l, t, func(_ int, t *testing.T, conn net.Conn) {
defer func() {
err := conn.Close()
if err != nil {
t.Errorf("conn.Close: %s", err)
}
}()
authenticateClient(t, conn)
buf := bufio.NewReader(conn)
if err := expect(t, buf, "MAIL FROM:<<[email protected]>> BODY=8BITMIME"); err != nil {
return
}
_, _ = conn.Write([]byte("250 Sure. Go on. \r\n"))
if err := expect(t, buf, "RCPT TO:<[email protected]>"); err != nil {
return
}
_, _ = conn.Write([]byte("999 1.1.1 This would probably be bad?\r\n"))
if err := expect(t, buf, "RSET"); err != nil {
return
}
_, _ = conn.Write([]byte("nop\r\n"))
})
err = m.Connect()
if err != nil {
t.Errorf("Failed to connect: %s", err)
}
err = m.SendMail([]string{"[email protected]"}, "You are already a winner!", "Just kidding")
// We expect there to be an error
test.AssertError(t, err, "SendMail didn't fail as expected")
test.AssertEquals(t, err.Error(), "999 1.1.1 This would probably be bad? (also, on sending RSET: short response: nop)")
}