Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_gitdb.py: 53%

62 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:25 +0000

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 

12#!/usr/bin/python3 

13# Copyright 2023 Google LLC 

14# 

15# Licensed under the Apache License, Version 2.0 (the "License"); 

16# you may not use this file except in compliance with the License. 

17# You may obtain a copy of the License at 

18# 

19# http://www.apache.org/licenses/LICENSE-2.0 

20# 

21# Unless required by applicable law or agreed to in writing, software 

22# distributed under the License is distributed on an "AS IS" BASIS, 

23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

24# See the License for the specific language governing permissions and 

25# limitations under the License. 

26"""Fuzzer for several modules in gitdb. We keep it in 

27one fuzzer for ease of development 

28""" 

29 

30import sys 

31import atheris 

32 

33 

34import zlib 

35from gitdb import ( 

36 DecompressMemMapReader, 

37 LooseObjectDB, 

38 MemoryDB 

39) 

40 

41from gitdb.pack import ( 

42 PackFile 

43) 

44 

45from gitdb.exc import ParseError 

46 

47 

48def fuzz_decompression_map_reader(data): 

49 """Targets gitdb/stream.py#DecompressMemMapReader""" 

50 fdp = atheris.FuzzedDataProvider(data) 

51 

52 zdata = zlib.compress(data) 

53 test_reader = DecompressMemMapReader(zdata, True) 

54 try: 

55 bytes_read = test_reader.read(fdp.ConsumeIntInRange(0, 10000)) 

56 except ValueError: 

57 # Ignore these as they're uninteresting 

58 return 

59 

60 

61def fuzz_loose_obj_db(data): 

62 """Targets gitdb/db/loose.py""" 

63 path = "/tmp/loosedb.db" 

64 with open(path, "wb") as f: 

65 f.write(data) 

66 if not os.path.isfile(path): 

67 return 

68 

69 ldb = LooseObjectDB(path) 

70 if ldb.size() != 0: 

71 mdb = MemoryDB() 

72 mdb.stream_copy(mdb.sha_iter(), ldb) 

73 

74 for sha1 in ldb.sha_iter(): 

75 ldb.info(sha1) 

76 ldb.stream(sha1) 

77 

78 

79def fuzz_pack_file(data): 

80 """Targets code in gitdb/pack.py""" 

81 

82 # Ensure we have enough data for a packet 

83 if len(data) < 100: 

84 return 

85 path = "/tmp/packfile.idx" 

86 with open(path, "wb") as f: 

87 f.write(data) 

88 if not os.path.isfile(path): 

89 return 

90 

91 pack_file = PackFile(path) 

92 try: 

93 pack_file.version() 

94 except ParseError: 

95 return 

96 

97 if pack_file.size() <= 0: 

98 return 

99 

100 for obj in pack_file.stream_iter(): 

101 info = pack_file.info(obj.pack_offset) 

102 

103def TestOneInput(data): 

104 fuzz_decompression_map_reader(data) 

105 fuzz_loose_obj_db(data) 

106 fuzz_pack_file(data) 

107 

108 

109def main(): 

110 atheris.instrument_all() 

111 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) 

112 atheris.Fuzz() 

113 

114 

115if __name__ == "__main__": 

116 main()