Coverage for /pythoncovmergedfiles/medio/medio/src/dulwich/fuzzing/fuzz-targets/fuzz_repo.py: 53%
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# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
14import os
15import sys
16import tempfile
18import atheris
20with atheris.instrument_imports():
21 # We instrument `test_utils` as well, so it doesn't block coverage analysis in Fuzz Introspector:
22 from test_utils import EnhancedFuzzedDataProvider, is_expected_exception
24 from dulwich.repo import (
25 InvalidUserIdentity,
26 Repo,
27 )
30def TestOneInput(data) -> int | None:
31 fdp = EnhancedFuzzedDataProvider(data)
32 with tempfile.TemporaryDirectory() as temp_dir:
33 repo = Repo.init(temp_dir)
34 repo.set_description(fdp.ConsumeRandomBytes())
35 repo.get_description()
37 try:
38 # Generate a minimal set of files based on fuzz data to minimize I/O operations.
39 file_names = [
40 f"File{i}{fdp.ConsumeRandomString(without_surrogates=True)}"
41 for i in range(min(3, fdp.ConsumeIntInRange(1, 3)))
42 ]
43 for file in file_names:
44 with open(os.path.join(temp_dir, file), "wb") as f:
45 f.write(fdp.ConsumeRandomBytes())
46 except (ValueError, OSError):
47 # Exit early if the fuzzer generates an invalid filename.
48 return -1
50 try:
51 repo.get_worktree().stage(file_names)
52 repo.get_worktree().commit(
53 message=fdp.ConsumeRandomBytes(),
54 committer=fdp.ConsumeRandomBytes(),
55 author=fdp.ConsumeRandomBytes(),
56 commit_timestamp=fdp.ConsumeRandomInt(),
57 commit_timezone=fdp.ConsumeRandomInt(),
58 author_timestamp=fdp.ConsumeRandomInt(),
59 author_timezone=fdp.ConsumeRandomInt(),
60 )
61 except InvalidUserIdentity:
62 return -1
63 except ValueError as e:
64 if is_expected_exception(["Unable to handle non-minute offset"], e):
65 return -1
66 else:
67 raise e
70def main() -> None:
71 atheris.Setup(sys.argv, TestOneInput)
72 atheris.Fuzz()
75if __name__ == "__main__":
76 main()