test(media): 补分块下载中断与 root 自动创建的实测

上一轮两条「未验证」的敞口,本次用可执行的测试消掉:

分块传输(Transfer-Encoding: chunked):
- download_limited 的 Content-Length 预检对分块响应无效,只剩按块累计
  这一道闸。此前只有代码没有测试,无法证明超限时连接真的被中断。
- 新增 _ChunkedServer:起一个真 socket 的最小 HTTP/1.1 服务端,不下发
  Content-Length、按 16 KiB 分块持续写 8 MiB。用真连接而不是桩,因为
  要验证的正是「读到超限就断开」——桩测不出中断。
- 断言两条:抛 MediaRejected 且 detail 含「已中断」;服务端 sent < total
  说明它没能把 8 MiB 写完,即连接在中途被断开,而不是读完再判。
- 变异验证:临时移除 media.py 里的累计判限后,该用例从通过变为
  DID NOT RAISE,确认它测的确实是这段逻辑;随后已还原,media.py 与
  HEAD 无差异(git diff 为空)。

root 不存在:
- store_media 用 mkdir(parents=True, exist_ok=True) 建目录,但此前没有
  断言覆盖「首次运行时 home/data/inbound 还不存在」这条路径。

全量 295 passed,ruff 全绿,新增两条连续跑 3 次均稳定(涉及真 socket
与线程,排除偶发)。
This commit is contained in:
openbot committed 2026-11-10 10:22:21 +08:00
1 parent 5ca40c6b0e
commit 13b8d7a466
1 file changed
+99
+99
View File
@@ -17,9 +17,12 @@ from __future__ import annotations
import asyncio import asyncio
import base64 import base64
import socket
import sys import sys
import threading
import types import types
from pathlib import Path from pathlib import Path
from typing import Self
import pytest import pytest
@@ -34,10 +37,12 @@ from bot042.agent.outbox import (
from bot042.channels.cli import CliChannel from bot042.channels.cli import CliChannel
from bot042.channels.manager import ChannelManager from bot042.channels.manager import ChannelManager
from bot042.channels.media import ( from bot042.channels.media import (
MB,
MediaLimits, MediaLimits,
MediaRejected, MediaRejected,
check_mime, check_mime,
decode_base64_limited, decode_base64_limited,
download_limited,
read_file_limited, read_file_limited,
safe_join, safe_join,
sanitize_filename, 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" channels=ChannelManager(), settings=Settings(home=tmp_path), db=tmp_path / "b.db"
) )
assert services.media_transform("no-such-channel", {}) is None 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