-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr_test.go
75 lines (71 loc) · 1.67 KB
/
csr_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
package certgo
import (
"crypto/x509"
"reflect"
"testing"
"github.com/Alonza0314/cert-go/model"
"github.com/Alonza0314/cert-go/util"
)
var testCaseCsr = []struct {
name string
cfg model.Certificate
exist bool
expect *x509.CertificateRequest
}{
{
name: "test without exist",
cfg: model.Certificate{
KeyFilePath: "./default_ca/test.key.pem",
CsrFilePath: "./default_ca/test.csr.pem",
},
exist: false,
},
{
name: "test with exist",
cfg: model.Certificate{
KeyFilePath: "./default_ca/test.key.pem",
CsrFilePath: "./default_ca/test.csr.pem",
},
exist: true,
},
}
func TestCreateCsr(t *testing.T) {
var err error
for _, testCase := range testCaseCsr {
t.Run(testCase.name, func(t *testing.T) {
testCase.expect, err = CreateCsr(testCase.cfg)
if testCase.exist {
if err == nil || err.Error() != "csr already exists" {
t.Fatalf("TestCreateCsr: csr should exist")
}
} else {
if err != nil {
t.Fatalf("TestCreateCsr: %v", err)
}
if testCase.expect == nil {
t.Fatalf("TestCreateCsr: csr is nil")
}
readCsr, err := util.ReadCsr(testCase.cfg.CsrFilePath)
if err != nil {
t.Fatalf("TestCreateCsr: %v", err)
}
if readCsr == nil {
t.Fatalf("TestCreateCsr: read csr is nil")
}
if !reflect.DeepEqual(testCase.expect, readCsr) {
t.Fatalf("TestCreateCsr: csr is not equal")
}
}
})
}
for _, testCase := range testCaseCsr {
if !testCase.exist {
if err := util.FileDelete(testCase.cfg.KeyFilePath); err != nil {
t.Fatalf("TestCreateCsr: %v", err)
}
if err := util.FileDelete(testCase.cfg.CsrFilePath); err != nil {
t.Fatalf("TestCreateCsr: %v", err)
}
}
}
}