2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2021-03-14 14:21:02 +03:00
|
|
|
import subprocess
|
2021-03-14 15:27:47 +03:00
|
|
|
import sys
|
2021-03-14 14:21:02 +03:00
|
|
|
|
2021-03-14 15:02:48 +03:00
|
|
|
import fuzzers
|
2021-04-10 13:03:39 +03:00
|
|
|
import packaging
|
2021-03-14 15:02:48 +03:00
|
|
|
import pytest
|
|
|
|
|
2021-04-10 13:03:39 +03:00
|
|
|
from PIL import Image, features
|
2023-08-24 12:02:27 +03:00
|
|
|
from Tests.helper import skip_unless_feature
|
2021-03-14 14:21:02 +03:00
|
|
|
|
2021-03-14 15:57:24 +03:00
|
|
|
if sys.platform.startswith("win32"):
|
2021-03-14 16:11:48 +03:00
|
|
|
pytest.skip("Fuzzer is linux only", allow_module_level=True)
|
2021-04-10 17:58:01 +03:00
|
|
|
if features.check("libjpeg_turbo"):
|
|
|
|
version = packaging.version.parse(features.version("libjpeg_turbo"))
|
2021-04-10 13:03:39 +03:00
|
|
|
if version.major == 2 and version.minor == 0:
|
2021-04-10 17:58:01 +03:00
|
|
|
pytestmark = pytest.mark.valgrind_known_error(
|
|
|
|
reason="Known failing with libjpeg_turbo 2.0"
|
|
|
|
)
|
|
|
|
|
2021-03-14 15:49:36 +03:00
|
|
|
|
2021-03-14 15:02:48 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path",
|
|
|
|
subprocess.check_output("find Tests/images -type f", shell=True).split(b"\n"),
|
|
|
|
)
|
2024-01-19 00:37:58 +03:00
|
|
|
def test_fuzz_images(path: str) -> None:
|
2021-03-14 14:21:02 +03:00
|
|
|
fuzzers.enable_decompressionbomb_error()
|
|
|
|
try:
|
2021-03-14 15:02:48 +03:00
|
|
|
with open(path, "rb") as f:
|
2021-03-14 14:21:02 +03:00
|
|
|
fuzzers.fuzz_image(f.read())
|
|
|
|
assert True
|
2021-03-14 15:49:36 +03:00
|
|
|
except (
|
|
|
|
OSError,
|
|
|
|
SyntaxError,
|
|
|
|
MemoryError,
|
|
|
|
ValueError,
|
|
|
|
NotImplementedError,
|
|
|
|
OverflowError,
|
|
|
|
):
|
2021-03-14 14:21:02 +03:00
|
|
|
# Known exceptions that are through from Pillow
|
|
|
|
assert True
|
2021-03-14 15:02:48 +03:00
|
|
|
except (
|
|
|
|
Image.DecompressionBombError,
|
|
|
|
Image.DecompressionBombWarning,
|
|
|
|
Image.UnidentifiedImageError,
|
|
|
|
):
|
2021-03-14 14:21:02 +03:00
|
|
|
# Known Image.* exceptions
|
|
|
|
assert True
|
2021-06-10 06:49:17 +03:00
|
|
|
finally:
|
|
|
|
fuzzers.disable_decompressionbomb_error()
|
2021-03-14 14:21:02 +03:00
|
|
|
|
2021-03-14 15:49:36 +03:00
|
|
|
|
2023-08-24 12:02:27 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2021-03-14 15:02:48 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path", subprocess.check_output("find Tests/fonts -type f", shell=True).split(b"\n")
|
|
|
|
)
|
2024-01-19 00:37:58 +03:00
|
|
|
def test_fuzz_fonts(path: str) -> None:
|
2021-03-15 02:18:07 +03:00
|
|
|
if not path:
|
2021-03-14 14:21:02 +03:00
|
|
|
return
|
2021-03-14 15:02:48 +03:00
|
|
|
with open(path, "rb") as f:
|
2021-03-15 02:21:18 +03:00
|
|
|
try:
|
|
|
|
fuzzers.fuzz_font(f.read())
|
2022-12-31 09:45:09 +03:00
|
|
|
except (Image.DecompressionBombError, Image.DecompressionBombWarning, OSError):
|
2021-03-15 02:21:18 +03:00
|
|
|
pass
|
2021-03-14 14:21:02 +03:00
|
|
|
assert True
|