-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpop3_test.go
94 lines (75 loc) · 1.67 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
package pop3
import (
"bufio"
"bytes"
"io"
"net"
"strings"
"testing"
"time"
)
type fakeAddr struct {}
func (fakeAddr) Network() string { return "" }
func (fakeAddr) String() string { return "" }
type faker struct {
io.ReadWriter
}
func (f faker) Close() error {
return nil
}
func (f faker) LocalAddr() net.Addr {
return fakeAddr{}
}
func (f faker) RemoteAddr() net.Addr {
return fakeAddr{}
}
func (f faker) SetDeadline(t time.Time) error {
return nil
}
func (f faker) SetReadDeadline(t time.Time) error {
return nil
}
func (f faker) SetWriteDeadline(t time.Time) error {
return nil
}
func TestBasic (t *testing.T) {
basicServer := strings.Join(strings.Split(basicServer, "\n"), "\r\n")
basicClient := strings.Join(strings.Split(basicClient, "\n"), "\r\n")
var cmdbuf bytes.Buffer
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(basicServer)), bcmdbuf)
c, err := NewClient(fake)
if err != nil {
t.Fatalf("NewClient failed: %s", err)
}
if err = c.User("uname"); err != nil {
t.Fatal("User failed: %s", err)
}
if err = c.Pass("password1"); err == nil {
t.Fatal("Pass succeeded inappropriately")
}
if err = c.Auth("uname", "password2"); err != nil {
t.Fatal("Auth failed: %s", err)
}
if err = c.Noop(); err != nil {
t.Fatal("Noop failed: %s", err)
}
bcmdbuf.Flush()
if basicClient != cmdbuf.String() {
t.Fatalf("Got:\n%s\nExpected:\n%s", cmdbuf.String(), basicClient)
}
}
var basicServer = `+OK good morning
+OK send PASS
-ERR [AUTH] mismatched username and password
+OK send PASS
+OK welcome
+OK
`
var basicClient = `USER uname
PASS password1
USER uname
PASS password2
NOOP
`