-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_test.go
89 lines (75 loc) · 1.95 KB
/
setup_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
package smtpd_test
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"sync"
"testing"
"github.com/mailproto/smtpd"
)
var tlsGen sync.Once
var tlsConfig *tls.Config
// TestingTLSConfig generates a TLS certificate for the testing session
func TestingTLSConfig() *tls.Config {
tlsGen.Do(func() {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
panic(err)
}
xc := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
IsCA: true,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
b, err := x509.CreateCertificate(rand.Reader, &xc, &xc, &priv.PublicKey, priv)
if err != nil {
panic(err)
}
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{
tls.Certificate{
Certificate: [][]byte{b},
PrivateKey: priv,
Leaf: &xc,
},
},
ClientAuth: tls.VerifyClientCertIfGiven,
Rand: rand.Reader,
}
})
return tlsConfig
}
// WaitUntilAlive is a helper function to allow us to not start tests until a server boots
func WaitUntilAlive(s *smtpd.Server) {
if alive := <-s.Ready; !alive {
panic("server reported Not Ready")
}
}
// TestLogger sends all log messages to the testing.T object, to be displayed as it sees fit
type TestLogger struct {
t *testing.T
}
func (t *TestLogger) Print(v ...interface{}) {
t.t.Log(v...)
}
func (t *TestLogger) Println(v ...interface{}) {
t.Print(v...)
}
func (t *TestLogger) Printf(format string, v ...interface{}) {
t.t.Logf(format, v)
}