fuzz_bundle.py 1.5 KB

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