From b2c3c4ce0fab3acde8de9e06214588f3493be273 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Wed, 30 Oct 2013 14:42:55 -0700 Subject: [PATCH] More testing Test DATA on handler Add a 2 second wait if the test fails so that all the logging data can be collected. --- smtpd/filestore_test.go | 12 ++++++++++ smtpd/handler_test.go | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/smtpd/filestore_test.go b/smtpd/filestore_test.go index 922b13b..82b9448 100644 --- a/smtpd/filestore_test.go +++ b/smtpd/filestore_test.go @@ -90,6 +90,8 @@ func TestFSDirStructure(t *testing.T) { assert.False(t, isPresent(expect), "Did not expect %q to exist", expect) if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -115,6 +117,8 @@ func TestFSAllMailboxes(t *testing.T) { assert.Equal(t, len(mboxes), 5) if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -163,6 +167,8 @@ func TestFSDeliverMany(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -235,6 +241,8 @@ func TestFSDelete(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -281,6 +289,8 @@ func TestFSPurge(t *testing.T) { assert.Equal(t, len(msgs), 0, "Expected mailbox to have zero messages, got %v", len(msgs)) if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -317,6 +327,8 @@ func TestFSSize(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } diff --git a/smtpd/handler_test.go b/smtpd/handler_test.go index 5f7b7b1..dfc1a48 100644 --- a/smtpd/handler_test.go +++ b/smtpd/handler_test.go @@ -51,6 +51,8 @@ func TestGreetState(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -94,6 +96,8 @@ func TestReadyState(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) } @@ -185,6 +189,51 @@ func TestMailState(t *testing.T) { } if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) + // Dump buffered log data if there was a failure + io.Copy(os.Stderr, logbuf) + } +} + +// Test commands in DATA state +func TestDataState(t *testing.T) { + server, logbuf := setupSmtpServer() + defer teardownSmtpServer(server) + var script []scriptStep + pipe := setupSmtpSession(server) + c := textproto.NewConn(pipe) + + // Get us into DATA state + if code, _, err := c.ReadCodeLine(220); err != nil { + t.Errorf("Expected a 220 greeting, got %v", code) + } + script = []scriptStep{ + {"HELO localhost", 250}, + {"MAIL FROM:", 250}, + {"RCPT TO:", 250}, + {"DATA", 354}, + } + if err := playScriptAgainst(t, c, script); err != nil { + t.Error(err) + } + // Send a message + body := `To: u1@gmail.com +From: john@gmail.com +Subject: test + +Hi! +` + dw := c.DotWriter() + io.WriteString(dw, body) + dw.Close() + if code, _, err := c.ReadCodeLine(250); err != nil { + t.Errorf("Expected a 250 greeting, got %v", code) + } + + if t.Failed() { + // Wait for handler to finish logging + time.Sleep(2 * time.Second) // Dump buffered log data if there was a failure io.Copy(os.Stderr, logbuf) }