A relay can take minutes to answer the final dot (content filter, large message, slow link). One deadline over the whole transaction cut it after the message had been accepted, so the delivery was retried and sent again. The connection now moves its deadline as the transaction advances: setup (dial, greeting, TLS, AUTH, MAIL, RCPT, DATA) has SMTPRelay.Timeout, 60 s by default; the upload has 60 s plus the size at 32 KiB/s, at most 10 minutes; the reply to the final dot has 10 minutes (RFC 5321 4.5.3.2.6). go-smtp sets and clears its own per-command deadlines, so a wrapper clips them to the current phase. Context cancellation still closes the connection at once. A timeout in any phase is temporary; after the final dot the error says the outcome is unknown.
231 lines
6.7 KiB
Go
231 lines
6.7 KiB
Go
package outbound
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// scaleTimeouts shrinks the phase budgets so the tests run in milliseconds.
|
|
func scaleTimeouts(t *testing.T, floor, finalReply time.Duration) {
|
|
t.Helper()
|
|
oldFloor, oldRate, oldCap, oldReply := dataFloor, dataRate, dataCap, finalReplyTimeout
|
|
dataFloor, finalReplyTimeout = floor, finalReply
|
|
t.Cleanup(func() { dataFloor, dataRate, dataCap, finalReplyTimeout = oldFloor, oldRate, oldCap, oldReply })
|
|
}
|
|
|
|
// slowRelay is a bare SMTP server that waits dotDelay before answering the final dot
|
|
// (forever when dotDelay is negative).
|
|
type slowRelay struct {
|
|
mu sync.Mutex
|
|
accepted int
|
|
}
|
|
|
|
func startSlowRelay(t *testing.T, dotDelay time.Duration) (*slowRelay, string) {
|
|
t.Helper()
|
|
s := &slowRelay{}
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
done := make(chan struct{})
|
|
t.Cleanup(func() { close(done); ln.Close() })
|
|
go func() {
|
|
for {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func() {
|
|
defer c.Close()
|
|
r := bufio.NewReader(c)
|
|
c.Write([]byte("220 slow ESMTP\r\n"))
|
|
inData := false
|
|
for {
|
|
l, err := r.ReadString('\n')
|
|
if err != nil {
|
|
return
|
|
}
|
|
l = strings.TrimRight(l, "\r\n")
|
|
switch {
|
|
case inData:
|
|
if l != "." {
|
|
continue
|
|
}
|
|
inData = false
|
|
if dotDelay < 0 {
|
|
<-done
|
|
return
|
|
}
|
|
time.Sleep(dotDelay)
|
|
s.mu.Lock()
|
|
s.accepted++
|
|
s.mu.Unlock()
|
|
c.Write([]byte("250 2.0.0 queued\r\n"))
|
|
case strings.HasPrefix(l, "EHLO"), strings.HasPrefix(l, "HELO"):
|
|
c.Write([]byte("250 slow\r\n"))
|
|
case strings.HasPrefix(l, "MAIL"), strings.HasPrefix(l, "RCPT"):
|
|
c.Write([]byte("250 2.1.0 ok\r\n"))
|
|
case l == "DATA":
|
|
inData = true
|
|
c.Write([]byte("354 go ahead\r\n"))
|
|
case l == "QUIT":
|
|
c.Write([]byte("221 bye\r\n"))
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
}()
|
|
return s, ln.Addr().String()
|
|
}
|
|
|
|
func TestASlowReplyToTheFinalDotIsWaitedForAndDeliversOnce(t *testing.T) {
|
|
// The relay takes longer than the whole setup budget to accept the message (a content filter):
|
|
// that is not a failure, or the retry would deliver it a second time.
|
|
scaleTimeouts(t, 100*time.Millisecond, 3*time.Second)
|
|
s, addr := startSlowRelay(t, 400*time.Millisecond)
|
|
r := &SMTPRelay{Addr: addr, Timeout: 100 * time.Millisecond}
|
|
if err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n")); err != nil {
|
|
t.Fatalf("the relay accepted the message: %v", err)
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.accepted != 1 {
|
|
t.Fatalf("accepted %d times", s.accepted)
|
|
}
|
|
}
|
|
|
|
func TestNoReplyToTheFinalDotTimesOutAsATemporaryFailure(t *testing.T) {
|
|
scaleTimeouts(t, 100*time.Millisecond, 300*time.Millisecond)
|
|
_, addr := startSlowRelay(t, -1)
|
|
r := &SMTPRelay{Addr: addr, Timeout: 5 * time.Second}
|
|
start := time.Now()
|
|
err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n"))
|
|
var ne net.Error
|
|
if !errors.As(err, &ne) || !ne.Timeout() {
|
|
t.Fatalf("want a timeout, got %v", err)
|
|
}
|
|
if IsPermanent(err) {
|
|
t.Fatalf("an unknown outcome is retried, not failed: %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "outcome unknown") {
|
|
t.Fatalf("the error says the relay may have the message: %v", err)
|
|
}
|
|
if took := time.Since(start); took < 250*time.Millisecond || took > 3*time.Second {
|
|
t.Fatalf("the final reply has its own budget, took %v", took)
|
|
}
|
|
}
|
|
|
|
func TestARelayThatStallsAtTheGreetingTimesOutInTheShortBudget(t *testing.T) {
|
|
r := &SMTPRelay{Addr: silent(t), Timeout: 150 * time.Millisecond}
|
|
start := time.Now()
|
|
err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("x"))
|
|
var ne net.Error
|
|
if !errors.As(err, &ne) || !ne.Timeout() {
|
|
t.Fatalf("want a timeout, got %v", err)
|
|
}
|
|
if IsPermanent(err) {
|
|
t.Fatalf("a timeout is temporary: %v", err)
|
|
}
|
|
if took := time.Since(start); took > 2*time.Second {
|
|
t.Fatalf("took %v", took)
|
|
}
|
|
}
|
|
|
|
func TestTheUploadBudgetGrowsWithTheMessageSize(t *testing.T) {
|
|
if got := uploadBudget(0); got != time.Minute {
|
|
t.Fatalf("floor: %v", got)
|
|
}
|
|
small, big := uploadBudget(10<<10), uploadBudget(10<<20)
|
|
if big <= small {
|
|
t.Fatalf("a big message gets more time: small %v, big %v", small, big)
|
|
}
|
|
if want := time.Minute + 320*time.Second; big != want { // 10 MiB at 32 KiB/s
|
|
t.Fatalf("10 MiB: got %v, want %v", big, want)
|
|
}
|
|
if got := uploadBudget(1 << 40); got != 10*time.Minute {
|
|
t.Fatalf("cap: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestCancellingTheContextAbortsTheWaitForTheFinalReply(t *testing.T) {
|
|
_, addr := startSlowRelay(t, -1) // the full 10 minutes apply
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
errc <- (&SMTPRelay{Addr: addr}).Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n"))
|
|
}()
|
|
time.Sleep(200 * time.Millisecond)
|
|
cancel()
|
|
select {
|
|
case err := <-errc:
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("want context.Canceled, got %v", err)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("Send ignored the cancelled context while waiting for the final reply")
|
|
}
|
|
}
|
|
|
|
// stopsReadingAtData answers up to DATA, then never reads again.
|
|
func stopsReadingAtData(t *testing.T) string {
|
|
t.Helper()
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
done := make(chan struct{})
|
|
t.Cleanup(func() { close(done); ln.Close() })
|
|
go func() {
|
|
for {
|
|
c, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
go func() {
|
|
defer c.Close()
|
|
r := bufio.NewReader(c)
|
|
c.Write([]byte("220 stuck ESMTP\r\n"))
|
|
for {
|
|
l, err := r.ReadString('\n')
|
|
if err != nil {
|
|
return
|
|
}
|
|
switch l = strings.TrimRight(l, "\r\n"); {
|
|
case l == "DATA":
|
|
c.Write([]byte("354 go ahead\r\n"))
|
|
<-done
|
|
return
|
|
case strings.HasPrefix(l, "EHLO"), strings.HasPrefix(l, "HELO"):
|
|
c.Write([]byte("250 stuck\r\n"))
|
|
default:
|
|
c.Write([]byte("250 ok\r\n"))
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
}()
|
|
return ln.Addr().String()
|
|
}
|
|
|
|
func TestAStalledUploadTimesOutInItsOwnBudget(t *testing.T) {
|
|
scaleTimeouts(t, 200*time.Millisecond, time.Minute)
|
|
dataRate = 1 << 40 // the budget is the floor
|
|
r := &SMTPRelay{Addr: stopsReadingAtData(t), Timeout: time.Minute}
|
|
big := []byte(strings.Repeat(strings.Repeat("x", 998)+"\r\n", 32<<10)) // 32 MiB, more than the socket buffers
|
|
start := time.Now()
|
|
err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, big)
|
|
var ne net.Error
|
|
if !errors.As(err, &ne) || !ne.Timeout() || IsPermanent(err) {
|
|
t.Fatalf("want a temporary timeout, got %v", err)
|
|
}
|
|
if took := time.Since(start); took > 5*time.Second {
|
|
t.Fatalf("the upload has its own budget, took %v", took)
|
|
}
|
|
}
|