]>
src.bluestatic.org Git - mailpopbox.git/blob - pop3/conn_test.go
2 // Copyright 2020 Blue Static <https://www.bluestatic.org>
3 // This program is free software licensed under the GNU General Public License,
4 // version 3.0. The full text of the license can be found in LICENSE.txt.
5 // SPDX-License-Identifier: GPL-3.0-only
25 func _fl(depth
int) string {
26 _
, file
, line
, _
:= runtime
.Caller(depth
+ 1)
27 return fmt
.Sprintf("[%s:%d]", filepath
.Base(file
), line
)
30 func ok(t testing
.TB
, err error
) {
32 t
.Errorf("%s unexpected error: %v", _fl(1), err
)
36 func responseOK(t testing
.TB
, conn
*textproto
.Conn
) string {
37 line
, err
:= conn
.ReadLine()
39 t
.Errorf("%s responseOK: %v", _fl(1), err
)
41 if !strings
.HasPrefix(line
, "+OK") {
42 t
.Errorf("%s expected +OK, got %q", _fl(1), line
)
47 func responseERR(t testing
.TB
, conn
*textproto
.Conn
) string {
48 line
, err
:= conn
.ReadLine()
50 t
.Errorf("%s responseERR: %v", _fl(1), err
)
52 if !strings
.HasPrefix(line
, "-ERR") {
53 t
.Errorf("%s expected -ERR, got %q", _fl(1), line
)
58 func runServer(t
*testing
.T
, po PostOffice
) net
.Listener
{
59 l
, err
:= net
.Listen("tcp", "localhost:0")
67 conn
, err
:= l
.Accept()
71 go AcceptConnection(conn
, po
, zap
.New(zap
.NullEncoder()))
77 type testServer
struct {
82 func (s
*testServer
) Name() string {
86 func (s
*testServer
) OpenMailbox(user
, pass
string) (Mailbox
, error
) {
87 if s
.user
== user
&& s
.pass
== pass
{
90 return nil, fmt
.Errorf("bad username/pass")
93 type testMailbox
struct {
94 msgs
map[int]*testMessage
97 type MessageList
[]Message
99 func (l MessageList
) Len() int {
102 func (l MessageList
) Less(i
, j
int) bool {
103 return l
[i
].ID() < l
[j
].ID()
105 func (l MessageList
) Swap(i
, j
int) {
106 l
[i
], l
[j
] = l
[j
], l
[i
]
109 func (mb
*testMailbox
) ListMessages() ([]Message
, error
) {
110 msgs
:= make([]Message
, 0, len(mb
.msgs
))
111 for i
, _
:= range mb
.msgs
{
112 msgs
= append(msgs
, mb
.msgs
[i
])
114 sort
.Sort(MessageList(msgs
))
118 func (mb
*testMailbox
) GetMessage(id
int) Message
{
119 if msg
, ok
:= mb
.msgs
[id
]; ok
{
125 func (mb
*testMailbox
) Retrieve(msg Message
) (io
.ReadCloser
, error
) {
126 r
:= strings
.NewReader(msg
.(*testMessage
).body
)
127 return ioutil
.NopCloser(r
), nil
130 func (mb
*testMailbox
) Delete(msg Message
) error
{
131 msg
.(*testMessage
).deleted
= true
135 func (mb
*testMailbox
) Close() error
{
139 func (mb
*testMailbox
) Reset() {
140 for _
, msg
:= range mb
.msgs
{
145 type testMessage
struct {
152 func (m
*testMessage
) UniqueID() string {
153 return fmt
.Sprintf("%p", m
)
156 func (m
*testMessage
) ID() int {
159 func (m
*testMessage
) Size() int {
162 func (m
*testMessage
) Deleted() bool {
166 func newTestServer() *testServer
{
171 msgs
: make(map[int]*testMessage
),
177 func TestExampleSession(t
*testing
.T
) {
182 s
.mb
.msgs
[1] = &testMessage
{1, 120, false, ""}
183 s
.mb
.msgs
[2] = &testMessage
{2, 200, false, ""}
185 conn
, err
:= textproto
.Dial(l
.Addr().Network(), l
.Addr().String())
188 line
:= responseOK(t
, conn
)
189 if !strings
.Contains(line
, s
.Name()) {
190 t
.Errorf("POP greeting did not include server name, got %q", line
)
193 ok(t
, conn
.PrintfLine("USER u"))
196 ok(t
, conn
.PrintfLine("PASS p"))
199 ok(t
, conn
.PrintfLine("STAT"))
200 line
= responseOK(t
, conn
)
201 expected
:= "+OK 2 320"
202 if line
!= expected
{
203 t
.Errorf("STAT expected %q, got %q", expected
, line
)
206 ok(t
, conn
.PrintfLine("LIST"))
208 lines
, err
:= conn
.ReadDotLines()
211 t
.Errorf("LIST expected 2 lines, got %d", len(lines
))
214 if lines
[0] != expected
{
215 t
.Errorf("LIST line 0 expected %q, got %q", expected
, lines
[0])
218 if lines
[1] != expected
{
219 t
.Errorf("LIST line 1 expected %q, got %q", expected
, lines
[1])
222 ok(t
, conn
.PrintfLine("QUIT"))
226 type requestResponse
struct {
228 expecter
func(testing
.TB
, *textproto
.Conn
) string
231 func expectOKResponse(predicate
func(string) bool) func(testing
.TB
, *textproto
.Conn
) string {
232 return func(t testing
.TB
, conn
*textproto
.Conn
) string {
233 line
:= responseOK(t
, conn
)
234 if !predicate(line
) {
235 t
.Errorf("%s Predicate failed, got %q", _fl(1), line
)
241 func clientServerTest(t
*testing
.T
, s
*testServer
, sequence
[]requestResponse
) {
245 conn
, err
:= textproto
.Dial(l
.Addr().Network(), l
.Addr().String())
250 for _
, pair
:= range sequence
{
251 ok(t
, conn
.PrintfLine(pair
.command
))
252 pair
.expecter(t
, conn
)
254 t
.Logf("command %q", pair
.command
)
259 func TestAuthStates(t
*testing
.T
) {
260 clientServerTest(t
, newTestServer(), []requestResponse
{
261 {"STAT", responseERR
},
262 {"NOOP", responseOK
},
263 {"USER bad", responseOK
},
264 {"PASS bad", responseERR
},
265 {"USER", responseERR
},
266 {"USER x", responseOK
},
267 {"PASS", responseERR
},
268 {"LIST", responseERR
},
269 {"USER u", responseOK
},
270 {"PASS bad", responseERR
},
271 {"STAT", responseERR
},
272 {"PASS p", responseOK
},
273 {"QUIT", responseOK
},
277 func TestDeleted(t
*testing
.T
) {
279 s
.mb
.msgs
[1] = &testMessage
{1, 999, false, ""}
280 s
.mb
.msgs
[2] = &testMessage
{2, 10, false, ""}
282 clientServerTest(t
, s
, []requestResponse
{
283 {"USER u", responseOK
},
284 {"PASS p", responseOK
},
285 {"STAT", expectOKResponse(func(s
string) bool {
286 return s
== "+OK 2 1009"
288 {"DELE 1", responseOK
},
289 {"RETR 1", responseERR
},
290 {"DELE 1", responseERR
},
291 {"STAT", expectOKResponse(func(s
string) bool {
292 return s
== "+OK 1 10"
294 {"RSET", responseOK
},
295 {"STAT", expectOKResponse(func(s
string) bool {
296 return s
== "+OK 2 1009"
298 {"QUIT", responseOK
},
302 func TestCaseSensitivty(t
*testing
.T
) {
304 s
.mb
.msgs
[999] = &testMessage
{999, 1, false, "a"}
306 clientServerTest(t
, s
, []requestResponse
{
307 {"user u", responseOK
},
308 {"PasS p", responseOK
},
309 {"sTaT", responseOK
},
310 {"retr 1", responseERR
},
311 {"dele 999", responseOK
},
312 {"QUIT", responseOK
},
316 func TestRetr(t
*testing
.T
) {
318 s
.mb
.msgs
[1] = &testMessage
{1, 5, false, "hello"}
319 s
.mb
.msgs
[2] = &testMessage
{2, 69, false, "this\r\nis a\r\n.\r\ntest"}
321 clientServerTest(t
, s
, []requestResponse
{
322 {"USER u", responseOK
},
323 {"PASS p", responseOK
},
324 {"STAT", responseOK
},
325 {"RETR 1", func(t testing
.TB
, tp
*textproto
.Conn
) string {
331 resp
, err
:= tp
.ReadDotLines()
337 expected
:= []string{"hello"}
338 if !reflect
.DeepEqual(resp
, expected
) {
339 t
.Errorf("Expected %v, got %v", expected
, resp
)
344 {"RETR 2", func(t testing
.TB
, tp
*textproto
.Conn
) string {
350 resp
, err
:= tp
.ReadDotLines()
356 expected
:= []string{"this", "is a", ".", "test"}
357 if !reflect
.DeepEqual(resp
, expected
) {
358 t
.Errorf("Expected %v, got %v", expected
, resp
)
363 {"QUIT", responseOK
},
367 func TestUidl(t
*testing
.T
) {
369 s
.mb
.msgs
[1] = &testMessage
{1, 3, false, "abc"}
370 s
.mb
.msgs
[2] = &testMessage
{2, 1, true, "Z"}
371 s
.mb
.msgs
[3] = &testMessage
{3, 4, false, "test"}
373 clientServerTest(t
, s
, []requestResponse
{
374 {"USER u", responseOK
},
375 {"PASS p", responseOK
},
376 {"UIDL", func(t testing
.TB
, tp
*textproto
.Conn
) string {
382 resp
, err
:= tp
.ReadDotLines()
388 expected
:= []string{
389 fmt
.Sprintf("1 %p", s
.mb
.msgs
[1]),
390 fmt
.Sprintf("3 %p", s
.mb
.msgs
[3]),
392 if !reflect
.DeepEqual(resp
, expected
) {
393 t
.Errorf("Expected %v, got %v", expected
, resp
)
398 {"QUIT", responseOK
},
402 func TestDele(t
*testing
.T
) {
404 s
.mb
.msgs
[1] = &testMessage
{1, 3, false, "abc"}
405 s
.mb
.msgs
[2] = &testMessage
{2, 1, false, "d"}
407 clientServerTest(t
, s
, []requestResponse
{
408 {"USER u", responseOK
},
409 {"PASS p", responseOK
},
410 {"STAT", expectOKResponse(func(s
string) bool {
411 return s
== "+OK 2 4"
413 {"DELE 1", responseOK
},
414 {"STAT", expectOKResponse(func(s
string) bool {
415 return s
== "+OK 1 1"
417 {"RSET", responseOK
},
418 {"STAT", expectOKResponse(func(s
string) bool {
419 return s
== "+OK 2 4"
421 {"QUIT", responseOK
},
424 if s
.mb
.msgs
[1].Deleted() || s
.mb
.msgs
[2].Deleted() {
425 t
.Errorf("RSET should not delete a message")
428 clientServerTest(t
, s
, []requestResponse
{
429 {"USER u", responseOK
},
430 {"PASS p", responseOK
},
431 {"STAT", expectOKResponse(func(s
string) bool {
432 return s
== "+OK 2 4"
434 {"DELE 1", responseOK
},
435 {"STAT", expectOKResponse(func(s
string) bool {
436 return s
== "+OK 1 1"
438 {"QUIT", responseOK
},
441 if !s
.mb
.msgs
[1].Deleted() {
442 t
.Errorf("DELE did not work")
444 if s
.mb
.msgs
[2].Deleted() {
445 t
.Errorf("DELE the wrong message")
449 func TestCapa(t
*testing
.T
) {
452 capaTest
:= func(t testing
.TB
, tp
*textproto
.Conn
) string {
458 resp
, err
:= tp
.ReadDotLines()
470 caps
:= map[string]int{
474 for _
, line
:= range resp
{
475 if val
, ok
:= caps
[line
]; ok
{
476 if val
== capNeeded
{
479 t
.Errorf("unxpected capa value %q", line
)
485 for c
, val
:= range caps
{
487 t
.Errorf("unexpected capa value for %q: %d", c
, val
)
493 clientServerTest(t
, s
, []requestResponse
{
495 {"USER u", responseOK
},
497 {"PASS p", responseOK
},
499 {"QUIT", responseOK
},