hermes-agent/tests/gateway/relay/test_auth.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

105 lines
4.3 KiB
Python

"""Unit tests for gateway/relay/auth.py — the gateway-side relay auth primitives.
Two layers:
1. **Self-consistency** — make_token/verify_token round-trip, delivery-signature
verify, rotation verify list, tamper + skew + expiry rejection.
2. **Cross-implementation conformance** — frozen vectors generated by the
connector's TypeScript (``src/core/relayAuthToken.ts`` ``makeToken``/``sign``)
are reproduced byte-for-byte by the Python port. If the connector ever
changes its wire scheme, these vectors must be regenerated in lockstep
(and that is the point — the test fails loudly on drift). Regenerate with:
node -e 'import("./dist/core/relayAuthToken.js").then(m=>{ \
const s="00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; \
console.log(m.makeToken("gw-instance-1", s, 0)); \
console.log(m.sign("1750000000."+JSON.stringify({a:1}), s)); })'
"""
from __future__ import annotations
import json
from gateway.relay.auth import (
DELIVERY_SIG_HEADER,
DELIVERY_TS_HEADER,
make_token,
make_upgrade_token,
sign,
verify_delivery_signature,
verify_signature,
verify_token,
)
# A fixed 256-bit hex secret used for the frozen connector vectors below.
_SECRET = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
# ── Frozen vectors produced by the connector's TypeScript (relayAuthToken.ts).
# Generated via dist/core/relayAuthToken.js makeToken/sign; see module docstring.
_CONN_TOKEN = "Z3ctaW5zdGFuY2UtMTowOjM3YWE3YjE0NWU4NzY0ZDQwM2JhOWM2MzlmMjMwZGQ2M2RlOGVkOTliODhmZWQzNmFhMDI2MjVhOGE3ZTM1NjQ"
# The EXACT bytes the connector signed: JS JSON.stringify emits compact JSON
# (no spaces). The gateway verifies over the literal received body, so the
# vector is the compact form — NOT Python's spaced json.dumps default. This is
# the raw-byte-preservation discipline (a single differing byte breaks the HMAC).
_CONN_BODY = '{"type":"message","event":{"text":"hi","source":{"chat_id":"c1"}}}'
_CONN_TS = 1750000000
_CONN_SIG = "ac9509c8dae52b5590f06378260877334ff1adc4b1c96bafa4b514165fae6dc6"
# ── Self-consistency ──────────────────────────────────────────────────────
def test_upgrade_token_is_make_token_of_gateway_id():
assert make_upgrade_token("gw-1", _SECRET, 0) == make_token("gw-1", _SECRET, 0)
def test_token_wrong_secret_rejected():
tok = make_token("p", _SECRET, 0)
assert verify_token(tok, ["deadbeef" * 8]) is None
def test_token_expired_rejected():
# ttl in the past -> exp < now -> rejected.
tok = make_token("p", _SECRET, ttl_seconds=1)
# Force expiry by signing with a manual past exp via the low-level helper.
# Simpler: a 1s ttl token is still valid now; instead assert a clearly-old one.
# Build an already-expired token by hand using the same scheme.
import base64
signed = "p:1" # exp=1 (1970) -> long past
sig = sign(signed, _SECRET)
raw = f"{signed}:{sig}".encode()
expired = base64.urlsafe_b64encode(raw).decode().rstrip("=")
assert verify_token(expired, [_SECRET]) is None
# And the fresh one is accepted.
assert verify_token(tok, [_SECRET]) == "p"
def test_verify_signature_constant_time_multi_secret():
payload = "1700000000.body"
s = sign(payload, _SECRET)
assert verify_signature(payload, s, ["wrong", _SECRET]) is True
assert verify_signature(payload, s, ["wrong"]) is False
assert verify_signature(payload, "zz", [_SECRET]) is False # bad hex
# ── Delivery signature (connector -> gateway inbound) ──────────────────────
def test_delivery_signature_skew_rejected():
body = "{}"
ts = 1700000000
s = sign(f"{ts}.{body}", _SECRET)
# Beyond the 300s replay window in either direction.
assert verify_delivery_signature(body, str(ts), s, [_SECRET], now=ts + 301) is False
assert verify_delivery_signature(body, str(ts), s, [_SECRET], now=ts - 301) is False
assert verify_delivery_signature(body, str(ts), s, [_SECRET], now=ts + 299) is True
# ── Cross-implementation conformance (frozen connector vectors) ────────────
def test_python_make_token_matches_connector_byte_for_byte():
assert make_token("gw-instance-1", _SECRET, 0) == _CONN_TOKEN