-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_helpers.go
94 lines (81 loc) · 2.97 KB
/
test_helpers.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
package msmtpd
import (
"fmt"
"net"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
// TestLogger is logger being used only in unit tests
type TestLogger struct {
Suite *testing.T
}
// Tracef records trace level message
func (tl *TestLogger) Tracef(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("TRACE: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
}
// Debugf records debug level message
func (tl *TestLogger) Debugf(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("DEBUG: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
}
// Infof records info level message
func (tl *TestLogger) Infof(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("INFO: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
}
// Warnf records warning level message
func (tl *TestLogger) Warnf(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("WARN: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
}
// Errorf records error level message
func (tl *TestLogger) Errorf(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("ERROR: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
}
// Fatalf records fatal level message and fails test
func (tl *TestLogger) Fatalf(transaction *Transaction, format string, args ...any) {
tl.Suite.Logf("FATAL: [%s] %s %s\n",
tl.Suite.Name(), transaction.ID, fmt.Sprintf(format, args...))
tl.Suite.Errorf(format, args...)
}
// AuthenticatorForTestsThatAlwaysWorks should not be used for production
func AuthenticatorForTestsThatAlwaysWorks(tr *Transaction, username, password string) error {
tr.LogInfo("Pretend we authenticate as %s %s and succeed!", username, password)
return nil
}
// AuthenticatorForTestsThatAlwaysFails should not be used for production
func AuthenticatorForTestsThatAlwaysFails(tr *Transaction, username, password string) error {
tr.LogInfo("Pretend we authenticate as %s %s and fail!", username, password)
return ErrorSMTP{Code: 550, Message: "Denied"}
}
// RunTestServerWithoutTLS runs test server for unit tests without TLS support
func RunTestServerWithoutTLS(t *testing.T, server *Server) (addr string, closer func()) {
logger := TestLogger{Suite: t}
server.Logger = &logger
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
go func() {
server.Serve(ln)
}()
done := make(chan bool)
go func() {
<-done
ln.Close()
}()
return ln.Addr().String(), func() {
done <- true
}
}
// RunTestServerWithTLS runs test server for unit tests with TLS support and cert for localhost
func RunTestServerWithTLS(t *testing.T, server *Server) (addr string, closer func()) {
cfg, err := internal.MakeTLSForLocalhost()
if err != nil {
t.Fatalf("%s : while loading test certs for localhost", err)
}
server.TLSConfig = cfg
return RunTestServerWithoutTLS(t, server)
}