-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlscert_test.go
252 lines (210 loc) · 5.73 KB
/
tlscert_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package tlscert_test
import (
"crypto/tls"
"net"
"os"
"testing"
"github.com/mdelapenya/tlscert"
)
func TestSelfSigned(t *testing.T) {
t.Run("No host returns error", func(t *testing.T) {
cert := tlscert.SelfSignedFromRequest(tlscert.Request{Host: ""})
if cert != nil {
t.Fatal("expected cert to be nil, got", cert)
}
})
t.Run("With host", func(tt *testing.T) {
cert := tlscert.SelfSigned("localhost")
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Key == nil {
tt.Fatal("expected key, got nil")
}
if cert.Bytes == nil {
t.Fatal("expected bytes, got nil")
}
if cert.KeyBytes == nil {
t.Fatal("expected key bytes, got nil")
}
_, err := tls.X509KeyPair(cert.Bytes, cert.KeyBytes)
if err != nil {
tt.Fatal(err)
}
})
t.Run("With multiple hosts", func(t *testing.T) {
ip := "1.2.3.4"
cert := tlscert.SelfSigned("localhost, " + ip)
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Key == nil {
t.Fatal("expected key, got nil")
}
c := cert.Cert
if len(c.IPAddresses) != 1 {
t.Fatal("expected 1 IP address, got", len(c.IPAddresses))
}
if c.IPAddresses[0].String() != ip {
t.Fatalf("expected IP address to be %s, got %s\n", ip, c.IPAddresses[0].String())
}
})
t.Run("With multiple hosts and IPs", func(t *testing.T) {
ip := "1.2.3.4"
ips := []net.IP{net.ParseIP("4.5.6.7"), net.ParseIP("8.9.10.11")}
cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost, " + ip,
IPAddresses: ips,
})
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Key == nil {
t.Fatal("expected key, got nil")
}
c := cert.Cert
if len(c.IPAddresses) != 3 {
t.Fatal("expected 3 IP address, got", len(c.IPAddresses))
}
for i, ip := range ips {
if c.IPAddresses[i].String() != ip.String() {
t.Fatalf("expected IP address to be %s, got %s\n", ip.String(), c.IPAddresses[i].String())
}
}
// the IP from the host comes after the IPs from the IPAddresses option
if c.IPAddresses[2].String() != ip {
t.Fatalf("expected IP address to be %s, got %s\n", ip, c.IPAddresses[0].String())
}
})
t.Run("As CA", func(t *testing.T) {
cert := tlscert.SelfSignedCA("localhost")
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Cert == nil {
t.Fatal("expected cert, got nil")
}
if cert.Key == nil {
t.Fatal("expected key, got nil")
}
if cert.Bytes == nil {
t.Fatal("expected bytes, got nil")
}
if !cert.Cert.IsCA {
t.Fatal("expected cert to be CA, got false")
}
})
t.Run("With Subject common name", func(t *testing.T) {
cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost",
SubjectCommonName: "Test",
})
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Cert == nil {
t.Fatal("expected cert, got nil")
}
c := cert.Cert
if c.Subject.CommonName != "Test" {
t.Fatal("expected common name to be Test, got", c.Subject.CommonName)
}
})
t.Run("With Parent cert", func(t *testing.T) {
parent := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost",
SubjectCommonName: "Acme Inc.",
IsCA: true,
})
if parent == nil {
t.Fatal("expected parent to be not nil, got", parent)
}
cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost",
Parent: parent,
})
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Cert == nil {
t.Fatal("expected cert, got nil")
}
if cert.Key == nil {
t.Fatal("expected key, got nil")
}
c := cert.Cert
if c.Issuer.CommonName != parent.Cert.Subject.CommonName {
t.Fatal("expected issuer to be parent, got", c.Issuer.CommonName)
}
})
t.Run("With IP addresses", func(t *testing.T) {
ip := "1.2.3.4"
cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost",
IPAddresses: []net.IP{net.ParseIP(ip)},
})
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
if cert.Cert == nil {
t.Fatal("expected cert, got nil")
}
c := cert.Cert
if len(c.IPAddresses) != 1 {
t.Fatal("expected 1 IP address, got", len(c.IPAddresses))
}
if c.IPAddresses[0].String() != ip {
t.Fatalf("expected IP address to be %s, got %s\n", ip, c.IPAddresses[0].String())
}
})
t.Run("Save to file", func(tt *testing.T) {
tmp := tt.TempDir()
cert := tlscert.SelfSignedFromRequest(tlscert.Request{
Host: "localhost",
ParentDir: tmp,
})
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
inMemoryCert, err := tls.X509KeyPair(cert.Bytes, cert.KeyBytes)
if err != nil {
tt.Fatal(err)
}
// check if file existed
certBytes, err := os.ReadFile(cert.CertPath)
if err != nil {
tt.Fatal(err)
}
certKeyBytes, err := os.ReadFile(cert.KeyPath)
if err != nil {
tt.Fatal(err)
}
fileCert, err := tls.X509KeyPair(certBytes, certKeyBytes)
if err != nil {
tt.Fatal(err)
}
for i, cert := range inMemoryCert.Certificate {
if string(cert) != string(fileCert.Certificate[i]) {
tt.Fatalf("expected certificate to be %s, got %s\n", string(cert), string(fileCert.Certificate[i]))
}
}
})
}
func TestTLSConfig(t *testing.T) {
t.Run("Cached", func(t *testing.T) {
cert := tlscert.SelfSigned("localhost")
if cert == nil {
t.Fatal("expected cert to be not nil, got", cert)
}
config := cert.TLSConfig()
if config == nil {
t.Fatal("expected config to be not nil, got", config)
}
// force the bytes to be null, but the config should not change
cert.Bytes = nil
config2 := cert.TLSConfig()
if config != config2 {
t.Fatal("expected config to be the same, got different")
}
})
}