-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcerty_test.go
More file actions
393 lines (339 loc) · 9.55 KB
/
certy_test.go
File metadata and controls
393 lines (339 loc) · 9.55 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package certy
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"math/big"
"os"
"path/filepath"
"testing"
"time"
)
func TestDomainAcme_RenewRequired(t *testing.T) {
tests := []struct {
name string
expire time.Time
expected bool
}{
{
name: "expired certificate",
expire: time.Now().AddDate(0, 0, -1),
expected: true,
},
{
name: "expiring soon",
expire: time.Now().AddDate(0, 0, 25),
expected: true,
},
{
name: "valid certificate",
expire: time.Now().AddDate(0, 0, 60),
expected: false,
},
{
name: "zero time",
expire: time.Time{},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DomainAcme{ExpireDate: tt.expire}
result := d.RenewRequired()
if result != tt.expected {
t.Errorf("RenewRequired() = %v, want %v", result, tt.expected)
}
})
}
}
func TestDomainAcme_Expired(t *testing.T) {
tests := []struct {
name string
expire time.Time
expected bool
}{
{
name: "expired certificate",
expire: time.Now().AddDate(0, 0, -1),
expected: true,
},
{
name: "valid certificate",
expire: time.Now().AddDate(0, 0, 60),
expected: false,
},
{
name: "zero time",
expire: time.Time{},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &DomainAcme{ExpireDate: tt.expire}
result := d.Expired()
if result != tt.expected {
t.Errorf("Expired() = %v, want %v", result, tt.expected)
}
})
}
}
func TestDomainAcme_IsNull(t *testing.T) {
tests := []struct {
name string
acme DomainAcme
expected bool
}{
{
name: "null acme",
acme: DomainAcme{},
expected: true,
},
{
name: "non-null acme",
acme: DomainAcme{
IssuerData: IssuerData{
URL: "https://example.com",
Ca: "CA",
},
AccountKey: rsaPrivateKeyJSON{&rsa.PrivateKey{}},
IssueDate: time.Now(),
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.acme.IsNull()
if result != tt.expected {
t.Errorf("IsNull() = %v, want %v", result, tt.expected)
}
})
}
}
func TestNewManager(t *testing.T) {
email := "test@example.com"
location := "/tmp/certs"
staging := true
manager := NewManager(email, location, staging)
if manager.Email != email {
t.Errorf("Expected email %s, got %s", email, manager.Email)
}
if manager.Location != location {
t.Errorf("Expected location %s, got %s", location, manager.Location)
}
if manager.Staging != staging {
t.Errorf("Expected staging %v, got %v", staging, manager.Staging)
}
if manager.issuings == nil {
t.Error("Expected issuings map to be initialized")
}
}
func TestManager_AddCustomCert(t *testing.T) {
// Create temporary directory for testing
tempDir, err := os.MkdirTemp("", "certy_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
manager := NewManager("test@example.com", tempDir, false)
domain := "example.com"
certData, keyData := generateSelfSignedCert(t, domain)
err = manager.AddCustomCert(domain, certData, keyData)
if err != nil {
t.Fatalf("AddCustomCert failed: %v", err)
}
// Check if files were created
acmeFile := filepath.Join(tempDir, domain, domain+"-acme.json")
certFile := filepath.Join(tempDir, domain, domain+"-cert.crt")
keyFile := filepath.Join(tempDir, domain, domain+"-key.pem")
if _, err := os.Stat(acmeFile); os.IsNotExist(err) {
t.Error("ACME file was not created")
}
if _, err := os.Stat(certFile); os.IsNotExist(err) {
t.Error("Certificate file was not created")
}
if _, err := os.Stat(keyFile); os.IsNotExist(err) {
t.Error("Key file was not created")
}
// Verify key file has restrictive permissions
keyInfo, err := os.Stat(keyFile)
if err != nil {
t.Fatalf("Failed to stat key file: %v", err)
}
if keyInfo.Mode().Perm() != 0600 {
t.Errorf("Expected key file permission 0600, got %o", keyInfo.Mode().Perm())
}
// Verify ACME data
acmeData, err := manager.GetAcmeFileData(domain)
if err != nil {
t.Fatalf("Failed to get ACME data: %v", err)
}
if !acmeData.CustomCert {
t.Error("Expected CustomCert to be true")
}
if len(acmeData.Sans) != 1 || acmeData.Sans[0] != domain {
t.Error("Expected domain in Sans array")
}
// Verify CertFile/KeyFile store paths, not PEM content
if acmeData.CertFile != certFile {
t.Errorf("Expected CertFile to be path %s, got %s", certFile, acmeData.CertFile)
}
if acmeData.KeyFile != keyFile {
t.Errorf("Expected KeyFile to be path %s, got %s", keyFile, acmeData.KeyFile)
}
// Verify expiry was parsed from the certificate (not hardcoded 1 year)
if acmeData.ExpireDate.IsZero() {
t.Error("Expected ExpireDate to be set from certificate")
}
}
func TestManager_GetAcmeFileData_NotFound(t *testing.T) {
tempDir, err := os.MkdirTemp("", "certy_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
manager := NewManager("test@example.com", tempDir, false)
_, err = manager.GetAcmeFileData("nonexistent.com")
if err == nil {
t.Error("Expected error for non-existent domain")
}
}
func TestManager_GetChallengeToken_NotFound(t *testing.T) {
tempDir, err := os.MkdirTemp("", "certy_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
manager := NewManager("test@example.com", tempDir, false)
_, err = manager.GetChallengeToken("nonexistent.com")
if err == nil {
t.Error("Expected error for non-existent domain")
}
}
func TestValidateDomain(t *testing.T) {
tests := []struct {
name string
domain string
wantErr bool
}{
{"valid domain", "example.com", false},
{"valid subdomain", "sub.example.com", false},
{"empty domain", "", true},
{"path traversal with ..", "../etc/passwd", true},
{"path traversal with /", "foo/bar", true},
{"path traversal with backslash", "foo\\bar", true},
{"null byte", "foo\x00bar", true},
{"too long domain", string(make([]byte, 254)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateDomain(tt.domain)
if (err != nil) != tt.wantErr {
t.Errorf("validateDomain(%q) error = %v, wantErr %v", tt.domain, err, tt.wantErr)
}
})
}
}
func TestRsaPrivateKeyJSON_RoundTrip(t *testing.T) {
// Generate a real RSA key
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("Failed to generate RSA key: %v", err)
}
original := DomainAcme{
Sans: []string{"example.com"},
IssuerData: IssuerData{
URL: "https://acme.example.com",
Ca: "https://ca.example.com",
},
AccountKey: rsaPrivateKeyJSON{key},
CertFile: "/path/to/cert",
KeyFile: "/path/to/key",
IssueDate: time.Now().Truncate(time.Second),
ExpireDate: time.Now().Add(90 * 24 * time.Hour).Truncate(time.Second),
}
// Marshal
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("Failed to marshal: %v", err)
}
// Unmarshal
var restored DomainAcme
if err := json.Unmarshal(data, &restored); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
// Verify the key survived the round trip
if restored.AccountKey.PrivateKey == nil {
t.Fatal("AccountKey is nil after round trip")
}
if restored.AccountKey.D.Cmp(key.D) != 0 {
t.Error("AccountKey.D mismatch after round trip")
}
if restored.AccountKey.N.Cmp(key.N) != 0 {
t.Error("AccountKey.N mismatch after round trip")
}
}
func TestRsaPrivateKeyJSON_NilRoundTrip(t *testing.T) {
original := DomainAcme{
Sans: []string{"example.com"},
}
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("Failed to marshal: %v", err)
}
var restored DomainAcme
if err := json.Unmarshal(data, &restored); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
if restored.AccountKey.PrivateKey != nil {
t.Error("Expected nil AccountKey after round trip with nil key")
}
}
func TestManager_DirectoryURL(t *testing.T) {
staging := NewManager("test@example.com", "/tmp", true)
if staging.directoryURL() != letsencryptStagingURL {
t.Errorf("Expected staging URL, got %s", staging.directoryURL())
}
prod := NewManager("test@example.com", "/tmp", false)
if prod.directoryURL() != letsencryptProdURL {
t.Errorf("Expected prod URL, got %s", prod.directoryURL())
}
}
func TestManager_AddCustomCert_InvalidDomain(t *testing.T) {
manager := NewManager("test@example.com", "/tmp", false)
err := manager.AddCustomCert("../evil", "cert", "key")
if err == nil {
t.Error("Expected error for path traversal domain")
}
}
// generateSelfSignedCert generates a self-signed certificate and key PEM for testing.
func generateSelfSignedCert(t *testing.T, domain string) (certPEM, keyPEM string) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: domain},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(365 * 24 * time.Hour),
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
t.Fatalf("Failed to create certificate: %v", err)
}
certBlock := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
keyBytes, err := x509.MarshalECPrivateKey(key)
if err != nil {
t.Fatalf("Failed to marshal key: %v", err)
}
keyBlock := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes})
return string(certBlock), string(keyBlock)
}