-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathintegration_test.go
More file actions
370 lines (324 loc) · 9.31 KB
/
integration_test.go
File metadata and controls
370 lines (324 loc) · 9.31 KB
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
package main
import (
"bytes"
"testing"
"time"
"github.com/jpillora/ipfilter"
"github.com/phires/go-guerrilla/log"
"github.com/phires/go-guerrilla/mail"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// setupTestLogger initializes the logger for testing.
func setupTestLogger(t *testing.T) {
var err error
Logger, err = log.GetLogger("stdout", "info")
require.NoError(t, err)
}
func TestSendMail_Success(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server
server := NewMockSMTPServer(t)
require.NoError(t, server.Start())
defer server.Stop()
// Configure for testing
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "",
Password: "",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient1", Host: "example.com"},
{User: "recipient2", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Test\r\n\r\nThis is a test email."),
RemoteIP: "127.0.0.1",
}
// Set up IP filter to allow this IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"127.0.0.1"},
BlockByDefault: false,
})
// Send email
err := sendMail(envelope, config)
assert.NoError(t, err)
// Verify the mock server received the email
conn := server.GetLastConnection()
require.NotNil(t, conn)
assert.Equal(t, "sender@test.com", conn.From)
assert.Equal(t, []string{"recipient1@example.com", "recipient2@example.com"}, conn.To)
assert.Contains(t, conn.Data, "Subject: Test")
assert.Contains(t, conn.Data, "This is a test email.")
}
func TestSendMail_WithAuthentication(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server with auth requirement
server := NewMockSMTPServer(t)
server.RequireAuth = true
require.NoError(t, server.Start())
defer server.Stop()
// Configure with authentication
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "testuser",
Password: "testpass",
SkipVerify: true,
HeloHost: "relay.test.com",
}
// Create test envelope
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Auth Test\r\n\r\nAuthenticated email."),
RemoteIP: "127.0.0.1",
}
// Allow IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
BlockByDefault: false,
})
// Send email
err := sendMail(envelope, config)
assert.NoError(t, err)
// Verify authentication was used
conn := server.GetLastConnection()
require.NotNil(t, conn)
assert.NotEmpty(t, conn.AuthUser)
assert.NotEmpty(t, conn.AuthPass)
}
func TestSendMail_WithLoginAuth(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server with LOGIN auth support
server := NewMockSMTPServer(t)
server.RequireAuth = true
server.SupportLoginAuth = true
require.NoError(t, server.Start())
defer server.Stop()
// Configure with LOGIN authentication
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: true,
Username: "testuser",
Password: "testpass",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: LOGIN Auth Test\r\n\r\nLOGIN authenticated email."),
RemoteIP: "127.0.0.1",
}
// Allow IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
BlockByDefault: false,
})
// Send email
err := sendMail(envelope, config)
assert.NoError(t, err)
// Verify LOGIN authentication was used
conn := server.GetLastConnection()
require.NotNil(t, conn)
assert.NotEmpty(t, conn.AuthUser)
assert.NotEmpty(t, conn.AuthPass)
}
func TestSendMail_IPFiltering_Blocked(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server
server := NewMockSMTPServer(t)
require.NoError(t, server.Start())
defer server.Stop()
// Configure server
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "",
Password: "",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope with blocked IP
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Test\r\n\r\nThis should be blocked."),
RemoteIP: "192.168.1.100", // This IP will be blocked
}
// Set up IP filter to block this IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"127.0.0.1"},
BlockByDefault: true,
})
// Send email - should fail due to IP filtering
err := sendMail(envelope, config)
assert.Error(t, err)
assert.Contains(t, err.Error(), "192.168.1.100")
assert.Contains(t, err.Error(), "not allowed to send email")
// Verify no email was sent to the server
conn := server.GetLastConnection()
// The connection might be nil or have no data since the IP was blocked before SMTP
if conn != nil {
assert.Empty(t, conn.From)
}
}
func TestSendMail_IPFiltering_Allowed(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server
server := NewMockSMTPServer(t)
require.NoError(t, server.Start())
defer server.Stop()
// Configure server
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "",
Password: "",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope with allowed IP
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Test\r\n\r\nThis should be allowed."),
RemoteIP: "192.168.1.100",
}
// Set up IP filter to allow this specific IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
AllowedIPs: []string{"192.168.1.0/24"},
BlockByDefault: true,
})
// Send email - should succeed
err := sendMail(envelope, config)
assert.NoError(t, err)
// Verify email was sent to the server
conn := server.GetLastConnection()
require.NotNil(t, conn)
assert.Equal(t, "sender@test.com", conn.From)
assert.Equal(t, []string{"recipient@example.com"}, conn.To)
}
func TestSendMail_ServerErrors(t *testing.T) {
setupTestLogger(t)
tests := []struct {
name string
failCommand string
expectError string
}{
{
name: "MAIL command fails",
failCommand: "MAIL",
expectError: "mail error",
},
{
name: "RCPT command fails",
failCommand: "RCPT",
expectError: "rcpt error",
},
{
name: "DATA command fails",
failCommand: "DATA",
expectError: "data error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Start mock SMTP server
server := NewMockSMTPServer(t)
server.FailCommands[tt.failCommand] = true
require.NoError(t, server.Start())
defer server.Stop()
// Configure server
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "",
Password: "",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Test\r\n\r\nThis should fail."),
RemoteIP: "127.0.0.1",
}
// Allow IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
BlockByDefault: false,
})
// Send email - should fail
err := sendMail(envelope, config)
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectError)
})
}
}
func TestSendMail_ConnectionTimeout(t *testing.T) {
setupTestLogger(t)
// Start mock SMTP server with delay
server := NewMockSMTPServer(t)
server.ResponseDelay = 100 * time.Millisecond
require.NoError(t, server.Start())
defer server.Stop()
// Configure server
config := &relayConfig{
Server: server.Address(),
Port: server.Port(),
STARTTLS: true,
LoginAuthType: false,
Username: "",
Password: "",
SkipVerify: true,
HeloHost: "",
}
// Create test envelope
envelope := &mail.Envelope{
MailFrom: mail.Address{User: "sender", Host: "test.com"},
RcptTo: []mail.Address{
{User: "recipient", Host: "example.com"},
},
Data: *bytes.NewBufferString("Subject: Timeout Test\r\n\r\nThis tests server delays."),
RemoteIP: "127.0.0.1",
}
// Allow IP
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
BlockByDefault: false,
})
// Send email - should still succeed despite delay
err := sendMail(envelope, config)
assert.NoError(t, err)
// Verify email was eventually sent
conn := server.GetLastConnection()
require.NotNil(t, conn)
assert.Equal(t, "sender@test.com", conn.From)
}