""" Tests for templates/mysql/server.py Covers the MySQL handshake happy path and regression tests for oversized length fields that could cause huge buffer allocations. """ import importlib.util import struct import sys from unittest.mock import MagicMock import pytest from hypothesis import given, settings from hypothesis import strategies as st from .conftest import _FUZZ_SETTINGS, make_fake_syslog_bridge, run_with_timeout # ── Helpers ─────────────────────────────────────────────────────────────────── def _load_mysql(): for key in list(sys.modules): if key in ("mysql_server", "syslog_bridge"): del sys.modules[key] sys.modules["syslog_bridge"] = make_fake_syslog_bridge() spec = importlib.util.spec_from_file_location("mysql_server", "templates/mysql/server.py") mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def _make_protocol(mod): proto = mod.MySQLProtocol() transport = MagicMock() written: list[bytes] = [] transport.write.side_effect = written.append proto.connection_made(transport) written.clear() # clear the greeting sent on connect return proto, transport, written def _make_packet(payload: bytes, seq: int = 1) -> bytes: length = len(payload) return struct.pack(" bytes: """Minimal MySQL client login packet.""" caps = struct.pack(" 1MB cap data = b"\xff\xff\xff\x01" run_with_timeout(proto.data_received, data) transport.close.assert_called() def test_length_just_over_1mb_closes(mysql_mod): proto, transport, _ = _make_protocol(mysql_mod) # 1MB + 1 byte — just over the cap just_over = struct.pack("