From b363b65322dbb66b5c0fc35f8151d618042a7de8 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 2 Jan 2017 01:42:52 -0500 Subject: [PATCH] Fix pop3.Mailbox.Reset implementation. --- pop3.go | 4 ++-- pop3/conn_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ pop3_test.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 pop3_test.go diff --git a/pop3.go b/pop3.go index dc476d4..53c4ebc 100644 --- a/pop3.go +++ b/pop3.go @@ -178,7 +178,7 @@ func (mb *mailbox) Close() error { } func (mb *mailbox) Reset() { - for _, message := range mb.messages { - message.deleted = false + for i, _ := range mb.messages { + mb.messages[i].deleted = false } } diff --git a/pop3/conn_test.go b/pop3/conn_test.go index 4d25a39..f70cf2b 100644 --- a/pop3/conn_test.go +++ b/pop3/conn_test.go @@ -375,3 +375,50 @@ func TestUidl(t *testing.T) { {"QUIT", responseOK}, }) } + +func TestDele(t *testing.T) { + s := newTestServer() + s.mb.msgs[1] = &testMessage{1, 3, false, "abc"} + s.mb.msgs[2] = &testMessage{2, 1, false, "d"} + + clientServerTest(t, s, []requestResponse{ + {"USER u", responseOK}, + {"PASS p", responseOK}, + {"STAT", expectOKResponse(func(s string) bool { + return s == "+OK 2 4" + })}, + {"DELE 1", responseOK}, + {"STAT", expectOKResponse(func(s string) bool { + return s == "+OK 1 1" + })}, + {"RSET", responseOK}, + {"STAT", expectOKResponse(func(s string) bool { + return s == "+OK 2 4" + })}, + {"QUIT", responseOK}, + }) + + if s.mb.msgs[1].Deleted() || s.mb.msgs[2].Deleted() { + t.Errorf("RSET should not delete a message") + } + + clientServerTest(t, s, []requestResponse{ + {"USER u", responseOK}, + {"PASS p", responseOK}, + {"STAT", expectOKResponse(func(s string) bool { + return s == "+OK 2 4" + })}, + {"DELE 1", responseOK}, + {"STAT", expectOKResponse(func(s string) bool { + return s == "+OK 1 1" + })}, + {"QUIT", responseOK}, + }) + + if !s.mb.msgs[1].Deleted() { + t.Errorf("DELE did not work") + } + if s.mb.msgs[2].Deleted() { + t.Errorf("DELE the wrong message") + } +} diff --git a/pop3_test.go b/pop3_test.go new file mode 100644 index 0000000..2a63884 --- /dev/null +++ b/pop3_test.go @@ -0,0 +1,37 @@ +package main + +import ( + "testing" +) + +func TestReset(t *testing.T) { + mbox := mailbox{ + messages: []message{ + {"msg1", 1, 4, false}, + {"msg2", 2, 4, false}, + }, + } + + msg := mbox.GetMessage(1) + if msg == nil { + t.Errorf("failed to GetMessage #1") + } + + if msg.Deleted() { + t.Errorf("message %v should not be deleted", msg) + } + + if err := mbox.Delete(msg); err != nil { + t.Error(err) + } + + if !msg.Deleted() { + t.Errorf("message %v should be deleted", msg) + } + + mbox.Reset() + + if msg.Deleted() { + t.Errorf("reset did not un-delete message %v", msg) + } +} -- 2.43.5