2023-03-15 16:00:52 +00:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2015-03-12 22:21:11 +00:00
package server
import (
2023-01-16 16:07:18 +00:00
"fmt"
"reflect"
"strings"
2015-03-12 22:21:11 +00:00
"testing"
2023-04-21 08:54:38 +00:00
"github.com/stretchr/testify/require"
2015-03-12 22:21:11 +00:00
)
func TestLoadConfigFile ( t * testing . T ) {
2019-10-17 17:33:00 +00:00
testLoadConfigFile ( t )
2015-03-12 22:21:11 +00:00
}
func TestLoadConfigFile_json ( t * testing . T ) {
2019-10-17 17:33:00 +00:00
testLoadConfigFile_json ( t )
2015-03-12 22:21:11 +00:00
}
2020-05-19 23:13:05 +00:00
func TestLoadConfigFileIntegerAndBooleanValues ( t * testing . T ) {
testLoadConfigFileIntegerAndBooleanValues ( t )
}
func TestLoadConfigFileIntegerAndBooleanValuesJson ( t * testing . T ) {
testLoadConfigFileIntegerAndBooleanValuesJson ( t )
}
2020-11-13 18:26:58 +00:00
func TestLoadConfigFileWithLeaseMetricTelemetry ( t * testing . T ) {
testLoadConfigFileLeaseMetrics ( t )
}
2015-03-12 22:21:11 +00:00
func TestLoadConfigDir ( t * testing . T ) {
2019-10-17 17:33:00 +00:00
testLoadConfigDir ( t )
2015-03-12 22:21:11 +00:00
}
2016-03-09 23:59:44 +00:00
2019-10-08 17:57:15 +00:00
func TestConfig_Sanitized ( t * testing . T ) {
2019-10-17 17:33:00 +00:00
testConfig_Sanitized ( t )
2019-10-08 17:57:15 +00:00
}
2017-08-03 11:33:06 +00:00
func TestParseListeners ( t * testing . T ) {
2019-10-17 17:33:00 +00:00
testParseListeners ( t )
2017-08-03 11:33:06 +00:00
}
2019-10-17 17:33:00 +00:00
2022-11-01 18:02:07 +00:00
func TestParseUserLockouts ( t * testing . T ) {
testParseUserLockouts ( t )
}
2021-10-21 14:10:48 +00:00
func TestParseSockaddrTemplate ( t * testing . T ) {
testParseSockaddrTemplate ( t )
}
2020-01-14 01:02:16 +00:00
func TestConfigRaftRetryJoin ( t * testing . T ) {
testConfigRaftRetryJoin ( t )
}
2020-07-23 17:53:00 +00:00
func TestParseSeals ( t * testing . T ) {
testParseSeals ( t )
}
2021-05-04 19:47:56 +00:00
2022-06-09 22:55:49 +00:00
func TestParseStorage ( t * testing . T ) {
testParseStorageTemplate ( t )
}
2023-07-06 10:05:43 +00:00
// TestConfigWithAdministrativeNamespace tests that .hcl and .json configurations are correctly parsed when the administrative_namespace_path is present.
func TestConfigWithAdministrativeNamespace ( t * testing . T ) {
testConfigWithAdministrativeNamespaceHcl ( t )
testConfigWithAdministrativeNamespaceJson ( t )
}
2021-05-04 19:47:56 +00:00
func TestUnknownFieldValidation ( t * testing . T ) {
testUnknownFieldValidation ( t )
}
2022-05-12 16:04:56 +00:00
2022-10-27 14:28:03 +00:00
func TestUnknownFieldValidationJson ( t * testing . T ) {
testUnknownFieldValidationJson ( t )
}
func TestUnknownFieldValidationHcl ( t * testing . T ) {
testUnknownFieldValidationHcl ( t )
}
2022-05-12 16:04:56 +00:00
func TestUnknownFieldValidationListenerAndStorage ( t * testing . T ) {
testUnknownFieldValidationStorageAndListener ( t )
}
2023-01-16 16:07:18 +00:00
func TestExperimentsConfigParsing ( t * testing . T ) {
const envKey = "VAULT_EXPERIMENTS"
originalValue := validExperiments
validExperiments = [ ] string { "foo" , "bar" , "baz" }
t . Cleanup ( func ( ) {
validExperiments = originalValue
} )
for name , tc := range map [ string ] struct {
fromConfig [ ] string
fromEnv [ ] string
fromCLI [ ] string
expected [ ] string
expectedError string
} {
// Multiple sources.
"duplication" : { [ ] string { "foo" } , [ ] string { "foo" } , [ ] string { "foo" } , [ ] string { "foo" } , "" } ,
"disjoint set" : { [ ] string { "foo" } , [ ] string { "bar" } , [ ] string { "baz" } , [ ] string { "foo" , "bar" , "baz" } , "" } ,
// Single source.
"config only" : { [ ] string { "foo" } , nil , nil , [ ] string { "foo" } , "" } ,
"env only" : { nil , [ ] string { "foo" } , nil , [ ] string { "foo" } , "" } ,
"CLI only" : { nil , nil , [ ] string { "foo" } , [ ] string { "foo" } , "" } ,
// Validation errors.
"config invalid" : { [ ] string { "invalid" } , nil , nil , nil , "from config" } ,
"env invalid" : { nil , [ ] string { "invalid" } , nil , nil , "from environment variable" } ,
"CLI invalid" : { nil , nil , [ ] string { "invalid" } , nil , "from command line flag" } ,
} {
t . Run ( name , func ( t * testing . T ) {
var configString string
t . Setenv ( envKey , strings . Join ( tc . fromEnv , "," ) )
if len ( tc . fromConfig ) != 0 {
configString = fmt . Sprintf ( "experiments = [\"%s\"]" , strings . Join ( tc . fromConfig , "\", \"" ) )
}
config , err := ParseConfig ( configString , "" )
if err == nil {
err = ExperimentsFromEnvAndCLI ( config , envKey , tc . fromCLI )
}
switch tc . expectedError {
case "" :
if err != nil {
t . Fatal ( err )
}
default :
if err == nil || ! strings . Contains ( err . Error ( ) , tc . expectedError ) {
t . Fatalf ( "Expected error to contain %q, but got: %s" , tc . expectedError , err )
}
}
} )
}
}
func TestValidate ( t * testing . T ) {
originalValue := validExperiments
for name , tc := range map [ string ] struct {
validSet [ ] string
input [ ] string
expectError bool
} {
// Valid cases
"minimal valid" : { [ ] string { "foo" } , [ ] string { "foo" } , false } ,
"valid subset" : { [ ] string { "foo" , "bar" } , [ ] string { "bar" } , false } ,
"repeated" : { [ ] string { "foo" } , [ ] string { "foo" , "foo" } , false } ,
// Error cases
"partially valid" : { [ ] string { "foo" , "bar" } , [ ] string { "foo" , "baz" } , true } ,
"empty" : { [ ] string { "foo" } , [ ] string { "" } , true } ,
"no valid experiments" : { [ ] string { } , [ ] string { "foo" } , true } ,
} {
t . Run ( name , func ( t * testing . T ) {
t . Cleanup ( func ( ) {
validExperiments = originalValue
} )
validExperiments = tc . validSet
err := validateExperiments ( tc . input )
if tc . expectError && err == nil {
t . Fatal ( "Expected error but got none" )
}
if ! tc . expectError && err != nil {
t . Fatal ( "Did not expect error but got" , err )
}
} )
}
}
func TestMerge ( t * testing . T ) {
for name , tc := range map [ string ] struct {
left [ ] string
right [ ] string
expected [ ] string
} {
"disjoint" : { [ ] string { "foo" } , [ ] string { "bar" } , [ ] string { "foo" , "bar" } } ,
"empty left" : { [ ] string { } , [ ] string { "foo" } , [ ] string { "foo" } } ,
"empty right" : { [ ] string { "foo" } , [ ] string { } , [ ] string { "foo" } } ,
"overlapping" : { [ ] string { "foo" , "bar" } , [ ] string { "foo" , "baz" } , [ ] string { "foo" , "bar" , "baz" } } ,
} {
t . Run ( name , func ( t * testing . T ) {
result := mergeExperiments ( tc . left , tc . right )
if ! reflect . DeepEqual ( tc . expected , result ) {
t . Fatalf ( "Expected %v but got %v" , tc . expected , result )
}
} )
}
}
2023-04-21 08:54:38 +00:00
// Test_parseDevTLSConfig verifies that both Windows and Unix directories are correctly escaped when creating a dev TLS
// configuration in HCL
func Test_parseDevTLSConfig ( t * testing . T ) {
tests := [ ] struct {
name string
certDirectory string
} {
{
name : "windows path" ,
certDirectory : ` C:\Users\ADMINI~1\AppData\Local\Temp\2\vault-tls4169358130 ` ,
} ,
{
name : "unix path" ,
certDirectory : "/tmp/vault-tls4169358130" ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
cfg , err := parseDevTLSConfig ( "file" , tt . certDirectory )
require . NoError ( t , err )
require . Equal ( t , fmt . Sprintf ( "%s/%s" , tt . certDirectory , VaultDevCertFilename ) , cfg . Listeners [ 0 ] . TLSCertFile )
require . Equal ( t , fmt . Sprintf ( "%s/%s" , tt . certDirectory , VaultDevKeyFilename ) , cfg . Listeners [ 0 ] . TLSKeyFile )
} )
}
}