Coverage for /pythoncovmergedfiles/medio/medio/src/pdfplumber/fuzz/pdf_load_fuzzer.py: 38%

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

53 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 enum import IntEnum 

14 

15import atheris 

16from fuzz_helpers import EnhancedFuzzedDataProvider 

17 

18with atheris.instrument_imports(include=["pdfplumber"]): 

19 from pdfminer.pdftypes import PDFException 

20 from pdfminer.psparser import PSException 

21 from pdfplumber.utils.exceptions import MalformedPDFException, PdfminerException 

22 

23 import pdfplumber 

24 

25 

26class CastType(IntEnum): 

27 CSV = 0 

28 IMAGE = 1 

29 JSON = 2 

30 DICT = 3 

31 MAX = 4 

32 

33 

34def TestOneInput(data: bytes): 

35 fdp = EnhancedFuzzedDataProvider(data) 

36 

37 try: 

38 with fdp.ConsumeMemoryFile(all_data=False, as_bytes=True) as f: 

39 pdf = pdfplumber.open(f) 

40 

41 # Test casting 

42 cast_ty = fdp.ConsumeEnum(CastType) 

43 

44 if cast_ty is CastType.CSV: 

45 pdf.to_csv() 

46 elif cast_ty is CastType.IMAGE and pdf.pages: 

47 pdf.pages[0].to_image() 

48 elif cast_ty is CastType.JSON: 

49 pdf.to_json() 

50 elif cast_ty is CastType.DICT: 

51 pdf.to_dict() 

52 

53 except (PDFException, PSException, AssertionError, MalformedPDFException, PdfminerException): 

54 return -1 

55 except ValueError as e: 

56 if "invalid literal for int" in str(e): 

57 return -1 

58 raise e 

59 except TypeError as e: 

60 if "argument must be a string" in str(e): 

61 return -1 

62 raise e 

63 

64 

65def main(): 

66 atheris.Setup(sys.argv, TestOneInput) 

67 atheris.Fuzz() 

68 

69 

70if __name__ == "__main__": 

71 main()