-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509wrapper_test.go
124 lines (102 loc) · 4.02 KB
/
x509wrapper_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
package x509wrapper
import (
"crypto/x509/pkix"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const testDir = "/tmp/test_cert"
func TestMain(m *testing.M) {
os.RemoveAll(testDir)
code := m.Run()
os.RemoveAll(testDir)
os.Exit(code)
}
func TestNewCertDefault(t *testing.T) {
cert := NewCert("", "")
assert.Equal(t, "ca", cert.Name, "Default certificate name")
default_dir, _ := filepath.Abs("./")
assert.Equal(t, default_dir, cert.Dir, "Default certificate file dir")
assert.Equal(t, default_dir+"/ca.crt", cert.CertFile, "Default certificate file name")
assert.Equal(t, default_dir+"/ca.key", cert.KeyFile, "Default private key file name")
assert.Nil(t, cert.Cert, "Empty certificate")
assert.Nil(t, cert.PrivateKey, "Empty private key")
}
func TestNewCert(t *testing.T) {
cert := NewCert(" -Test - CA ", "/tmp/smth/../test_cert")
assert.Equal(t, "-Test - CA", cert.Name, "Normalized certificate name")
assert.Equal(t, testDir, cert.Dir, "Normalized certificate file dir")
assert.Equal(t, testDir+"/test-ca.crt", cert.CertFile, "Normalized certificate file name")
assert.Equal(t, testDir+"/test-ca.key", cert.KeyFile, "Normalized private key file name")
assert.Nil(t, cert.Cert, "Empty certificate")
assert.Nil(t, cert.PrivateKey, "Empty private key")
}
func TestCreateNewCert(t *testing.T) {
cert := NewCert("test $ CA", "/tmp/./test_cert")
// Try to load cert from testDir
err := cert.Load()
assert.Error(t, err, "Certificate not found")
// Generate new CA certificate
subject := pkix.Name{
Country: []string{"Test Country"},
Organization: []string{"Test Organization"},
OrganizationalUnit: []string{"Test Unit"},
Locality: []string{"Test Locality"},
Province: []string{"Test Province"},
StreetAddress: []string{"Test Address"},
PostalCode: []string{"Test Postal Code"},
SerialNumber: "",
CommonName: "Test",
Names: []pkix.AttributeTypeAndValue{},
ExtraNames: []pkix.AttributeTypeAndValue{},
}
newCAcert := PrepareCA(subject, time.Now(), time.Now().AddDate(0, 0, 1))
// Generate new CA cert and private key
err = cert.AddCertAndKey(newCAcert, 4096)
assert.NoError(t, err, "No error creating new CA certificate")
// Generate one more time (idempotent operation)
err = cert.AddCertAndKey(newCAcert, 4096)
assert.NoError(t, err, "No error creating new CA certificate")
// Save new CA cert
err = cert.Save(nil)
assert.NoError(t, err, "No error while saving new CA certificate")
assert.True(t, cert.ExistsBoth(), "Both file should exist")
err = cert.AddCertAndKey(newCAcert, 4096)
assert.Error(t, err, "Inposible to recreate certificate after saving")
}
func TestSignNewCert(t *testing.T) {
cert := NewCert("Test CA", "/tmp/./test_cert")
// Try to load cert from testDir
err := cert.Load()
assert.NoError(t, err, "Certificate should be found")
assert.NotEmpty(t, cert.Cert, "Certificate should be loaded")
assert.NotEmpty(t, cert.PrivateKey, "Key pair should be loaded")
// Initiate new certificate wrapper
clientCert := NewCert("Client", "/tmp/test_cert")
// Try to load cert from testDir
err = clientCert.Load()
assert.Error(t, err, "Certificate not found")
// Generate new CA certificate
subject := pkix.Name{
Country: []string{"Test Country"},
Organization: []string{"Test Organization"},
OrganizationalUnit: []string{"Test Unit"},
Locality: []string{"Test Locality"},
Province: []string{"Test Province"},
StreetAddress: []string{"Test Address"},
PostalCode: []string{"Test Postal Code"},
SerialNumber: "",
CommonName: "Test Client",
Names: []pkix.AttributeTypeAndValue{},
ExtraNames: []pkix.AttributeTypeAndValue{},
}
err = clientCert.AddCertAndKey(
PrepareCert(subject, []string{}, time.Now(), time.Now().AddDate(0, 0, 1)),
2048,
)
assert.NoError(t, err, "No error creating new certificate")
err = clientCert.Save(cert)
assert.NoError(t, err, "No error saving certificate")
}