forked from flashmob/go-guerrilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
469 lines (420 loc) · 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package guerrilla
import (
"testing"
"bufio"
"net/textproto"
"strings"
"sync"
"crypto/tls"
"fmt"
"github.com/flashmob/go-guerrilla/backends"
"github.com/flashmob/go-guerrilla/log"
"github.com/flashmob/go-guerrilla/mail"
"github.com/flashmob/go-guerrilla/mocks"
"io/ioutil"
"net"
"os"
)
// getMockServerConfig gets a mock ServerConfig struct used for creating a new server
func getMockServerConfig() *ServerConfig {
sc := &ServerConfig{
IsEnabled: true, // not tested here
Hostname: "saggydimes.test.com",
MaxSize: 1024, // smtp message max size
TLS: ServerTLSConfig{
PrivateKeyFile: "./tests/mail.guerrillamail.com.key.pem",
PublicKeyFile: "./tests/mail.guerrillamail.com.cert.pem",
StartTLSOn: true,
AlwaysOn: false,
},
Timeout: 5,
ListenInterface: "127.0.0.1:2529",
MaxClients: 30, // not tested here
LogFile: "./tests/testlog",
}
return sc
}
// getMockServerConn gets a new server using sc. Server will be using a mocked TCP connection
// using the dummy backend
// RCP TO command only allows test.com host
func getMockServerConn(sc *ServerConfig, t *testing.T) (*mocks.Conn, *server) {
var logOpenError error
var mainlog log.Logger
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
backend, err := backends.New(
backends.BackendConfig{"log_received_mails": true, "save_workers_size": 1},
mainlog)
if err != nil {
t.Error("new dummy backend failed because:", err)
}
server, err := newServer(sc, backend, mainlog)
if err != nil {
//t.Error("new server failed because:", err)
} else {
server.setAllowedHosts([]string{"test.com"})
}
conn := mocks.NewConn()
return conn, server
}
// test the RootCAs tls config setting
var rootCAPK = `-----BEGIN CERTIFICATE-----
MIIDqjCCApKgAwIBAgIJALh2TrsBR5MiMA0GCSqGSIb3DQEBCwUAMGkxCzAJBgNV
BAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEhMB8G
A1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMRIwEAY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VQQDDAlsb2NhbGhv
c3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDlkuYwF0qRXJWY3RWE
NIUa+Aqnbh6Mc8K/7B+5/e+VT1zAp8dHe96OV5kBVFyJ2xx8DQmy5v8aWyvRfB5x
qC2m9Mj4WnWKTqwgI7P5Vbf3nypoCLxONtQyNwMcl5ItyOvvbkIfm4i+FHNH51ph
olCEBwvYwS3wz1YgXoCO0NkQf1Hqgzy3kWQ7RqYiaWeKlhc0EWCGbORnN8ZddFAv
DYD67qR4tyMKuf2HYFqh8YiNB9hy7/d8fhwgVHjP+ygYpqZ6qcJTPt1aZt/i0Aj7
xBHRVdcEqGIcOb3y09J4f33iELnZS6WuMRVwzlSjHmFrNLY8rTqhJzVi4Vk42p54
aqPrAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAIQmlo8iCpyYggkbpfDmThBPHfy1
cZcCi/tRFoFe1ccwn2ezLMIKmW38ZebiroawwqrZgU6AP+dMxVKLMjpyLPSrpFKa
3o/LbVF7qMfH8/y2q8t7javd6rxoENH9uxLyHhauzI1iWy0whoDWBNiZrPBTBCjq
jDGZARZqGyrPeXi+RNe1cMvZCxAFy7gqEtWFLWWrp0gYNPvxkHhhQBrUcF+8T/Nf
9G4hKZSN/KAgC0CNBVuNrdyNc3l8H66BfwwL5X0+pesBYZM+MEfmBZOo+p7OWx2r
ug8tR8eSL1vGleONtFRBUVG7NbtjhBf9FhvPZcSRR10od/vWHku9E01i4xg=
-----END CERTIFICATE-----`
func TestTLSConfig(t *testing.T) {
if err := ioutil.WriteFile("rootca.test.pem", []byte(rootCAPK), 0644); err != nil {
t.Fatal("couldn't create rootca.test.pem file.", err)
return
}
if err := ioutil.WriteFile("client.test.key", []byte(clientPrvKey), 0644); err != nil {
t.Fatal("couldn't create client.test.key file.", err)
return
}
if err := ioutil.WriteFile("client.test.pem", []byte(clientPubKey), 0644); err != nil {
t.Fatal("couldn't create client.test.pem file.", err)
return
}
s := server{}
s.setConfig(&ServerConfig{
TLS: ServerTLSConfig{
StartTLSOn: true,
PrivateKeyFile: "client.test.key",
PublicKeyFile: "client.test.pem",
RootCAs: "rootca.test.pem",
ClientAuthType: "NoClientCert",
Curves: []string{"P521", "P384"},
Ciphers: []string{"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"},
Protocols: []string{"tls1.0", "tls1.2"},
},
})
s.configureSSL()
c := s.tlsConfigStore.Load().(*tls.Config)
if len(c.CurvePreferences) != 2 {
t.Error("c.CurvePreferences should have two elements")
} else if c.CurvePreferences[0] != tls.CurveP521 && c.CurvePreferences[1] != tls.CurveP384 {
t.Error("c.CurvePreferences curves not setup")
}
if strings.Index(string(c.RootCAs.Subjects()[0]), "Mountain View") == -1 {
t.Error("c.RootCAs not correctly set")
}
if c.ClientAuth != tls.NoClientCert {
t.Error("c.ClientAuth should be tls.NoClientCert")
}
if len(c.CipherSuites) != 2 {
t.Error("c.CipherSuites length should be 2")
}
if c.CipherSuites[0] != tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 && c.CipherSuites[1] != tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA {
t.Error("c.CipherSuites not correctly set ")
}
if c.MinVersion != tls.VersionTLS10 {
t.Error("c.MinVersion should be tls.VersionTLS10")
}
if c.MaxVersion != tls.VersionTLS12 {
t.Error("c.MinVersion should be tls.VersionTLS10")
}
if c.PreferServerCipherSuites != false {
t.Error("PreferServerCipherSuites should be false")
}
os.Remove("rootca.test.pem")
os.Remove("client.test.key")
os.Remove("client.test.pem")
}
func TestHandleClient(t *testing.T) {
var mainlog log.Logger
var logOpenError error
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
// call the serve.handleClient() func in a goroutine.
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
w.PrintfLine("HELO test.test.com")
line, _ = r.ReadLine()
//fmt.Println(line)
w.PrintfLine("QUIT")
line, _ = r.ReadLine()
//fmt.Println("line is:", line)
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
func TestXClient(t *testing.T) {
var mainlog log.Logger
var logOpenError error
sc := getMockServerConfig()
sc.XClientOn = true
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
// call the serve.handleClient() func in a goroutine.
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
w.PrintfLine("HELO test.test.com")
line, _ = r.ReadLine()
//fmt.Println(line)
w.PrintfLine("XCLIENT ADDR=212.96.64.216 NAME=[UNAVAILABLE]")
line, _ = r.ReadLine()
if client.RemoteIP != "212.96.64.216" {
t.Error("client.RemoteIP should be 212.96.64.216, but got:", client.RemoteIP)
}
expected := "250 2.1.0 OK"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
// try malformed input
w.PrintfLine("XCLIENT c")
line, _ = r.ReadLine()
expected = "250 2.1.0 OK"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
w.PrintfLine("QUIT")
line, _ = r.ReadLine()
wg.Wait() // wait for handleClient to exit
}
// The backend gateway should time out after 1 second because it sleeps for 2 sec.
// The transaction should wait until finished, and then test to see if we can do
// a second transaction
func TestGatewayTimeout(t *testing.T) {
bcfg := backends.BackendConfig{
"save_workers_size": 1,
"save_process": "HeadersParser|Debugger",
"log_received_mails": true,
"primary_mail_host": "example.com",
"gw_save_timeout": "1s",
"gw_val_rcpt_timeout": "1s",
"sleep_seconds": 2,
}
cfg := &AppConfig{
LogFile: log.OutputOff.String(),
AllowedHosts: []string{"grr.la"},
}
cfg.BackendConfig = bcfg
d := Daemon{Config: cfg}
err := d.Start()
if err != nil {
t.Error("server didn't start")
} else {
conn, err := net.Dial("tcp", "127.0.0.1:2525")
if err != nil {
return
}
in := bufio.NewReader(conn)
str, err := in.ReadString('\n')
fmt.Fprint(conn, "HELO host\r\n")
str, err = in.ReadString('\n')
// perform 2 transactions
// both should panic.
for i := 0; i < 2; i++ {
fmt.Fprint(conn, "MAIL FROM:<[email protected]>r\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "RCPT TO:[email protected]\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "DATA\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "Subject: Test subject\r\n")
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "A an email body\r\n")
fmt.Fprint(conn, ".\r\n")
str, err = in.ReadString('\n')
expect := "transaction timeout"
if strings.Index(str, expect) == -1 {
t.Error("Expected the reply to have'", expect, "'but got", str)
}
}
_ = str
d.Shutdown()
}
}
// The processor will panic and gateway should recover from it
func TestGatewayPanic(t *testing.T) {
bcfg := backends.BackendConfig{
"save_workers_size": 1,
"save_process": "HeadersParser|Debugger",
"log_received_mails": true,
"primary_mail_host": "example.com",
"gw_save_timeout": "2s",
"gw_val_rcpt_timeout": "2s",
"sleep_seconds": 1,
}
cfg := &AppConfig{
LogFile: log.OutputOff.String(),
AllowedHosts: []string{"grr.la"},
}
cfg.BackendConfig = bcfg
d := Daemon{Config: cfg}
err := d.Start()
if err != nil {
t.Error("server didn't start")
} else {
conn, err := net.Dial("tcp", "127.0.0.1:2525")
if err != nil {
return
}
in := bufio.NewReader(conn)
str, err := in.ReadString('\n')
fmt.Fprint(conn, "HELO host\r\n")
str, err = in.ReadString('\n')
// perform 2 transactions
// both should timeout. The reason why 2 is because we want to make
// sure that the client waits until processing finishes, and the
// timeout event is captured.
for i := 0; i < 2; i++ {
fmt.Fprint(conn, "MAIL FROM:<[email protected]>r\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "RCPT TO:[email protected]\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "DATA\r\n")
str, err = in.ReadString('\n')
fmt.Fprint(conn, "Subject: Test subject\r\n")
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "A an email body\r\n")
fmt.Fprint(conn, ".\r\n")
str, err = in.ReadString('\n')
expect := "storage failed"
if strings.Index(str, expect) == -1 {
t.Error("Expected the reply to have'", expect, "'but got", str)
}
}
_ = str
d.Shutdown()
}
}
func TestAllowsHosts(t *testing.T) {
s := server{}
allowedHosts := []string{
"spam4.me",
"grr.la",
"newhost.com",
"example.*",
"*.test",
"wild*.card",
"multiple*wild*cards.*",
}
s.setAllowedHosts(allowedHosts)
testTable := map[string]bool{
"spam4.me": true,
"dont.match": false,
"example.com": true,
"another.example.com": false,
"anything.test": true,
"wild.card": true,
"wild.card.com": false,
"multipleXwildXcards.com": true,
}
for host, allows := range testTable {
if res := s.allowsHost(host); res != allows {
t.Error(host, ": expected", allows, "but got", res)
}
}
// only wildcard - should match anything
s.setAllowedHosts([]string{"*"})
if !s.allowsHost("match.me") {
t.Error("match.me: expected true but got false")
}
// turns off
s.setAllowedHosts([]string{"."})
if !s.allowsHost("match.me") {
t.Error("match.me: expected true but got false")
}
// no wilcards
s.setAllowedHosts([]string{"grr.la", "example.com"})
}
// TODO
// - test github issue #44 and #42