-
Notifications
You must be signed in to change notification settings - Fork 3
/
session_test.go
328 lines (249 loc) · 8.51 KB
/
session_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"bufio"
"bytes"
"fmt"
p "github.com/mpapi/failmail/parse"
"testing"
)
type mockStringReader struct {
data string
err error
}
func (m mockStringReader) ReadString(delim byte) (string, error) {
return m.data, m.err
}
func TestResponseIsClose(t *testing.T) {
r := Response{221, "Whatever"}
if !r.IsClose() {
t.Errorf("expected 221 IsClose()")
}
r = Response{200, "Whatever"}
if r.IsClose() {
t.Errorf("expected non-221 !IsClose()")
}
}
func TestResponseNeedsData(t *testing.T) {
r := Response{354, "Whatever"}
if !r.NeedsData() {
t.Errorf("expected 354 NeedsData()")
}
r = Response{200, "Whatever"}
if r.NeedsData() {
t.Errorf("expected non-354 !NeedsData()")
}
}
func TestResponseWriteTo(t *testing.T) {
output := new(bytes.Buffer)
buf := bufio.NewWriter(output)
Response{220, "Hello"}.WriteTo(buf)
contents := string(output.Bytes())
if contents != "220 Hello\r\n" {
t.Errorf("unexpected response: %s", contents)
}
}
func TestResponseWriteToMultiLine(t *testing.T) {
output := new(bytes.Buffer)
buf := bufio.NewWriter(output)
Response{250, "host1.example.com Hello host2.example.com\r\nAUTH PLAIN"}.WriteTo(buf)
contents := string(output.Bytes())
if contents != "250-host1.example.com Hello host2.example.com\r\n250 AUTH PLAIN\r\n" {
t.Errorf("unexpected response: %s", contents)
}
}
func TestSessionStart(t *testing.T) {
s := new(Session)
resp := s.Start(nil, UNENCRYPTED)
if s.Received == nil {
t.Errorf("start should set up a message")
}
if resp.Code != 220 {
t.Errorf("start should return a 220 response")
}
}
func TestSessionReadCommand(t *testing.T) {
s := new(Session)
s.Start(nil, UNENCRYPTED)
buf := bytes.NewBufferString("HELO test.example.com\r\n")
resp, err := s.ReadCommand(buf)
if err != nil {
t.Errorf("ReadCommand of a valid string shouldn't return an error")
}
if resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
resp, err = s.ReadCommand(mockStringReader{"", fmt.Errorf("Error")})
if err == nil {
t.Errorf("ReadCommand should return an error when the reader does")
}
if resp.Code != 500 {
t.Errorf("ReadCommand should return a 500 response when the reader errors")
}
}
func TestSessionAdvance(t *testing.T) {
s := new(Session)
s.Start(nil, UNENCRYPTED)
if resp := s.Advance(nil); resp.Code != 500 {
t.Errorf("nil node is not a parse error")
}
if resp := s.Advance(&p.Node{Text: "", Children: make(map[string]*p.Node), Next: nil}); resp.Code != 500 {
t.Errorf("empty node is not a parse error")
}
parser := SMTPParser()
if resp := s.Advance(parser("HELO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
if resp := s.Advance(parser("EHLO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("EHLO should get a 250 response")
}
if resp := s.Advance(parser("NOOP\r\n")); resp.Code != 250 {
t.Errorf("NOOP should get a 250 response")
}
if s.Received.From != "" {
t.Errorf("from shouldn't be set before MAIL command")
}
if resp := s.Advance(parser("RCPT TO:<test@example.com>\r\n")); resp.Code != 503 {
t.Errorf("RCPT before FROM should get a 503 response")
}
buf := bytes.NewBufferString(".\r\n")
if resp, msg := s.ReadData(buf); resp.Code != 503 || msg != nil {
t.Errorf("data read before FROM should get a 503 response")
}
if resp := s.Advance(parser("MAIL FROM:<test@example.com>\r\n")); resp.Code != 250 {
t.Errorf("FROM should get a 250 response")
}
if s.Received.From != "test@example.com" {
t.Errorf("unexpected from after MAIL command")
}
if resp := s.Advance(parser("MAIL FROM:<test@example.com>\r\n")); resp.Code != 503 {
t.Errorf("repeated MAIL should get a 503 response")
}
if len(s.Received.To) > 0 {
t.Errorf("to shouldn't be set before RCPT command")
}
if resp := s.Advance(parser("RCPT TO:<test1@example.com>\r\n")); resp.Code != 250 {
t.Errorf("RCPT should get a 250 response")
}
if !(len(s.Received.To) == 1 && s.Received.To[0] == "test1@example.com") {
t.Errorf("unexpected to after first RCPT command")
}
if resp := s.Advance(parser("RCPT TO:<test2@example.com>\r\n")); resp.Code != 250 {
t.Errorf("RCPT should get a 250 response")
}
if !(len(s.Received.To) == 2 && s.Received.To[1] == "test2@example.com") {
t.Errorf("unexpected to after second RCPT command")
}
if resp := s.Advance(parser("RCPT TO:<test2@example.com>\r\n")); resp.Code != 250 {
t.Errorf("RCPT should get a 250 response")
}
if resp := s.Advance(parser("DATA\r\n")); resp.Code != 354 {
t.Errorf("DATA should get a 354 response")
}
buf = bytes.NewBufferString("\x00\xff\r\n.\r\n")
if resp, msg := s.ReadData(buf); resp.Code != 451 || msg != nil {
t.Fatalf("bad data read should get a 451 response: %d", resp.Code)
}
if resp, msg := s.ReadData(mockStringReader{"", fmt.Errorf("error")}); resp.Code != 451 || msg != nil {
t.Errorf("expected a 451 from an error reading DATA")
}
// TODO data followed by anything else should fail
buf = bytes.NewBufferString("Subject: test\r\n\r\ntest\r\n.\r\n")
resp, msg := s.ReadData(buf)
if resp.Code != 250 || msg == nil {
t.Errorf("DATA payload should get a 250 response")
}
subject := msg.Parsed.Header.Get("subject")
if subject != "test" {
t.Errorf("failed to parse subject from data payload: %s", subject)
}
if resp := s.Advance(parser("RSET\r\n")); resp.Code != 502 {
t.Errorf("RSET should get a 502 response")
}
if resp := s.Advance(parser("VRFY test\r\n")); resp.Code != 252 {
t.Errorf("VRFY should get a 252 response, got: %d", resp.Code)
}
if resp := s.Advance(parser("QUIT\r\n")); resp.Code != 221 {
t.Errorf("QUIT should get a 221 response")
}
}
func TestSingleUserPlainAuth(t *testing.T) {
auth := &SingleUserPlainAuth{Username: "testuser", Password: "testpass"}
valid, err := auth.ValidCredentials("testuser\x00testuser\x00testpass")
if !valid {
t.Errorf("expected valid credentials")
}
if err != nil {
t.Errorf("expected no errors validating credentials")
}
}
func TestSingleUserPlainAuthBadCredentials(t *testing.T) {
auth := &SingleUserPlainAuth{Username: "testuser", Password: "testpass"}
valid, err := auth.ValidCredentials("testuser\x00testuser\x00badpass")
if valid {
t.Errorf("expected invalid credentials")
}
if err != nil {
t.Errorf("expected no errors validating credentials")
}
}
func TestSingleUserPlainAuthError(t *testing.T) {
auth := &SingleUserPlainAuth{Username: "testuser", Password: "testpass"}
valid, err := auth.ValidCredentials("testuser\x00testpass")
if valid {
t.Errorf("expected invalid credentials")
}
if err == nil {
t.Errorf("expected errors validating credentials")
}
}
func TestAuthRequired(t *testing.T) {
auth := &SingleUserPlainAuth{Username: "testuser", Password: "testpass"}
parser := SMTPParser()
s := new(Session)
s.Start(auth, UNENCRYPTED)
if resp := s.Advance(parser("HELO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
if resp := s.Advance(parser("VRFY test\r\n")); resp.Code != 530 {
t.Errorf("VRFY with auth required should get a 530 response")
}
}
func TestAuthBadMethod(t *testing.T) {
auth := &SingleUserPlainAuth{"testuser", "testpass", true}
parser := SMTPParser()
s := new(Session)
s.Start(auth, UNENCRYPTED)
if resp := s.Advance(parser("HELO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
if resp := s.Advance(parser("AUTH BADMETHOD\r\n")); resp.Code != 504 {
t.Errorf("AUTH with a bad method should get a 504 response")
}
}
func TestAuthBadCredentials(t *testing.T) {
auth := &SingleUserPlainAuth{"testuser", "testpass", true}
parser := SMTPParser()
s := new(Session)
s.Start(auth, UNENCRYPTED)
if resp := s.Advance(parser("HELO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
if resp := s.Advance(parser("AUTH PLAIN notbase64\r\n")); resp.Code != 501 {
t.Errorf("AUTH with a non-base64 payload should get a 501 response")
}
}
func TestAuthRepeated(t *testing.T) {
auth := &SingleUserPlainAuth{"testuser", "testpass", true}
parser := SMTPParser()
s := new(Session)
s.Start(auth, UNENCRYPTED)
if resp := s.Advance(parser("HELO test.example.com\r\n")); resp.Code != 250 {
t.Errorf("HELO should get a 250 response")
}
if resp := s.Advance(parser("AUTH PLAIN dGVzdHVzZXIAdGVzdHVzZXIAdGVzdHBhc3M=\r\n")); resp.Code != 235 {
t.Errorf("AUTH with a valid payload should get a 235 response")
}
if resp := s.Advance(parser("AUTH PLAIN dGVzdHVzZXIAdGVzdHVzZXIAdGVzdHBhc3M=\r\n")); resp.Code != 503 {
t.Errorf("repeated AUTH with a valid payload should get a 503 response")
}
}