mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 04:44:19 +01:00
ci-local.sh runs the six jobs side by side on one machine, and three of them run the same test files. Eleven files took a port number out of a socket they then closed, to start a proxy on it or to have a port where nothing listens. Anyone could be handed that port before the server bound it: a stranger could then answer in its place, and a dead port could come alive. run_suite.py ran those files one at a time, which kept them apart inside the suite job and not from the other jobs (code review, 2026-10-28, never seen in three runs). tests/localports.py gives three answers. held() keeps a port bound and never listening: a connection to it is refused and no other program can bind it. serve() starts a server of the test's process on a held port, beside the socket that keeps it (SO_REUSEPORT), so an engine that stops and starts again keeps its port in between. spawn() starts a proxy in a process of its own and keeps it once the socket listening on its port is its own, read in /proc, else starts it again on another port. The certificate test passes port 0, since nobody connects to a proxy that refuses to start, and now checks that the refusal is the certificate's. test_localports.py proves each answer on the machine, and fails on a test file that takes a port the old way: on the tree before this commit it names the eleven. With none left, the suite runs them four at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.7 KiB
Python
111 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""A refusal the proxy sends before reading a body reaches the client.
|
|
|
|
The 413 for a body past MAX_BODY_BYTES went out without the body being read, and the
|
|
socket closed at once: the kernel answered the rest of the upload with a reset, and the
|
|
reset destroyed the 413 on its way, so a client sending 20 MB past a 1 MB cap saw a broken
|
|
pipe five times in five (found in review, 2026-09-24). And a client that asks first
|
|
(Expect: 100-continue) was invited to send it all before being refused.
|
|
|
|
The same holds for the 401 the proxy answers since v6.34 when a request does not carry the
|
|
key the engine admits: it is sent before the body is read, so it must arrive whole too, and
|
|
a client that asks first must be refused before it sends."""
|
|
import http.client
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
import localports # noqa: E402
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[1]
|
|
CAP = 1024 * 1024
|
|
KEY = "k-test" # the key this harness gives the proxy
|
|
|
|
|
|
class TheRefusalArrives(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.home = pathlib.Path(tempfile.mkdtemp(prefix="kp-413-"))
|
|
(cls.home / ".config" / "qwen38").mkdir(parents=True)
|
|
(cls.home / ".config" / "qwen38" / "api-key").write_text(KEY + "\n")
|
|
env = {"PATH": os.environ["PATH"], "HOME": str(cls.home), "PROXY_BIND": "127.0.0.1",
|
|
"UPSTREAM": "http://127.0.0.1:9", "MAX_BODY_BYTES": str(CAP)}
|
|
cls.proxy, cls.port = localports.spawn(lambda port: subprocess.Popen(
|
|
[sys.executable, str(REPO / "keepalive-proxy.py"), str(port)], env=env,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL))
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.proxy.terminate()
|
|
cls.proxy.wait(10)
|
|
shutil.rmtree(cls.home, ignore_errors=True)
|
|
|
|
def test_a_client_that_just_sends_reads_the_413(self):
|
|
body = b"x" * (20 * CAP)
|
|
for _ in range(5):
|
|
c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=20)
|
|
try:
|
|
c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {KEY}"})
|
|
r = c.getresponse()
|
|
self.assertEqual(r.status, 413)
|
|
self.assertIn(b"body_too_large", r.read())
|
|
finally:
|
|
c.close()
|
|
|
|
def test_a_client_that_asks_first_is_refused_before_sending(self):
|
|
s = socket.create_connection(("127.0.0.1", self.port), timeout=10)
|
|
try:
|
|
s.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n"
|
|
b"Authorization: Bearer " + KEY.encode() + b"\r\n"
|
|
b"Expect: 100-continue\r\nContent-Length: %d\r\n\r\n" % (20 * CAP))
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
piece = s.recv(4096)
|
|
if not piece:
|
|
break
|
|
head += piece
|
|
finally:
|
|
s.close()
|
|
self.assertTrue(head.startswith(b"HTTP/1.1 413"), head[:80])
|
|
|
|
def test_a_client_without_the_key_that_just_sends_reads_the_401(self):
|
|
"""Under the cap, so only the key decides: the refusal goes out before the body is
|
|
read, and the client, still sending, reads it whole, five times in five."""
|
|
body = b"x" * (CAP - 1)
|
|
for _ in range(5):
|
|
c = http.client.HTTPConnection("127.0.0.1", self.port, timeout=20)
|
|
try:
|
|
c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json"})
|
|
r = c.getresponse()
|
|
self.assertEqual(r.status, 401)
|
|
self.assertEqual(r.read(), b'{"error":"Unauthorized"}')
|
|
finally:
|
|
c.close()
|
|
|
|
def test_a_client_without_the_key_that_asks_first_is_refused_before_sending(self):
|
|
s = socket.create_connection(("127.0.0.1", self.port), timeout=10)
|
|
try:
|
|
s.sendall(b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n"
|
|
b"Authorization: Bearer wrong\r\n"
|
|
b"Expect: 100-continue\r\nContent-Length: %d\r\n\r\n" % (CAP - 1))
|
|
head = b""
|
|
while b"\r\n\r\n" not in head:
|
|
piece = s.recv(4096)
|
|
if not piece:
|
|
break
|
|
head += piece
|
|
finally:
|
|
s.close()
|
|
self.assertTrue(head.startswith(b"HTTP/1.1 401"), head[:80])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|