nolint:revive // Yes, useTLS is a control flag.
(be *Backend, useTLS bool)
| 181 | |
| 182 | // nolint:revive // Yes, useTLS is a control flag. |
| 183 | func CreateMockSMTPServer(be *Backend, useTLS bool) (*smtp.Server, net.Listener, error) { |
| 184 | // nolint:gosec |
| 185 | tlsCfg := &tls.Config{ |
| 186 | GetCertificate: readCert, |
| 187 | } |
| 188 | |
| 189 | l, err := net.Listen("tcp", "localhost:0") |
| 190 | if err != nil { |
| 191 | return nil, nil, xerrors.Errorf("connect: tls? %v: %w", useTLS, err) |
| 192 | } |
| 193 | |
| 194 | if useTLS { |
| 195 | l = tls.NewListener(l, tlsCfg) |
| 196 | } |
| 197 | |
| 198 | addr, ok := l.Addr().(*net.TCPAddr) |
| 199 | if !ok { |
| 200 | return nil, nil, xerrors.Errorf("unexpected address type: %T", l.Addr()) |
| 201 | } |
| 202 | |
| 203 | s := smtp.NewServer(be) |
| 204 | |
| 205 | s.Addr = addr.String() |
| 206 | s.WriteTimeout = 10 * time.Second |
| 207 | s.ReadTimeout = 10 * time.Second |
| 208 | s.MaxMessageBytes = 1024 * 1024 |
| 209 | s.MaxRecipients = 50 |
| 210 | s.AllowInsecureAuth = !useTLS |
| 211 | s.TLSConfig = tlsCfg |
| 212 | |
| 213 | return s, l, nil |
| 214 | } |
| 215 | |
| 216 | func readCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| 217 | crt, err := tls.X509KeyPair(certFile, keyFile) |