Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_gitdb.py: 34%
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
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
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"""
30import sys
31import atheris
34import zlib
35from gitdb import (
36 DecompressMemMapReader,
37 LooseObjectDB,
38 MemoryDB
39)
41from gitdb.pack import (
42 PackFile
43)
45from gitdb.exc import ParseError
48def fuzz_decompression_map_reader(data):
49 """Targets gitdb/stream.py#DecompressMemMapReader"""
50 fdp = atheris.FuzzedDataProvider(data)
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
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
69 ldb = LooseObjectDB(path)
70 if ldb.size() != 0:
71 mdb = MemoryDB()
72 mdb.stream_copy(mdb.sha_iter(), ldb)
74 for sha1 in ldb.sha_iter():
75 ldb.info(sha1)
76 ldb.stream(sha1)
79def fuzz_pack_file(data):
80 """Targets code in gitdb/pack.py"""
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
91 pack_file = PackFile(path)
92 try:
93 pack_file.version()
94 except ParseError:
95 return
97 if pack_file.size() <= 0:
98 return
100 for obj in pack_file.stream_iter():
101 info = pack_file.info(obj.pack_offset)
103def TestOneInput(data):
104 fuzz_decompression_map_reader(data)
105 fuzz_loose_obj_db(data)
106 fuzz_pack_file(data)
109def main():
110 atheris.instrument_all()
111 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True)
112 atheris.Fuzz()
115if __name__ == "__main__":
116 main()