Skip to content

Commit

Permalink
Test PILfont even when FreeType is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 1, 2024
1 parent 8676cbd commit ecd3948
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Tests/test_imagefontpil.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from __future__ import annotations
import struct
import pytest
from io import BytesIO

from PIL import Image, ImageDraw, ImageFont, features
from PIL import Image, ImageDraw, ImageFont, features, _util

from .helper import assert_image_equal_tofile

pytestmark = pytest.mark.skipif(
features.check_module("freetype2"),
reason="PILfont superseded if FreeType is supported",
)
original_core = ImageFont.core


def setup_module():
if features.check_module("freetype2"):
ImageFont.core = _util.DeferredError(ImportError)


def teardown_module():
ImageFont.core = original_core


def test_default_font():
Expand Down Expand Up @@ -44,3 +52,23 @@ def test_textbbox():
default_font = ImageFont.load_default()
assert d.textlength("test", font=default_font) == 24
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, 24, 11)


def test_decompression_bomb():
glyph = struct.pack(">hhhhhhhhhh", 1, 0, 0, 0, 256, 256, 0, 0, 256, 256)
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)

font = ImageFont.ImageFont()
font._load_pilfont_data(fp, Image.new("L", (256, 256)))
with pytest.raises(Image.DecompressionBombError):
font.getmask("A" * 1_000_000)


@pytest.mark.timeout(4)
def test_oom():
glyph = struct.pack(">hhhhhhhhhh", 1, 0, 0, 0, 32767, 32767, 0, 0, 32767, 32767)
fp = BytesIO(b"PILfont\n\nDATA\n" + glyph * 256)

font = ImageFont.ImageFont()
font._load_pilfont_data(fp, Image.new("L", (1, 1)))
font.getmask("A" * 1_000_000)

0 comments on commit ecd3948

Please sign in to comment.