Coverage for /pythoncovmergedfiles/medio/medio/src/dulwich/fuzzing/fuzz-targets/fuzz_repo.py: 55%

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

42 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 os 

13import sys 

14import tempfile 

15 

16import atheris 

17 

18with atheris.instrument_imports(): 

19 # We instrument `test_utils` as well, so it doesn't block coverage analysis in Fuzz Introspector: 

20 from test_utils import EnhancedFuzzedDataProvider, is_expected_exception 

21 

22 from dulwich.repo import ( 

23 InvalidUserIdentity, 

24 Repo, 

25 ) 

26 

27 

28def TestOneInput(data): 

29 fdp = EnhancedFuzzedDataProvider(data) 

30 with tempfile.TemporaryDirectory() as temp_dir: 

31 repo = Repo.init(temp_dir) 

32 repo.set_description(fdp.ConsumeRandomBytes()) 

33 repo.get_description() 

34 

35 try: 

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

37 file_names = [ 

38 f"File{i}{fdp.ConsumeRandomString(without_surrogates=True)}" 

39 for i in range(min(3, fdp.ConsumeIntInRange(1, 3))) 

40 ] 

41 for file in file_names: 

42 with open(os.path.join(temp_dir, file), "wb") as f: 

43 f.write(fdp.ConsumeRandomBytes()) 

44 except (ValueError, OSError): 

45 # Exit early if the fuzzer generates an invalid filename. 

46 return -1 

47 

48 try: 

49 repo.stage(file_names) 

50 repo.do_commit( 

51 message=fdp.ConsumeRandomBytes(), 

52 committer=fdp.ConsumeRandomBytes(), 

53 author=fdp.ConsumeRandomBytes(), 

54 commit_timestamp=fdp.ConsumeRandomInt(), 

55 commit_timezone=fdp.ConsumeRandomInt(), 

56 author_timestamp=fdp.ConsumeRandomInt(), 

57 author_timezone=fdp.ConsumeRandomInt(), 

58 ) 

59 except InvalidUserIdentity: 

60 return -1 

61 except ValueError as e: 

62 if is_expected_exception(["Unable to handle non-minute offset"], e): 

63 return -1 

64 else: 

65 raise e 

66 

67 

68def main(): 

69 atheris.Setup(sys.argv, TestOneInput) 

70 atheris.Fuzz() 

71 

72 

73if __name__ == "__main__": 

74 main()