github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/context/store/storeconfig_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  )
     8  
     9  type (
    10  	testCtx struct{}
    11  	testEP1 struct{}
    12  	testEP2 struct{}
    13  	testEP3 struct{}
    14  )
    15  
    16  func TestConfigModification(t *testing.T) {
    17  	cfg := NewConfig(func() interface{} { return &testCtx{} }, EndpointTypeGetter("ep1", func() interface{} { return &testEP1{} }))
    18  	assert.Equal(t, &testCtx{}, cfg.contextType())
    19  	assert.Equal(t, &testEP1{}, cfg.endpointTypes["ep1"]())
    20  	cfgCopy := cfg
    21  
    22  	// modify existing endpoint
    23  	cfg.SetEndpoint("ep1", func() interface{} { return &testEP2{} })
    24  	// add endpoint
    25  	cfg.SetEndpoint("ep2", func() interface{} { return &testEP3{} })
    26  	assert.Equal(t, &testCtx{}, cfg.contextType())
    27  	assert.Equal(t, &testEP2{}, cfg.endpointTypes["ep1"]())
    28  	assert.Equal(t, &testEP3{}, cfg.endpointTypes["ep2"]())
    29  	// check it applied on already initialized store
    30  	assert.Equal(t, &testCtx{}, cfgCopy.contextType())
    31  	assert.Equal(t, &testEP2{}, cfgCopy.endpointTypes["ep1"]())
    32  	assert.Equal(t, &testEP3{}, cfgCopy.endpointTypes["ep2"]())
    33  }
    34  
    35  func TestValidFilePaths(t *testing.T) {
    36  	paths := map[string]bool{
    37  		"tls/_/../../something":        false,
    38  		"tls/../../something":          false,
    39  		"../../something":              false,
    40  		"/tls/absolute/unix/path":      false,
    41  		`C:\tls\absolute\windows\path`: false,
    42  		"C:/tls/absolute/windows/path": false,
    43  	}
    44  	for p, expectedValid := range paths {
    45  		err := isValidFilePath(p)
    46  		assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", p, expectedValid)
    47  	}
    48  }
    49  
    50  func TestValidateContextName(t *testing.T) {
    51  	names := map[string]bool{
    52  		"../../invalid/escape": false,
    53  		"/invalid/absolute":    false,
    54  		`\invalid\windows`:     false,
    55  		"validname":            true,
    56  	}
    57  	for n, expectedValid := range names {
    58  		err := ValidateContextName(n)
    59  		assert.Equal(t, err == nil, expectedValid, "%q should report valid as: %v", n, expectedValid)
    60  	}
    61  }