-
Notifications
You must be signed in to change notification settings - Fork 234
/
t.go
402 lines (335 loc) · 10 KB
/
t.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
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Package tests provides the framework for integration testing of maddy.
//
// The packages core object is tests.T object that encapsulates all test
// state. It runs the server using test-provided configuration file and acts as
// a proxy for all interactions with the server.
package tests
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/foxcpp/go-mockdns"
)
var (
TestBinary = "./maddy"
CoverageOut string
DebugLog bool
)
type T struct {
*testing.T
testDir string
cfg string
dnsServ *mockdns.Server
env []string
ports map[string]uint16
portsRev map[uint16]string
servProc *exec.Cmd
}
func NewT(t *testing.T) *T {
return &T{
T: t,
ports: map[string]uint16{},
portsRev: map[uint16]string{},
}
}
// Config sets the configuration to use for the server. It must be called
// before Run.
func (t *T) Config(cfg string) {
t.Helper()
if t.servProc != nil {
panic("tests: Config called after Run")
}
t.cfg = cfg
}
// DNS sets the DNS zones to emulate for the tested server instance.
//
// If it is not called before Run, DNS(nil) call is assumed which makes the
// mockdns server respond with NXDOMAIN to all queries.
func (t *T) DNS(zones map[string]mockdns.Zone) {
t.Helper()
if t.dnsServ != nil {
t.Log("NOTE: Multiple DNS calls, replacing the server instance...")
t.dnsServ.Close()
}
dnsServ, err := mockdns.NewServer(zones)
if err != nil {
t.Fatal("Test configuration failed:", err)
}
dnsServ.Log = t
t.dnsServ = dnsServ
}
// Port allocates the random TCP port for use by test. It will made accessible
// in the configuration via environment variables with name in the form
// TEST_PORT_name.
//
// If there is a port with name remote_smtp, it will be passed as the value for
// the -debug.smtpport parameter.
func (t *T) Port(name string) uint16 {
if port := t.ports[name]; port != 0 {
return port
}
// TODO: Try to bind on port to test its usability.
port := rand.Int31n(45536) + 20000
t.ports[name] = uint16(port)
t.portsRev[uint16(port)] = name
return uint16(port)
}
func (t *T) Env(kv string) {
t.env = append(t.env, kv)
}
// Run completes the configuration of test environment and starts the test server.
//
// T.Close should be called by the end of test to release any resources and
// shutdown the server.
//
// The parameter waitListeners specifies the amount of listeners the server is
// supposed to configure. Run() will block before all of them are up.
func (t *T) Run(waitListeners int) {
if t.cfg == "" {
panic("tests: Run called without configuration set")
}
if t.dnsServ == nil {
// If there is no DNS zones set in test - start a server that will
// respond with NXDOMAIN to all queries to avoid accidentally leaking
// any DNS queries to the real world.
t.Log("NOTE: Explicit DNS(nil) is recommended.")
t.DNS(nil)
}
// Setup file system, create statedir, runtimedir, write out config.
testDir, err := ioutil.TempDir("", "maddy-tests-")
if err != nil {
t.Fatal("Test configuration failed:", err)
}
t.testDir = testDir
t.Log("Using", t.testDir)
defer func() {
if !t.Failed() {
return
}
// Clean-up on test failure (if Run failed somewhere)
t.dnsServ.Close()
t.dnsServ = nil
os.RemoveAll(t.testDir)
t.testDir = ""
}()
if err := os.MkdirAll(filepath.Join(t.testDir, "statedir"), os.ModePerm); err != nil {
t.Fatal("Test configuration failed:", err)
}
if err := os.MkdirAll(filepath.Join(t.testDir, "runtimedir"), os.ModePerm); err != nil {
t.Fatal("Test configuration failed:", err)
}
configPreable := "state_dir " + filepath.Join(t.testDir, "statedir") + "\n" +
"runtime_dir " + filepath.Join(t.testDir, "runtime") + "\n\n"
err = ioutil.WriteFile(filepath.Join(t.testDir, "maddy.conf"), []byte(configPreable+t.cfg), os.ModePerm)
if err != nil {
t.Fatal("Test configuration failed:", err)
}
// Assigning 0 by default will make outbound SMTP unusable.
remoteSmtp := "0"
if port := t.ports["remote_smtp"]; port != 0 {
remoteSmtp = strconv.Itoa(int(port))
}
cmd := exec.Command(TestBinary,
"-config", filepath.Join(t.testDir, "maddy.conf"),
"-debug.smtpport", remoteSmtp,
"-debug.dnsoverride", t.dnsServ.LocalAddr().String(),
"-log", "stderr")
if CoverageOut != "" {
cmd.Args = append(cmd.Args, "-test.coverprofile", CoverageOut+"."+strconv.FormatInt(time.Now().UnixNano(), 16))
}
if DebugLog {
cmd.Args = append(cmd.Args, "-debug")
}
t.Logf("launching %v", cmd.Args)
pwd, err := os.Getwd()
if err != nil {
t.Fatal("Test configuration failed:", err)
}
// Set environment variables.
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env,
"TEST_PWD="+pwd,
"TEST_STATE_DIR="+filepath.Join(t.testDir, "statedir"),
"TEST_RUNTIME_DIR="+filepath.Join(t.testDir, "statedir"),
)
for name, port := range t.ports {
cmd.Env = append(cmd.Env, fmt.Sprintf("TEST_PORT_%s=%d", name, port))
}
cmd.Env = append(cmd.Env, t.env...)
// Capture maddy log and redirect it.
logOut, err := cmd.StderrPipe()
if err != nil {
t.Fatal("Test configuration failed:", err)
}
if err := cmd.Start(); err != nil {
t.Fatal("Test configuration failed:", err)
}
// Log scanning goroutine checks for the "listening" messages and sends 'true'
// on the channel each time.
listeningMsg := make(chan bool)
go func() {
defer logOut.Close()
defer close(listeningMsg)
scnr := bufio.NewScanner(logOut)
for scnr.Scan() {
line := scnr.Text()
if strings.Contains(line, "listening on") {
listeningMsg <- true
line += " (test runner>listener wait trigger<)"
}
t.Log("maddy:", line)
}
if err := scnr.Err(); err != nil {
t.Log("stderr I/O error:", err)
}
}()
for i := 0; i < waitListeners; i++ {
if !<-listeningMsg {
t.Fatal("Log ended before all expected listeners are up. Start-up error?")
}
}
t.servProc = cmd
}
func (t *T) StateDir() string {
return filepath.Join(t.testDir, "statedir")
}
func (t *T) RuntimeDir() string {
return filepath.Join(t.testDir, "statedir")
}
func (t *T) Close() {
if err := t.servProc.Process.Signal(os.Interrupt); err != nil {
t.Log("Unable to kill the server process:", err)
os.RemoveAll(t.testDir)
return // Return, as now it is pointless to wait for it.
}
go func() {
time.Sleep(5 * time.Second)
if t.servProc != nil {
t.servProc.Process.Kill() //nolint:errcheck
}
}()
if err := t.servProc.Wait(); err != nil {
t.Error("The server did not stop cleanly, deadlock?")
}
t.servProc = nil
if err := os.RemoveAll(t.testDir); err != nil {
t.Log("Failed to remove test directory:", err)
}
t.testDir = ""
// Shutdown the DNS server after maddy to make sure it will not spend time
// timing out queries.
if err := t.dnsServ.Close(); err != nil {
t.Log("Unable to stop the DNS server:", err)
}
t.dnsServ = nil
}
// Printf implements Logger interfaces used by some libraries.
func (t *T) Printf(f string, a ...interface{}) {
t.Logf(f, a...)
}
// Conn6 connects to the server listener at the specified named port using IPv6 loopback.
func (t *T) Conn6(portName string) Conn {
port := t.ports[portName]
if port == 0 {
panic("tests: connection for the unused port name is requested")
}
conn, err := net.Dial("tcp6", "[::1]:"+strconv.Itoa(int(port)))
if err != nil {
t.Fatal("Could not connect, is server listening?", err)
}
return Conn{
T: t,
WriteTimeout: 1 * time.Second,
ReadTimeout: 15 * time.Second,
Conn: conn,
Scanner: bufio.NewScanner(conn),
}
}
// Conn4 connects to the server listener at the specified named port using one
// of 127.0.0.0/8 addresses as a source.
func (t *T) Conn4(sourceIP, portName string) Conn {
port := t.ports[portName]
if port == 0 {
panic("tests: connection for the unused port name is requested")
}
localIP := net.ParseIP(sourceIP)
if localIP == nil {
panic("tests: invalid localIP argument")
}
if localIP.To4() == nil {
panic("tests: only IPv4 addresses are allowed")
}
conn, err := net.DialTCP("tcp4", &net.TCPAddr{
IP: localIP,
Port: 0,
}, &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: int(port),
})
if err != nil {
t.Fatal("Could not connect, is server listening?", err)
}
return Conn{
T: t,
WriteTimeout: 1 * time.Second,
ReadTimeout: 15 * time.Second,
Conn: conn,
Scanner: bufio.NewScanner(conn),
}
}
func (t *T) ConnUnnamed(port uint16) Conn {
conn, err := net.Dial("tcp4", "127.0.0.1:"+strconv.Itoa(int(port)))
if err != nil {
t.Fatal("Could not connect, is server listening?", err)
}
return Conn{
T: t,
WriteTimeout: 1 * time.Second,
ReadTimeout: 15 * time.Second,
Conn: conn,
Scanner: bufio.NewScanner(conn),
}
}
func (t *T) Conn(portName string) Conn {
port := t.ports[portName]
if port == 0 {
panic("tests: connection for the unused port name is requested")
}
return t.ConnUnnamed(port)
}
func (t *T) Subtest(name string, f func(t *T)) {
t.T.Run(name, func(subTT *testing.T) {
subT := *t
subT.T = subTT
f(&subT)
})
}
func init() {
flag.StringVar(&TestBinary, "integration.executable", "./maddy", "executable to test")
flag.StringVar(&CoverageOut, "integration.coverprofile", "", "write coverage stats to file (requires special maddy executable)")
flag.BoolVar(&DebugLog, "integration.debug", false, "pass -debug to maddy executable")
}