From 0bc26d57d4cbcfc2a082d691387189c4d635ae75 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 17 Dec 2016 16:23:18 -0500 Subject: [PATCH] Add a test for POP3 RETR. Fixes a bug where the code line was not sent. --- pop3/conn.go | 2 ++ pop3/conn_test.go | 64 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/pop3/conn.go b/pop3/conn.go index 88bd0fa..15b7eb4 100644 --- a/pop3/conn.go +++ b/pop3/conn.go @@ -209,6 +209,8 @@ func (conn *connection) doRETR() { return } + conn.ok(fmt.Sprintf("%d", msg.Size())) + w := conn.tp.DotWriter() io.Copy(w, rc) w.Close() diff --git a/pop3/conn_test.go b/pop3/conn_test.go index bfabbba..061deff 100644 --- a/pop3/conn_test.go +++ b/pop3/conn_test.go @@ -3,9 +3,11 @@ package pop3 import ( "fmt" "io" + "io/ioutil" "net" "net/textproto" "path/filepath" + "reflect" "runtime" "strings" "testing" @@ -92,11 +94,15 @@ func (mb *testMailbox) ListMessages() ([]Message, error) { } func (mb *testMailbox) GetMessage(id int) Message { - return mb.msgs[id] + if msg, ok := mb.msgs[id]; ok { + return msg + } + return nil } func (mb *testMailbox) Retrieve(msg Message) (io.ReadCloser, error) { - return nil, nil + r := strings.NewReader(msg.(*testMessage).body) + return ioutil.NopCloser(r), nil } func (mb *testMailbox) Delete(msg Message) error { @@ -118,6 +124,7 @@ type testMessage struct { id int size int deleted bool + body string } func (m *testMessage) ID() int { @@ -146,8 +153,8 @@ func TestExampleSession(t *testing.T) { l := runServer(t, s) defer l.Close() - s.mb.msgs[1] = &testMessage{1, 120, false} - s.mb.msgs[2] = &testMessage{2, 200, false} + s.mb.msgs[1] = &testMessage{1, 120, false, ""} + s.mb.msgs[2] = &testMessage{2, 200, false, ""} conn, err := textproto.Dial(l.Addr().Network(), l.Addr().String()) ok(t, err) @@ -240,8 +247,8 @@ func TestAuthStates(t *testing.T) { func TestDeleted(t *testing.T) { s := newTestServer() - s.mb.msgs[1] = &testMessage{1, 999, false} - s.mb.msgs[2] = &testMessage{2, 10, false} + s.mb.msgs[1] = &testMessage{1, 999, false, ""} + s.mb.msgs[2] = &testMessage{2, 10, false, ""} clientServerTest(t, s, []requestResponse{ {"USER u", responseOK}, @@ -265,7 +272,7 @@ func TestDeleted(t *testing.T) { func TestCaseSensitivty(t *testing.T) { s := newTestServer() - s.mb.msgs[999] = &testMessage{999, 1, false} + s.mb.msgs[999] = &testMessage{999, 1, false, "a"} clientServerTest(t, s, []requestResponse{ {"user u", responseOK}, @@ -276,3 +283,46 @@ func TestCaseSensitivty(t *testing.T) { {"QUIT", responseOK}, }) } + +func TestRetr(t *testing.T) { + s := newTestServer() + s.mb.msgs[1] = &testMessage{1, 5, false, "hello"} + s.mb.msgs[2] = &testMessage{2, 69, false, "this\r\nis a\r\n.\r\ntest"} + + clientServerTest(t, s, []requestResponse{ + {"USER u", responseOK}, + {"PASS p", responseOK}, + {"STAT", responseOK}, + {"RETR 1", func(t testing.TB, tp *textproto.Conn) string { + responseOK(t, tp) + resp, err := tp.ReadDotLines() + if err != nil { + t.Error(err) + return "" + } + + expected := []string{"hello"} + if !reflect.DeepEqual(resp, expected) { + t.Errorf("Expected %v, got %v", expected, resp) + } + + return "" + }}, + {"RETR 2", func(t testing.TB, tp *textproto.Conn) string { + responseOK(t, tp) + resp, err := tp.ReadDotLines() + if err != nil { + t.Error(err) + return "" + } + + expected := []string{"this", "is a", ".", "test"} + if !reflect.DeepEqual(resp, expected) { + t.Errorf("Expected %v, got %v", expected, resp) + } + + return "" + }}, + {"QUIT", responseOK}, + }) +} -- 2.43.5