-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtls_test.go
More file actions
365 lines (327 loc) · 8.74 KB
/
Copy pathtls_test.go
File metadata and controls
365 lines (327 loc) · 8.74 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestResolvePath tests the resolvePath function with various path inputs
func TestResolvePath(t *testing.T) {
// We need to create some temporary state to test resolvePath with
tempDir, err := os.MkdirTemp("", "tls_test")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
testFile := filepath.Join(tempDir, "test.cert")
err = os.WriteFile(testFile, []byte("test cert content"), 0644)
require.NoError(t, err)
subDir := filepath.Join(tempDir, "subdir")
err = os.MkdirAll(subDir, 0755)
require.NoError(t, err)
subFile := filepath.Join(subDir, "sub.cert")
err = os.WriteFile(subFile, []byte("sub cert content"), 0644)
require.NoError(t, err)
tests := []struct {
name string
path string
fileType string
shouldErr bool
errMsg string
setup func() string // function to set up path relative to tempDir
}{
{
name: "empty path",
path: "",
fileType: "certificate",
},
{
name: "existing absolute path",
fileType: "certificate",
setup: func() string {
return testFile
},
},
{
name: "existing relative path",
fileType: "certificate",
setup: func() string {
// Change to tempDir and use relative path
original, _ := os.Getwd()
os.Chdir(tempDir)
t.Cleanup(func() { os.Chdir(original) })
return "test.cert"
},
},
{
name: "non-existing file",
path: "/non/existent/file.cert",
fileType: "certificate",
shouldErr: true,
errMsg: "certificate file does not exist",
},
{
name: "non-existent file from relative path",
fileType: "certificate",
shouldErr: true,
errMsg: "certificate file does not exist",
setup: func() string {
return filepath.Join(tempDir, "nonexistent.cert")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := tt.path
if tt.setup != nil {
path = tt.setup()
}
result, err := resolvePath(path, tt.fileType)
if tt.shouldErr {
assert.Error(t, err)
if tt.errMsg != "" {
assert.Contains(t, err.Error(), tt.errMsg)
}
return
}
assert.NoError(t, err)
if path == "" {
assert.Equal(t, "", result)
} else {
// Result should be absolute path
assert.True(t, filepath.IsAbs(result))
// If file should exist, verify it does
if !strings.Contains(tt.name, "non-existing") && !strings.Contains(tt.name, "nonexistent") {
_, statErr := os.Stat(result)
assert.NoError(t, statErr, "resolved file should exist")
}
}
})
}
}
func TestParsedTLSConfigValidate(t *testing.T) {
tempDir, err := os.MkdirTemp("", "tls_config_test")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
certFile := filepath.Join(tempDir, "cert.pem")
keyFile := filepath.Join(tempDir, "key.pem")
caFile := filepath.Join(tempDir, "ca.pem")
for _, file := range []string{certFile, keyFile, caFile} {
err = os.WriteFile(file, []byte("dummy content"), 0644)
require.NoError(t, err)
}
tests := []struct {
name string
config ParsedTLSConfig
shouldErr bool
errMsg string
setup func(*ParsedTLSConfig)
}{
{
name: "valid absolute paths",
config: ParsedTLSConfig{
CertFile: certFile,
KeyFile: keyFile,
CAFile: caFile,
},
},
{
name: "valid relative paths",
config: ParsedTLSConfig{
CertFile: "./cert.pem",
KeyFile: "./key.pem",
CAFile: "./ca.pem",
},
setup: func(config *ParsedTLSConfig) {
// Change to temp directory for relative paths to work
original, _ := os.Getwd()
os.Chdir(tempDir)
t.Cleanup(func() { os.Chdir(original) })
},
},
{
name: "cert without key",
config: ParsedTLSConfig{
CertFile: certFile,
// KeyFile is empty
},
shouldErr: true,
errMsg: "TLS key file must be specified when certificate file is provided",
},
{
name: "key without cert",
config: ParsedTLSConfig{
KeyFile: keyFile,
// CertFile is empty
},
shouldErr: true,
errMsg: "TLS certificate file must be specified when key file is provided",
},
{
name: "non-existent cert file",
config: ParsedTLSConfig{
CertFile: "/non/existent/cert.pem",
KeyFile: keyFile,
},
shouldErr: true,
errMsg: "TLS certificate file does not exist",
},
{
name: "non-existent key file",
config: ParsedTLSConfig{
CertFile: certFile,
KeyFile: "/non/existent/key.pem",
},
shouldErr: true,
errMsg: "TLS key file does not exist",
},
{
name: "non-existent CA file",
config: ParsedTLSConfig{
CertFile: certFile,
KeyFile: keyFile,
CAFile: "/non/existent/ca.pem",
},
shouldErr: true,
errMsg: "TLS CA file does not exist",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := tt.config
if tt.setup != nil {
tt.setup(&config)
}
err := config.Validate()
if tt.shouldErr {
assert.Error(t, err)
if tt.errMsg != "" {
assert.Contains(t, err.Error(), tt.errMsg)
}
return
}
assert.NoError(t, err)
// Verify that paths have been resolved to absolute paths
if config.CertFile != "" {
assert.True(t, filepath.IsAbs(config.CertFile))
}
if config.KeyFile != "" {
assert.True(t, filepath.IsAbs(config.KeyFile))
}
if config.CAFile != "" {
assert.True(t, filepath.IsAbs(config.CAFile))
}
})
}
}
func TestLoadTLSConfig(t *testing.T) {
// Create temporary directory for test files
tempDir, err := os.MkdirTemp("", "tls_config_load_test")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create test certificate files
certFile := filepath.Join(tempDir, "server.crt")
keyFile := filepath.Join(tempDir, "server.key")
caFile := filepath.Join(tempDir, "ca.crt")
for _, file := range []string{certFile, keyFile, caFile} {
err = os.WriteFile(file, []byte("dummy cert content"), 0644)
require.NoError(t, err)
}
tests := []struct {
name string
configJSON map[string]any
shouldErr bool
errMsg string
validateFunc func(*testing.T, *ParsedTLSConfig)
}{
{
name: "valid config with absolute paths",
configJSON: map[string]any{
"cert_file": certFile,
"key_file": keyFile,
"ca_file": caFile,
"server_name": "test",
"insecure_skip_verify": false,
},
validateFunc: func(t *testing.T, config *ParsedTLSConfig) {
assert.Equal(t, certFile, config.CertFile)
assert.Equal(t, keyFile, config.KeyFile)
assert.Equal(t, caFile, config.CAFile)
assert.Equal(t, "test", config.ServerName)
assert.False(t, config.InsecureSkipVerify)
},
},
{
name: "config with relative paths",
configJSON: map[string]any{
"cert_file": "./server.crt",
"key_file": "./server.key",
"ca_file": "./ca.crt",
},
validateFunc: func(t *testing.T, config *ParsedTLSConfig) {
// Paths should be resolved to absolute paths
assert.True(t, filepath.IsAbs(config.CertFile))
assert.True(t, filepath.IsAbs(config.KeyFile))
assert.True(t, filepath.IsAbs(config.CAFile))
// Files should exist
for _, path := range []string{config.CertFile, config.KeyFile, config.CAFile} {
_, err := os.Stat(path)
assert.NoError(t, err)
}
},
},
{
name: "cert without key",
configJSON: map[string]any{
"cert_file": certFile,
},
shouldErr: true,
errMsg: "TLS key file must be specified when certificate file is provided",
},
{
name: "non-existent files",
configJSON: map[string]any{
"cert_file": "/non/existent/cert.pem",
"key_file": "/non/existent/key.pem",
},
shouldErr: true,
errMsg: "TLS certificate file does not exist",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
configFile := filepath.Join(tempDir, fmt.Sprintf("config_%s.json", strings.ReplaceAll(tt.name, " ", "_")))
// For relative paths test, change to temp directory
if tt.name == "config with relative paths" {
original, _ := os.Getwd()
os.Chdir(tempDir)
defer os.Chdir(original)
}
configData, err := json.Marshal(tt.configJSON)
require.NoError(t, err)
err = os.WriteFile(configFile, configData, 0644)
require.NoError(t, err)
// Load configuration
config, err := LoadTLSConfig(configFile)
if tt.shouldErr {
assert.Error(t, err)
if tt.errMsg != "" {
assert.Contains(t, err.Error(), tt.errMsg)
}
return
}
require.NoError(t, err)
require.NotNil(t, config)
if tt.validateFunc != nil {
tt.validateFunc(t, config)
}
})
}
}