Coverage for /pythoncovmergedfiles/medio/medio/src/gitpython/fuzzing/fuzz-targets/fuzz_repo.py: 45%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

38 statements  

1###### Coverage stub 

2import atexit 

3import coverage 

4cov = coverage.coverage(data_file='.coverage', cover_pylib=True) 

5cov.start() 

6# Register an exist handler that will print coverage 

7def exit_handler(): 

8 cov.stop() 

9 cov.save() 

10atexit.register(exit_handler) 

11####### End of coverage stub 

12import atheris 

13import io 

14import sys 

15import os 

16import tempfile 

17 

18if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): 

19 path_to_bundled_git_binary = os.path.abspath(os.path.join(os.path.dirname(__file__), "git")) 

20 os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = path_to_bundled_git_binary 

21 

22with atheris.instrument_imports(): 

23 import git 

24 

25 

26def TestOneInput(data): 

27 fdp = atheris.FuzzedDataProvider(data) 

28 

29 with tempfile.TemporaryDirectory() as temp_dir: 

30 repo = git.Repo.init(path=temp_dir) 

31 

32 # Generate a minimal set of files based on fuzz data to minimize I/O operations. 

33 file_paths = [os.path.join(temp_dir, f"File{i}") for i in range(min(3, fdp.ConsumeIntInRange(1, 3)))] 

34 for file_path in file_paths: 

35 with open(file_path, "wb") as f: 

36 # The chosen upperbound for count of bytes we consume by writing to these 

37 # files is somewhat arbitrary and may be worth experimenting with if the 

38 # fuzzer coverage plateaus. 

39 f.write(fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, 512))) 

40 

41 repo.index.add(file_paths) 

42 repo.index.commit(fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(1, 80))) 

43 

44 fuzz_tree = git.Tree(repo, git.Tree.NULL_BIN_SHA, 0, "") 

45 

46 try: 

47 fuzz_tree._deserialize(io.BytesIO(data)) 

48 except IndexError: 

49 return -1 

50 

51 

52def main(): 

53 atheris.Setup(sys.argv, TestOneInput) 

54 atheris.Fuzz() 

55 

56 

57if __name__ == "__main__": 

58 main()