#!/usr/bin/env python3 """ Connection-abort helper for regression tests. Completes a TLS handshake and then closes the connection without ever sending a request line. This is the shape of traffic that used to underflow the server's in-flight request counter and refuse every later request with "41". Usage: python3 tests/abort_connection.py [count] Environment: GEMINI_PORT, GEMINI_CONNECT_HOST """ import os import socket import ssl import sys def main(): hostname = sys.argv[1] if len(sys.argv) > 1 else "localhost" count = int(sys.argv[2]) if len(sys.argv) > 2 else 1 port = int(os.environ.get("GEMINI_PORT", "1965")) connect_host = os.environ.get("GEMINI_CONNECT_HOST", hostname) context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.check_hostname = False context.verify_mode = ssl.CERT_NONE aborted = 0 for _ in range(count): try: sock = socket.create_connection((connect_host, port), timeout=5.0) tls_sock = context.wrap_socket(sock, server_hostname=hostname) # Close with no request bytes sent at all. tls_sock.close() aborted += 1 except Exception as e: print(f"Error: {e}") sys.exit(1) print(f"aborted {aborted}") if __name__ == "__main__": main()