From 54b4c4c68181abed89b4ee03ac64cffa828d1d6e Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Tue, 22 Apr 2014 23:27:07 +0100 Subject: [PATCH] More auth tests, fix bug --- auth_test.go | 61 +++++++++++++++++++++++++++++++++++++++++ mailhog/smtp/session.go | 10 +++---- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/auth_test.go b/auth_test.go index 9902dbc..5f787b4 100644 --- a/auth_test.go +++ b/auth_test.go @@ -87,4 +87,65 @@ func TestBasicSMTPAuth(t *testing.T) { 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 LOGIN\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 VXNlcm5hbWU6\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]), "334 UGFzc3dvcmQ6\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") + + // 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 CRAM-MD5\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 PDQxOTI5NDIzNDEuMTI4Mjg0NzJAc291cmNlZm91ci5hbmRyZXcuY211LmVkdT4=\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 27d5984..39c1072 100644 --- a/mailhog/smtp/session.go +++ b/mailhog/smtp/session.go @@ -160,7 +160,7 @@ func (c *Session) Process(line string) { case c.state == AUTH2: c.log("Got LOGIN authentication response: '%s', switching to AUTH state", args) c.state = AUTH - c.Write("334", "VXNlcm5hbWU6") + c.Write("334", "UGFzc3dvcmQ6") case c.state == MAIL: // TODO rename/split state switch command { case "AUTH": @@ -171,10 +171,10 @@ func (c *Session) Process(line string) { c.Write("235", "Authentication successful") case args == "LOGIN": c.log("Got LOGIN authentication, switching to AUTH state") - c.state = AUTH + c.state = AUTH2 c.Write("334", "VXNlcm5hbWU6") case args == "PLAIN": - c.log("Got PLAIN authentication (no args), switching to AUTH state") + c.log("Got PLAIN authentication (no args), switching to AUTH2 state") c.state = AUTH c.Write("334", "") case args == "CRAM-MD5": @@ -189,7 +189,7 @@ func (c *Session) Process(line string) { } case "MAIL": c.log("Got MAIL command, switching to RCPT state") - r, _ := regexp.Compile("From:<([^>]+)>") + r, _ := regexp.Compile("(?i:From):<([^>]+)>") match := r.FindStringSubmatch(args) c.message.From = match[1] c.state = RCPT @@ -202,7 +202,7 @@ func (c *Session) Process(line string) { switch command { case "RCPT": c.log("Got RCPT command") - r, _ := regexp.Compile("To:<([^>]+)>") + r, _ := regexp.Compile("(?i:To):<([^>]+)>") match := r.FindStringSubmatch(args) c.message.To = append(c.message.To, match[1]) c.state = RCPT