forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_test.go
90 lines (68 loc) · 1.83 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
// +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)
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)
assert.True(t, cert1.Equals(*cert1))
assert.False(t, cert1.Equals(*cert2))
}
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()))
}