@@ -43,6 +43,10 @@ func TestSMTP(t *testing.T) {
43
43
44
44
subject = "This is the subject"
45
45
body = "This is the body"
46
+
47
+ caFile = "fixtures/ca.crt"
48
+ certFile = "fixtures/server.crt"
49
+ keyFile = "fixtures/server.key"
46
50
)
47
51
48
52
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true , IgnoredErrorIs : []error {}}).Leveled (slog .LevelDebug )
@@ -195,6 +199,7 @@ func TestSMTP(t *testing.T) {
195
199
retryable : false ,
196
200
},
197
201
{
202
+ // No auth, no problem!
198
203
name : "No auth mechanisms supported, none configured" ,
199
204
authMechs : []string {},
200
205
cfg : codersdk.NotificationsEmailConfig {
@@ -209,17 +214,14 @@ func TestSMTP(t *testing.T) {
209
214
*/
210
215
{
211
216
// TLS is forced but certificate used by mock server is untrusted.
212
- name : "TLS conn fails: x509 untrusted" ,
213
- useTLS : true ,
214
- cfg : codersdk.NotificationsEmailConfig {
215
- ForceTLS : true ,
216
- },
217
+ name : "TLS: x509 untrusted" ,
218
+ useTLS : true ,
217
219
expectedErr : "certificate is not trusted" ,
218
220
retryable : true ,
219
221
},
220
222
{
221
223
// TLS is forced and self-signed certificate used by mock server is not verified.
222
- name : "TLS conn succeeds : x509 untrusted ignored" ,
224
+ name : "TLS: x509 untrusted ignored" ,
223
225
useTLS : true ,
224
226
cfg : codersdk.NotificationsEmailConfig {
225
227
Hello : hello ,
@@ -234,12 +236,11 @@ func TestSMTP(t *testing.T) {
234
236
{
235
237
// TLS is forced and STARTTLS is configured, but STARTTLS cannot be used by TLS connections.
236
238
// STARTTLS should be disabled and connection should succeed.
237
- name : "TLS conn succeeds : STARTTLS is ignored" ,
239
+ name : "TLS: STARTTLS is ignored" ,
238
240
useTLS : true ,
239
241
cfg : codersdk.NotificationsEmailConfig {
240
- Hello : hello ,
241
- From : from ,
242
- ForceTLS : true ,
242
+ Hello : hello ,
243
+ From : from ,
243
244
TLS : codersdk.NotificationsEmailTLSConfig {
244
245
InsecureSkipVerify : true ,
245
246
StartTLS : true ,
@@ -249,7 +250,7 @@ func TestSMTP(t *testing.T) {
249
250
},
250
251
{
251
252
// Plain connection is established and upgraded via STARTTLS, but certificate is untrusted.
252
- name : "TLS conn fails : STARTTLS untrusted" ,
253
+ name : "TLS: STARTTLS untrusted" ,
253
254
useTLS : false ,
254
255
cfg : codersdk.NotificationsEmailConfig {
255
256
TLS : codersdk.NotificationsEmailTLSConfig {
@@ -263,7 +264,7 @@ func TestSMTP(t *testing.T) {
263
264
},
264
265
{
265
266
// Plain connection is established and upgraded via STARTTLS, certificate is not verified.
266
- name : "TLS conn succeeds : STARTTLS" ,
267
+ name : "TLS: STARTTLS" ,
267
268
useTLS : false ,
268
269
cfg : codersdk.NotificationsEmailConfig {
269
270
Hello : hello ,
@@ -278,73 +279,68 @@ func TestSMTP(t *testing.T) {
278
279
},
279
280
{
280
281
// TLS connection using self-signed certificate.
281
- name : "TLS conn succeeds : self-signed" ,
282
+ name : "TLS: self-signed" ,
282
283
useTLS : true ,
283
284
cfg : codersdk.NotificationsEmailConfig {
284
285
Hello : hello ,
285
286
From : from ,
286
287
TLS : codersdk.NotificationsEmailTLSConfig {
287
- CAFile : "fixtures/ca.crt" ,
288
- CertFile : "fixtures/server.crt" ,
289
- KeyFile : "fixtures/server.key" ,
288
+ CAFile : caFile ,
289
+ CertFile : certFile ,
290
+ KeyFile : keyFile ,
290
291
},
291
- ForceTLS : true ,
292
292
},
293
293
toAddrs : []string {to },
294
294
},
295
295
{
296
296
// TLS connection using self-signed certificate & specifying the DNS name configured in the certificate.
297
- name : "TLS conn succeeds : self-signed + SNI" ,
297
+ name : "TLS: self-signed + SNI" ,
298
298
useTLS : true ,
299
299
cfg : codersdk.NotificationsEmailConfig {
300
300
Hello : hello ,
301
301
From : from ,
302
302
TLS : codersdk.NotificationsEmailTLSConfig {
303
303
ServerName : "myserver.local" ,
304
- CAFile : "fixtures/ca.crt" ,
305
- CertFile : "fixtures/server.crt" ,
306
- KeyFile : "fixtures/server.key" ,
304
+ CAFile : caFile ,
305
+ CertFile : certFile ,
306
+ KeyFile : keyFile ,
307
307
},
308
- ForceTLS : true ,
309
308
},
310
309
toAddrs : []string {to },
311
310
},
312
311
{
313
- name : "TLS conn fails : load CA" ,
312
+ name : "TLS: load CA" ,
314
313
useTLS : true ,
315
314
cfg : codersdk.NotificationsEmailConfig {
316
315
TLS : codersdk.NotificationsEmailTLSConfig {
317
316
CAFile : "nope.crt" ,
318
317
},
319
- ForceTLS : true ,
320
318
},
321
319
expectedErr : "open nope.crt: no such file or directory" ,
322
320
retryable : true ,
323
321
},
324
322
{
325
- name : "TLS conn fails : load cert" ,
323
+ name : "TLS: load cert" ,
326
324
useTLS : true ,
327
325
cfg : codersdk.NotificationsEmailConfig {
328
326
TLS : codersdk.NotificationsEmailTLSConfig {
329
- CAFile : "fixtures/ca.crt" ,
327
+ CAFile : caFile ,
330
328
CertFile : "fixtures/nope.cert" ,
331
- KeyFile : "fixtures/server.key" ,
329
+ KeyFile : keyFile ,
332
330
},
333
- ForceTLS : true ,
334
331
},
335
332
expectedErr : "open fixtures/nope.cert: no such file or directory" ,
336
333
retryable : true ,
337
334
},
338
335
{
339
- name : "TLS conn fails : load cert key" ,
336
+ name : "TLS: load cert key" ,
340
337
useTLS : true ,
341
338
cfg : codersdk.NotificationsEmailConfig {
342
339
TLS : codersdk.NotificationsEmailTLSConfig {
343
- CAFile : "fixtures/ca.crt" ,
344
- CertFile : "fixtures/server.crt" ,
340
+ CAFile : caFile ,
341
+ CertFile : certFile ,
345
342
KeyFile : "fixtures/nope.key" ,
346
343
},
347
- ForceTLS : true ,
348
344
},
349
345
expectedErr : "open fixtures/nope.key: no such file or directory" ,
350
346
retryable : true ,
@@ -365,11 +361,10 @@ func TestSMTP(t *testing.T) {
365
361
Password : password ,
366
362
},
367
363
TLS : codersdk.NotificationsEmailTLSConfig {
368
- CAFile : "fixtures/ca.crt" ,
369
- CertFile : "fixtures/server.crt" ,
370
- KeyFile : "fixtures/server.key" ,
364
+ CAFile : caFile ,
365
+ CertFile : certFile ,
366
+ KeyFile : keyFile ,
371
367
},
372
- ForceTLS : true ,
373
368
},
374
369
toAddrs : []string {to },
375
370
expectedAuthMeth : sasl .Plain ,
@@ -382,6 +377,8 @@ func TestSMTP(t *testing.T) {
382
377
383
378
ctx := testutil .Context (t , testutil .WaitShort )
384
379
380
+ tc .cfg .ForceTLS = serpent .Bool (tc .useTLS )
381
+
385
382
backend := NewBackend (Config {
386
383
AuthMechanisms : tc .authMechs ,
387
384
@@ -394,7 +391,6 @@ func TestSMTP(t *testing.T) {
394
391
srv , listen , err := createMockSMTPServer (backend , tc .useTLS )
395
392
require .NoError (t , err )
396
393
t .Cleanup (func () {
397
- _ = listen .Close ()
398
394
// We expect that the server has already been closed in the test
399
395
assert .ErrorIs (t , srv .Shutdown (ctx ), smtp .ErrServerClosed )
400
396
})
0 commit comments