go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/selector/check_key_test.go (about)

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