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

20 statements  

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

2 

3import logging 

4 

5import atheris 

6 

7from pdfminer.layout import LAParams 

8 

9PDF_MAGIC_BYTES = b"%PDF-" 

10 

11 

12def prepare_pdfminer_fuzzing() -> None: 

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

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

15 

16 

17@atheris.instrument_func # type: ignore[misc, untyped-decorator] 

18def generate_layout_parameters( 

19 fdp: atheris.FuzzedDataProvider, 

20) -> LAParams | None: 

21 if fdp.ConsumeBool(): 

22 return None 

23 

24 boxes_flow: float | None = None 

25 if fdp.ConsumeBool(): 

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

27 

28 return LAParams( 

29 line_overlap=fdp.ConsumeFloat(), 

30 char_margin=fdp.ConsumeFloat(), 

31 line_margin=fdp.ConsumeFloat(), 

32 word_margin=fdp.ConsumeFloat(), 

33 boxes_flow=boxes_flow, 

34 detect_vertical=fdp.ConsumeBool(), 

35 all_texts=fdp.ConsumeBool(), 

36 ) 

37 

38 

39@atheris.instrument_func # type: ignore[misc, untyped-decorator] 

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

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

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

43 """ 

44 if not data.startswith(PDF_MAGIC_BYTES): 

45 return False 

46 return b"/Root" in data