More auth tests, fix bug

This commit is contained in:
Ian Kent 2014-04-22 23:27:07 +01:00
parent a4c1f6a4d0
commit 54b4c4c681
2 changed files with 66 additions and 5 deletions

View file

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

View file

@ -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