fuzz_validate.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """
  2. Fuzzing setup for OSS-Fuzz.
  3. See https://github.com/google/oss-fuzz/tree/master/projects/jsonschema for the
  4. other half of the setup here.
  5. """
  6. import sys
  7. from hypothesis import given, strategies
  8. import jsonschema
  9. PRIM = strategies.one_of(
  10. strategies.booleans(),
  11. strategies.integers(),
  12. strategies.floats(allow_nan=False, allow_infinity=False),
  13. strategies.text(),
  14. )
  15. DICT = strategies.recursive(
  16. base=strategies.one_of(
  17. strategies.booleans(),
  18. strategies.dictionaries(strategies.text(), PRIM),
  19. ),
  20. extend=lambda inner: strategies.dictionaries(strategies.text(), inner),
  21. )
  22. @given(obj1=DICT, obj2=DICT)
  23. def test_schemas(obj1, obj2):
  24. try:
  25. jsonschema.validate(instance=obj1, schema=obj2)
  26. except jsonschema.exceptions.ValidationError:
  27. pass
  28. except jsonschema.exceptions.SchemaError:
  29. pass
  30. def main():
  31. atheris.instrument_all()
  32. atheris.Setup(
  33. sys.argv,
  34. test_schemas.hypothesis.fuzz_one_input,
  35. enable_python_coverage=True,
  36. )
  37. atheris.Fuzz()
  38. if __name__ == "__main__":
  39. import atheris
  40. main()