15 "github.com/uber-go/zap"
18 func _fl(depth int) string {
19 _, file, line, _ := runtime.Caller(depth + 1)
20 return fmt.Sprintf("[%s:%d]", filepath.Base(file), line)
23 func ok(t testing.TB, err error) {
25 t.Errorf("%s unexpected error: %v", _fl(1), err)
29 func responseOK(t testing.TB, conn *textproto.Conn) string {
30 line, err := conn.ReadLine()
32 t.Errorf("%s responseOK: %v", _fl(1), err)
34 if !strings.HasPrefix(line, "+OK") {
35 t.Errorf("%s expected +OK, got %q", _fl(1), line)
40 func responseERR(t testing.TB, conn *textproto.Conn) string {
41 line, err := conn.ReadLine()
43 t.Errorf("%s responseERR: %v", _fl(1), err)
45 if !strings.HasPrefix(line, "-ERR") {
46 t.Errorf("%s expected -ERR, got %q", _fl(1), line)
51 func runServer(t *testing.T, po PostOffice) net.Listener {
52 l, err := net.Listen("tcp", "localhost:0")
60 conn, err := l.Accept()
64 go AcceptConnection(conn, po, zap.New(zap.NullEncoder()))
70 type testServer struct {
75 func (s *testServer) Name() string {
79 func (s *testServer) OpenMailbox(user, pass string) (Mailbox, error) {
80 if s.user == user && s.pass == pass {
83 return nil, fmt.Errorf("bad username/pass")
86 type testMailbox struct {
87 msgs map[int]*testMessage
90 func (mb *testMailbox) ListMessages() ([]Message, error) {
91 msgs := make([]Message, 0, len(mb.msgs))
92 for i, _ := range mb.msgs {
93 msgs = append(msgs, mb.msgs[i])
98 func (mb *testMailbox) GetMessage(id int) Message {
99 if msg, ok := mb.msgs[id]; ok {
105 func (mb *testMailbox) Retrieve(msg Message) (io.ReadCloser, error) {
106 r := strings.NewReader(msg.(*testMessage).body)
107 return ioutil.NopCloser(r), nil
110 func (mb *testMailbox) Delete(msg Message) error {
111 msg.(*testMessage).deleted = true
115 func (mb *testMailbox) Close() error {
119 func (mb *testMailbox) Reset() {
120 for _, msg := range mb.msgs {
125 type testMessage struct {
132 func (m *testMessage) ID() int {
135 func (m *testMessage) Size() int {
138 func (m *testMessage) Deleted() bool {
142 func newTestServer() *testServer {
147 msgs: make(map[int]*testMessage),
153 func TestExampleSession(t *testing.T) {
158 s.mb.msgs[1] = &testMessage{1, 120, false, ""}
159 s.mb.msgs[2] = &testMessage{2, 200, false, ""}
161 conn, err := textproto.Dial(l.Addr().Network(), l.Addr().String())
164 line := responseOK(t, conn)
165 if !strings.Contains(line, s.Name()) {
166 t.Errorf("POP greeting did not include server name, got %q", line)
169 ok(t, conn.PrintfLine("USER u"))
172 ok(t, conn.PrintfLine("PASS p"))
175 ok(t, conn.PrintfLine("STAT"))
176 line = responseOK(t, conn)
177 expected := "+OK 2 320"
178 if line != expected {
179 t.Errorf("STAT expected %q, got %q", expected, line)
182 ok(t, conn.PrintfLine("LIST"))
184 lines, err := conn.ReadDotLines()
187 t.Errorf("LIST expected 2 lines, got %d", len(lines))
190 if lines[0] != expected {
191 t.Errorf("LIST line 0 expected %q, got %q", expected, lines[0])
194 if lines[1] != expected {
195 t.Errorf("LIST line 1 expected %q, got %q", expected, lines[1])
198 ok(t, conn.PrintfLine("QUIT"))
202 type requestResponse struct {
204 expecter func(testing.TB, *textproto.Conn) string
207 func expectOKResponse(predicate func(string) bool) func(testing.TB, *textproto.Conn) string {
208 return func(t testing.TB, conn *textproto.Conn) string {
209 line := responseOK(t, conn)
210 if !predicate(line) {
211 t.Errorf("%s Predicate failed, got %q", _fl(1), line)
217 func clientServerTest(t *testing.T, s *testServer, sequence []requestResponse) {
221 conn, err := textproto.Dial(l.Addr().Network(), l.Addr().String())
226 for _, pair := range sequence {
227 ok(t, conn.PrintfLine(pair.command))
228 pair.expecter(t, conn)
230 t.Logf("command %q", pair.command)
235 func TestAuthStates(t *testing.T) {
236 clientServerTest(t, newTestServer(), []requestResponse{
237 {"STAT", responseERR},
238 {"NOOP", responseOK},
239 {"USER bad", responseOK},
240 {"PASS bad", responseERR},
241 {"LIST", responseERR},
242 {"USER u", responseOK},
243 {"PASS bad", responseERR},
244 {"STAT", responseERR},
245 {"PASS p", responseOK},
246 {"QUIT", responseOK},
250 func TestDeleted(t *testing.T) {
252 s.mb.msgs[1] = &testMessage{1, 999, false, ""}
253 s.mb.msgs[2] = &testMessage{2, 10, false, ""}
255 clientServerTest(t, s, []requestResponse{
256 {"USER u", responseOK},
257 {"PASS p", responseOK},
258 {"STAT", expectOKResponse(func(s string) bool {
259 return s == "+OK 2 1009"
261 {"DELE 1", responseOK},
262 {"RETR 1", responseERR},
263 {"DELE 1", responseERR},
264 {"STAT", expectOKResponse(func(s string) bool {
265 return s == "+OK 1 10"
267 {"RSET", responseOK},
268 {"STAT", expectOKResponse(func(s string) bool {
269 return s == "+OK 2 1009"
271 {"QUIT", responseOK},
275 func TestCaseSensitivty(t *testing.T) {
277 s.mb.msgs[999] = &testMessage{999, 1, false, "a"}
279 clientServerTest(t, s, []requestResponse{
280 {"user u", responseOK},
281 {"PasS p", responseOK},
282 {"sTaT", responseOK},
283 {"retr 1", responseERR},
284 {"dele 999", responseOK},
285 {"QUIT", responseOK},
289 func TestRetr(t *testing.T) {
291 s.mb.msgs[1] = &testMessage{1, 5, false, "hello"}
292 s.mb.msgs[2] = &testMessage{2, 69, false, "this\r\nis a\r\n.\r\ntest"}
294 clientServerTest(t, s, []requestResponse{
295 {"USER u", responseOK},
296 {"PASS p", responseOK},
297 {"STAT", responseOK},
298 {"RETR 1", func(t testing.TB, tp *textproto.Conn) string {
300 resp, err := tp.ReadDotLines()
306 expected := []string{"hello"}
307 if !reflect.DeepEqual(resp, expected) {
308 t.Errorf("Expected %v, got %v", expected, resp)
313 {"RETR 2", func(t testing.TB, tp *textproto.Conn) string {
315 resp, err := tp.ReadDotLines()
321 expected := []string{"this", "is a", ".", "test"}
322 if !reflect.DeepEqual(resp, expected) {
323 t.Errorf("Expected %v, got %v", expected, resp)
328 {"QUIT", responseOK},