Skip to content

Commit

Permalink
include sentinel value in submsg generator to sometimes skip fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Boling committed Jul 6, 2017
1 parent 9127758 commit 0b7e4e1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions protofuzz/protofuzz.py
Expand Up @@ -28,6 +28,8 @@
'''

import itertools

from google.protobuf import descriptor as D
from google.protobuf import message
from google.protobuf.internal import containers
Expand Down Expand Up @@ -68,6 +70,9 @@ def _enum_generator(descriptor):
return gen.IterValueGenerator(descriptor.name, vals)


Blank = object()


def _prototype_to_generator(descriptor, cls):
'Helper to map a descriptor to a protofuzz generator'
_fd = D.FieldDescriptor
Expand Down Expand Up @@ -97,8 +102,15 @@ def _prototype_to_generator(descriptor, cls):
elif descriptor.type == _fd.TYPE_ENUM:
generator = _enum_generator(descriptor)
elif descriptor.type == _fd.TYPE_MESSAGE:
generator = descriptor_to_generator(descriptor.message_type, cls)
generator.set_name(descriptor.name)
subgen = descriptor_to_generator(descriptor.message_type, cls)
subgen.set_name(descriptor.name)
generator = gen.IterValueGenerator(
descriptor.name,
itertools.chain.from_iterable(zip(
subgen,
gen.IterValueGenerator(descriptor.name, [Blank])
))
)
else:
raise RuntimeError("type {} unsupported".format(descriptor.type))

Expand All @@ -123,6 +135,9 @@ def descriptor_to_generator(cls_descriptor, cls, limit=0):

def _assign_to_field(obj, name, val):
'Helper to assign an arbitrary value to a protobuf field'
if val is Blank:
return

target = getattr(obj, name)

if isinstance(target, containers.RepeatedScalarFieldContainer):
Expand Down

0 comments on commit 0b7e4e1

Please sign in to comment.