Repository navigation
Expand file tree
/
Copy pathsign_test.go
More file actions
181 lines (151 loc) · 4.58 KB
/
Copy pathsign_test.go
File metadata and controls
181 lines (151 loc) · 4.58 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
package cmd
import (
"certwiz/pkg/cert"
"os"
"path/filepath"
"testing"
)
func TestSignCommand(t *testing.T) {
// Create a temporary directory for test outputs
tmpDir, err := os.MkdirTemp("", "certwiz-sign-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
// Later subtests set a SAN override; start every run without one.
signSANs = nil
t.Cleanup(func() { signSANs = nil })
// First, create a CA
caOptions := cert.CAOptions{
CommonName: "Test CA",
Organization: "Test Org",
Country: "US",
Days: 365,
KeySize: 2048,
}
caCertPath := filepath.Join(tmpDir, "ca.crt")
caKeyPath := filepath.Join(tmpDir, "ca.key")
err = cert.GenerateCA(caOptions, caCertPath, caKeyPath)
if err != nil {
t.Fatalf("Failed to generate CA: %v", err)
}
// Create a CSR
csrOptions := cert.CSROptions{
CommonName: "test.example.com",
Organization: "Test Company",
Country: "US",
SANs: []string{"test.example.com", "www.test.example.com"},
KeySize: 2048,
}
csrPath := filepath.Join(tmpDir, "test.csr")
keyPath := filepath.Join(tmpDir, "test.key")
err = cert.GenerateCSR(csrOptions, csrPath, keyPath)
if err != nil {
t.Fatalf("Failed to generate CSR: %v", err)
}
// Test signing the CSR
t.Run("SignCSR", func(t *testing.T) {
signCSR = csrPath
signCA = caCertPath
signCAKey = caKeyPath
signDays = 365
signOutput = tmpDir
err := signCmd.RunE(signCmd, []string{})
if err != nil {
t.Fatalf("Certificate signing failed: %v", err)
}
// Check if certificate was created
certPath := filepath.Join(tmpDir, "test.crt")
if _, err := os.Stat(certPath); os.IsNotExist(err) {
t.Errorf("Signed certificate was not created: %s", certPath)
}
// Verify the certificate
signedCert, err := cert.InspectFile(certPath)
if err != nil {
t.Fatalf("Failed to inspect signed certificate: %v", err)
}
// Check that it's not a CA
if signedCert.IsCA {
t.Error("Signed certificate should not be a CA")
}
// Check subject matches CSR
if signedCert.Subject.CommonName != "test.example.com" {
t.Errorf("Certificate CN mismatch: got %s, expected test.example.com",
signedCert.Subject.CommonName)
}
// Check issuer matches CA
if signedCert.Issuer.CommonName != "Test CA" {
t.Errorf("Certificate issuer mismatch: got %s, expected Test CA",
signedCert.Issuer.CommonName)
}
// Check SANs
if len(signedCert.DNSNames) != 2 {
t.Errorf("Certificate SAN count mismatch: got %d, expected 2",
len(signedCert.DNSNames))
}
})
// Test signing with SAN override
t.Run("SignWithSANOverride", func(t *testing.T) {
signCSR = csrPath
signCA = caCertPath
signCAKey = caKeyPath
signDays = 365
signOutput = tmpDir
signSANs = []string{"override.example.com", "IP:10.0.0.1"}
// Use a different output name to avoid overwriting
certPath := filepath.Join(tmpDir, "test-override.crt")
// Temporarily modify the sign command to output to a different file
options := cert.SignOptions{
CSRPath: csrPath,
CACert: caCertPath,
CAKey: caKeyPath,
Days: 365,
SANs: signSANs,
}
err := cert.SignCSR(options, certPath)
if err != nil {
t.Fatalf("Certificate signing with SAN override failed: %v", err)
}
// Verify the certificate has overridden SANs
signedCert, err := cert.InspectFile(certPath)
if err != nil {
t.Fatalf("Failed to inspect signed certificate: %v", err)
}
// Check that SANs were overridden
if len(signedCert.DNSNames) != 1 || signedCert.DNSNames[0] != "override.example.com" {
t.Errorf("Certificate DNS SAN override failed: got %v, expected [override.example.com]",
signedCert.DNSNames)
}
if len(signedCert.IPAddresses) != 1 || signedCert.IPAddresses[0].String() != "10.0.0.1" {
t.Errorf("Certificate IP SAN override failed: got %v, expected [10.0.0.1]",
signedCert.IPAddresses)
}
})
// Test missing required arguments
t.Run("MissingArguments", func(t *testing.T) {
// Test missing CSR
signCSR = ""
signCA = caCertPath
signCAKey = caKeyPath
err := signCmd.RunE(signCmd, []string{})
if err == nil {
t.Error("Expected error for missing CSR, but got none")
}
// Test missing CA cert
signCSR = csrPath
signCA = ""
signCAKey = caKeyPath
err = signCmd.RunE(signCmd, []string{})
if err == nil {
t.Error("Expected error for missing CA cert, but got none")
}
// Test missing CA key
signCSR = csrPath
signCA = caCertPath
signCAKey = ""
err = signCmd.RunE(signCmd, []string{})
if err == nil {
t.Error("Expected error for missing CA key, but got none")
}
})
}