github.com/hashicorp/vault/sdk@v0.11.0/helper/custommetadata/custom_metadata_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package custommetadata
     5  
     6  import (
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestValidate(t *testing.T) {
    13  	cases := []struct {
    14  		name       string
    15  		input      map[string]string
    16  		shouldPass bool
    17  	}{
    18  		{
    19  			"valid",
    20  			map[string]string{
    21  				"foo": "abc",
    22  				"bar": "def",
    23  				"baz": "ghi",
    24  			},
    25  			true,
    26  		},
    27  		{
    28  			"too_many_keys",
    29  			func() map[string]string {
    30  				cm := make(map[string]string)
    31  
    32  				for i := 0; i < maxKeyLength+1; i++ {
    33  					s := strconv.Itoa(i)
    34  					cm[s] = s
    35  				}
    36  
    37  				return cm
    38  			}(),
    39  			false,
    40  		},
    41  		{
    42  			"key_too_long",
    43  			map[string]string{
    44  				strings.Repeat("a", maxKeyLength+1): "abc",
    45  			},
    46  			false,
    47  		},
    48  		{
    49  			"value_too_long",
    50  			map[string]string{
    51  				"foo": strings.Repeat("a", maxValueLength+1),
    52  			},
    53  			false,
    54  		},
    55  		{
    56  			"unprintable_key",
    57  			map[string]string{
    58  				"unprint\u200bable": "abc",
    59  			},
    60  			false,
    61  		},
    62  		{
    63  			"unprintable_value",
    64  			map[string]string{
    65  				"foo": "unprint\u200bable",
    66  			},
    67  			false,
    68  		},
    69  	}
    70  
    71  	for _, tc := range cases {
    72  		tc := tc
    73  
    74  		t.Run(tc.name, func(t *testing.T) {
    75  			t.Parallel()
    76  
    77  			err := Validate(tc.input)
    78  
    79  			if tc.shouldPass && err != nil {
    80  				t.Fatalf("expected validation to pass, input: %#v, err: %v", tc.input, err)
    81  			}
    82  
    83  			if !tc.shouldPass && err == nil {
    84  				t.Fatalf("expected validation to fail, input: %#v, err: %v", tc.input, err)
    85  			}
    86  		})
    87  	}
    88  }