16 func _fl(depth int) string {
17 _, file, line, _ := runtime.Caller(depth + 1)
18 return fmt.Sprintf("[%s:%d]", filepath.Base(file), line)
21 func ok(t testing.TB, err error) {
23 t.Errorf("%s unexpected error: %v", _fl(1), err)
27 func responseOK(t testing.TB, conn *textproto.Conn) string {
28 line, err := conn.ReadLine()
30 t.Errorf("%s responseOK: %v", _fl(1), err)
32 if !strings.HasPrefix(line, "+OK") {
33 t.Errorf("%s expected +OK, got %q", _fl(1), line)
38 func responseERR(t testing.TB, conn *textproto.Conn) string {
39 line, err := conn.ReadLine()
41 t.Errorf("%s responseERR: %v", _fl(1), err)
43 if !strings.HasPrefix(line, "-ERR") {
44 t.Errorf("%s expected -ERR, got %q", _fl(1), line)
49 func runServer(t *testing.T, po PostOffice) net.Listener {
50 l, err := net.Listen("tcp", "localhost:0")
58 conn, err := l.Accept()
62 go AcceptConnection(conn, po)
68 type testServer struct {
73 func (s *testServer) Name() string {
77 func (s *testServer) OpenMailbox(user, pass string) (Mailbox, error) {
78 if s.user == user && s.pass == pass {
81 return nil, fmt.Errorf("bad username/pass")
84 type testMailbox struct {
85 msgs map[int]*testMessage
88 func (mb *testMailbox) ListMessages() ([]Message, error) {
89 msgs := make([]Message, 0, len(mb.msgs))
90 for i, _ := range mb.msgs {
91 msgs = append(msgs, mb.msgs[i])
96 func (mb *testMailbox) GetMessage(id int) Message {
97 if msg, ok := mb.msgs[id]; ok {
103 func (mb *testMailbox) Retrieve(msg Message) (io.ReadCloser, error) {
104 r := strings.NewReader(msg.(*testMessage).body)
105 return ioutil.NopCloser(r), nil
108 func (mb *testMailbox) Delete(msg Message) error {
109 msg.(*testMessage).deleted = true
113 func (mb *testMailbox) Close() error {
117 func (mb *testMailbox) Reset() {
118 for _, msg := range mb.msgs {
123 type testMessage struct {
130 func (m *testMessage) ID() int {
133 func (m *testMessage) Size() int {
136 func (m *testMessage) Deleted() bool {
140 func newTestServer() *testServer {
145 msgs: make(map[int]*testMessage),
151 func TestExampleSession(t *testing.T) {
156 s.mb.msgs[1] = &testMessage{1, 120, false, ""}
157 s.mb.msgs[2] = &testMessage{2, 200, false, ""}
159 conn, err := textproto.Dial(l.Addr().Network(), l.Addr().String())
162 line := responseOK(t, conn)
163 if !strings.Contains(line, s.Name()) {
164 t.Errorf("POP greeting did not include server name, got %q", line)
167 ok(t, conn.PrintfLine("USER u"))
170 ok(t, conn.PrintfLine("PASS p"))
173 ok(t, conn.PrintfLine("STAT"))
174 line = responseOK(t, conn)
175 expected := "+OK 2 320"
176 if line != expected {
177 t.Errorf("STAT expected %q, got %q", expected, line)
180 ok(t, conn.PrintfLine("LIST"))
182 lines, err := conn.ReadDotLines()
185 t.Errorf("LIST expected 2 lines, got %d", len(lines))
188 if lines[0] != expected {
189 t.Errorf("LIST line 0 expected %q, got %q", expected, lines[0])
192 if lines[1] != expected {
193 t.Errorf("LIST line 1 expected %q, got %q", expected, lines[1])
196 ok(t, conn.PrintfLine("QUIT"))
200 type requestResponse struct {
202 expecter func(testing.TB, *textproto.Conn) string
205 func expectOKResponse(predicate func(string) bool) func(testing.TB, *textproto.Conn) string {
206 return func(t testing.TB, conn *textproto.Conn) string {
207 line := responseOK(t, conn)
208 if !predicate(line) {
209 t.Errorf("%s Predicate failed, got %q", _fl(1), line)
215 func clientServerTest(t *testing.T, s *testServer, sequence []requestResponse) {
219 conn, err := textproto.Dial(l.Addr().Network(), l.Addr().String())
224 for _, pair := range sequence {
225 ok(t, conn.PrintfLine(pair.command))
226 pair.expecter(t, conn)
228 t.Logf("command %q", pair.command)
233 func TestAuthStates(t *testing.T) {
234 clientServerTest(t, newTestServer(), []requestResponse{
235 {"STAT", responseERR},
236 {"NOOP", responseOK},
237 {"USER bad", responseOK},
238 {"PASS bad", responseERR},
239 {"LIST", responseERR},
240 {"USER u", responseOK},
241 {"PASS bad", responseERR},
242 {"STAT", responseERR},
243 {"PASS p", responseOK},
244 {"QUIT", responseOK},
248 func TestDeleted(t *testing.T) {
250 s.mb.msgs[1] = &testMessage{1, 999, false, ""}
251 s.mb.msgs[2] = &testMessage{2, 10, false, ""}
253 clientServerTest(t, s, []requestResponse{
254 {"USER u", responseOK},
255 {"PASS p", responseOK},
256 {"STAT", expectOKResponse(func(s string) bool {
257 return s == "+OK 2 1009"
259 {"DELE 1", responseOK},
260 {"RETR 1", responseERR},
261 {"DELE 1", responseERR},
262 {"STAT", expectOKResponse(func(s string) bool {
263 return s == "+OK 1 10"
265 {"RSET", responseOK},
266 {"STAT", expectOKResponse(func(s string) bool {
267 return s == "+OK 2 1009"
269 {"QUIT", responseOK},
273 func TestCaseSensitivty(t *testing.T) {
275 s.mb.msgs[999] = &testMessage{999, 1, false, "a"}
277 clientServerTest(t, s, []requestResponse{
278 {"user u", responseOK},
279 {"PasS p", responseOK},
280 {"sTaT", responseOK},
281 {"retr 1", responseERR},
282 {"dele 999", responseOK},
283 {"QUIT", responseOK},
287 func TestRetr(t *testing.T) {
289 s.mb.msgs[1] = &testMessage{1, 5, false, "hello"}
290 s.mb.msgs[2] = &testMessage{2, 69, false, "this\r\nis a\r\n.\r\ntest"}
292 clientServerTest(t, s, []requestResponse{
293 {"USER u", responseOK},
294 {"PASS p", responseOK},
295 {"STAT", responseOK},
296 {"RETR 1", func(t testing.TB, tp *textproto.Conn) string {
298 resp, err := tp.ReadDotLines()
304 expected := []string{"hello"}
305 if !reflect.DeepEqual(resp, expected) {
306 t.Errorf("Expected %v, got %v", expected, resp)
311 {"RETR 2", func(t testing.TB, tp *textproto.Conn) string {
313 resp, err := tp.ReadDotLines()
319 expected := []string{"this", "is a", ".", "test"}
320 if !reflect.DeepEqual(resp, expected) {
321 t.Errorf("Expected %v, got %v", expected, resp)
326 {"QUIT", responseOK},