Files
hyprland-headless-stream-pl…/tests/unit/test_jpeg.py
T
Hermenix 5177b4c47d tests: keep the pixel buffer alive instead of taking an address of a temporary
The sandbox caught this and the local run did not: ctypes.create_string_buffer in
an expression is collected as soon as the statement ends, so the address handed to
the encoder dangles and the memory is free to be reused. Locally the freed bytes
still held the raster; under the build's allocator they did not, and the encoder
was handed garbage -- 'block (0, 0) came back as (27, 0, 30)'.

Same class of bug as the two the encoder itself documents, which is why the helper
now owns the buffer and says so.
2026-10-11 23:07:15 +00:00

183 lines
6.7 KiB
Python

"""`hhstream.jpeg`: the libjpeg-turbo binding, checked by round-tripping pixels.
A lossy codec cannot be tested by looking at a file header, so these tests encode a
raster and decode it again through the same library. The raster is chosen to fail
loudly on the two mistakes that matter here: uniformly filled blocks (JPEG
reproduces a solid block almost exactly, so a tolerance of a few counts is honest)
whose channels differ enough that a BGR/RGB mix-up is unmistakable, and a padded
stride, because assuming `width * 4` is the bug grim shipped in 1.5.
This is also the only test of the pixel-format claim the backend rests on:
`TJPF_BGRX` exists precisely so the compositor's B, G, R, X buffer can go into the
encoder unconverted. If that were the wrong format, the decoded block colours come
back swapped and these tests say so.
"""
from __future__ import annotations
import ctypes
import pytest
from hhstream.jpeg import DEFAULT_QUALITY, JPEGError, TurboJPEG
BLOCK = 16
def blocks() -> list[tuple[int, int, int]]:
"""Four colours whose channels differ enough that a swap cannot hide."""
return [(200, 30, 30), (30, 200, 30), (30, 30, 200), (140, 120, 100)]
def as_rgb(colour: tuple[int, int, int]) -> tuple[int, int, int]:
"""`blocks()` writes memory order (B, G, R); assertions read (R, G, B).
Getting these two the wrong way round is indistinguishable from a wrong pixel
format in the encoder -- which is exactly what this file exists to detect, so
the conversion is named rather than done inline.
"""
blue, green, red = colour
return red, green, blue
def colour_at(x: int, y: int, width: int) -> tuple[int, int, int]:
"""The block colour at a pixel, wrapping so any frame size is covered.
One formula for both the raster and the assertions: they used to index the
four colours independently, which ran off the end of the list for anything
bigger than a 2x2 grid of blocks.
"""
column = x // BLOCK
row = y // BLOCK
return blocks()[((row * (width // BLOCK)) + column) % len(blocks())]
def raster(width: int = 64, height: int = 64, stride: int | None = None) -> tuple[bytes, int]:
"""A BGRX frame of solid blocks; returns (bytes, stride)."""
stride = stride if stride is not None else width * 4
out = bytearray()
for y in range(height):
row = bytearray()
for x in range(width):
b, g, r = colour_at(x, y, width)
row += bytes((b, g, r, 0x00))
row += b"\x77" * (stride - width * 4) # padding, if any
out += row
return bytes(out), stride
class Frame:
"""Pixels in a ctypes buffer that outlives the address taken from it.
`ctypes.addressof(ctypes.create_string_buffer(...))` is the obvious one-liner
and it is a use-after-free: the buffer is temporary, so the address is
dangling the moment the call returns. Locally the freed memory still happened
to hold the pixels and the tests passed; inside the build sandbox the
allocator reused it and the encoder was handed whatever was there -- which is
the same class of bug this file's docstring warns about, so it should not be
the way these tests fail either.
"""
def __init__(self, pixels: bytes) -> None:
self.buffer = ctypes.create_string_buffer(pixels, len(pixels))
self.address = ctypes.addressof(self.buffer)
def centre_colour(pixels: bytes, stride: int, x: int, y: int) -> tuple[int, int, int]:
offset = y * stride + x * 4
blue, green, red, _ = pixels[offset:offset + 4]
return red, green, blue
def test_a_frame_round_trips_with_its_channels_in_the_right_place():
enc = TurboJPEG()
try:
pixels, stride = raster()
frame = Frame(pixels)
encoded = enc.compress(frame.address, 64, 64, stride)
width, height, decoded = enc.decompress(encoded)
assert (width, height) == (64, 64)
for column, row in ((0, 0), (1, 0), (2, 0), (0, 1), (3, 3)):
x, y = column * BLOCK + BLOCK // 2, row * BLOCK + BLOCK // 2
want = as_rgb(colour_at(x, y, 64))
got = centre_colour(decoded, width * 4, x, y)
assert got == pytest.approx(want, abs=12), (
f"block ({row}, {column}) came back as {got}, expected ~{want} "
"-- a swapped channel order or a wrong pixel format"
)
# Being close is not enough on its own: assert the channels are
# distinguishable, so a swap could not pass as "close enough".
assert max(want) - min(want) > 24, "pick colours a swap would expose"
finally:
enc.close()
def test_a_padded_stride_is_honoured():
"""The padding bytes are deliberately not black; leaking them changes the
last column of every row."""
enc = TurboJPEG()
try:
pixels, stride = raster(64, 64, stride=64 * 4 + 32)
assert stride != 64 * 4
frame = Frame(pixels)
encoded = enc.compress(frame.address, 64, 64, stride)
_, _, decoded = enc.decompress(encoded)
got = centre_colour(decoded, 64 * 4, 8, 8)
assert got == pytest.approx(as_rgb(colour_at(8, 8, 64)), abs=12), got
finally:
enc.close()
def test_quality_trades_bytes_for_fidelity():
enc = TurboJPEG()
try:
pixels, stride = raster()
frame = Frame(pixels)
small = enc.compress(frame.address, 64, 64, stride, quality=20)
large = enc.compress(frame.address, 64, 64, stride, quality=95)
assert len(small) < len(large)
finally:
enc.close()
def test_the_output_is_really_a_jpeg():
enc = TurboJPEG()
try:
pixels, stride = raster()
frame = Frame(pixels)
encoded = enc.compress(frame.address, 64, 64, stride)
assert encoded.startswith(b"\xff\xd8"), "missing SOI marker"
assert b"\xff\xc0" in encoded, "missing SOF0 marker"
assert encoded.endswith(b"\xff\xd9"), "missing EOI marker"
finally:
enc.close()
def test_the_default_quality_is_the_one_the_capture_command_used():
"""`grim -c -t jpeg -q 80 -` is the deployment's command; a swap must not
silently change the frames' size or quality."""
assert DEFAULT_QUALITY == 80
@pytest.mark.parametrize("quality", [0, -1, 101])
def test_an_impossible_quality_is_refused(quality):
enc = TurboJPEG()
try:
pixels, stride = raster()
frame = Frame(pixels)
with pytest.raises(ValueError, match="1..100"):
enc.compress(frame.address, 64, 64, stride, quality=quality)
finally:
enc.close()
def test_a_library_that_is_not_there_is_a_clear_error_not_a_crash():
with pytest.raises(JPEGError, match="cannot load libjpeg-turbo"):
TurboJPEG(path="/nonexistent/libturbojpeg.so")
def test_close_is_safe_twice():
enc = TurboJPEG()
enc.close()
enc.close()