2019-01-21 03:37:20 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2019-01-21 03:37:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type testCtx struct{}
|
|
|
|
type testEP1 struct{}
|
|
|
|
type testEP2 struct{}
|
|
|
|
type testEP3 struct{}
|
|
|
|
|
|
|
|
func TestConfigModification(t *testing.T) {
|
|
|
|
cfg := NewConfig(func() interface{} { return &testCtx{} }, EndpointTypeGetter("ep1", func() interface{} { return &testEP1{} }))
|
|
|
|
assert.Equal(t, &testCtx{}, cfg.contextType())
|
|
|
|
assert.Equal(t, &testEP1{}, cfg.endpointTypes["ep1"]())
|
|
|
|
cfgCopy := cfg
|
|
|
|
|
|
|
|
// modify existing endpoint
|
|
|
|
cfg.SetEndpoint("ep1", func() interface{} { return &testEP2{} })
|
|
|
|
// add endpoint
|
|
|
|
cfg.SetEndpoint("ep2", func() interface{} { return &testEP3{} })
|
|
|
|
assert.Equal(t, &testCtx{}, cfg.contextType())
|
|
|
|
assert.Equal(t, &testEP2{}, cfg.endpointTypes["ep1"]())
|
|
|
|
assert.Equal(t, &testEP3{}, cfg.endpointTypes["ep2"]())
|
|
|
|
// check it applied on already initialized store
|
|
|
|
assert.Equal(t, &testCtx{}, cfgCopy.contextType())
|
|
|
|
assert.Equal(t, &testEP2{}, cfgCopy.endpointTypes["ep1"]())
|
|
|
|
assert.Equal(t, &testEP3{}, cfgCopy.endpointTypes["ep2"]())
|
|
|
|
}
|
2020-09-16 10:35:04 -04:00
|
|
|
|
|
|
|
func TestValidFilePaths(t *testing.T) {
|
|
|
|
paths := map[string]bool{
|
|
|
|
"tls/_/../../something": false,
|
|
|
|
"tls/../../something": false,
|
|
|
|
"../../something": false,
|
|
|
|
"/tls/absolute/unix/path": false,
|
|
|
|
`C:\tls\absolute\windows\path`: false,
|
|
|
|
"C:/tls/absolute/windows/path": false,
|
|
|
|
"tls/kubernetes/key.pem": true,
|
|
|
|
}
|
|
|
|
for p, expectedValid := range paths {
|
|
|
|
err := isValidFilePath(p)
|
|
|
|
assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", p, expectedValid)
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 10:24:24 -04:00
|
|
|
|
|
|
|
func TestValidateContextName(t *testing.T) {
|
|
|
|
names := map[string]bool{
|
|
|
|
"../../invalid/escape": false,
|
|
|
|
"/invalid/absolute": false,
|
|
|
|
`\invalid\windows`: false,
|
|
|
|
"validname": true,
|
|
|
|
}
|
|
|
|
for n, expectedValid := range names {
|
|
|
|
err := ValidateContextName(n)
|
|
|
|
assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", n, expectedValid)
|
|
|
|
}
|
|
|
|
}
|