Coverage for /pythoncovmergedfiles/medio/medio/src/gitpython/fuzzing/fuzz-targets/fuzz_diff.py: 35%

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

54 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 sys 

13import os 

14import io 

15import tempfile 

16from binascii import Error as BinasciiError 

17 

18import atheris 

19 

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

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

22 os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = path_to_bundled_git_binary 

23 

24with atheris.instrument_imports(): 

25 from git import Repo, Diff 

26 

27 

28class BytesProcessAdapter: 

29 """Allows bytes to be used as process objects returned by subprocess.Popen.""" 

30 

31 @atheris.instrument_func 

32 def __init__(self, input_string): 

33 self.stdout = io.BytesIO(input_string) 

34 self.stderr = io.BytesIO() 

35 

36 @atheris.instrument_func 

37 def wait(self): 

38 return 0 

39 

40 poll = wait 

41 

42 

43@atheris.instrument_func 

44def TestOneInput(data): 

45 fdp = atheris.FuzzedDataProvider(data) 

46 

47 with tempfile.TemporaryDirectory() as temp_dir: 

48 repo = Repo.init(path=temp_dir) 

49 try: 

50 diff = Diff( 

51 repo, 

52 a_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

53 b_rawpath=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

54 a_blob_id=fdp.ConsumeBytes(20), 

55 b_blob_id=fdp.ConsumeBytes(20), 

56 a_mode=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

57 b_mode=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

58 new_file=fdp.ConsumeBool(), 

59 deleted_file=fdp.ConsumeBool(), 

60 copied_file=fdp.ConsumeBool(), 

61 raw_rename_from=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

62 raw_rename_to=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

63 diff=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), 

64 change_type=fdp.PickValueInList(["A", "D", "C", "M", "R", "T", "U"]), 

65 score=fdp.ConsumeIntInRange(0, fdp.remaining_bytes()), 

66 ) 

67 except BinasciiError: 

68 return -1 

69 except AssertionError as e: 

70 if "Require 20 byte binary sha, got" in str(e): 

71 return -1 

72 else: 

73 raise e 

74 

75 _ = diff.__str__() 

76 _ = diff.a_path 

77 _ = diff.b_path 

78 _ = diff.rename_from 

79 _ = diff.rename_to 

80 _ = diff.renamed_file 

81 

82 diff_index = diff._index_from_patch_format( 

83 repo, proc=BytesProcessAdapter(fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes()))) 

84 ) 

85 

86 diff._handle_diff_line( 

87 lines_bytes=fdp.ConsumeBytes(fdp.ConsumeIntInRange(0, fdp.remaining_bytes())), repo=repo, index=diff_index 

88 ) 

89 

90 

91def main(): 

92 atheris.Setup(sys.argv, TestOneInput) 

93 atheris.Fuzz() 

94 

95 

96if __name__ == "__main__": 

97 main()