Files
Nexus/tests/test_guacamole_protocol.py
T
Nexus Agent aeca2fd7cb
Nexus CI/CD / test (push) Waiting to run
Nexus CI/CD / e2e (push) Blocked by required conditions
Nexus CI/CD / deploy (push) Blocked by required conditions
Nexus Pre-commit Checks / quick-check (push) Waiting to run
fix(rdp): guacd→WebSocket 按完整指令转发,修复黑屏
TCP 块转发会截断 img/blob 指令导致 guacamole-common-js 无法渲染;connect 不再经 URL 传密码。
2026-06-10 21:37:10 +08:00

57 lines
1.5 KiB
Python

"""Guacamole protocol encode/decode and internal opcode detection."""
import asyncio
import pytest
from server.infrastructure.guacamole.protocol import (
GuacdStream,
encode_instruction,
is_internal_instruction,
parse_instruction,
tunnel_open_instruction,
)
def test_encode_instruction():
assert encode_instruction("select", "rdp") == "6.select,3.rdp;"
def test_parse_instruction_roundtrip():
raw = encode_instruction("connect", "host", "3389").rstrip(";")
opcode, args = parse_instruction(raw)
assert opcode == "connect"
assert args == ["host", "3389"]
def test_tunnel_open_instruction():
msg = tunnel_open_instruction("abc-uuid")
assert is_internal_instruction(msg)
opcode, args = parse_instruction(msg.rstrip(";"))
assert opcode == ""
assert args == ["abc-uuid"]
def test_is_internal_ping():
ping = encode_instruction("", "ping", "12345")
assert is_internal_instruction(ping)
@pytest.mark.asyncio
async def test_guacd_stream_splits_tcp_chunks_on_semicolon():
class FakeReader:
def __init__(self, chunks: list[bytes]):
self._chunks = list(chunks)
async def read(self, n: int) -> bytes:
if not self._chunks:
return b""
return self._chunks.pop(0)
ins = encode_instruction("sync", "123").encode("utf-8")
reader = FakeReader([ins[:10], ins[10:]])
stream = GuacdStream(reader) # type: ignore[arg-type]
raw = await stream.read_raw_instruction()
assert raw == ins
assert raw.endswith(b";")