fuzz_object_store.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import sys
  2. import stat
  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.objects import Blob, Tree, Commit, S_IFGITLINK
  8. from dulwich.errors import ObjectFormatException
  9. from dulwich.repo import (
  10. MemoryRepo,
  11. InvalidUserIdentity,
  12. )
  13. def TestOneInput(data):
  14. fdp = EnhancedFuzzedDataProvider(data)
  15. repo = MemoryRepo()
  16. blob = Blob.from_string(fdp.ConsumeRandomBytes())
  17. tree = Tree()
  18. tree.add(
  19. fdp.ConsumeRandomBytes(),
  20. fdp.PickValueInList([stat.S_IFREG, stat.S_IFLNK, stat.S_IFDIR, S_IFGITLINK]),
  21. blob.id,
  22. )
  23. commit = Commit()
  24. commit.tree = tree.id
  25. commit.author = fdp.ConsumeRandomBytes()
  26. commit.committer = fdp.ConsumeRandomBytes()
  27. commit.commit_time = fdp.ConsumeRandomInt()
  28. commit.commit_timezone = fdp.ConsumeRandomInt()
  29. commit.author_time = fdp.ConsumeRandomInt()
  30. commit.author_timezone = fdp.ConsumeRandomInt()
  31. commit.message = fdp.ConsumeRandomBytes()
  32. object_store = repo.object_store
  33. try:
  34. object_store.add_object(blob)
  35. object_store.add_object(tree)
  36. object_store.add_object(commit)
  37. except (InvalidUserIdentity, ObjectFormatException):
  38. return -1
  39. except ValueError as e:
  40. expected_exceptions = [
  41. "subsection not found",
  42. "Unable to handle non-minute offset",
  43. ]
  44. if is_expected_exception(expected_exceptions, e):
  45. return -1
  46. else:
  47. raise e
  48. def main():
  49. atheris.Setup(sys.argv, TestOneInput)
  50. atheris.Fuzz()
  51. if __name__ == "__main__":
  52. main()