From 7ee4e47c8ec4691bf84ecdac7f70263a047f69ca Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 19 Nov 2024 17:29:26 +0100 Subject: [PATCH] Add EchoBuffer to serverProps for capturing SMTP data Introduced a new io.Writer field `EchoBuffer` and its associated `BufferMutex` to `serverProps`. Updated relevant test code to write SMTP transaction data to `EchoBuffer` if it is set, ensuring thread safety with `BufferMutex`. --- client_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client_test.go b/client_test.go index 97289b8..a6530f0 100644 --- a/client_test.go +++ b/client_test.go @@ -3665,6 +3665,8 @@ func testingKey(s string) string { return strings.ReplaceAll(s, "TESTING KEY", " // serverProps represents the configuration properties for the SMTP server. type serverProps struct { + BufferMutex sync.RWMutex + EchoBuffer io.Writer FailOnAuth bool FailOnDataInit bool FailOnDataClose bool @@ -3754,6 +3756,13 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server if err != nil { t.Logf("failed to write line: %s", err) } + if props.EchoBuffer != nil { + props.BufferMutex.Lock() + if _, berr := props.EchoBuffer.Write([]byte(data + "\r\n")); berr != nil { + t.Errorf("failed write to echo buffer: %s", berr) + } + props.BufferMutex.Unlock() + } _ = writer.Flush() } writeOK := func() { @@ -3770,6 +3779,13 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server break } time.Sleep(time.Millisecond) + if props.EchoBuffer != nil { + props.BufferMutex.Lock() + if _, berr := props.EchoBuffer.Write([]byte(data)); berr != nil { + t.Errorf("failed write to echo buffer: %s", berr) + } + props.BufferMutex.Unlock() + } var datastring string data = strings.TrimSpace(data) @@ -3830,6 +3846,13 @@ func handleTestServerConnection(connection net.Conn, t *testing.T, props *server t.Logf("failed to read data from connection: %s", derr) break } + if props.EchoBuffer != nil { + props.BufferMutex.Lock() + if _, berr := props.EchoBuffer.Write([]byte(ddata)); berr != nil { + t.Errorf("failed write to echo buffer: %s", berr) + } + props.BufferMutex.Unlock() + } ddata = strings.TrimSpace(ddata) if ddata == "." { if props.FailOnDataClose {