fuzz_bundle.py 1.6 KB

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