Add AUTH tests and fix a bug

This commit is contained in:
Ian Kent 2014-04-22 21:31:32 +01:00
parent 8709ba9de6
commit a4c1f6a4d0
2 changed files with 91 additions and 1 deletions

90
auth_test.go Normal file
View file

@ -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")
}

View file

@ -181,7 +181,7 @@ func (c *Session) Process(line string) {
c.log("Got CRAM-MD5 authentication, switching to AUTH state") c.log("Got CRAM-MD5 authentication, switching to AUTH state")
c.state = AUTH c.state = AUTH
c.Write("334", "PDQxOTI5NDIzNDEuMTI4Mjg0NzJAc291cmNlZm91ci5hbmRyZXcuY211LmVkdT4=") c.Write("334", "PDQxOTI5NDIzNDEuMTI4Mjg0NzJAc291cmNlZm91ci5hbmRyZXcuY211LmVkdT4=")
case args == "EXTERNAL ": case strings.HasPrefix(args, "EXTERNAL "):
c.log("Got EXTERNAL authentication: %s", strings.TrimPrefix(args, "EXTERNAL ")) c.log("Got EXTERNAL authentication: %s", strings.TrimPrefix(args, "EXTERNAL "))
c.Write("235", "Authentication successful") c.Write("235", "Authentication successful")
default: default: