Pillow/Tests/oss-fuzz/fuzzers.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.2 KiB
Python
Raw Normal View History

from __future__ import annotations
2021-03-14 14:21:02 +03:00
import io
import warnings
2021-03-14 15:14:39 +03:00
from PIL import Image, ImageDraw, ImageFile, ImageFilter, ImageFont
2021-03-14 14:21:02 +03:00
def enable_decompressionbomb_error():
ImageFile.LOAD_TRUNCATED_IMAGES = True
warnings.filterwarnings("ignore")
warnings.simplefilter("error", Image.DecompressionBombWarning)
def disable_decompressionbomb_error():
ImageFile.LOAD_TRUNCATED_IMAGES = False
warnings.resetwarnings()
2021-03-14 14:21:02 +03:00
def fuzz_image(data):
# This will fail on some images in the corpus, as we have many
# invalid images in the test suite.
with Image.open(io.BytesIO(data)) as im:
im.rotate(45)
im.filter(ImageFilter.DETAIL)
im.save(io.BytesIO(), "BMP")
2021-03-14 14:21:02 +03:00
def fuzz_font(data):
wrapper = io.BytesIO(data)
try:
font = ImageFont.truetype(wrapper)
except OSError:
2021-03-15 02:18:07 +03:00
# Catch pcf/pilfonts/random garbage here. They return
# different font objects.
2021-03-14 14:21:02 +03:00
return
font.getbbox("ABC")
2021-03-14 14:21:02 +03:00
font.getmask("test text")
with Image.new(mode="RGBA", size=(200, 200)) as im:
draw = ImageDraw.Draw(im)
draw.multiline_textbbox((10, 10), "ABC\nAaaa", font, stroke_width=2)
draw.text((10, 10), "Test Text", font=font, fill="#000")