forked from emersion/go-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
585 lines (472 loc) · 13.2 KB
/
server_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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package smtp_test
import (
"bufio"
"errors"
"io"
"io/ioutil"
"net"
"strings"
"testing"
"github.com/emersion/go-smtp"
)
type message struct {
From string
To []string
Data []byte
}
type backend struct {
messages []*message
anonmsgs []*message
userErr error
}
func (be *backend) Login(username, password string) (smtp.User, error) {
if be.userErr != nil {
return &user{}, be.userErr
}
if username != "username" || password != "password" {
return nil, errors.New("Invalid username or password")
}
return &user{backend: be}, nil
}
func (be *backend) AnonymousLogin() (smtp.User, error) {
if be.userErr != nil {
return &user{}, be.userErr
}
return &user{backend: be, anonymous: true}, nil
}
type user struct {
backend *backend
anonymous bool
}
func (u *user) Send(from string, to []string, r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
msg := &message{
From: from,
To: to,
Data: b,
}
if u.anonymous {
u.backend.anonmsgs = append(u.backend.anonmsgs, msg)
} else {
u.backend.messages = append(u.backend.messages, msg)
}
}
return nil
}
func (u *user) Logout() error {
return nil
}
type serverConfigureFunc func(*smtp.Server)
var (
authDisabled = func(s *smtp.Server) {
s.AuthDisabled = true
}
)
func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
be = new(backend)
s = smtp.NewServer(be)
s.Domain = "localhost"
s.AllowInsecureAuth = true
for _, f := range fn {
f(s)
}
go s.Serve(l)
c, err = net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
scanner = bufio.NewScanner(c)
return
}
func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServer(t, fn...)
scanner.Scan()
if scanner.Text() != "220 localhost ESMTP Service Ready" {
t.Fatal("Invalid greeting:", scanner.Text())
}
return
}
func testServerEhlo(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner, caps map[string]bool) {
be, s, c, scanner = testServerGreeted(t, fn...)
io.WriteString(c, "EHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid EHLO response:", scanner.Text())
}
expectedCaps := []string{"PIPELINING", "8BITMIME"}
caps = make(map[string]bool)
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
caps[strings.TrimPrefix(s, "250 ")] = true
break
} else {
if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
caps[strings.TrimPrefix(s, "250-")] = true
}
}
for _, cap := range expectedCaps {
if !caps[cap] {
t.Fatal("Missing capability:", cap)
}
}
return
}
func TestServer_helo(t *testing.T) {
_, s, c, scanner := testServerGreeted(t)
defer s.Close()
io.WriteString(c, "HELO localhost\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid HELO response:", scanner.Text())
}
}
func testServerAuthenticated(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner, caps := testServerEhlo(t)
if _, ok := caps["AUTH PLAIN"]; !ok {
t.Fatal("AUTH PLAIN capability is missing when auth is enabled")
}
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "334 " {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "235 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
return
}
func TestServerEmptyFrom1(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
return
}
func TestServerEmptyFrom2(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<>\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
return
}
func TestServerBadESMTPVar(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> RABBIT\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
return
}
func TestServerBadSize(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> SIZE=rabbit\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
return
}
func TestServerTooBig(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> SIZE=4294967295\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
return
}
func TestServerEmptyTo(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
return
}
func TestServer(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "DATA\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "354 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 1 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
msg := be.messages[0]
if msg.From != "[email protected]" {
t.Fatal("Invalid mail sender:", msg.From)
}
if len(msg.To) != 1 || msg.To[0] != "[email protected]" {
t.Fatal("Invalid mail recipients:", msg.To)
}
if string(msg.Data) != "Hey <3\n" {
t.Fatal("Invalid mail data:", string(msg.Data))
}
}
func TestServer_authDisabled(t *testing.T) {
_, s, c, scanner, caps := testServerEhlo(t, authDisabled)
defer s.Close()
defer c.Close()
if _, ok := caps["AUTH PLAIN"]; ok {
t.Fatal("AUTH PLAIN capability is present when auth is disabled")
}
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "500 Syntax error, AUTH command unrecognized" {
t.Fatal("Invalid AUTH response with auth disabled:", scanner.Text())
}
}
func TestServer_otherCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
io.WriteString(c, "HELP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "502 ") {
t.Fatal("Invalid HELP response:", scanner.Text())
}
io.WriteString(c, "VRFY\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "252 ") {
t.Fatal("Invalid VRFY response:", scanner.Text())
}
io.WriteString(c, "NOOP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid NOOP response:", scanner.Text())
}
io.WriteString(c, "RSET\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RSET response:", scanner.Text())
}
io.WriteString(c, "QUIT\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "221 ") {
t.Fatal("Invalid QUIT response:", scanner.Text())
}
}
func TestServer_tooManyInvalidCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
// Let's assume XXXX is a non-existing command
for i := 0; i < 4; i++ {
io.WriteString(c, "XXXX\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
func TestServer_tooLongMessage(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
s.MaxMessageBytes = 50
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "This is a very long message.\r\n")
io.WriteString(c, "Much longer than you can possibly imagine.\r\n")
io.WriteString(c, "And much longer than the server's MaxMessageBytes.\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "552 ") {
t.Fatal("Invalid DATA response, expected an error but got:", scanner.Text())
}
}
func TestServer_anonymousUserError(t *testing.T) {
be, s, c, scanner, _ := testServerEhlo(t)
defer s.Close()
defer c.Close()
be.userErr = smtp.ErrAuthRequired
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if scanner.Text() != "502 Please authenticate first" {
t.Fatal("Backend refused anonymous mail but client was permitted:", scanner.Text())
}
}
func TestServer_anonymousUserOK(t *testing.T) {
be, s, c, scanner, _ := testServerEhlo(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM: [email protected]\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
func testStrictServer(t *testing.T) (s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
s = smtp.NewServer(new(backend))
s.Domain = "localhost"
s.AllowInsecureAuth = true
s.AuthDisabled = true
s.Strict = true
go s.Serve(l)
c, err = net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
scanner = bufio.NewScanner(c)
scanner.Scan()
if scanner.Text() != "220 localhost ESMTP Service Ready" {
t.Fatal("Invalid greeting:", scanner.Text())
}
io.WriteString(c, "EHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid EHLO response:", scanner.Text())
}
expectedCaps := []string{"PIPELINING", "8BITMIME"}
caps := make(map[string]bool)
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
caps[strings.TrimPrefix(s, "250 ")] = true
break
} else {
if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
caps[strings.TrimPrefix(s, "250-")] = true
}
}
for _, cap := range expectedCaps {
if !caps[cap] {
t.Fatal("Missing capability:", cap)
}
}
return
}
func TestStrictServerGood(t *testing.T) {
s, c, scanner := testStrictServer(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestStrictServerBad(t *testing.T) {
s, c, scanner := testStrictServer(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM: [email protected]\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServer_lmtpOK(t *testing.T) {
be, s, c, scanner := testServerGreeted(t, func(s *smtp.Server) {
s.LMTP = true
})
defer s.Close()
defer c.Close()
io.WriteString(c, "LHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid LHLO response:", scanner.Text())
}
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
break
} else if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
}
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA second response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}