-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcerts_test.go
54 lines (44 loc) · 1.35 KB
/
certs_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
package main
import (
"crypto/x509"
"encoding/pem"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCertExpiryServer(t *testing.T) {
caBundle, err := generateCACert("CA")
require.NoError(t, err)
serverBundle, err := generateServerCert(CertConfig{
CommonName: "mtls.dev",
Hosts: "127.0.0.1",
Expiration: "127h",
CACert: caBundle.Cert,
CAKey: caBundle.Key,
})
require.NoError(t, err)
certDERBlock, _ := pem.Decode(serverBundle.Cert)
cert, err := x509.ParseCertificate(certDERBlock.Bytes)
require.NoError(t, err)
// check that the expiry is ~127 hours from now
require.True(t, cert.NotAfter.Sub(time.Now()) > 126*time.Hour)
require.True(t, cert.NotAfter.Sub(time.Now()) < 128*time.Hour)
}
func TestCertExpiryClient(t *testing.T) {
caBundle, err := generateCACert("CA")
require.NoError(t, err)
clientBundle, err := generateClientCert(CertConfig{
CommonName: "mtls.dev",
Hosts: "127.0.0.1",
Expiration: "127h",
CACert: caBundle.Cert,
CAKey: caBundle.Key,
})
require.NoError(t, err)
certDERBlock, _ := pem.Decode(clientBundle.Cert)
cert, err := x509.ParseCertificate(certDERBlock.Bytes)
require.NoError(t, err)
// check that the expiry is ~127 hours from now
require.True(t, cert.NotAfter.Sub(time.Now()) > 126*time.Hour)
require.True(t, cert.NotAfter.Sub(time.Now()) < 128*time.Hour)
}