forked from wneessen/postfix-policy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pps_test.go
453 lines (413 loc) · 12.4 KB
/
pps_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
package pps
import (
"bufio"
"context"
"fmt"
"net"
"os"
"testing"
"time"
)
// Empty struct to test the Handler interface
type Hi struct {
r PostfixResp
}
// Handle is the function required by the Handler Interface
func (h Hi) Handle(*PolicySet) PostfixResp {
if h.r == "" {
h.r = RespDunno
}
return h.r
}
const exampleReq = `request=smtpd_access_policy
protocol_state=RCPT
protocol_name=SMTP
client_address=127.0.0.1
client_name=localhost
client_port=45140
reverse_client_name=localhost
server_address=127.0.0.1
server_port=25
helo_name=example.com
recipient_count=0
queue_id=
instance=1234.5678910a.bcdef.0
size=0
etrn_domain=
stress=
sasl_method=
sasl_username=
sasl_sender=
ccert_subject=
ccert_issuer=
ccert_fingerprint=
ccert_pubkey_fingerprint=
encryption_protocol=
encryption_cipher=
encryption_keysize=0
policy_context=
`
// TestNew tests the New() method
func TestNew(t *testing.T) {
s := New()
if s.lp != DefaultPort {
t.Errorf("policy server creation failed: configured port mismatch => Expected: %s, got: %s",
DefaultPort, s.lp)
}
if s.la != DefaultAddr {
t.Errorf("policy server creation failed: configured listen address mismatch => Expected: %s, got: %s",
DefaultAddr, s.la)
}
}
// TestNewWithAddr tests the New() method with the WithAddr() option
func TestNewWithAddr(t *testing.T) {
a := "1.2.3.4"
s := New(WithAddr(a))
if s.la != a {
t.Errorf("policy server creation failed: configured listen address mismatch => Expected: %s, got: %s",
a, s.la)
}
}
// TestNewWithPort tests the New() method with the WithPort() option
func TestNewWithPort(t *testing.T) {
p := "1234"
s := New(WithPort(p))
if s.lp != p {
t.Errorf("policy server creation failed: configured listen address mismatch => Expected: %s, got: %s",
p, s.lp)
}
}
// TestSetAddr tests the SetAddr() option on an existing policy server
func TestSetAddr(t *testing.T) {
a := "1.2.3.4"
s := New()
s.SetAddr(a)
if s.la != a {
t.Errorf("policy server address setting failed => Expected: %s, got: %s",
a, s.la)
}
}
// TestSetPort tests the SetPort() option on an existing policy server
func TestSetPort(t *testing.T) {
p := "1234"
s := New()
s.SetPort(p)
if s.lp != p {
t.Errorf("policy server port setting failed => Expected: %s, got: %s",
p, s.lp)
}
}
// TestNewWithEmptyOpt tests the New() method with a nil-option
func TestNewWithEmptyOpt(t *testing.T) {
emptyOpt := func(p *string) ServerOpt { return nil }
s := New(emptyOpt(nil))
if s.lp != DefaultPort {
t.Errorf("policy server creation failed: configured listen address mismatch => Expected: %s, got: %s",
DefaultPort, s.lp)
}
}
// TestRun starts a new server listening for connections
func TestRun(t *testing.T) {
testTable := []struct {
testName string
listenAddr string
listenPort string
shouldFail bool
}{
{`Successfully start with defaults`, DefaultAddr, DefaultPort, false},
{`Fail to on invalid IP`, "256.256.256.256", DefaultPort, true},
{`Fail to on invalid port`, DefaultAddr, "1", true},
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
s := New(WithAddr(tc.listenAddr), WithPort(tc.listenPort))
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200)
defer cancel()
vctx := context.WithValue(ctx, CtxNoLog, true)
h := Hi{}
err := s.Run(vctx, h)
if err != nil && !tc.shouldFail {
t.Errorf("could not run server: %s", err)
}
})
}
}
// TestRunDial starts a new server listening for connections and tries to connect to it
func TestRunDial(t *testing.T) {
s := New(WithPort("44440"))
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
h := Hi{}
go func() {
if err := s.Run(vsctx, h); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "tcp",
fmt.Sprintf("%s:%s", s.la, s.lp))
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
if err := conn.Close(); err != nil {
t.Errorf("failed to close client connection: %s", err)
}
}
// TestRunDialWithRequest starts a new server listening for connections and tries to connect to it
// and sends example data
func TestRunDialWithRequest(t *testing.T) {
s := New(WithPort("44441"))
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
h := Hi{}
go func() {
if err := s.Run(vsctx, h); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "tcp",
fmt.Sprintf("%s:%s", s.la, s.lp))
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
defer func() { _ = conn.Close() }()
rb := bufio.NewReader(conn)
_, err = conn.Write([]byte(exampleReq))
if err != nil {
t.Errorf("failed to send request to server: %s", err)
}
resp, err := rb.ReadString('\n')
if err != nil {
t.Errorf("failed to read response from server: %s", err)
}
exresp := fmt.Sprintf("action=%s\n", RespDunno)
if resp != exresp {
t.Errorf("unexpected server response => expected: %s, got: %s", exresp, resp)
}
}
// TestRunDialReponses starts a new server listening for connections and tries to connect to it,
// sends example data and tests all possible responses
func TestRunDialResponses(t *testing.T) {
testTable := []struct {
testName string
response PostfixResp
port uint
}{
{`Test OK`, RespOk, 44442},
{`Test REJECT`, RespReject, 44443},
{`Test DEFER`, RespDefer, 44444},
{`Test DEFER_IF_REJECT`, RespDeferIfReject, 44445},
{`Test DEFER_IF_PERMIT`, RespDeferIfPermit, 44446},
{`Test DISCARD`, RespDiscard, 44447},
{`Test DUNNO`, RespDunno, 44448},
{`Test HOLD`, RespHold, 44449},
{`Test INFO`, RespInfo, 44450},
{`Test WARN`, RespWarn, 44451},
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
s := New(WithPort(fmt.Sprintf("%d", tc.port)))
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
h := Hi{r: tc.response}
go func() {
if err := s.Run(vsctx, h); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "tcp",
fmt.Sprintf("%s:%s", s.la, s.lp))
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
defer func() { _ = conn.Close() }()
rb := bufio.NewReader(conn)
_, err = conn.Write([]byte(exampleReq))
if err != nil {
t.Errorf("failed to send request to server: %s", err)
}
resp, err := rb.ReadString('\n')
if err != nil {
t.Errorf("failed to read response from server: %s", err)
}
exresp := fmt.Sprintf("action=%s\n", tc.response)
if resp != exresp {
t.Errorf("unexpected server response => expected: %s, got: %s", exresp, resp)
}
})
}
}
// TestRunDialWithRequestSocket starts a new server listening for connections on a UNIX socket,
// tries to connect to it and sends example data
func TestRunDialWithRequestSocket(t *testing.T) {
s := New()
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
us := "/tmp/pps_test_server"
l, err := net.Listen("unix", us)
if err != nil {
t.Errorf("failed to create new UNIX socket listener: %s", err)
}
defer func() {
_ = os.Remove(us)
}()
h := Hi{}
go func() {
if err := s.RunWithListener(vsctx, h, l); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "unix", us)
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
defer func() { _ = conn.Close() }()
rb := bufio.NewReader(conn)
_, err = conn.Write([]byte(exampleReq))
if err != nil {
t.Errorf("failed to send request to server: %s", err)
}
resp, err := rb.ReadString('\n')
if err != nil {
t.Errorf("failed to read response from server: %s", err)
}
exresp := fmt.Sprintf("action=%s\n", RespDunno)
if resp != exresp {
t.Errorf("unexpected server response => expected: %s, got: %s", exresp, resp)
}
}
// TestRunDialOptTextResponse starts a new server listening for connections and tries to connect to it,
// sends example data and tests the TextResponseOpt() method
func TestRunDialTextResponseOpt(t *testing.T) {
testTable := []struct {
testName string
postfixResp PostfixResp
optText string
port uint
}{
{`Test OK`, RespOk, "testtext", 44460},
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
s := New(WithPort(fmt.Sprintf("%d", tc.port)))
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
custResp := TextResponseOpt(tc.postfixResp, tc.optText)
h := Hi{r: custResp}
go func() {
if err := s.Run(vsctx, h); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "tcp",
fmt.Sprintf("%s:%s", s.la, s.lp))
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
defer func() { _ = conn.Close() }()
rb := bufio.NewReader(conn)
_, err = conn.Write([]byte(exampleReq))
if err != nil {
t.Errorf("failed to send request to server: %s", err)
}
resp, err := rb.ReadString('\n')
if err != nil {
t.Errorf("failed to read response from server: %s", err)
}
exresp := fmt.Sprintf("action=%s %s\n", tc.postfixResp, tc.optText)
if resp != exresp {
t.Errorf("unexpected server response => expected: %s, got: %s", exresp, resp)
}
})
}
}
// TestRunDialNonOptTextResponse starts a new server listening for connections and tries to connect to it,
// sends example data and tests the TextResponseNonOpt() method
func TestRunDialTextResponseNonOpt(t *testing.T) {
testTable := []struct {
testName string
postfixResp PostfixTextResp
optText string
port uint
}{
{`Test PREPEND`, TextRespPrepend, "headername: headervalue", 44461},
{`Test FILTER`, TextRespFilter, "transport:destination", 44462},
{`Test REDIRECT`, TextRespRedirect, "user@domain", 44463},
}
for _, tc := range testTable {
t.Run(tc.testName, func(t *testing.T) {
s := New(WithPort(fmt.Sprintf("%d", tc.port)))
sctx, scancel := context.WithCancel(context.Background())
defer scancel()
vsctx := context.WithValue(sctx, CtxNoLog, true)
custResp := TextResponseNonOpt(tc.postfixResp, tc.optText)
h := Hi{r: custResp}
go func() {
if err := s.Run(vsctx, h); err != nil {
t.Errorf("could not run server: %s", err)
}
}()
// Wait a brief moment for the server to start
time.Sleep(time.Millisecond * 200)
d := net.Dialer{}
cctx, ccancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer ccancel()
conn, err := d.DialContext(cctx, "tcp",
fmt.Sprintf("%s:%s", s.la, s.lp))
if err != nil {
t.Errorf("failed to connect to running server: %s", err)
return
}
defer func() { _ = conn.Close() }()
rb := bufio.NewReader(conn)
_, err = conn.Write([]byte(exampleReq))
if err != nil {
t.Errorf("failed to send request to server: %s", err)
}
resp, err := rb.ReadString('\n')
if err != nil {
t.Errorf("failed to read response from server: %s", err)
}
exresp := fmt.Sprintf("action=%s %s\n", tc.postfixResp, tc.optText)
if resp != exresp {
t.Errorf("unexpected server response => expected: %s, got: %s", exresp, resp)
}
})
}
}