github.com/blend/go-sdk@v1.20220411.3/selector/check_key_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package selector
     9  
    10  import (
    11  	"fmt"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestCheckKey(t *testing.T) {
    19  	its := assert.New(t)
    20  
    21  	its.Nil(CheckKey("foo"))
    22  	its.Nil(CheckKey("bar/foo"))
    23  	its.Nil(CheckKey("bar.io/foo"))
    24  	its.NotNil(CheckKey("_foo"))
    25  	its.NotNil(CheckKey("-foo"))
    26  	its.NotNil(CheckKey("foo-"))
    27  	its.NotNil(CheckKey("foo_"))
    28  	its.NotNil(CheckKey("bar/foo/baz"))
    29  
    30  	its.NotNil(CheckKey(""), "should error on empty keys")
    31  
    32  	its.NotNil(CheckKey("/foo"), "should error on empty dns prefixes")
    33  	superLongDNSPrefixed := fmt.Sprintf("%s/%s", strings.Repeat("a", MaxLabelKeyDNSSubdomainLen), strings.Repeat("a", MaxLabelKeyLen))
    34  	its.Nil(CheckKey(superLongDNSPrefixed), len(superLongDNSPrefixed))
    35  	superLongDNSPrefixed = fmt.Sprintf("%s/%s", strings.Repeat("a", MaxLabelKeyDNSSubdomainLen+1), strings.Repeat("a", MaxLabelKeyLen))
    36  	its.NotNil(CheckKey(superLongDNSPrefixed), len(superLongDNSPrefixed))
    37  	superLongDNSPrefixed = fmt.Sprintf("%s/%s", strings.Repeat("a", MaxLabelKeyDNSSubdomainLen+1), strings.Repeat("a", MaxLabelKeyLen+1))
    38  	its.NotNil(CheckKey(superLongDNSPrefixed), len(superLongDNSPrefixed))
    39  	superLongDNSPrefixed = fmt.Sprintf("%s/%s", strings.Repeat("a", MaxLabelKeyDNSSubdomainLen), strings.Repeat("a", MaxLabelKeyLen+1))
    40  	its.NotNil(CheckKey(superLongDNSPrefixed), len(superLongDNSPrefixed))
    41  }
    42  
    43  func TestCheckKeyK8S(t *testing.T) {
    44  	assert := assert.New(t)
    45  
    46  	values := []string{
    47  		"simple",
    48  		"now-with-dashes",
    49  		"1-starts-with-num",
    50  		"1234",
    51  		"simple/simple",
    52  		"now-with-dashes/simple",
    53  		"now-with-dashes/now-with-dashes",
    54  		"now.with.dots/simple",
    55  		"now-with.dashes-and.dots/simple",
    56  		"1-num.2-num/3-num",
    57  		"1234/5678",
    58  		"1.2.3.4/5678",
    59  		"Uppercase_Is_OK_123",
    60  		"example.com/Uppercase_Is_OK_123",
    61  		"requests.storage-foo",
    62  		strings.Repeat("a", 63),
    63  		strings.Repeat("a", 253) + "/" + strings.Repeat("b", 63),
    64  	}
    65  	badValues := []string{
    66  		"nospecialchars%^=@",
    67  		"cantendwithadash-",
    68  		"-cantstartwithadash-",
    69  		"only/one/slash",
    70  		"example_com/abc",
    71  		"example.com/",
    72  		"Example.com/abc",
    73  		"/simple",
    74  		strings.Repeat("a", 64),
    75  		strings.Repeat("a", 254) + "/abc",
    76  	}
    77  	for _, val := range values {
    78  		assert.Nil(CheckKey(val), "input:", val)
    79  	}
    80  	for _, val := range badValues {
    81  		assert.NotNil(CheckKey(val), "input:", val)
    82  	}
    83  }