-
Notifications
You must be signed in to change notification settings - Fork 8
/
gencerts_test.go
175 lines (146 loc) · 4.86 KB
/
gencerts_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package testcerts
import (
"os"
"path/filepath"
"testing"
)
func TestGeneratingCerts(t *testing.T) {
t.Run("No Domain", func(t *testing.T) {
cert, key, err := GenerateCerts()
if err != nil {
t.Errorf("Error while generating certificates - %s", err)
}
if len(cert) == 0 || len(key) == 0 {
t.Errorf("Cert %d or Key %d is empty", len(cert), len(key))
}
})
t.Run("With Domain", func(t *testing.T) {
cert, key, err := GenerateCerts("example.com")
if err != nil {
t.Errorf("Error while generating certificates - %s", err)
}
if len(cert) == 0 || len(key) == 0 {
t.Errorf("Cert %d or Key %d is empty", len(cert), len(key))
}
})
t.Run("With Many Domains", func(t *testing.T) {
cert, key, err := GenerateCerts("example.com", "example.org", "example.net")
if err != nil {
t.Errorf("Error while generating certificates - %s", err)
}
if len(cert) == 0 || len(key) == 0 {
t.Errorf("Cert %d or Key %d is empty", len(cert), len(key))
}
})
}
func TestGeneratingCertsToFile(t *testing.T) {
t.Run("Test the happy path", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
certPath := filepath.Join(tempDir, "cert")
keyPath := filepath.Join(tempDir, "key")
err = GenerateCertsToFile(certPath, keyPath)
if err != nil {
t.Errorf("Error while generating certificates to files - %s", err)
}
// Check if Cert file exists
_, err = os.Stat(certPath)
if err != nil {
t.Errorf("Error while generating certificates to files file error - %s", err)
}
// Check if Key file exists
_, err = os.Stat(keyPath)
if err != nil {
t.Errorf("Error while generating certificates to files file error - %s", err)
}
})
t.Run("Testing the unhappy path for cert files", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
certPath := filepath.Join(tempDir, "doesntexist", "cert")
keyPath := filepath.Join(tempDir, "key")
err = GenerateCertsToFile(certPath, keyPath)
if err == nil {
t.Errorf("Expected error when generating a certificate with a bad path got nil")
}
})
t.Run("Testing the unhappy path for key files", func(t *testing.T) {
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)
certPath := filepath.Join(tempDir, "cert")
keyPath := filepath.Join(tempDir, "doesntexist", "key")
err = GenerateCertsToFile(certPath, keyPath)
if err == nil {
t.Errorf("Expected error when generating a key with a bad path got nil")
}
})
t.Run("Testing the unhappy path for insufficient permissions", func(t *testing.T) {
dir, err := os.MkdirTemp("", "permission-test")
if err != nil {
t.Errorf("Error creating temp directory - %s", err)
}
defer os.RemoveAll(dir)
// Change permissions of the temp directory so that it can't be written to
err = os.Chmod(dir, 0444)
if err != nil {
t.Errorf("Error changing permissions of temp directory - %s", err)
}
certPath := filepath.Join(dir, "cert")
keyPath := filepath.Join(dir, "key")
err = GenerateCertsToFile(certPath, keyPath)
if err == nil {
t.Errorf("Expected error when generating certificate with insufficient permissions, got nil")
}
})
}
func TestGenerateCertsToTempFile(t *testing.T) {
t.Run("Test the happy path", func(t *testing.T) {
certFile, keyFile, err := GenerateCertsToTempFile("/tmp")
if err != nil {
t.Errorf("Error while generating certificates to temp files - %s", err)
}
// Check if Cert file exists
_, err = os.Stat(certFile)
if err != nil {
t.Errorf("Error while generating certificates to temp files file error - %s", err)
}
_ = os.Remove(certFile)
// Check if Key file exists
_, err = os.Stat(keyFile)
if err != nil {
t.Errorf("Error while generating certificates to temp files file error - %s", err)
}
_ = os.Remove(keyFile)
})
t.Run("Testing the unhappy path when creating cert temp file", func(t *testing.T) {
_, _, err := GenerateCertsToTempFile("/doesnotexist")
if err == nil {
t.Errorf("Expected error when generating a certificate with a bad directory path got nil")
}
})
t.Run("Testing the unhappy path for insufficient permissions when creating temp file", func(t *testing.T) {
dir, err := os.MkdirTemp("", "permission-test")
if err != nil {
t.Errorf("Error creating temp directory - %s", err)
}
defer os.RemoveAll(dir)
// Change permissions of the temp directory so that it can't be written to
err = os.Chmod(dir, 0444)
if err != nil {
t.Errorf("Error changing permissions of temp directory - %s", err)
}
_, _, err = GenerateCertsToTempFile(dir)
if err == nil {
t.Errorf("Expected error when generating a key with a bad directory path got nil")
}
})
}