Coverage for /pythoncovmergedfiles/medio/medio/src/pdfminer.six/fuzzing/utils.py: 100%

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

22 statements  

1"""Utilities shared across the various PDF fuzzing harnesses""" 

2 

3import logging 

4from typing import Optional 

5 

6import atheris 

7 

8from pdfminer.layout import LAParams 

9 

10PDF_MAGIC_BYTES = b"%PDF-" 

11 

12 

13def prepare_pdfminer_fuzzing() -> None: 

14 """Used to disable logging of the pdfminer module""" 

15 logging.getLogger("pdfminer").setLevel(logging.CRITICAL) 

16 

17 

18@atheris.instrument_func # type: ignore[misc] 

19def generate_layout_parameters( 

20 fdp: atheris.FuzzedDataProvider, 

21) -> Optional[LAParams]: 

22 if fdp.ConsumeBool(): 

23 return None 

24 

25 boxes_flow: Optional[float] = None 

26 if fdp.ConsumeBool(): 

27 boxes_flow = fdp.ConsumeFloatInRange(-1.0, 1.0) 

28 

29 return LAParams( 

30 line_overlap=fdp.ConsumeFloat(), 

31 char_margin=fdp.ConsumeFloat(), 

32 line_margin=fdp.ConsumeFloat(), 

33 word_margin=fdp.ConsumeFloat(), 

34 boxes_flow=boxes_flow, 

35 detect_vertical=fdp.ConsumeBool(), 

36 all_texts=fdp.ConsumeBool(), 

37 ) 

38 

39 

40@atheris.instrument_func # type: ignore[misc] 

41def is_valid_byte_stream(data: bytes) -> bool: 

42 """Quick check to see if this is worth of passing to atheris 

43 :return: Whether the byte-stream passes the basic checks 

44 """ 

45 if not data.startswith(PDF_MAGIC_BYTES): 

46 return False 

47 if b"/Root" not in data: 

48 return False 

49 

50 return True