-
Notifications
You must be signed in to change notification settings - Fork 56
/
util_test.go
49 lines (43 loc) · 1.23 KB
/
util_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
package typhon
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
Client = Service(BareClient).Filter(ErrorFilter)
os.Exit(m.Run())
}
func keypair(t *testing.T, hosts []string) tls.Certificate {
template := x509.Certificate{
SerialNumber: big.NewInt(100),
Subject: pkix.Name{
Organization: []string{"MomCorp"}},
NotBefore: time.Now().Add(-24 * time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true}
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
priv, err := rsa.GenerateKey(rand.Reader, 1024)
require.NoError(t, err)
certDer, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
require.NoError(t, err)
return tls.Certificate{
Certificate: [][]byte{certDer},
PrivateKey: priv}
}