fuzz_bundle.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import sys
  2. from io import BytesIO
  3. from typing import Optional
  4. import atheris
  5. with atheris.instrument_imports():
  6. # We instrument `test_utils` as well, so it doesn't block coverage analysis in Fuzz Introspector:
  7. from test_utils import EnhancedFuzzedDataProvider, is_expected_exception
  8. from dulwich.bundle import Bundle, read_bundle, write_bundle
  9. from dulwich.pack import PackData, write_pack_objects
  10. def TestOneInput(data) -> Optional[int]:
  11. fdp = EnhancedFuzzedDataProvider(data)
  12. bundle = Bundle()
  13. bundle.version = fdp.PickValueInList([2, 3, None])
  14. bundle.references = {fdp.ConsumeRandomString(): fdp.ConsumeBytes(20)}
  15. bundle.prerequisites = [(fdp.ConsumeBytes(20), fdp.ConsumeRandomBytes())]
  16. bundle.capabilities = {
  17. fdp.ConsumeRandomString(): fdp.ConsumeRandomString(),
  18. }
  19. b = BytesIO()
  20. write_pack_objects(b.write, [])
  21. b.seek(0)
  22. bundle.pack_data = PackData.from_file(b)
  23. # Test __repr__ method
  24. _ = repr(bundle)
  25. try:
  26. bundle_file = BytesIO()
  27. write_bundle(bundle_file, bundle)
  28. _ = read_bundle(bundle_file)
  29. except (AttributeError, UnicodeEncodeError, AssertionError) as e:
  30. expected_exceptions = [
  31. "'bytes' object has no attribute 'encode'",
  32. "surrogates not allowed",
  33. "unsupported bundle format header",
  34. ]
  35. if is_expected_exception(expected_exceptions, e):
  36. return -1
  37. else:
  38. raise e
  39. def main() -> None:
  40. atheris.Setup(sys.argv, TestOneInput)
  41. atheris.Fuzz()
  42. if __name__ == "__main__":
  43. main()