Coverage for /pythoncovmergedfiles/medio/medio/src/fuzzing/projects/pycups/fuzzer/fuzz_file_handling.py: 24%

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

50 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#!/usr/bin/python3 

13import sys 

14import atheris 

15import cups 

16import tempfile 

17import io 

18import os 

19 

20def TestOneInput(data: bytes): 

21 # if len(data) < 4: 

22 # return 

23 

24 fdp = atheris.FuzzedDataProvider(data) 

25 

26 resource = fdp.ConsumeUnicodeNoSurrogates(20) 

27 filename = fdp.ConsumeUnicodeNoSurrogates(20) 

28 dest_choice = fdp.ConsumeIntInRange(0, 2) 

29 

30 try: 

31 conn = cups.Connection() 

32 

33 if dest_choice == 0: 

34 # using file descriptor 

35 with tempfile.NamedTemporaryFile() as tmp: 

36 fd = tmp.fileno() 

37 if fdp.ConsumeBool(): 

38 conn.getFile(resource, fd=fd) 

39 else: 

40 conn.putFile(resource, fd=fd) 

41 

42 elif dest_choice == 1: 

43 # Using filename 

44 temp_path = os.path.join(tempfile.gettempdir(), filename) 

45 try: 

46 if fdp.ConsumeBool(): 

47 conn.getFile(resource, filename=temp_path) 

48 else: 

49 conn.putFile(resource, filename=temp_path) 

50 finally: 

51 if os.path.exists(temp_path): 

52 os.remove(temp_path) 

53 

54 else: 

55 # using a file-like object 

56 content = fdp.ConsumeBytes(50) 

57 fileobj = io.BytesIO(content) 

58 if fdp.ConsumeBool(): 

59 conn.getFile(resource, file=fileobj) 

60 else: 

61 fileobj.seek(0) # reset the position before writing 

62 conn.putFile(resource, file=fileobj) 

63 

64 except Exception: 

65 pass 

66 

67def main(): 

68 atheris.instrument_all() 

69 atheris.Setup(sys.argv, TestOneInput, enable_python_coverage=True) 

70 atheris.Fuzz() 

71 

72if __name__ == "__main__": 

73 main()