-
Notifications
You must be signed in to change notification settings - Fork 0
/
privateKey_test.go
60 lines (56 loc) · 1.38 KB
/
privateKey_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
package certgo
import (
"crypto/ecdsa"
"testing"
"github.com/Alonza0314/cert-go/util"
)
var testCasePrivateKey = []struct {
name string
keyPath string
exist bool
expect *ecdsa.PrivateKey
}{
{
name: "test without exist",
keyPath: "./default_ca/test.key.pem",
exist: false,
},
{
name: "test with exist",
keyPath: "./default_ca/test.key.pem",
exist: true,
},
}
func TestCreatePrivateKey(t *testing.T) {
for _, testCase := range testCasePrivateKey {
t.Run(testCase.name, func(t *testing.T) {
privateKey, err := CreatePrivateKey(testCase.keyPath)
if testCase.exist {
if err == nil || err.Error() != "private key already exists" {
t.Fatalf("TestCreatePrivateKey: private key should exist")
}
} else {
if privateKey == nil {
t.Fatalf("TestCreatePrivateKey: private key is nil")
}
readPrivateKey, err := util.ReadPrivateKey(testCase.keyPath)
if err != nil {
t.Fatalf("TestCreatePrivateKey: %v", err)
}
if readPrivateKey == nil {
t.Fatalf("TestCreatePrivateKey: read private key is nil")
}
if privateKey.D.Cmp(readPrivateKey.D) != 0 {
t.Fatalf("TestCreatePrivateKey: private key is not equal")
}
}
})
}
for _, testCase := range testCasePrivateKey {
if !testCase.exist {
if err := util.FileDelete(testCase.keyPath); err != nil {
t.Fatalf("TestCreatePrivateKey: %v", err)
}
}
}
}