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

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

41 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 

12import sys 

13from io import BytesIO 

14 

15import atheris 

16 

17with atheris.instrument_imports(): 

18 # We instrument `test_utils` as well, so it doesn't block coverage analysis in Fuzz Introspector: 

19 from test_utils import EnhancedFuzzedDataProvider, is_expected_exception 

20 

21 from dulwich.bundle import Bundle, read_bundle, write_bundle 

22 from dulwich.pack import PackData, write_pack_objects 

23 

24 

25def TestOneInput(data): 

26 fdp = EnhancedFuzzedDataProvider(data) 

27 bundle = Bundle() 

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

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

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

31 bundle.capabilities = { 

32 fdp.ConsumeRandomString(): fdp.ConsumeRandomString(), 

33 } 

34 

35 b = BytesIO() 

36 write_pack_objects(b.write, []) 

37 b.seek(0) 

38 bundle.pack_data = PackData.from_file(b) 

39 

40 # Test __repr__ method 

41 _ = repr(bundle) 

42 

43 try: 

44 bundle_file = BytesIO() 

45 write_bundle(bundle_file, bundle) 

46 _ = read_bundle(bundle_file) 

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

48 expected_exceptions = [ 

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

50 "surrogates not allowed", 

51 "unsupported bundle format header", 

52 ] 

53 if is_expected_exception(expected_exceptions, e): 

54 return -1 

55 else: 

56 raise e 

57 

58 

59def main(): 

60 atheris.Setup(sys.argv, TestOneInput) 

61 atheris.Fuzz() 

62 

63 

64if __name__ == "__main__": 

65 main()