Files
OBLITERATUS/tests/test_mcp_transport.py
Joseph Magly 002e6e56e1 feat: complete local readiness across UI, MCP and memory admission
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
2026-10-20 17:58:20 -04:00

325 lines
11 KiB
Python

"""Direct framing and SDK-adapter contracts without subprocess coverage gaps."""
from __future__ import annotations
import asyncio
import io
import json
import logging
from contextlib import asynccontextmanager
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from obliteratus import mcp_transport as transport
@pytest.mark.parametrize(
("payload", "maximum", "expected"),
[
(b"", 4, b""),
(b"abc\n", 4, b"abc\n"),
(b"abcd", 4, b"abcd"),
(b"abcd\n", 4, transport.REJECTED_FRAME.encode()),
(b"x" * 140000, 4, transport.REJECTED_FRAME.encode()),
],
)
def test_frame_boundaries_and_unterminated_overflow(payload, maximum, expected):
class RecordingInput(io.BytesIO):
def readline(self, size=-1):
sizes.append(size)
return super().readline(size)
sizes = []
stream = RecordingInput(payload)
assert transport.read_frame(stream, maximum) == expected
assert sizes[0] == maximum + 1
assert all(size == 65536 for size in sizes[1:])
assert stream.read() == b""
def test_overflow_drain_stops_at_exactly_one_newline():
stream = io.BytesIO(b"x" * 140000 + b"\nnext\n")
assert transport.read_frame(stream, 8) == transport.REJECTED_FRAME.encode()
assert transport.read_frame(stream, 8) == b"next\n"
@pytest.mark.parametrize("name", ["mcp", "mcp.server", "mcp_types.validation"])
def test_diagnostic_redaction_removes_cached_traceback_and_stack(name):
record = logging.LogRecord(
name,
logging.ERROR,
"/private/path",
1,
"secret %s",
("token",),
(ValueError, ValueError("secret"), None),
)
record.exc_text = "cached secret traceback"
record.stack_info = "secret stack"
assert transport.DiagnosticFilter().filter(record) is True
assert record.getMessage() == "MCP diagnostic (details redacted)"
assert record.args == ()
assert record.exc_info is record.exc_text is record.stack_info is None
@pytest.mark.parametrize("name", ["application", "mcproxy", "mcp_types"])
def test_unrelated_diagnostics_preserve_message_and_exception(name):
record = logging.LogRecord(name, logging.INFO, "", 1, "value %s", ("visible",), None)
record.exc_text = "traceback"
assert transport.DiagnosticFilter().filter(record) is True
assert record.getMessage() == "value visible"
assert record.exc_text == "traceback"
@pytest.fixture
def stdio_seam(monkeypatch):
monkeypatch.setattr(transport, "os", SimpleNamespace(**vars(transport.os)))
sdk_stdio = pytest.importorskip("mcp.server.stdio")
pytest.importorskip("mcp_types")
operations = []
output = io.StringIO()
state = SimpleNamespace(
raw=b"",
output=output,
operations=operations,
input=None,
adapter=None,
fail_enter=False,
callback=None,
)
class WireOutput(io.StringIO):
def close(self):
state.written = self.getvalue()
super().close()
def fdopen(fd, mode, **kwargs):
if mode == "rb":
state.input = io.BytesIO(state.raw)
return state.input
assert kwargs == {"encoding": "utf-8"}
state.output = WireOutput()
return state.output
descriptors = iter([100, 101, 102, 103])
monkeypatch.setattr(transport.os, "dup", lambda fd: next(descriptors))
monkeypatch.setattr(transport.os, "fdopen", fdopen)
monkeypatch.setattr(transport.os, "open", lambda path, flags: 104)
monkeypatch.setattr(transport.os, "close", lambda fd: operations.append(("close", fd)))
monkeypatch.setattr(
transport.os, "dup2", lambda src, dst: operations.append(("dup2", src, dst))
)
@asynccontextmanager
async def stdio_server(*, stdin, stdout):
state.adapter = (stdin, stdout)
if state.fail_enter:
raise RuntimeError("SDK enter failed")
yield "sdk-read", "sdk-write"
async def run(read, write, options):
assert (read, write, options) == ("sdk-read", "sdk-write", {"initialization": True})
if state.callback is not None:
await state.callback(*state.adapter)
lowlevel = SimpleNamespace(
run=AsyncMock(side_effect=run),
create_initialization_options=lambda: {"initialization": True},
)
state.server = SimpleNamespace(_lowlevel_server=lowlevel)
state.policy = SimpleNamespace(max_input_bytes=64, max_output_bytes=1024)
monkeypatch.setattr(sdk_stdio, "stdio_server", stdio_server)
return state
def run_seam(state):
asyncio.run(transport.run_stdio(state.server, state.policy))
def test_tiny_output_budget_closes_without_emitting_oversized_error(stdio_seam):
stdio_seam.policy.max_output_bytes = 1
async def produce(stdin, stdout):
await stdout.write('{"jsonrpc":"2.0","id":1,"result":{}}')
stdio_seam.callback = produce
with pytest.raises(ValueError, match="output limit"):
run_seam(stdio_seam)
assert stdio_seam.written == ""
assert stdio_seam.input.closed and stdio_seam.output.closed
def test_second_descriptor_duplication_failure_closes_first(stdio_seam, monkeypatch):
def duplicate(descriptor):
if descriptor == 0:
return 100
raise OSError("descriptor unavailable")
monkeypatch.setattr(transport.os, "dup", duplicate)
with pytest.raises(OSError, match="descriptor unavailable"):
run_seam(stdio_seam)
assert stdio_seam.operations == [("close", 100)]
@pytest.mark.parametrize("identifier", [None, 0, 2**53 - 1, -(2**53 - 1), "x" * 128])
def test_sdk_input_accepts_notifications_and_bounded_request_ids(stdio_seam, identifier):
message = {"jsonrpc": "2.0", "method": "tools/list"}
if identifier is not None:
message["id"] = identifier
raw = (json.dumps(message) + "\n").encode()
stdio_seam.raw = raw
async def consume(stdin, stdout):
assert stdin.__aiter__() is stdin
assert await anext(stdin) == raw.decode()
with pytest.raises(StopAsyncIteration):
await anext(stdin)
stdio_seam.callback = consume
run_seam(stdio_seam)
stdio_seam.server._lowlevel_server.run.assert_awaited_once()
@pytest.mark.parametrize(
"raw",
[
b"private invalid JSON\n",
b"\xff\n",
b"[]\n",
b"null\n",
b'{"jsonrpc":"1.0","id":1,"method":"tools/list"}\n',
b'{"jsonrpc":"2.0","id":true,"method":"tools/list"}\n',
b'{"jsonrpc":"2.0","id":1.0,"method":"tools/list"}\n',
(json.dumps({"jsonrpc": "2.0", "id": "x" * 129, "method": "tools/list"}) + "\n").encode(),
(json.dumps({"jsonrpc": "2.0", "id": 2**53, "method": "tools/list"}) + "\n").encode(),
(json.dumps({"jsonrpc": "2.0", "id": -(2**53), "method": "tools/list"}) + "\n").encode(),
b"x" * 20000 + b"\n",
],
)
def test_sdk_input_rejects_invalid_frames_then_resumes(stdio_seam, raw):
valid = b'{"jsonrpc":"2.0","id":7,"method":"tools/list"}\n'
stdio_seam.raw = raw + valid
async def consume(stdin, stdout):
assert await anext(stdin) == transport.REJECTED_FRAME
assert await anext(stdin) == valid.decode()
with pytest.raises(StopAsyncIteration):
await anext(stdin)
stdio_seam.callback = consume
run_seam(stdio_seam)
@pytest.mark.parametrize(
("message", "expected"),
[
(
{"jsonrpc": "2.0", "id": 1, "result": {"text": "é"}},
{"jsonrpc": "2.0", "id": 1, "result": {"text": "é"}},
),
(
{
"jsonrpc": "2.0",
"id": 1,
"error": {"code": -32602, "message": "secret", "data": "token"},
},
{
"jsonrpc": "2.0",
"id": 1,
"error": {"code": -32602, "message": "MCP request rejected."},
},
),
(
{"jsonrpc": "2.0", "id": 1, "error": {}},
{
"jsonrpc": "2.0",
"id": 1,
"error": {"code": -32600, "message": "MCP request rejected."},
},
),
(
{"jsonrpc": "2.0", "id": 1, "result": "private" * 1024},
{
"jsonrpc": "2.0",
"id": 1,
"error": {"code": -32000, "message": "MCP response exceeds limit."},
},
),
],
)
def test_sdk_output_redacts_errors_and_bounds_results(stdio_seam, message, expected):
async def produce(stdin, stdout):
await stdout.write(json.dumps(message))
await stdout.flush()
stdio_seam.callback = produce
run_seam(stdio_seam)
assert json.loads(stdio_seam.written) == expected
assert stdio_seam.written.endswith("\n")
assert len(stdio_seam.written.encode()) <= stdio_seam.policy.max_output_bytes
assert stdio_seam.written.isascii()
@pytest.mark.parametrize(
("raw", "exception"),
[
("not JSON", json.JSONDecodeError),
("null", TypeError),
('{"error": null}', AttributeError),
('{"error": []}', AttributeError),
],
)
def test_malformed_sdk_output_fails_without_writing_and_restores_streams(
stdio_seam, raw, exception
):
async def produce(stdin, stdout):
await stdout.write(raw)
stdio_seam.callback = produce
with pytest.raises(exception):
run_seam(stdio_seam)
assert stdio_seam.written == ""
assert stdio_seam.operations[-4:] == [
("dup2", 100, 0),
("dup2", 101, 1),
("close", 100),
("close", 101),
]
@pytest.mark.parametrize("failure", ["none", "enter", "run", "cancel"])
def test_descriptor_and_log_filter_lifecycle(stdio_seam, monkeypatch, failure):
handler = logging.NullHandler()
prior = logging.Filter("preserve")
handler.addFilter(prior)
monkeypatch.setattr(logging.getLogger(), "handlers", [handler])
async def callback(stdin, stdout):
assert prior in handler.filters
assert any(isinstance(item, transport.DiagnosticFilter) for item in handler.filters)
if failure == "run":
raise RuntimeError("SDK run failed")
if failure == "cancel":
raise asyncio.CancelledError
stdio_seam.callback = callback
stdio_seam.fail_enter = failure == "enter"
if failure == "none":
run_seam(stdio_seam)
else:
with pytest.raises(asyncio.CancelledError if failure == "cancel" else RuntimeError):
run_seam(stdio_seam)
assert handler.filters == [prior]
assert stdio_seam.input.closed and stdio_seam.output.closed
assert stdio_seam.operations == [
("dup2", 104, 0),
("dup2", 2, 1),
("close", 104),
("dup2", 100, 0),
("dup2", 101, 1),
("close", 100),
("close", 101),
]