Coverage for /pythoncovmergedfiles/medio/medio/src/dulwich/fuzzing/fuzz-targets/fuzz_bundle.py: 49%

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

43 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 

12# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 

13 

14import sys 

15from io import BytesIO 

16from typing import Optional 

17 

18import atheris 

19 

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 

23 

24 from dulwich.bundle import Bundle, read_bundle, write_bundle 

25 from dulwich.pack import PackData, write_pack_objects 

26 

27 

28def TestOneInput(data) -> Optional[int]: 

29 fdp = EnhancedFuzzedDataProvider(data) 

30 bundle = Bundle() 

31 bundle.version = fdp.PickValueInList([2, 3, None]) 

32 bundle.references = {fdp.ConsumeRandomString(): fdp.ConsumeBytes(20)} 

33 bundle.prerequisites = [(fdp.ConsumeBytes(20), fdp.ConsumeRandomBytes())] 

34 bundle.capabilities = { 

35 fdp.ConsumeRandomString(): fdp.ConsumeRandomString(), 

36 } 

37 

38 b = BytesIO() 

39 write_pack_objects(b.write, []) 

40 b.seek(0) 

41 bundle.pack_data = PackData.from_file(b) 

42 

43 # Test __repr__ method 

44 _ = repr(bundle) 

45 

46 try: 

47 bundle_file = BytesIO() 

48 write_bundle(bundle_file, bundle) 

49 _ = read_bundle(bundle_file) 

50 except (AttributeError, UnicodeEncodeError, AssertionError) as e: 

51 expected_exceptions = [ 

52 "'bytes' object has no attribute 'encode'", 

53 "surrogates not allowed", 

54 "unsupported bundle format header", 

55 ] 

56 if is_expected_exception(expected_exceptions, e): 

57 return -1 

58 else: 

59 raise e 

60 

61 

62def main() -> None: 

63 atheris.Setup(sys.argv, TestOneInput) 

64 atheris.Fuzz() 

65 

66 

67if __name__ == "__main__": 

68 main()