From 8f3ff1a9a3983e8e94043d899c15f4ba6038a7c6 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 12 Dec 2016 22:48:20 -0500 Subject: [PATCH] Complete the unittest by adding smtp.Server.VerifyAddress. --- smtp.go | 5 +++++ smtp/conn.go | 5 +++++ smtp/conn_test.go | 30 +++++++++++++++++++++++++----- smtp/server.go | 6 ++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/smtp.go b/smtp.go index 156ace1..fd071fd 100644 --- a/smtp.go +++ b/smtp.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "fmt" "net" + "net/mail" "src.bluestatic.org/mailpopbox/smtp" ) @@ -48,6 +49,10 @@ func (server *smtpServer) TLSConfig() *tls.Config { return nil } +func (server *smtpServer) VerifyAddress(addr mail.Address) smtp.ReplyLine { + return smtp.ReplyOK +} + func (server *smtpServer) OnEHLO() *smtp.ReplyLine { return nil } diff --git a/smtp/conn.go b/smtp/conn.go index ac07595..a256998 100644 --- a/smtp/conn.go +++ b/smtp/conn.go @@ -165,6 +165,11 @@ func (conn *connection) doRCPT() { conn.reply(ReplyBadSyntax) } + if reply := conn.server.VerifyAddress(*address); reply != ReplyOK { + conn.reply(reply) + return + } + conn.rcptTo = append(conn.rcptTo, *address) conn.state = stateRecipient diff --git a/smtp/conn_test.go b/smtp/conn_test.go index d0998cd..3456a39 100644 --- a/smtp/conn_test.go +++ b/smtp/conn_test.go @@ -1,7 +1,9 @@ package smtp import ( + "fmt" "net" + "net/mail" "net/textproto" "path/filepath" "runtime" @@ -9,16 +11,22 @@ import ( "testing" ) +func _fl(depth int) string { + _, file, line, _ := runtime.Caller(depth + 1) + return fmt.Sprintf("[%s:%d]", filepath.Base(file), line) +} + func ok(t testing.TB, err error) { if err != nil { - _, file, line, _ := runtime.Caller(1) - t.Errorf("[%s:%d] unexpected error: %v", filepath.Base(file), line, err) + t.Errorf("%s unexpected error: %v", _fl(1), err) } } func readCodeLine(t testing.TB, conn *textproto.Conn, code int) string { _, message, err := conn.ReadCodeLine(code) - ok(t, err) + if err != nil { + t.Errorf("%s ReadCodeLine error: %v", _fl(1), err) + } return message } @@ -46,12 +54,22 @@ func runServer(t *testing.T, server Server) net.Listener { type testServer struct { EmptyServerCallbacks + blockList []string } func (s *testServer) Name() string { return "Test-Server" } +func (s *testServer) VerifyAddress(addr mail.Address) ReplyLine { + for _, block := range s.blockList { + if block == addr.Address { + return ReplyBadMailbox + } + } + return ReplyOK +} + func createClient(t *testing.T, addr net.Addr) *textproto.Conn { conn, err := textproto.Dial(addr.Network(), addr.String()) if err != nil { @@ -63,7 +81,9 @@ func createClient(t *testing.T, addr net.Addr) *textproto.Conn { // RFC 5321 § D.1 func TestScenarioTypical(t *testing.T) { - s := testServer{} + s := testServer{ + blockList: []string{"Green@foo.com"}, + } l := runServer(t, &s) defer l.Close() @@ -90,7 +110,7 @@ func TestScenarioTypical(t *testing.T) { readCodeLine(t, conn, 250) ok(t, conn.PrintfLine("RCPT TO:")) - readCodeLine(t, conn, 250) // TODO: make this 55o by rejecting Green + readCodeLine(t, conn, 550) ok(t, conn.PrintfLine("RCPT TO:")) readCodeLine(t, conn, 250) diff --git a/smtp/server.go b/smtp/server.go index 3ecb24a..e992cda 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -15,6 +15,7 @@ var ( ReplyOK = ReplyLine{250, "OK"} ReplyBadSyntax = ReplyLine{501, "syntax error"} ReplyBadSequence = ReplyLine{503, "bad sequence of commands"} + ReplyBadMailbox = ReplyLine{550, "mailbox unavailable"} ) type Envelope struct { @@ -29,6 +30,7 @@ type Server interface { Name() string TLSConfig() *tls.Config OnEHLO() *ReplyLine + VerifyAddress(mail.Address) ReplyLine OnMessageDelivered(Envelope) *ReplyLine } @@ -42,6 +44,10 @@ func (*EmptyServerCallbacks) OnEHLO() *ReplyLine { return nil } +func (*EmptyServerCallbacks) VerifyAddress(mail.Address) ReplyLine { + return ReplyOK +} + func (*EmptyServerCallbacks) OnMessageDelivered(Envelope) *ReplyLine { return nil } -- 2.43.5