forked from emersion/go-smtp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
273 lines (222 loc) · 6.15 KB
/
server_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
package smtp_test
import (
"bufio"
"errors"
"io"
"io/ioutil"
"net"
"strings"
"testing"
"github.com/emersion/go-smtp"
)
type message struct {
From string
To []string
Data []byte
}
type backend struct {
messages []*message
}
func (be *backend) Login(username, password string) (smtp.User, error) {
if username != "username" || password != "password" {
return nil, errors.New("Invalid username or password")
}
return &user{be}, nil
}
type user struct {
backend *backend
}
func (u *user) Send(from string, to []string, r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
u.backend.messages = append(u.backend.messages, &message{
From: from,
To: to,
Data: b,
})
}
return nil
}
func (u *user) Logout() error {
return nil
}
func testServer(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
be = new(backend)
s = smtp.NewServer(be)
s.Domain = "localhost"
s.AllowInsecureAuth = true
go s.Serve(l)
c, err = net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
scanner = bufio.NewScanner(c)
return
}
func testServerGreeted(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServer(t)
scanner.Scan()
if scanner.Text() != "220 localhost ESMTP Service Ready" {
t.Fatal("Invalid greeting:", scanner.Text())
}
return
}
func testServerEhlo(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServerGreeted(t)
io.WriteString(c, "EHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid EHLO response:", scanner.Text())
}
expectedCaps := []string{"PIPELINING", "8BITMIME", "AUTH PLAIN"}
caps := map[string]bool{}
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
caps[strings.TrimPrefix(s, "250 ")] = true
break
} else {
if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
caps[strings.TrimPrefix(s, "250-")] = true
}
}
for _, cap := range expectedCaps {
if !caps[cap] {
t.Fatal("Missing capability:", cap)
}
}
return
}
func TestServer_helo(t *testing.T) {
_, s, c, scanner := testServerGreeted(t)
defer s.Close()
io.WriteString(c, "HELO localhost\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid HELO response:", scanner.Text())
}
}
func testServerAuthenticated(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServerEhlo(t)
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "334 " {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "235 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
return
}
func TestServer(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "DATA\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "354 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages)
}
msg := be.messages[0]
if msg.From != "[email protected]" {
t.Fatal("Invalid mail sender:", msg.From)
}
if len(msg.To) != 1 || msg.To[0] != "[email protected]" {
t.Fatal("Invalid mail recipients:", msg.To)
}
if string(msg.Data) != "Hey <3\n" {
t.Fatal("Invalid mail data:", string(msg.Data))
}
}
func TestServer_otherCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
io.WriteString(c, "HELP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "502 ") {
t.Fatal("Invalid HELP response:", scanner.Text())
}
io.WriteString(c, "VRFY\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "252 ") {
t.Fatal("Invalid VRFY response:", scanner.Text())
}
io.WriteString(c, "NOOP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid NOOP response:", scanner.Text())
}
io.WriteString(c, "RSET\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RSET response:", scanner.Text())
}
io.WriteString(c, "QUIT\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "221 ") {
t.Fatal("Invalid QUIT response:", scanner.Text())
}
}
func TestServer_tooManyInvalidCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
// Let's assume XXXX is a non-existing command
for i := 0; i < 4; i++ {
io.WriteString(c, "XXXX\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
func TestServer_tooLongMessage(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
s.MaxMessageBytes = 50
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "This is a very long message.\r\n")
io.WriteString(c, "Much longer than you can possibly imagine.\r\n")
io.WriteString(c, "And much longer than the server's MaxMessageBytes.\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "552 ") {
t.Fatal("Invalid DATA response, expected an error but got:", scanner.Text())
}
}