forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_test.go
117 lines (90 loc) · 2.49 KB
/
certificate_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
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
//go:build !js
// +build !js
package webrtc
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGenerateCertificateRSA(t *testing.T) {
sk, err := rsa.GenerateKey(rand.Reader, 2048)
assert.Nil(t, err)
skPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(sk),
})
cert, err := GenerateCertificate(sk)
assert.Nil(t, err)
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.x509Cert.Raw,
})
_, err = tls.X509KeyPair(certPEM, skPEM)
assert.Nil(t, err)
}
func TestGenerateCertificateECDSA(t *testing.T) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
skDER, err := x509.MarshalECPrivateKey(sk)
assert.Nil(t, err)
skPEM := pem.EncodeToMemory(&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: skDER,
})
cert, err := GenerateCertificate(sk)
assert.Nil(t, err)
certPEM := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.x509Cert.Raw,
})
_, err = tls.X509KeyPair(certPEM, skPEM)
assert.Nil(t, err)
}
func TestGenerateCertificateEqual(t *testing.T) {
sk1, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
sk3, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
cert1, err := GenerateCertificate(sk1)
assert.Nil(t, err)
sk2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
cert2, err := GenerateCertificate(sk2)
assert.Nil(t, err)
cert3, err := GenerateCertificate(sk3)
assert.NoError(t, err)
assert.True(t, cert1.Equals(*cert1))
assert.False(t, cert1.Equals(*cert2))
assert.True(t, cert3.Equals(*cert3))
}
func TestGenerateCertificateExpires(t *testing.T) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
cert, err := GenerateCertificate(sk)
assert.Nil(t, err)
now := time.Now()
assert.False(t, cert.Expires().IsZero() || now.After(cert.Expires()))
x509Cert := CertificateFromX509(sk, &x509.Certificate{})
assert.NotNil(t, x509Cert)
assert.Contains(t, x509Cert.statsID, "certificate")
}
func TestPEM(t *testing.T) {
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
cert, err := GenerateCertificate(sk)
assert.Nil(t, err)
pem, err := cert.PEM()
assert.Nil(t, err)
cert2, err := CertificateFromPEM(pem)
assert.Nil(t, err)
pem2, err := cert2.PEM()
assert.Nil(t, err)
assert.Equal(t, pem, pem2)
}