forked from emersion/go-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmtp_server_test.go
209 lines (176 loc) · 5.18 KB
/
lmtp_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
package smtp_test
import (
"bufio"
"errors"
"io"
"net"
"strings"
"testing"
"github.com/emersion/go-smtp"
)
func testServerGreetedLMTP(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServer(t, fn...)
scanner.Scan()
if scanner.Text() != "220 localhost LMTP Service Ready" {
t.Fatal("Invalid greeting:", scanner.Text())
}
return
}
func sendDeliveryCmdsLMTP(t *testing.T, scanner *bufio.Scanner, c io.Writer) {
sendLHLO(t, scanner, c)
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, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
}
func sendLHLO(t *testing.T, scanner *bufio.Scanner, c io.Writer) {
io.WriteString(c, "LHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid LHLO response:", scanner.Text())
}
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
break
} else if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
}
}
func TestServer_LMTP(t *testing.T) {
be, s, c, scanner := testServerGreetedLMTP(t, func(s *smtp.Server) {
s.LMTP = true
be := s.Backend.(*backend)
be.implementLMTPData = true
be.lmtpStatus = []struct {
addr string
err error
}{
{"[email protected]", errors.New("nah")},
{"[email protected]", nil},
}
})
defer s.Close()
defer c.Close()
sendDeliveryCmdsLMTP(t, scanner, c)
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "554 5.0.0 <[email protected]>") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA second response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
func TestServer_LMTP_Early(t *testing.T) {
// This test confirms responses are sent as early as possible
// e.g. right after SetStatus is called.
lmtpStatusSync := make(chan struct{})
be, s, c, scanner := testServerGreetedLMTP(t, func(s *smtp.Server) {
s.LMTP = true
be := s.Backend.(*backend)
be.implementLMTPData = true
be.lmtpStatusSync = lmtpStatusSync
be.lmtpStatus = []struct {
addr string
err error
}{
{"[email protected]", errors.New("nah")},
{"[email protected]", nil},
}
})
defer s.Close()
defer c.Close()
sendDeliveryCmdsLMTP(t, scanner, c)
// Test backend sends to sync channel after calling SetStatus.
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "554 5.0.0 <[email protected]>") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
<-be.lmtpStatusSync
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA second response:", scanner.Text())
}
<-be.lmtpStatusSync
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
func TestServer_LMTP_Expand(t *testing.T) {
// This checks whether handleDataLMTP
// correctly expands results if backend doesn't
// implement LMTPSession.
be, s, c, scanner := testServerGreetedLMTP(t, func(s *smtp.Server) {
s.LMTP = true
})
defer s.Close()
defer c.Close()
sendDeliveryCmdsLMTP(t, scanner, c)
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA second response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
func TestServer_LMTP_DuplicatedRcpt(t *testing.T) {
be, s, c, scanner := testServerGreetedLMTP(t, func(s *smtp.Server) {
s.LMTP = true
be := s.Backend.(*backend)
be.implementLMTPData = true
be.lmtpStatus = []struct {
addr string
err error
}{
{"[email protected]", &smtp.SMTPError{Code: 555}},
{"[email protected]", nil},
{"[email protected]", &smtp.SMTPError{Code: 556}},
}
})
defer s.Close()
defer c.Close()
sendLHLO(t, scanner, c)
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, "RCPT TO:<[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, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "555 5.0.0 <[email protected]>") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA second response:", scanner.Text())
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "556 5.0.0 <[email protected]>") {
t.Fatal("Invalid DATA first response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}