Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add various formats for data types #38

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions fuzz_lightyear/fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Optional
from typing import Tuple

import hypothesis.provisional as pr
import hypothesis.strategies as st
from hypothesis.searchstrategy.strategies import SearchStrategy
from swagger_spec_validator.common import SwaggerValidationError # type: ignore
Expand Down Expand Up @@ -92,6 +93,8 @@ def _fuzz_string(
parameter: Dict[str, Any],
required: bool = False,
) -> SearchStrategy:
# TODO: Handle date and date-time string formats.
# https://swagger.io/docs/specification/data-models/data-types/#string
if parameter.get('in', None) == 'header':
return st.text(
# According to RFC 7230, non-ascii letters are deprecated, and there's
Expand All @@ -101,15 +104,33 @@ def _fuzz_string(
alphabet=string.ascii_letters,
)

# TODO: Handle a bunch of swagger string formats.
# https://swagger.io/docs/specification/data-models/data-types/#string
if parameter.get('pattern'):
full_match = (
parameter['pattern'][0] == '^'
and parameter['pattern'][-1] == '$'
and parameter['pattern'][-2] != '\\'
)
return st.from_regex(parameter['pattern'], fullmatch=full_match)

kwargs = {} # type: Dict[str, Any]

kwargs['min_size'] = parameter.get('minLength', 0)
kwargs['max_size'] = parameter.get('maxLength')

string_format = parameter.get('format')

if string_format == 'ipv4':
return pr.ip4_addr_strings()
elif string_format == 'ipv6':
return pr.ip6_addr_strings()
elif string_format == 'binary':
return st.binary(**kwargs)

if parameter.get('required', required):
kwargs['min_size'] = 1

if not get_settings().unicode_enabled:
kwargs['alphabet'] = string.printable

return st.text(**kwargs)


Expand All @@ -121,7 +142,7 @@ def _find_bounds(schema: Dict[str, Any]) -> Dict[str, Any]:
if 'minimum' in schema:
bounds['min_value'] = schema['minimum']
if schema.get('exclusiveMinimum'):
bounds['min_vallue'] += 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

bounds['min_value'] += 1.0

if 'maximum' in schema:
bounds['max_value'] = schema['maximum']
Expand Down