Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset state properly #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,10 @@ func (proto *Protocol) Command(command *Command) (reply *Reply) {
return ReplyBye()
case "RSET" == command.verb:
proto.logf("Got RSET command, switching to MAIL state")
helo := proto.Message.Helo
proto.State = MAIL
proto.Message = &data.SMTPMessage{}
proto.Message.Helo = helo
return ReplyOk()
case "NOOP" == command.verb:
proto.logf("Got NOOP verb, staying in %s state", StateMap[proto.State])
Expand Down Expand Up @@ -410,6 +412,7 @@ func (proto *Protocol) Command(command *Command) (reply *Reply) {
// HELO creates a reply to a HELO command
func (proto *Protocol) HELO(args string) (reply *Reply) {
proto.logf("Got HELO command, switching to MAIL state")
proto.resetState()
proto.State = MAIL
proto.Message.Helo = args
return ReplyOk("Hello " + args)
Expand All @@ -418,6 +421,7 @@ func (proto *Protocol) HELO(args string) (reply *Reply) {
// EHLO creates a reply to a EHLO command
func (proto *Protocol) EHLO(args string) (reply *Reply) {
proto.logf("Got EHLO command, switching to MAIL state")
proto.resetState()
proto.State = MAIL
proto.Message.Helo = args
replyArgs := []string{"Hello " + args, "PIPELINING"}
Expand Down
65 changes: 57 additions & 8 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ func TestEHLO(t *testing.T) {
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost")
})
Convey("HELO should work in MAIL state", t, func() {
Convey("EHLO should work in MAIL state", t, func() {
proto := NewProtocol()
proto.Start()
proto.Command(ParseCommand("HELO localhost"))
proto.Command(ParseCommand("EHLO localhost"))
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost")
reply := proto.Command(ParseCommand("EHLO localhost"))
Expand All @@ -309,19 +309,43 @@ func TestEHLO(t *testing.T) {
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost")
})
Convey("HELO should work in RCPT state", t, func() {
Convey("EHLO should work in RCPT state", t, func() {
proto := NewProtocol()
proto.Start()
proto.Command(ParseCommand("HELO localhost"))
proto.Command(ParseCommand("EHLO localhost"))
proto.Command(ParseCommand("MAIL From:<test>"))
So(proto.State, ShouldEqual, RCPT)
So(proto.Message.Helo, ShouldEqual, "localhost")
reply := proto.Command(ParseCommand("EHLO localhost"))
So(proto.Message.From, ShouldEqual, "test")
So(proto.Message.To, ShouldBeZeroValue)
reply := proto.Command(ParseCommand("EHLO localhost2"))
So(reply, ShouldNotBeNil)
So(reply.Status, ShouldEqual, 250)
So(reply.Lines(), ShouldResemble, []string{"250-Hello localhost\r\n", "250 PIPELINING\r\n"})
So(reply.Lines(), ShouldResemble, []string{"250-Hello localhost2\r\n", "250 PIPELINING\r\n"})
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost2")
So(proto.Message.From, ShouldBeZeroValue)
So(proto.Message.To, ShouldBeZeroValue)
})
Convey("EHLO should work in RCPT state with recipients", t, func() {
proto := NewProtocol()
proto.Start()
proto.Command(ParseCommand("EHLO localhost"))
proto.Command(ParseCommand("MAIL From:<test>"))
proto.Command(ParseCommand("RCPT To:<rcpt1>"))
proto.Command(ParseCommand("RCPT To:<rcpt2>"))
So(proto.State, ShouldEqual, RCPT)
So(proto.Message.Helo, ShouldEqual, "localhost")
So(proto.Message.From, ShouldEqual, "test")
So(proto.Message.To, ShouldResemble, []string{"rcpt1", "rcpt2"})
reply := proto.Command(ParseCommand("EHLO localhost2"))
So(reply, ShouldNotBeNil)
So(reply.Status, ShouldEqual, 250)
So(reply.Lines(), ShouldResemble, []string{"250-Hello localhost2\r\n", "250 PIPELINING\r\n"})
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost2")
So(proto.Message.From, ShouldBeZeroValue)
So(proto.Message.To, ShouldBeZeroValue)
})
}

Expand Down Expand Up @@ -370,12 +394,36 @@ func TestHELO(t *testing.T) {
proto.Command(ParseCommand("MAIL From:<test>"))
So(proto.State, ShouldEqual, RCPT)
So(proto.Message.Helo, ShouldEqual, "localhost")
reply := proto.Command(ParseCommand("HELO localhost"))
So(proto.Message.From, ShouldEqual, "test")
So(proto.Message.To, ShouldBeZeroValue)
reply := proto.Command(ParseCommand("HELO localhost2"))
So(reply, ShouldNotBeNil)
So(reply.Status, ShouldEqual, 250)
So(reply.Lines(), ShouldResemble, []string{"250 Hello localhost\r\n"})
So(reply.Lines(), ShouldResemble, []string{"250 Hello localhost2\r\n"})
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost2")
So(proto.Message.From, ShouldBeZeroValue)
So(proto.Message.To, ShouldBeZeroValue)
})
Convey("HELO should work in RCPT state with recipients", t, func() {
proto := NewProtocol()
proto.Start()
proto.Command(ParseCommand("HELO localhost"))
proto.Command(ParseCommand("MAIL From:<test>"))
proto.Command(ParseCommand("RCPT To:<rcpt1>"))
proto.Command(ParseCommand("RCPT To:<rcpt2>"))
So(proto.State, ShouldEqual, RCPT)
So(proto.Message.Helo, ShouldEqual, "localhost")
So(proto.Message.From, ShouldEqual, "test")
So(proto.Message.To, ShouldResemble, []string{"rcpt1", "rcpt2"})
reply := proto.Command(ParseCommand("HELO localhost2"))
So(reply, ShouldNotBeNil)
So(reply.Status, ShouldEqual, 250)
So(reply.Lines(), ShouldResemble, []string{"250 Hello localhost2\r\n"})
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost2")
So(proto.Message.From, ShouldBeZeroValue)
So(proto.Message.To, ShouldBeZeroValue)
})
}

Expand Down Expand Up @@ -485,6 +533,7 @@ func TestRSET(t *testing.T) {
So(reply.Status, ShouldEqual, 250)
So(reply.Lines(), ShouldResemble, []string{"250 Ok\r\n"})
So(proto.State, ShouldEqual, MAIL)
So(proto.Message.Helo, ShouldEqual, "localhost")
So(proto.Message.From, ShouldEqual, "")
So(len(proto.Message.To), ShouldEqual, 0)
})
Expand Down