test: pin the relay settings that reach the SMTP client

- Check the EHLO name, its host-name default, server_name and the password end to end
- Pin the wording and codes of the authentication error
This commit is contained in:
tgiachi 2026-11-10 11:11:50 +01:00
commit cc9fbc6b24
2 changed files with 77 additions and 3 deletions

View file

@ -743,6 +743,7 @@ type tlsRelay struct {
caFile string
mu sync.Mutex
auths []string
hellos []string // the EHLO name seen with each message
}
type tlsRelayBackend struct {
@ -785,6 +786,9 @@ func (s *tlsRelaySession) Mail(from string, _ *smtp.MailOptions) error {
return errors.New("the relay was reached without TLS")
}
s.from = from
s.b.r.mu.Lock()
s.b.r.hellos = append(s.b.r.hellos, s.conn.Hostname())
s.b.r.mu.Unlock()
return nil
}
func (s *tlsRelaySession) Rcpt(to string, _ *smtp.RcptOptions) error {
@ -930,3 +934,73 @@ func TestTheRelayAddressIsNotDialledAtStartup(t *testing.T) {
}
a.Close()
}
func TestTheConfiguredEHLONameReachesTheRelay(t *testing.T) {
relay := startTLSRelay(t, "ses-user", "p")
a, err := Start(relayConfig(t, relay, "p"), "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
postOne(t, a)
relay.sink.waitCount(t, 1)
relay.mu.Lock()
defer relay.mu.Unlock()
if len(relay.hellos) != 1 || relay.hellos[0] != "lists.example.org" {
t.Fatalf("EHLO names: %v", relay.hellos)
}
}
func TestWithoutAConfiguredNameEHLOIsTheHostName(t *testing.T) {
host, err := os.Hostname()
if err != nil || host == "" {
t.Skip("no host name on this machine")
}
relay := startTLSRelay(t, "ses-user", "p")
cfg := relayConfig(t, relay, "p")
cfg.Relay.Helo = ""
a, err := Start(cfg, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
postOne(t, a)
relay.sink.waitCount(t, 1)
relay.mu.Lock()
defer relay.mu.Unlock()
if len(relay.hellos) != 1 || relay.hellos[0] != host {
t.Fatalf("EHLO %v, want the host name %q (real relays dislike \"localhost\")", relay.hellos, host)
}
}
func TestTheServerNameSettingReachesTheTLSCheck(t *testing.T) {
relay := startTLSRelay(t, "ses-user", "p") // its certificate is for 127.0.0.1
cfg := relayConfig(t, relay, "p")
cfg.Relay.ServerName = "other.example"
a, err := Start(cfg, "tok")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { a.Close() })
postOne(t, a)
peek, _ := store.Open(cfg.DB)
defer peek.Close()
deadline := time.Now().Add(5 * time.Second)
for {
list, _ := peek.ListOutbox(10)
if len(list) == 1 && list[0].Attempts == 1 {
if !strings.Contains(strings.ToLower(list[0].LastError), "certificate") || list[0].Failed {
t.Fatalf("a name mismatch is a retryable certificate error: %+v", list[0])
}
break
}
if time.Now().After(deadline) {
t.Fatal("the delivery never failed on the certificate name")
}
time.Sleep(10 * time.Millisecond)
}
if relay.sink.count() != 0 {
t.Fatal("nothing may be sent to a relay whose certificate does not match")
}
}

View file

@ -324,8 +324,8 @@ func TestAWrongPasswordKeepsTheMailQueuedAndLeaksNothing(t *testing.T) {
if IsPermanent(err) {
t.Fatalf("a wrong password must not fail every message for good: the admin fixes it and the mail goes out: %v", err)
}
if !strings.Contains(strings.ToLower(err.Error()), "authentication") {
t.Fatalf("say what went wrong: %v", err)
if !strings.Contains(err.Error(), "relay authentication failed (535") {
t.Fatalf("say what went wrong, with the reply code: %v", err)
}
if strings.Contains(err.Error(), "Wr0ngPassw0rd") {
t.Fatal("never put the password in an error")
@ -343,7 +343,7 @@ func TestARelayThatWantsAuthenticationIsATemporaryProblem(t *testing.T) {
if err == nil || IsPermanent(err) {
t.Fatalf("530 means credentials are missing: retry after the admin fixes it, got %v", err)
}
if !strings.Contains(strings.ToLower(err.Error()), "authentication") {
if !strings.Contains(err.Error(), "relay authentication failed (530") {
t.Fatalf("%v", err)
}
}