From a4c1f6a4d081c193c14e5226c790edb7002cf201 Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 22 Apr 2014 21:31:32 +0100 Subject: [PATCH] Add AUTH tests and fix a bug --- auth_test.go | 90 +++++++++++++++++++++++++++++++++++++++++ mailhog/smtp/session.go | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 auth_test.go diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..9902dbc --- /dev/null +++ b/auth_test.go @@ -0,0 +1,90 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "net" + "testing" +) + +// FIXME requires a running instance of MailHog +// FIXME clean up tests, repeated conn.Read(buf) is a mess + +func TestBasicSMTPAuth(t *testing.T) { + buf := make([]byte, 1024) + + // Open a connection + conn, err := net.Dial("tcp", "127.0.0.1:1025") + assert.Nil(t, err) + + // Read the greeting + n, err := conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "220 mailhog.example ESMTP Go-MailHog\n") + + // Send EHLO + _, err = conn.Write([]byte("EHLO localhost\r\n")) + assert.Nil(t, err) + + // Read the response + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "250-Hello localhost\n") + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "250-PIPELINING\n") + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "250 AUTH EXTERNAL CRAM-MD5 LOGIN PLAIN\n") + + // Send AUTH + _, err = conn.Write([]byte("AUTH EXTERNAL =\r\n")) + assert.Nil(t, err) + + // Read the response + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "235 Authentication successful\n") + + // Send RSET and EHLO + _, err = conn.Write([]byte("RSET\r\n")) + n, err = conn.Read(buf) + _, err = conn.Write([]byte("EHLO localhost\r\n")) + n, err = conn.Read(buf) + n, err = conn.Read(buf) + n, err = conn.Read(buf) + + // Send AUTH + _, err = conn.Write([]byte("AUTH PLAIN foobar\r\n")) + assert.Nil(t, err) + + // Read the response + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "235 Authentication successful\n") + + // Send RSET and EHLO + _, err = conn.Write([]byte("RSET\r\n")) + n, err = conn.Read(buf) + _, err = conn.Write([]byte("EHLO localhost\r\n")) + n, err = conn.Read(buf) + n, err = conn.Read(buf) + n, err = conn.Read(buf) + + // Send AUTH + _, err = conn.Write([]byte("AUTH PLAIN\r\n")) + assert.Nil(t, err) + + // Read the response + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "334 \n") + + // Send AUTH + _, err = conn.Write([]byte("foobar\r\n")) + assert.Nil(t, err) + + // Read the response + n, err = conn.Read(buf) + assert.Nil(t, err) + assert.Equal(t, string(buf[0:n]), "235 Authentication successful\n") +} diff --git a/mailhog/smtp/session.go b/mailhog/smtp/session.go index 0f4f182..27d5984 100644 --- a/mailhog/smtp/session.go +++ b/mailhog/smtp/session.go @@ -181,7 +181,7 @@ func (c *Session) Process(line string) { c.log("Got CRAM-MD5 authentication, switching to AUTH state") c.state = AUTH c.Write("334", "PDQxOTI5NDIzNDEuMTI4Mjg0NzJAc291cmNlZm91ci5hbmRyZXcuY211LmVkdT4=") - case args == "EXTERNAL ": + case strings.HasPrefix(args, "EXTERNAL "): c.log("Got EXTERNAL authentication: %s", strings.TrimPrefix(args, "EXTERNAL ")) c.Write("235", "Authentication successful") default: