-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_test.go
154 lines (150 loc) · 4.49 KB
/
cert_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
package certgo
import (
"crypto/x509"
"reflect"
"testing"
"github.com/Alonza0314/cert-go/model"
"github.com/Alonza0314/cert-go/util"
)
var testCaseCert = []struct {
name string
yamlPath string
certPath string
exist bool
expect *x509.Certificate
}{
{
name: "root without exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/root/root.cert.pem",
exist: false,
},
{
name: "root with exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/root/root.cert.pem",
exist: true,
},
{
name: "intermediate without exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/intermediate/intermediate.cert.pem",
exist: false,
},
{
name: "intermediate with exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/intermediate/intermediate.cert.pem",
exist: true,
},
{
name: "server without exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/server/server.cert.pem",
exist: false,
},
{
name: "server with exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/server/server.cert.pem",
exist: true,
},
{
name: "client without exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/client/client.cert.pem",
exist: false,
},
{
name: "client with exist",
yamlPath: "./defaultCfg.yml",
certPath: "./default_ca/client/client.cert.pem",
exist: true,
},
}
func TestSignCertificate(t *testing.T) {
var err error
for _, testCase := range testCaseCert {
t.Run(testCase.name, func(t *testing.T) {
switch testCase.name {
case "root without exist", "root with exist":
testCase.expect, err = SignRootCertificate(testCase.yamlPath)
case "intermediate without exist", "intermediate with exist":
testCase.expect, err = SignIntermediateCertificate(testCase.yamlPath)
case "server without exist", "server with exist":
testCase.expect, err = SignServerCertificate(testCase.yamlPath)
case "client without exist", "client with exist":
testCase.expect, err = SignClientCertificate(testCase.yamlPath)
}
if testCase.exist {
if err == nil || err.Error() != "certificate already exists" {
t.Fatalf("TestSignRootCertificate: certificate should exist")
}
} else {
if err != nil {
t.Fatalf("TestSignRootCertificate: %v", err)
}
if testCase.expect == nil {
t.Fatalf("TestSignRootCertificate: certificate is nil")
}
readCert, err := util.ReadCertificate(testCase.certPath)
if err != nil {
t.Fatalf("TestSignRootCertificate: %v", err)
}
if readCert == nil {
t.Fatalf("TestSignRootCertificate: read certificate is nil")
}
if !reflect.DeepEqual(testCase.expect, readCert) {
t.Fatalf("TestSignRootCertificate: certificate is not equal")
}
}
})
}
for _, testCase := range testCaseCert {
if !testCase.exist {
var cfg model.CAConfig
if err := util.ReadYamlFileToStruct(testCase.yamlPath, &cfg); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
switch testCase.name {
case "root without exist":
if err := util.FileDelete(cfg.CA.Root.CertFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Root.KeyFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
case "intermediate without exist":
if err := util.FileDelete(cfg.CA.Intermediate.CertFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Intermediate.CsrFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Intermediate.KeyFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
case "server without exist":
if err := util.FileDelete(cfg.CA.Server.CertFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Server.CsrFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Server.KeyFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
case "client without exist":
if err := util.FileDelete(cfg.CA.Client.CertFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Client.CsrFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
if err := util.FileDelete(cfg.CA.Client.KeyFilePath); err != nil {
t.Fatalf("TestSignCertificate: %v", err)
}
}
}
}
}