12 func ok(t testing.TB, err error) {
14 _, file, line, _ := runtime.Caller(1)
15 t.Errorf("[%s:%d] unexpected error: %v", filepath.Base(file), line, err)
19 func readCodeLine(t testing.TB, conn *textproto.Conn, code int) string {
20 _, message, err := conn.ReadCodeLine(code)
25 // runServer creates a TCP socket, runs a listening server, and returns the connection.
26 // The server exits when the Conn is closed.
27 func runServer(t *testing.T, server Server) net.Listener {
28 l, err := net.Listen("tcp", "localhost:0")
36 conn, err := l.Accept()
40 go AcceptConnection(conn, server)
47 type testServer struct {
51 func (s *testServer) Name() string {
55 func createClient(t *testing.T, addr net.Addr) *textproto.Conn {
56 conn, err := textproto.Dial(addr.Network(), addr.String())
65 func TestScenarioTypical(t *testing.T) {
70 conn := createClient(t, l.Addr())
72 message := readCodeLine(t, conn, 220)
73 if !strings.HasPrefix(message, s.Name()) {
74 t.Errorf("Greeting does not have server name, got %q", message)
77 greet := "greeting.TestScenarioTypical"
78 ok(t, conn.PrintfLine("EHLO "+greet))
80 _, message, err := conn.ReadResponse(250)
82 if !strings.Contains(message, greet) {
83 t.Errorf("EHLO response does not contain greeting, got %q", message)
86 ok(t, conn.PrintfLine("MAIL FROM:<Smith@bar.com>"))
87 readCodeLine(t, conn, 250)
89 ok(t, conn.PrintfLine("RCPT TO:<Jones@foo.com>"))
90 readCodeLine(t, conn, 250)
92 ok(t, conn.PrintfLine("RCPT TO:<Green@foo.com>"))
93 readCodeLine(t, conn, 250) // TODO: make this 55o by rejecting Green
95 ok(t, conn.PrintfLine("RCPT TO:<Brown@foo.com>"))
96 readCodeLine(t, conn, 250)
98 ok(t, conn.PrintfLine("DATA"))
99 readCodeLine(t, conn, 354)
101 ok(t, conn.PrintfLine("Blah blah blah..."))
102 ok(t, conn.PrintfLine("...etc. etc. etc."))
103 ok(t, conn.PrintfLine("."))
104 readCodeLine(t, conn, 250)
106 ok(t, conn.PrintfLine("QUIT"))
107 readCodeLine(t, conn, 221)