2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-07-28 14:40:38 +00:00
|
|
|
package custommetadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
2022-07-29 14:04:57 +00:00
|
|
|
input map[string]string
|
2022-07-28 14:40:38 +00:00
|
|
|
shouldPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid",
|
2022-07-29 14:04:57 +00:00
|
|
|
map[string]string{
|
2022-07-28 14:40:38 +00:00
|
|
|
"foo": "abc",
|
|
|
|
"bar": "def",
|
|
|
|
"baz": "ghi",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"too_many_keys",
|
2022-07-29 14:04:57 +00:00
|
|
|
func() map[string]string {
|
|
|
|
cm := make(map[string]string)
|
2022-07-28 14:40:38 +00:00
|
|
|
|
|
|
|
for i := 0; i < maxKeyLength+1; i++ {
|
|
|
|
s := strconv.Itoa(i)
|
|
|
|
cm[s] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm
|
|
|
|
}(),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key_too_long",
|
2022-07-29 14:04:57 +00:00
|
|
|
map[string]string{
|
2022-07-28 14:40:38 +00:00
|
|
|
strings.Repeat("a", maxKeyLength+1): "abc",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"value_too_long",
|
2022-07-29 14:04:57 +00:00
|
|
|
map[string]string{
|
2022-07-28 14:40:38 +00:00
|
|
|
"foo": strings.Repeat("a", maxValueLength+1),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unprintable_key",
|
2022-07-29 14:04:57 +00:00
|
|
|
map[string]string{
|
2022-07-28 14:40:38 +00:00
|
|
|
"unprint\u200bable": "abc",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unprintable_value",
|
2022-07-29 14:04:57 +00:00
|
|
|
map[string]string{
|
2022-07-28 14:40:38 +00:00
|
|
|
"foo": "unprint\u200bable",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
err := Validate(tc.input)
|
|
|
|
|
|
|
|
if tc.shouldPass && err != nil {
|
|
|
|
t.Fatalf("expected validation to pass, input: %#v, err: %v", tc.input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tc.shouldPass && err == nil {
|
|
|
|
t.Fatalf("expected validation to fail, input: %#v, err: %v", tc.input, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|