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
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
1"""Utilities shared across the various PDF fuzzing harnesses"""
3import logging
5import atheris
7from pdfminer.layout import LAParams
9PDF_MAGIC_BYTES = b"%PDF-"
12def prepare_pdfminer_fuzzing() -> None:
13 """Used to disable logging of the pdfminer module"""
14 logging.getLogger("pdfminer").setLevel(logging.CRITICAL)
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
24 boxes_flow: float | None = None
25 if fdp.ConsumeBool():
26 boxes_flow = fdp.ConsumeFloatInRange(-1.0, 1.0)
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 )
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