SMTPServerTLS starts a SMTP server listening on the specified addr with Implicit TLS.
(t *testing.T, addr string, fn ...SMTPServerConfigureFunc)
| 329 | // SMTPServerTLS starts a SMTP server listening on the specified addr with |
| 330 | // Implicit TLS. |
| 331 | func SMTPServerTLS(t *testing.T, addr string, fn ...SMTPServerConfigureFunc) (*tls.Config, *SMTPBackend, *smtp.Server) { |
| 332 | t.Helper() |
| 333 | |
| 334 | cert, err := tls.X509KeyPair([]byte(testServerCert), []byte(testServerKey)) |
| 335 | if err != nil { |
| 336 | panic(err) |
| 337 | } |
| 338 | |
| 339 | l, err := tls.Listen("tcp", addr, &tls.Config{ |
| 340 | Certificates: []tls.Certificate{cert}, |
| 341 | }) |
| 342 | if err != nil { |
| 343 | t.Fatal(err) |
| 344 | } |
| 345 | |
| 346 | be := new(SMTPBackend) |
| 347 | s := smtp.NewServer(be) |
| 348 | s.Domain = "localhost" |
| 349 | for _, f := range fn { |
| 350 | f(s) |
| 351 | } |
| 352 | |
| 353 | pool := x509.NewCertPool() |
| 354 | pool.AppendCertsFromPEM([]byte(testServerCert)) |
| 355 | |
| 356 | clientCfg := &tls.Config{ |
| 357 | ServerName: "127.0.0.1", |
| 358 | Time: func() time.Time { |
| 359 | return time.Date(2019, time.November, 18, 17, 59, 41, 0, time.UTC) |
| 360 | }, |
| 361 | RootCAs: pool, |
| 362 | } |
| 363 | |
| 364 | go func() { |
| 365 | if err := s.Serve(l); err != nil { |
| 366 | t.Error(err) |
| 367 | } |
| 368 | }() |
| 369 | |
| 370 | // Dial it once it make sure Server completes its initialization before |
| 371 | // we try to use it. Notably, if test fails before connecting to the server, |
| 372 | // it will call Server.Close which will call Server.listener.Close with a |
| 373 | // nil Server.listener (Serve sets it to a non-nil value, so it is racy and |
| 374 | // happens only sometimes). |
| 375 | testConn, err := net.Dial("tcp", addr) |
| 376 | if err != nil { |
| 377 | t.Fatal(err) |
| 378 | } |
| 379 | require.NoError(t, testConn.Close()) |
| 380 | |
| 381 | return clientCfg, be, s |
| 382 | } |
| 383 | |
| 384 | type smtpBackendConnCounter interface { |
| 385 | ConnectionCount() int |