diff --git a/tests/test_media_pipeline.py b/tests/test_media_pipeline.py index 115ae87..aa92dda 100644 --- a/tests/test_media_pipeline.py +++ b/tests/test_media_pipeline.py @@ -17,9 +17,12 @@ from __future__ import annotations import asyncio import base64 +import socket import sys +import threading import types from pathlib import Path +from typing import Self import pytest @@ -34,10 +37,12 @@ from bot042.agent.outbox import ( from bot042.channels.cli import CliChannel from bot042.channels.manager import ChannelManager from bot042.channels.media import ( + MB, MediaLimits, MediaRejected, check_mime, decode_base64_limited, + download_limited, read_file_limited, safe_join, sanitize_filename, @@ -529,3 +534,97 @@ def test_channel_services_media_transform_none_for_unknown_channel(tmp_path: Pat channels=ChannelManager(), settings=Settings(home=tmp_path), db=tmp_path / "b.db" ) assert services.media_transform("no-such-channel", {}) is None + + +# --------------------------------------------------------------------------- +# 4. 下载限长:分块传输(无 Content-Length)必须靠累计判限中断 +# --------------------------------------------------------------------------- + + +class _ChunkedServer: + """极简 HTTP/1.1 分块响应服务端,用于验证「服务端不给 Content-Length」时的闸。 + + 真实平台可能用 ``Transfer-Encoding: chunked`` 下发媒体,此时 + ``Content-Length`` 预检形同虚设,只剩按块累计这一道。用真 socket 而不是 + 桩,是因为要验证的正是「读到超限就断开连接」——桩连接测不出中断。 + """ + + def __init__(self, total: int, piece: int = 16 * 1024) -> None: + self.total = total + self.piece = piece + self.sent = 0 + self._sock = socket.socket() + self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self._sock.bind(("127.0.0.1", 0)) + self._sock.listen(1) + self._sock.settimeout(10) + self.url = f"http://127.0.0.1:{self._sock.getsockname()[1]}/media" + + def __enter__(self) -> Self: + self._thread = threading.Thread(target=self._serve, daemon=True) + self._thread.start() + return self + + def __exit__(self, *exc_info: object) -> None: + self._thread.join(timeout=10) + self._sock.close() + + def _serve(self) -> None: + try: + conn, _ = self._sock.accept() + except OSError: + return + with conn: + conn.settimeout(10) + head = b"" + while b"\r\n\r\n" not in head: + try: + piece = conn.recv(4096) + except OSError: + return + if not piece: + return + head += piece + conn.sendall( + b"HTTP/1.1 200 OK\r\n" + b"Content-Type: image/png\r\n" + b"Transfer-Encoding: chunked\r\n\r\n" + ) + payload = PNG_HEAD + b"\x00" * (self.piece - len(PNG_HEAD)) + try: + while self.sent < self.total: + conn.sendall(b"%x\r\n" % len(payload) + payload + b"\r\n") + self.sent += len(payload) + conn.sendall(b"0\r\n\r\n") + except OSError: + # 客户端超限后主动断开,这正是被测行为 + return + + +def test_download_limited_aborts_chunked_stream() -> None: + """没有 Content-Length 时,累计超过上限就要中断,不能把整份读完。""" + total = 8 * MB + limit = 256 * 1024 + with ( + _ChunkedServer(total=total) as server, + pytest.raises(MediaRejected) as excinfo, + ): + asyncio.run( + download_limited(server.url, limit=limit, chunk=16 * 1024, timeout=10.0) + ) + assert "已中断" in excinfo.value.detail + # 服务端没能把 8 MiB 全部写出:说明连接是在中途被断开的,不是读完再判 + assert server.sent < total + + +def test_store_media_creates_missing_root(tmp_path: Path) -> None: + """root 不存在时应自行创建:首次运行 ``home/data/inbound`` 还不存在。""" + root = tmp_path / "data" / "inbound" + part = ImageContent(mime_type="image/png").model_copy( + update={"data": base64.b64encode(PNG_HEAD).decode()} + ) + stored = asyncio.run(store_media(part, root=root)) + + assert root.is_dir() + assert stored.path.parent == root + assert stored.path.read_bytes() == PNG_HEAD