Skip to content

Commit

Permalink
chore: enhanced fuzzing support for enum lists and bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Aug 7, 2024
1 parent f686448 commit 37a6e64
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/test/fuzz/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,29 @@ func fillWithRandom0(t *testing.T, randStream *rand.Rand, msg protoreflect.Messa
if count > 4 {
count = 0
}
listVal := msg.Mutable(field).List()
switch field.Kind() {
case protoreflect.MessageKind:
listVal := msg.Mutable(field).List()
for j := 0; j < count; j++ {
el := listVal.AppendMutable()
fillWithRandom0(t, randStream, el.Message())
}
case protoreflect.StringKind:
listVal := msg.Mutable(field).List()
for j := 0; j < count; j++ {
s := randomString(randStream)
listVal.Append(protoreflect.ValueOf(s))
}

case protoreflect.EnumKind:
listVal := msg.Mutable(field).List()
for j := 0; j < count; j++ {
enumDescriptor := field.Enum()
n := enumDescriptor.Values().Len()
val := enumDescriptor.Values().Get(randStream.Intn(n))
listVal.Append(protoreflect.ValueOf(val.Number()))
}

default:
t.Fatalf("unhandled field kind %v: %v", field.Kind(), field)
}
Expand Down Expand Up @@ -265,6 +275,15 @@ func Visit(msgPath string, msg protoreflect.Message, setter func(v protoreflect.
visitor.VisitPrimitive(path+"[]", el, setter)
}

case protoreflect.EnumKind:
for j := 0; j < count; j++ {
el := listVal.Get(j)
setter := func(v protoreflect.Value) {
listVal.Set(j, v)
}
visitor.VisitPrimitive(path+"[]", el, setter)
}

default:
klog.Fatalf("unhandled field kind %v: %v", field.Kind(), field)
}
Expand Down Expand Up @@ -318,6 +337,7 @@ func Visit(msgPath string, msg protoreflect.Message, setter func(v protoreflect.
protoreflect.Int32Kind,
protoreflect.Int64Kind,
protoreflect.StringKind,
protoreflect.BytesKind,
protoreflect.EnumKind:
setter := func(v protoreflect.Value) {
if v.IsValid() {
Expand Down

0 comments on commit 37a6e64

Please sign in to comment.