Pillow/Tests/oss-fuzz/fuzzers.py

42 lines
1.2 KiB
Python
Raw Permalink Normal View History

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.getsize_multiline("ABC\nAaaa")
font.getmask("test text")
with Image.new(mode="RGBA", size=(200, 200)) as im:
draw = ImageDraw.Draw(im)
draw.multiline_textsize("ABC\nAaaa", font, stroke_width=2)
draw.text((10, 10), "Test Text", font=font, fill="#000")