/
conn.go
290 lines (243 loc) · 6.64 KB
/
conn.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
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tests
import (
"bufio"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"net"
"path"
"strconv"
"strings"
"time"
)
// Conn is a helper that simplifies testing of text protocol interactions.
type Conn struct {
T *T
WriteTimeout time.Duration
ReadTimeout time.Duration
allowIOErr bool
Conn net.Conn
Scanner *bufio.Scanner
}
// AllowIOErr toggles whether I/O errors should be returned to the caller of
// Conn method or should immedately fail the test.
//
// By default (ok = false), the latter happens.
func (c *Conn) AllowIOErr(ok bool) {
c.allowIOErr = ok
}
// Write writes the string to the connection socket.
func (c *Conn) Write(s string) {
c.T.Helper()
// Make sure the test will not accidentally hang waiting for I/O forever if
// the server breaks.
if err := c.Conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout)); err != nil {
c.fatal("Cannot set write deadline: %v", err)
}
defer func() {
if err := c.Conn.SetWriteDeadline(time.Time{}); err != nil {
c.log('-', "Failed to reset connection deadline: %v", err)
}
}()
c.log('>', "%s", s)
if _, err := io.WriteString(c.Conn, s); err != nil {
c.fatal("Unexpected I/O error: %v", err)
}
}
func (c *Conn) Writeln(s string) {
c.T.Helper()
c.Write(s + "\r\n")
}
func (c *Conn) Readln() (string, error) {
c.T.Helper()
// Make sure the test will not accidentally hang waiting for I/O forever if
// the server breaks.
if err := c.Conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)); err != nil {
c.fatal("Cannot set write deadline: %v", err)
}
defer func() {
if err := c.Conn.SetReadDeadline(time.Time{}); err != nil {
c.log('-', "Failed to reset connection deadline: %v", err)
}
}()
if !c.Scanner.Scan() {
if err := c.Scanner.Err(); err != nil {
if c.allowIOErr {
return "", err
}
c.fatal("Unexpected I/O error: %v", err)
}
if c.allowIOErr {
return "", io.EOF
}
c.fatal("Unexpected EOF")
}
c.log('<', "%v", c.Scanner.Text())
return c.Scanner.Text(), nil
}
func (c *Conn) Expect(line string) error {
c.T.Helper()
actual, err := c.Readln()
if err != nil {
return err
}
if line != actual {
c.T.Fatalf("Response line not matching the expected one, want %q", line)
}
return nil
}
// ExpectPattern reads a line from the connection socket and checks whether is
// matches the supplied shell pattern (as defined by path.Match). The original
// line is returned.
func (c *Conn) ExpectPattern(pat string) string {
c.T.Helper()
line, err := c.Readln()
if err != nil {
c.T.Fatal("Unexpected I/O error:", err)
}
match, err := path.Match(pat, line)
if err != nil {
c.T.Fatal("Malformed pattern:", err)
}
if !match {
c.T.Fatalf("Response line not matching the expected pattern, want %q", pat)
}
return line
}
func (c *Conn) fatal(f string, args ...interface{}) {
c.T.Helper()
c.log('-', f, args...)
c.T.FailNow()
}
func (c *Conn) log(direction rune, f string, args ...interface{}) {
c.T.Helper()
local, remote := c.Conn.LocalAddr().(*net.TCPAddr), c.Conn.RemoteAddr().(*net.TCPAddr)
msg := strings.Builder{}
if local.IP.IsLoopback() {
msg.WriteString(strconv.Itoa(local.Port))
} else {
msg.WriteString(local.String())
}
msg.WriteRune(' ')
msg.WriteRune(direction)
msg.WriteRune(' ')
if remote.IP.IsLoopback() {
textPort := c.T.portsRev[uint16(remote.Port)]
if textPort != "" {
msg.WriteString(textPort)
} else {
msg.WriteString(strconv.Itoa(remote.Port))
}
} else {
msg.WriteString(local.String())
}
if _, ok := c.Conn.(*tls.Conn); ok {
msg.WriteString(" [tls]")
}
msg.WriteString(": ")
fmt.Fprintf(&msg, f, args...)
c.T.Log(strings.TrimRight(msg.String(), "\r\n "))
}
func (c *Conn) TLS() {
c.T.Helper()
tlsC := tls.Client(c.Conn, &tls.Config{
ServerName: "maddy.test",
InsecureSkipVerify: true,
})
if err := tlsC.Handshake(); err != nil {
c.fatal("TLS handshake fail: %v", err)
}
c.Conn = tlsC
c.Scanner = bufio.NewScanner(c.Conn)
}
func (c *Conn) SMTPPlainAuth(username, password string, expectOk bool) {
c.T.Helper()
resp := append([]byte{0x00}, username...)
resp = append(resp, 0x00)
resp = append(resp, password...)
c.Writeln("AUTH PLAIN " + base64.StdEncoding.EncodeToString(resp))
if expectOk {
c.ExpectPattern("235 *")
} else {
c.ExpectPattern("*")
}
}
func (c *Conn) SMTPNegotation(ourName string, requireExts, blacklistExts []string) {
c.T.Helper()
needCapsMap := make(map[string]bool)
blacklistCapsMap := make(map[string]bool)
for _, ext := range requireExts {
needCapsMap[ext] = false
}
for _, ext := range blacklistExts {
blacklistCapsMap[ext] = false
}
c.Writeln("EHLO " + ourName)
// Consume the first line from socket, it is either initial greeting (sent
// before we sent EHLO) or the EHLO reply in case of re-negotiation after
// STARTTLS.
l, err := c.Readln()
if err != nil {
c.T.Fatal("I/O error during SMTP negotiation:", err)
}
if strings.HasPrefix(l, "220") {
// That was initial greeting, consume one more line.
c.ExpectPattern("250-*")
}
var caps []string
capsloop:
for {
line, err := c.Readln()
if err != nil {
c.T.Fatal("I/O error during SMTP negotiation:", err)
}
switch {
case strings.HasPrefix(line, "250-"):
caps = append(caps, strings.TrimPrefix(line, "250-"))
case strings.HasPrefix(line, "250 "):
caps = append(caps, strings.TrimPrefix(line, "250 "))
break capsloop
default:
c.T.Fatal("Unexpected reply during SMTP negotiation:", line)
}
}
for _, ext := range caps {
needCapsMap[ext] = true
if _, ok := blacklistCapsMap[ext]; ok {
blacklistCapsMap[ext] = true
}
}
for ext, status := range needCapsMap {
if !status {
c.T.Fatalf("Capability %v is missing but required", ext)
}
}
for ext, status := range blacklistCapsMap {
if status {
c.T.Fatalf("Capability %v is present but not allowed", ext)
}
}
}
func (c *Conn) Close() error {
return c.Conn.Close()
}
func (c *Conn) Rebind(subtest *T) *Conn {
cpy := *c
cpy.T = subtest
return &cpy
}