forked from flashmob/go-guerrilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
273 lines (246 loc) · 7.53 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
package guerrilla
import (
"testing"
"bufio"
"net/textproto"
"strings"
"sync"
"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"
"net"
)
// 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
PrivateKeyFile: "./tests/mail.guerrillamail.com.key.pem",
PublicKeyFile: "./tests/mail.guerrillamail.com.cert.pem",
Timeout: 5,
ListenInterface: "127.0.0.1:2529",
StartTLSOn: true,
TLSAlwaysOn: false,
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
}
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()
}
}
// TODO
// - test github issue #44 and #42