Coverage for /pythoncovmergedfiles/medio/medio/src/dulwich/fuzzing/fuzz-targets/fuzz_object_store.py: 67%
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 stat
15import sys
16from io import BytesIO
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.errors import ObjectFormatException
25 from dulwich.objects import S_IFGITLINK, Blob, Commit, Tree
26 from dulwich.patch import write_tree_diff
27 from dulwich.repo import (
28 InvalidUserIdentity,
29 MemoryRepo,
30 )
33def TestOneInput(data) -> int | None:
34 fdp = EnhancedFuzzedDataProvider(data)
35 repo = MemoryRepo()
36 blob = Blob.from_string(fdp.ConsumeRandomBytes())
37 tree = Tree()
38 tree.add(
39 fdp.ConsumeRandomBytes(),
40 fdp.PickValueInList([stat.S_IFREG, stat.S_IFLNK, stat.S_IFDIR, S_IFGITLINK]),
41 blob.id,
42 )
43 commit = Commit()
44 commit.tree = tree.id
45 commit.author = fdp.ConsumeRandomBytes()
46 commit.committer = fdp.ConsumeRandomBytes()
47 commit.commit_time = fdp.ConsumeRandomInt()
48 commit.commit_timezone = fdp.ConsumeRandomInt()
49 commit.author_time = fdp.ConsumeRandomInt()
50 commit.author_timezone = fdp.ConsumeRandomInt()
51 commit.message = fdp.ConsumeRandomBytes()
53 object_store = repo.object_store
55 try:
56 object_store.add_object(blob)
57 object_store.add_object(tree)
58 object_store.add_object(commit)
59 except (InvalidUserIdentity, ObjectFormatException):
60 return -1
61 except ValueError as e:
62 expected_exceptions = [
63 "subsection not found",
64 "Unable to handle non-minute offset",
65 ]
66 if is_expected_exception(expected_exceptions, e):
67 return -1
68 else:
69 raise e
71 commit2 = Commit()
72 commit2.tree = tree.id
73 commit2.parents = [commit.id]
74 commit2.author = commit.author
75 commit2.committer = commit.committer
76 commit2.commit_time = fdp.ConsumeRandomInt()
77 commit2.commit_timezone = fdp.ConsumeRandomInt()
78 commit2.author_time = fdp.ConsumeRandomInt()
79 commit2.author_timezone = fdp.ConsumeRandomInt()
80 commit2.message = fdp.ConsumeRandomBytes()
82 try:
83 blob.data = fdp.ConsumeRandomBytes()
84 repo.object_store.add_object(blob)
85 repo.object_store.add_object(tree)
86 repo.object_store.add_object(commit2)
87 out = BytesIO()
88 write_tree_diff(out, repo.object_store, commit.tree, tree.id)
89 except (InvalidUserIdentity, ObjectFormatException):
90 return -1
91 except ValueError as e:
92 expected_exceptions = [
93 "Unable to handle non-minute offset",
94 ]
95 if is_expected_exception(expected_exceptions, e):
96 return -1
97 else:
98 raise e
101def main() -> None:
102 atheris.Setup(sys.argv, TestOneInput)
103 atheris.Fuzz()
106if __name__ == "__main__":
107 main()