-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
quicksend_test.go
368 lines (351 loc) · 12.5 KB
/
quicksend_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
// SPDX-FileCopyrightText: 2024 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package mail
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"strings"
"testing"
"time"
)
func TestNewAuthData(t *testing.T) {
t.Run("AuthData with username and password", func(t *testing.T) {
auth := NewAuthData("username", "password")
if !auth.Auth {
t.Fatal("expected auth to be true")
}
if auth.Username != "username" {
t.Fatalf("expected username to be %s, got %s", "username", auth.Username)
}
if auth.Password != "password" {
t.Fatalf("expected password to be %s, got %s", "password", auth.Password)
}
})
t.Run("AuthData with username and empty password", func(t *testing.T) {
auth := NewAuthData("username", "")
if !auth.Auth {
t.Fatal("expected auth to be true")
}
if auth.Username != "username" {
t.Fatalf("expected username to be %s, got %s", "username", auth.Username)
}
if auth.Password != "" {
t.Fatalf("expected password to be %s, got %s", "", auth.Password)
}
})
t.Run("AuthData with empty username and set password", func(t *testing.T) {
auth := NewAuthData("", "password")
if !auth.Auth {
t.Fatal("expected auth to be true")
}
if auth.Username != "" {
t.Fatalf("expected username to be %s, got %s", "", auth.Username)
}
if auth.Password != "password" {
t.Fatalf("expected password to be %s, got %s", "password", auth.Password)
}
})
t.Run("AuthData with empty data", func(t *testing.T) {
auth := NewAuthData("", "")
if !auth.Auth {
t.Fatal("expected auth to be true")
}
if auth.Username != "" {
t.Fatalf("expected username to be %s, got %s", "", auth.Username)
}
if auth.Password != "" {
t.Fatalf("expected password to be %s, got %s", "", auth.Password)
}
})
}
func TestQuickSend(t *testing.T) {
subject := "This is a test subject"
body := []byte("This is a test body\r\nWith multiple lines\r\n\r\nBest,\r\n The go-mail team")
sender := TestSenderValid
rcpts := []string{TestRcptValid}
t.Run("QuickSend with authentication and TLS", func(t *testing.T) {
ctxAuth, cancelAuth := context.WithCancel(context.Background())
defer cancelAuth()
PortAdder.Add(1)
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250-STARTTLS\r\n250 SMTPUTF8"
echoBuffer := bytes.NewBuffer(nil)
props := &serverProps{
EchoBuffer: echoBuffer,
FeatureSet: featureSet,
ListenPort: serverPort,
}
go func() {
if err := simpleSMTPServer(ctxAuth, t, props); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 30)
addr := TestServerAddr + ":" + fmt.Sprint(serverPort)
testHookTLSConfig = func() *tls.Config { return &tls.Config{InsecureSkipVerify: true} }
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err != nil {
t.Fatalf("failed to send email: %s", err)
}
props.BufferMutex.RLock()
resp := strings.Split(echoBuffer.String(), "\r\n")
props.BufferMutex.RUnlock()
expects := []struct {
line int
data string
}{
{8, "STARTTLS"},
{17, "AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk"},
{21, "MAIL FROM:<[email protected]> BODY=8BITMIME SMTPUTF8"},
{23, "RCPT TO:<[email protected]>"},
{30, "Subject: " + subject},
{33, "From: <[email protected]>"},
{34, "To: <[email protected]>"},
{35, "Content-Type: text/plain; charset=UTF-8"},
{36, "Content-Transfer-Encoding: quoted-printable"},
{38, "This is a test body"},
{39, "With multiple lines"},
{40, ""},
{41, "Best,"},
{42, " The go-mail team"},
}
for _, expect := range expects {
if !strings.EqualFold(resp[expect.line], expect.data) {
t.Errorf("expected %q at line %d, got: %q", expect.data, expect.line, resp[expect.line])
}
}
})
t.Run("QuickSend with authentication and TLS and multiple receipients", func(t *testing.T) {
ctxAuth, cancelAuth := context.WithCancel(context.Background())
defer cancelAuth()
PortAdder.Add(1)
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250-STARTTLS\r\n250 SMTPUTF8"
echoBuffer := bytes.NewBuffer(nil)
props := &serverProps{
EchoBuffer: echoBuffer,
FeatureSet: featureSet,
ListenPort: serverPort,
}
go func() {
if err := simpleSMTPServer(ctxAuth, t, props); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 30)
addr := TestServerAddr + ":" + fmt.Sprint(serverPort)
testHookTLSConfig = func() *tls.Config { return &tls.Config{InsecureSkipVerify: true} }
multiRcpts := []string{TestRcptValid, TestRcptValid, TestRcptValid}
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, multiRcpts, subject, body)
if err != nil {
t.Fatalf("failed to send email: %s", err)
}
props.BufferMutex.RLock()
resp := strings.Split(echoBuffer.String(), "\r\n")
props.BufferMutex.RUnlock()
expects := []struct {
line int
data string
}{
{8, "STARTTLS"},
{17, "AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk"},
{21, "MAIL FROM:<[email protected]> BODY=8BITMIME SMTPUTF8"},
{23, "RCPT TO:<[email protected]>"},
{25, "RCPT TO:<[email protected]>"},
{27, "RCPT TO:<[email protected]>"},
{34, "Subject: " + subject},
{37, "From: <[email protected]>"},
{39, "Content-Type: text/plain; charset=UTF-8"},
{40, "Content-Transfer-Encoding: quoted-printable"},
{42, "This is a test body"},
{43, "With multiple lines"},
{44, ""},
{45, "Best,"},
{46, " The go-mail team"},
}
for _, expect := range expects {
if !strings.EqualFold(resp[expect.line], expect.data) {
t.Errorf("expected %q at line %d, got: %q", expect.data, expect.line, resp[expect.line])
}
}
})
t.Run("QuickSend uses stronged authentication method", func(t *testing.T) {
ctxAuth, cancelAuth := context.WithCancel(context.Background())
defer cancelAuth()
PortAdder.Add(1)
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH PLAIN CRAM-MD5 SCRAM-SHA-256-PLUS SCRAM-SHA-256\r\n250-8BITMIME\r\n250-DSN\r\n250-STARTTLS\r\n250 SMTPUTF8"
echoBuffer := bytes.NewBuffer(nil)
props := &serverProps{
EchoBuffer: echoBuffer,
FeatureSet: featureSet,
ListenPort: serverPort,
}
go func() {
if err := simpleSMTPServer(ctxAuth, t, props); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 30)
addr := TestServerAddr + ":" + fmt.Sprint(serverPort)
testHookTLSConfig = func() *tls.Config { return &tls.Config{InsecureSkipVerify: true} }
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err != nil {
t.Fatalf("failed to send email: %s", err)
}
props.BufferMutex.RLock()
resp := strings.Split(echoBuffer.String(), "\r\n")
props.BufferMutex.RUnlock()
expects := []struct {
line int
data string
}{
{17, "AUTH SCRAM-SHA-256-PLUS"},
}
for _, expect := range expects {
if !strings.EqualFold(resp[expect.line], expect.data) {
t.Errorf("expected %q at line %d, got: %q", expect.data, expect.line, resp[expect.line])
}
}
})
t.Run("QuickSend uses stronged authentication method without TLS", func(t *testing.T) {
ctxAuth, cancelAuth := context.WithCancel(context.Background())
defer cancelAuth()
PortAdder.Add(1)
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH PLAIN CRAM-MD5 SCRAM-SHA-256-PLUS SCRAM-SHA-256\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
echoBuffer := bytes.NewBuffer(nil)
props := &serverProps{
EchoBuffer: echoBuffer,
FeatureSet: featureSet,
ListenPort: serverPort,
}
go func() {
if err := simpleSMTPServer(ctxAuth, t, props); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 30)
addr := TestServerAddr + ":" + fmt.Sprint(serverPort)
testHookTLSConfig = func() *tls.Config { return &tls.Config{InsecureSkipVerify: true} }
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err != nil {
t.Fatalf("failed to send email: %s", err)
}
props.BufferMutex.RLock()
resp := strings.Split(echoBuffer.String(), "\r\n")
props.BufferMutex.RUnlock()
expects := []struct {
line int
data string
}{
{7, "AUTH SCRAM-SHA-256"},
}
for _, expect := range expects {
if !strings.EqualFold(resp[expect.line], expect.data) {
t.Errorf("expected %q at line %d, got: %q", expect.data, expect.line, resp[expect.line])
}
}
})
t.Run("QuickSend fails during DialAndSned", func(t *testing.T) {
ctxAuth, cancelAuth := context.WithCancel(context.Background())
defer cancelAuth()
PortAdder.Add(1)
serverPort := int(TestServerPortBase + PortAdder.Load())
featureSet := "250-AUTH PLAIN CRAM-MD5 SCRAM-SHA-256-PLUS SCRAM-SHA-256\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
props := &serverProps{
FailOnMailFrom: true,
FeatureSet: featureSet,
ListenPort: serverPort,
}
go func() {
if err := simpleSMTPServer(ctxAuth, t, props); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 30)
addr := TestServerAddr + ":" + fmt.Sprint(serverPort)
testHookTLSConfig = func() *tls.Config { return &tls.Config{InsecureSkipVerify: true} }
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err == nil {
t.Error("expected QuickSend to fail during DialAndSend")
}
expect := `failed to dial and send message: send failed: sending SMTP MAIL FROM command: 500 ` +
`5.5.2 Error: fail on MAIL FROM`
if !strings.EqualFold(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
t.Run("QuickSend fails on server address without port", func(t *testing.T) {
addr := TestServerAddr
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err == nil {
t.Error("expected QuickSend to fail with invalid server address")
}
expect := "failed to split host and port from address: address 127.0.0.1: missing port in address"
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
t.Run("QuickSend fails on server address with invalid port", func(t *testing.T) {
addr := TestServerAddr + ":invalid"
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err == nil {
t.Error("expected QuickSend to fail with invalid server port")
}
expect := `failed to convert port to int: strconv.Atoi: parsing "invalid": invalid syntax`
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
t.Run("QuickSend fails on nil TLS config (test hook only)", func(t *testing.T) {
addr := TestServerAddr + ":587"
testHookTLSConfig = func() *tls.Config { return nil }
defer func() {
testHookTLSConfig = nil
}()
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, rcpts, subject, body)
if err == nil {
t.Error("expected QuickSend to fail with nil-tlsConfig")
}
expect := `failed to set TLS config: invalid TLS config`
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
t.Run("QuickSend fails with invalid from address", func(t *testing.T) {
addr := TestServerAddr + ":587"
invalid := "invalid-fromdomain.tld"
_, err := QuickSend(addr, NewAuthData("username", "password"), invalid, rcpts, subject, body)
if err == nil {
t.Error("expected QuickSend to fail with invalid from address")
}
expect := `failed to set MAIL FROM address: failed to parse mail address "invalid-fromdomain.tld": ` +
`mail: missing '@' or angle-addr`
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
t.Run("QuickSend fails with invalid from address", func(t *testing.T) {
addr := TestServerAddr + ":587"
invalid := []string{"invalid-todomain.tld"}
_, err := QuickSend(addr, NewAuthData("username", "password"), sender, invalid, subject, body)
if err == nil {
t.Error("expected QuickSend to fail with invalid to address")
}
expect := `failed to set RCPT TO address: failed to parse mail address "invalid-todomain.tld": ` +
`mail: missing '@' or angle-add`
if !strings.Contains(err.Error(), expect) {
t.Errorf("expected error to contain %s, got %s", expect, err)
}
})
}