-
Notifications
You must be signed in to change notification settings - Fork 2
/
pop3_test.go
275 lines (235 loc) · 5.74 KB
/
pop3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// mailpopbox
// Copyright 2020 Blue Static <https://www.bluestatic.org>
// This program is free software licensed under the GNU General Public License,
// version 3.0. The full text of the license can be found in LICENSE.txt.
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"io/ioutil"
"net/textproto"
"os"
"path/filepath"
"testing"
"go.uber.org/zap"
)
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)
}
}
func TestOpenMailboxAuth(t *testing.T) {
dir, err := ioutil.TempDir("", "maildrop")
if err != nil {
t.Errorf("Failed to create temp dir: %v", err)
return
}
defer os.RemoveAll(dir)
s := &pop3Server{
config: Config{
Servers: []Server{
{
Domain: "example.com",
MailboxPassword: "letmein",
MaildropPath: dir,
},
{
Domain: "test.net",
MailboxPassword: "open-sesame",
MaildropPath: dir,
},
},
},
log: zap.NewNop(),
}
cases := []struct {
user, pass string
ok bool
}{
{"[email protected]", "letmein", true},
{"[email protected]", "open-sesame", true},
{"[email protected]", "open-sesame", false},
{"[email protected]", "open-sesame", false},
{"[email protected]", "letmein", false},
}
for i, c := range cases {
mb, err := s.OpenMailbox(c.user, c.pass)
got := (mb != nil && err == nil)
if got != c.ok {
t.Errorf("Expected error=%v for case %d (%#v), got %v (error=%v, mb=%v)", c.ok, i, c, got, err, mb)
}
}
}
func TestBasicListener(t *testing.T) {
dir, err := ioutil.TempDir("", "maildrop")
if err != nil {
t.Errorf("Failed to create temp dir: %v", err)
return
}
defer os.RemoveAll(dir)
s := &pop3Server{
config: Config{
POP3Port: 9648,
Hostname: "example.com",
Servers: []Server{
{
Domain: "example.com",
MaildropPath: dir,
},
},
},
log: zap.NewNop(),
}
go s.run()
conn, err := textproto.Dial("tcp", "localhost:9648")
if err != nil {
t.Errorf("Failed to dial test server: %v", err)
return
}
_, err = conn.ReadLine()
if err != nil {
t.Errorf("Failed to read line: %v", err)
return
}
}
func TestMailbox(t *testing.T) {
dir, err := ioutil.TempDir("", "maildrop")
if err != nil {
t.Errorf("Failed to create temp dir: %v", err)
return
}
defer os.RemoveAll(dir)
// Create the first message.
f, err := os.Create(filepath.Join(dir, "a.msg"))
if err != nil {
t.Errorf("Failed to create a.msg: %v", err)
return
}
for i := 0; i < 1024*10; i++ {
buf := []byte{'a'}
_, err = f.Write(buf)
if err != nil {
t.Errorf("Failed to write a.msg: %v", err)
}
}
f.Close()
// Create the second message.
f, err = os.Create(filepath.Join(dir, "b.msg"))
if err != nil {
t.Errorf("Failed to create b.msg: %v", err)
return
}
for i := 0; i < 1024*3; i++ {
buf := []byte{'z'}
_, err = f.Write(buf)
if err != nil {
t.Errorf("Failed to write z.msg: %v", err)
}
}
f.Close()
s := &pop3Server{
config: Config{
Servers: []Server{
{
Domain: "example.com",
MailboxPassword: "letmein",
MaildropPath: dir,
},
},
},
log: zap.NewNop(),
}
// Test message metadata.
mb, err := s.OpenMailbox("[email protected]", "letmein")
if err != nil {
t.Errorf("Failed to open mailbox: %v", err)
}
msgs, err := mb.ListMessages()
if err != nil {
t.Errorf("Failed to list messages: %v", err)
}
if want, got := 2, len(msgs); want != got {
t.Errorf("Want %d messages, got %d", want, got)
}
if mb.GetMessage(0) != nil {
t.Errorf("Messages should be 1-indexed")
}
if mb.GetMessage(3) != nil {
t.Errorf("Retreived unexpected message")
}
if msgs[0] != mb.GetMessage(msgs[0].ID()) {
t.Errorf("Failed to look up message by ID")
}
if want, got := "a", msgs[0].UniqueID(); want != got {
t.Errorf("Want message #1 unique ID to be %s, got %s", want, got)
}
if want, got := 1024*10, msgs[0].Size(); want != got {
t.Errorf("Want message #1 size to be %v, got %v", want, got)
}
if want, got := "b", msgs[1].UniqueID(); want != got {
t.Errorf("Want message #2 unique ID to be %s, got %s", want, got)
}
if want, got := 1024*3, msgs[1].Size(); want != got {
t.Errorf("Want message #2 size to be %v, got %v", want, got)
}
// Test message contents.
rc, err := mb.Retrieve(msgs[0])
if err != nil {
t.Errorf("Failed to retrieve message: %v", err)
}
rc.Close()
// Test deletion marking and reset.
err = mb.Delete(msgs[1])
if err != nil {
t.Errorf("Failed to mark message #2 for deletion: %v", err)
}
if !msgs[1].Deleted() {
t.Errorf("Message should be marked for deletion and isn't")
}
mb.Reset()
if msgs[1].Deleted() {
t.Errorf("Message is marked for deletion and shouldn't be")
}
// Test deletion for real.
err = mb.Delete(msgs[0])
if err != nil {
t.Errorf("Failed to mark message for deletion: %v", err)
}
err = mb.Close()
if err != nil {
t.Errorf("Failed to close mailbox: %v", err)
}
mb, err = s.OpenMailbox("[email protected]", "letmein")
if err != nil {
t.Errorf("Failed to re-open mailbox: %v", err)
}
msgs, err = mb.ListMessages()
if err != nil {
t.Errorf("Failed to list messages: %v", err)
}
if want, got := 1, len(msgs); want != got {
t.Errorf("Number of messages should be %d, got %d", want, got)
}
if want, got := "b", msgs[0].UniqueID(); want != got {
t.Errorf("Message Unique ID should be %s, got %s", want, got)
}
}