package smtp
import (
+ "fmt"
"net"
+ "net/mail"
"net/textproto"
"path/filepath"
"runtime"
"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
}
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 {
// 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()
readCodeLine(t, conn, 250)
ok(t, conn.PrintfLine("RCPT TO:<Green@foo.com>"))
- readCodeLine(t, conn, 250) // TODO: make this 55o by rejecting Green
+ readCodeLine(t, conn, 550)
ok(t, conn.PrintfLine("RCPT TO:<Brown@foo.com>"))
readCodeLine(t, conn, 250)
ReplyOK = ReplyLine{250, "OK"}
ReplyBadSyntax = ReplyLine{501, "syntax error"}
ReplyBadSequence = ReplyLine{503, "bad sequence of commands"}
+ ReplyBadMailbox = ReplyLine{550, "mailbox unavailable"}
)
type Envelope struct {
Name() string
TLSConfig() *tls.Config
OnEHLO() *ReplyLine
+ VerifyAddress(mail.Address) ReplyLine
OnMessageDelivered(Envelope) *ReplyLine
}
return nil
}
+func (*EmptyServerCallbacks) VerifyAddress(mail.Address) ReplyLine {
+ return ReplyOK
+}
+
func (*EmptyServerCallbacks) OnMessageDelivered(Envelope) *ReplyLine {
return nil
}