add fuzz tests to resourcehcl package and fix some panics (#18798)

* add a simple fuzz test for the resourcehcl package and fix some panics uncovered by the test

* fix default for null values
This commit is contained in:
Dhia Ayachi 2023-09-14 10:59:33 -04:00 committed by GitHub
parent 7f2a1d9812
commit 658c27a684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 6 deletions

View File

@ -63,15 +63,21 @@ func protoEnumFromCty(desc protoreflect.FieldDescriptor, val cty.Value) (protore
}
if val.IsNull() {
if desc.HasDefault() {
defaultValDesc := desc.DefaultEnumValue()
return protoreflect.ValueOfEnum(defaultValDesc.Number()), nil
}
return protoreflect.Value{}, fmt.Errorf("no default value for type and value is null")
}
valDesc := desc.Enum().Values().ByName(protoreflect.Name(val.AsString()))
if valDesc == nil {
if desc.HasDefault() {
defaultValDesc := desc.DefaultEnumValue()
return protoreflect.ValueOfEnum(defaultValDesc.Number()), nil
}
return protoreflect.Value{}, fmt.Errorf("no default value for type and value is invalid")
}
return protoreflect.ValueOfEnum(valDesc.Number()), nil
}

View File

@ -34,12 +34,15 @@ func (p anyProvider) AnyType(ctx *protohcl.UnmarshalContext, decoder protohcl.Me
if !isResource {
return p.base.AnyType(ctx, decoder)
}
resourceType := res.GetId().GetType()
if res == nil {
return "", nil, errors.New("ID.Type not found")
}
resourceType := res.GetId().GetType()
if resourceType == nil {
return "", nil, errors.New("ID.Type is nil")
}
reg, ok := p.reg.Resolve(resourceType)
if !ok {
return "", nil, fmt.Errorf("unknown resource type: %s", resource.ToGVK(resourceType))

View File

@ -33,6 +33,9 @@ func (n fieldNamer) GetField(fds protoreflect.FieldDescriptors, name string) pro
}
}
if len(name) <= 1 {
return fds.ByJSONName(name)
}
camel := strings.ToLower(name[:1]) + name[1:]
return fds.ByJSONName(camel)
}

View File

@ -0,0 +1,2 @@
go test fuzz v1
[]byte("Data{}")

View File

@ -0,0 +1,2 @@
go test fuzz v1
[]byte("ID={\"\"=\"\"}")

View File

@ -0,0 +1,2 @@
go test fuzz v1
[]byte("ID {\nType = gvk(\"demo.v1.Artist\")\n} \nData {\nGenre = \"\"\n} ")

View File

@ -23,6 +23,47 @@ import (
var update = flag.Bool("update", false, "update golden files")
func FuzzUnmarshall(f *testing.F) {
entries, err := os.ReadDir("./testdata")
require.NoError(f, err)
read := func(t *testing.F, path string) ([]byte, bool) {
t.Helper()
bytes, err := os.ReadFile(fmt.Sprintf("./testdata/%s", path))
switch {
case err == nil:
return bytes, true
case os.IsNotExist(err):
return nil, false
}
t.Fatalf("failed to read file %s %v", path, err)
return nil, false
}
for _, entry := range entries {
ext := path.Ext(entry.Name())
if ext != ".hcl" {
continue
}
input, _ := read(f, entry.Name())
f.Add(input)
}
registry := resource.NewRegistry()
demo.RegisterTypes(registry)
mesh.RegisterTypes(registry)
f.Fuzz(func(t *testing.T, input []byte) {
got, err := resourcehcl.Unmarshal(input, registry)
if err != nil {
return
}
require.NotNil(t, got)
})
}
func TestUnmarshal(t *testing.T) {
entries, err := os.ReadDir("./testdata")
require.NoError(t, err)