Coverage for /pythoncovmergedfiles/medio/medio/src/gitpython/fuzzing/fuzz-targets/fuzz_submodule.py: 56%

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

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

14import os 

15import tempfile 

16from configparser import ParsingError 

17from utils import ( 

18 setup_git_environment, 

19 handle_exception, 

20 get_max_filename_length, 

21) 

22 

23# Setup the git environment 

24setup_git_environment() 

25from git import Repo, GitCommandError, InvalidGitRepositoryError 

26 

27 

28def TestOneInput(data): 

29 fdp = atheris.FuzzedDataProvider(data) 

30 

31 with tempfile.TemporaryDirectory() as repo_temp_dir: 

32 repo = Repo.init(path=repo_temp_dir) 

33 repo.index.commit("Initial commit") 

34 

35 try: 

36 with tempfile.TemporaryDirectory() as submodule_temp_dir: 

37 sub_repo = Repo.init(submodule_temp_dir, bare=fdp.ConsumeBool()) 

38 sub_repo.index.commit(fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(1, 512))) 

39 

40 submodule_name = fdp.ConsumeUnicodeNoSurrogates( 

41 fdp.ConsumeIntInRange(1, max(1, get_max_filename_length(repo.working_tree_dir))) 

42 ) 

43 submodule_path = os.path.join(repo.working_tree_dir, submodule_name) 

44 

45 submodule = repo.create_submodule(submodule_name, submodule_path, url=sub_repo.git_dir) 

46 repo.index.commit("Added submodule") 

47 

48 with submodule.config_writer() as writer: 

49 key_length = fdp.ConsumeIntInRange(1, max(1, fdp.remaining_bytes())) 

50 value_length = fdp.ConsumeIntInRange(1, max(1, fdp.remaining_bytes())) 

51 

52 writer.set_value( 

53 fdp.ConsumeUnicodeNoSurrogates(key_length), fdp.ConsumeUnicodeNoSurrogates(value_length) 

54 ) 

55 writer.release() 

56 

57 submodule.update(init=fdp.ConsumeBool(), dry_run=fdp.ConsumeBool(), force=fdp.ConsumeBool()) 

58 submodule_repo = submodule.module() 

59 

60 new_file_name = fdp.ConsumeUnicodeNoSurrogates( 

61 fdp.ConsumeIntInRange(1, max(1, get_max_filename_length(submodule_repo.working_tree_dir))) 

62 ) 

63 new_file_path = os.path.join(submodule_repo.working_tree_dir, new_file_name) 

64 with open(new_file_path, "wb") as new_file: 

65 new_file.write(fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, 512))) 

66 submodule_repo.index.add([new_file_path]) 

67 submodule_repo.index.commit("Added new file to submodule") 

68 

69 repo.submodule_update(recursive=fdp.ConsumeBool()) 

70 submodule_repo.head.reset(commit="HEAD~1", working_tree=fdp.ConsumeBool(), head=fdp.ConsumeBool()) 

71 # Use fdp.PickValueInList to ensure at least one of 'module' or 'configuration' is True 

72 module_option_value, configuration_option_value = fdp.PickValueInList( 

73 [(True, False), (False, True), (True, True)] 

74 ) 

75 submodule.remove( 

76 module=module_option_value, 

77 configuration=configuration_option_value, 

78 dry_run=fdp.ConsumeBool(), 

79 force=fdp.ConsumeBool(), 

80 ) 

81 repo.index.commit(f"Removed submodule {submodule_name}") 

82 

83 except ( 

84 ParsingError, 

85 GitCommandError, 

86 InvalidGitRepositoryError, 

87 FileNotFoundError, 

88 FileExistsError, 

89 IsADirectoryError, 

90 NotADirectoryError, 

91 BrokenPipeError, 

92 PermissionError, 

93 ): 

94 return -1 

95 except Exception as e: 

96 if isinstance(e, ValueError) and "embedded null byte" in str(e): 

97 return -1 

98 elif isinstance(e, OSError) and "File name too long" in str(e): 

99 return -1 

100 else: 

101 return handle_exception(e) 

102 

103 

104def main(): 

105 atheris.instrument_all() 

106 atheris.Setup(sys.argv, TestOneInput) 

107 atheris.Fuzz() 

108 

109 

110if __name__ == "__main__": 

111 main()