-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdownstream_test.go
407 lines (322 loc) · 10.6 KB
/
downstream_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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"net/textproto"
"os"
"strings"
"sync"
"testing"
"time"
)
var testLogger = log.New(ioutil.Discard, "", log.LstdFlags)
type BadClient struct{}
func (b BadClient) Read(p []byte) (int, error) {
return 0, fmt.Errorf("bad read from bad client")
}
func (b BadClient) Write(p []byte) (int, error) {
return len(p), nil
}
func (b BadClient) Close() error {
return nil
}
type BadServerSocket struct {
}
func (b *BadServerSocket) Accept() (net.Conn, error) {
return nil, fmt.Errorf("bad accept")
}
func (b *BadServerSocket) Close() error {
return fmt.Errorf("bad close")
}
func (b *BadServerSocket) Addr() net.Addr {
return nil
}
func (b *BadServerSocket) Fd() (uintptr, error) {
return 0, nil
}
func (b *BadServerSocket) String() string {
return "bad"
}
type MockServerSocket struct {
conns []net.Conn
closes chan bool
}
func NewMockSocket() (*MockServerSocket, net.Conn) {
server, client := net.Pipe()
socket := &MockServerSocket{[]net.Conn{server}, make(chan bool, 0)}
return socket, client
}
func (m *MockServerSocket) Accept() (net.Conn, error) {
if len(m.conns) == 0 {
<-m.closes
return nil, fmt.Errorf("no more connections")
}
conn := m.conns[0]
m.conns = append(m.conns[:0], m.conns[1:]...)
return conn, nil
}
func (m *MockServerSocket) Addr() net.Addr {
return nil
}
func (m *MockServerSocket) Close() error {
m.closes <- true
return nil
}
func (m *MockServerSocket) Fd() (uintptr, error) {
return 99, nil
}
func (m *MockServerSocket) String() string {
return "mock"
}
func TestListener(t *testing.T) {
socket, client := NewMockSocket()
listener := &Listener{Socket: socket}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go connectAndShutdown(t, textproto.NewConn(client), shutdown, GracefulShutdown)
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func TestListenerWithFileSocket(t *testing.T) {
tcpSocket, err := NewTCPServerSocket("localhost:10010")
if err != nil {
t.Fatalf("failed to create TCP socket: %s", err)
}
fd, err := tcpSocket.Fd()
if err != nil {
t.Fatalf("failed to get file descriptor from TCP socket: %s", err)
}
socket, err := NewFileServerSocket(fd)
if err != nil {
t.Fatalf("failed to create socket from fd: %s", err)
}
defer socket.Close()
listener := &Listener{Socket: socket, Debug: true}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go dialAndShutdown(t, "localhost:10010", shutdown, GracefulShutdown)
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func TestListenerReload(t *testing.T) {
socket, err := NewTCPServerSocket("localhost:10020")
if err != nil {
t.Fatalf("failed to create socket")
}
defer socket.Close()
listener := &Listener{Socket: socket}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go dialAndShutdown(t, "localhost:10020", shutdown, Reload)
if fd, err := listener.Listen(received, shutdown, 1*time.Second); err != nil {
t.Fatalf("unexpected error returned from Listen(): %s", err)
} else if fd <= 0 {
t.Fatalf("unexpected file descriptor returned from Listen(): %d", fd)
}
}
func TestListenerWithMessage(t *testing.T) {
socket, client := NewMockSocket()
listener := &Listener{Socket: socket}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go func() {
req := <-received
req.StorageErrors <- nil
}()
go func() {
conn := textproto.NewConn(client)
if _, _, err := conn.ReadCodeLine(220); err != nil {
t.Errorf("unexpected response from server: %s", err)
}
sendAndExpect(conn, t, "HELO localhost", 250)
sendAndExpect(conn, t, "MAIL FROM:<test@localhost>", 250)
sendAndExpect(conn, t, "RCPT TO:<test@localhost>", 250)
sendAndExpect(conn, t, "DATA", 354)
sendAndExpect(conn, t, "Subject: test\r\n\r\nbody\r\n.", 250)
sendAndExpect(conn, t, "QUIT", 221)
if err := conn.Close(); err != nil {
t.Errorf("failed to close listener: %s", err)
}
shutdown <- GracefulShutdown
}()
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func TestListenerWithBadClient(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
defer log.SetOutput(os.Stderr)
l := &Listener{}
received := make(chan *StorageRequest, 1)
l.handleConnection(BadClient{}, received)
if msg := string(buf.Bytes()); !strings.Contains(msg, "bad read from bad client") {
t.Errorf("bad client didn't trigger failure in handleConnection(): %#v", msg)
}
}
func TestListenerWithBadServer(t *testing.T) {
buf := new(bytes.Buffer)
log.SetOutput(buf)
defer log.SetOutput(os.Stderr)
socket := new(BadServerSocket)
l := &Listener{Socket: socket}
received := make(chan *StorageRequest, 1)
l.Listen(received, make(chan TerminationRequest, 0), 100*time.Millisecond)
if msg := string(buf.Bytes()); !strings.Contains(msg, "bad accept") {
t.Errorf("bad socket Accept() didn't trigger failure in Listen(): %#v", msg)
}
}
func TestListenerWithAuth(t *testing.T) {
socket, client := NewMockSocket()
auth := &SingleUserPlainAuth{"test", "test", true}
listener := &Listener{Socket: socket, Auth: auth}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go func() {
conn := textproto.NewConn(client)
_, _, err := conn.ReadCodeLine(220)
if err != nil {
t.Errorf("unexpected response from server: %s", err)
}
sendAndExpect(conn, t, "HELO localhost", 250)
sendAndExpect(conn, t, "AUTH PLAIN dGVzdAB0ZXN0AHRlc3Q=", 235)
sendAndExpect(conn, t, "QUIT", 221)
err = conn.Close()
if err != nil {
t.Errorf("failed to close listener: %s", err)
}
shutdown <- GracefulShutdown
}()
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func TestListenerWithPartialAuth(t *testing.T) {
socket, client := NewMockSocket()
auth := &SingleUserPlainAuth{"test", "test", true}
listener := &Listener{Socket: socket, Auth: auth}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go func() {
conn := textproto.NewConn(client)
_, _, err := conn.ReadCodeLine(220)
if err != nil {
t.Errorf("unexpected response from server: %s", err)
}
sendAndExpect(conn, t, "HELO localhost", 250)
sendAndExpect(conn, t, "AUTH PLAIN", 334)
sendAndExpect(conn, t, "dGVzdAB0ZXN0AHRlc3Q=", 235)
sendAndExpect(conn, t, "QUIT", 221)
err = conn.Close()
if err != nil {
t.Errorf("failed to close listener: %s", err)
}
shutdown <- GracefulShutdown
}()
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func TestListenerWithTLS(t *testing.T) {
socket, err := NewTCPServerSocket("localhost:10030")
if err != nil {
t.Fatalf("failed to create socket")
}
defer socket.Close()
certs := buildCerts()
if len(certs) == 0 {
t.Fatalf("failed to read certificates for TLS test")
}
listener := &Listener{Socket: socket, Security: TLS_PRE_STARTTLS, TLSConfig: &tls.Config{Certificates: certs}}
shutdown := make(chan TerminationRequest, 0)
received := make(chan *StorageRequest, 1)
go func() {
rawConn, err := net.Dial("tcp", "localhost:10030")
if err != nil {
t.Fatalf("failed to connect to listener: %s", err)
}
conn := textproto.NewConn(rawConn)
_, _, err = conn.ReadCodeLine(220)
if err != nil {
t.Errorf("unexpected response from server: %s", err)
}
sendAndExpect(conn, t, "HELO localhost", 250)
sendAndExpect(conn, t, "STARTTLS", 220)
conn = textproto.NewConn(tls.Client(rawConn, &tls.Config{InsecureSkipVerify: true}))
sendAndExpect(conn, t, "QUIT", 221)
if err := conn.Close(); err != nil {
t.Errorf("failed to close listener: %s", err)
}
shutdown <- GracefulShutdown
}()
listener.Listen(received, shutdown, 100*time.Millisecond)
}
func sendAndExpect(conn *textproto.Conn, t *testing.T, line string, code int) {
err := conn.PrintfLine(line)
if err != nil {
t.Errorf("unexpected error writing to server: %s", err)
}
_, _, err = conn.ReadCodeLine(code)
if err != nil {
t.Errorf("unexpected response from server: %s", err)
}
}
func TestWaitWithTimeoutNoTimeout(t *testing.T) {
wg := new(sync.WaitGroup)
wg.Add(1)
if noTimeout := WaitWithTimeout(wg, 10*time.Millisecond); noTimeout {
t.Errorf("expected a timeout from WaitWithTimeout")
}
}
func TestWaitWithTimeout(t *testing.T) {
wg := new(sync.WaitGroup)
if noTimeout := WaitWithTimeout(wg, 10*time.Millisecond); !noTimeout {
t.Errorf("expected no timeout from WaitWithTimeout")
}
}
func buildCerts() []tls.Certificate {
pemCert := `-----BEGIN CERTIFICATE-----
MIIB0zCCAX2gAwIBAgIJAN3/7+49TYhaMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTQwOTI4MTMyODMzWhcNMTQxMDI4MTMyODMzWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMQB
p1QnWVSC8kkc1HViRMUR7GIBuE4dlb/8rJ/WLaD0lT1t1eNWYZNrbWJ3vSRVSNv+
1CCKj1rDyjfSfX8O430CAwEAAaNQME4wHQYDVR0OBBYEFJA4xJvhsRGC/xlBTlMS
XCf8McIMMB8GA1UdIwQYMBaAFJA4xJvhsRGC/xlBTlMSXCf8McIMMAwGA1UdEwQF
MAMBAf8wDQYJKoZIhvcNAQELBQADQQCm1i+WaR/2y0jBsHBoX5BkqqAemZeGXtxM
P1Vcabz8ZWDEPjAliWBzQuWO15cDMiLXxW2QekVPTO1b4ZiB1Mvp
-----END CERTIFICATE-----`
pemKey := `-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAMQBp1QnWVSC8kkc1HViRMUR7GIBuE4dlb/8rJ/WLaD0lT1t1eNW
YZNrbWJ3vSRVSNv+1CCKj1rDyjfSfX8O430CAwEAAQJAIdETOH6td9o7yQdzVGlG
6iVEfkhDrx6FlqEWe2EtcCZVR3nyl6d2HbRy9kyvwECQlPqpHZRVzqq1Q8gElAuz
1QIhAONmXF36or6hrzr8ov4kOQ24QuyyE5l0aOo/YFMteh9fAiEA3KiDdqZuRSmC
Zv+GaFr1+MRXt1ZAXV5RL6e5lsodVqMCIQDTCUsNeK4ShpDOCGnnu4wrXGbXrcgc
sPkw89IcP2dHtwIgduZOwHZ54Ma3P6zczgqFlCCoa2AMmsMh2B32wSvzlyUCIDnu
3kB1gcsw+gLW70mbZxw+tAx6a7kBDNz+VCLW6RDT
-----END RSA PRIVATE KEY-----`
cert, err := tls.X509KeyPair([]byte(pemCert), []byte(pemKey))
if err != nil {
return []tls.Certificate{}
}
return []tls.Certificate{cert}
}
func dialAndShutdown(t *testing.T, addr string, shutdown chan<- TerminationRequest, req TerminationRequest) {
conn, err := textproto.Dial("tcp", addr)
if err != nil {
t.Fatalf("failed to connect to listener: %s", err)
}
connectAndShutdown(t, conn, shutdown, req)
}
func connectAndShutdown(t *testing.T, conn *textproto.Conn, shutdown chan<- TerminationRequest, req TerminationRequest) {
if _, _, err := conn.ReadCodeLine(220); err != nil {
t.Errorf("unexpected response from server: %s", err)
}
if err := conn.PrintfLine("QUIT"); err != nil {
t.Errorf("unexpected error writing to server: %s", err)
}
if _, _, err := conn.ReadCodeLine(221); err != nil {
t.Errorf("unexpected response from server: %s", err)
}
if err := conn.Close(); err != nil {
t.Errorf("failed to close listener: %s", err)
}
shutdown <- req
}