"""Protocol errors, slow consumer, TLS identity and recovery on the live server.""" from pathlib import Path import json import ssl import struct import sys import time from headless import connection, exact, send, receive ca = sys.argv[1] config = json.loads(Path(sys.argv[2]).read_bytes()) config["simulation"].update(width=1024, height=48, generations=15, max_generations_per_second=240) config["view"]["roi"].update(width=1024, height=48) raw = json.dumps(config).encode() def header(kind, n): return struct.pack(">4sHHQ", b"RPS1", 1, kind, n) def error(tls): try: receive(tls) except ValueError as e: assert str(e) return raise AssertionError("expected server ERROR") def begin(tls): msg = header(1, len(raw)) + raw for i in range(0, len(msg), 3): tls.sendall(msg[i:i+3]) assert receive(tls) == (2, raw) typ, payload = receive(tls) assert typ == 3 and struct.unpack_from(">QQ", payload) == (1, 0) for invalid in (header(1, 65537), header(4, 8), header(0, 0), b"FAIL"+header(1, 1)[4:], header(1, 1)[:4]+b"\0\2"+header(1, 1)[6:]): with connection(ca) as tls: tls.sendall(invalid) error(tls) with connection(ca) as tls: send(tls, 1, raw[:-1]) error(tls) with connection(ca) as tls: tls.sendall(header(1, len(raw))+raw + header(4, 8)+struct.pack(">Q", 1)) assert receive(tls) == (2, raw) error(tls) # The RTX 3080 cannot fit both states for this valid grid. The server # rejects the session before launching a simulation or writing CONFIG_ACCEPTED. large = json.loads(raw) large["simulation"]["width"] = 2**31 with connection(ca) as tls: send(tls, 1, json.dumps(large).encode()) error(tls) # Drop the transport inside the first frame, then reconnect to the same listener. with connection(ca) as tls: send(tls, 1, raw) assert receive(tls) == (2, raw) assert exact(tls, 16)[:8] == header(3, 0)[:8] exact(tls, 33) for reply in ((4, struct.pack(">Q", 2)), (1, raw)): with connection(ca) as tls: begin(tls) send(tls, *reply) error(tls) with connection(ca) as tls: begin(tls) tls.sendall((header(4, 8)+struct.pack(">Q", 1))*2) error(tls) with connection(ca) as tls: tls.sendall(header(1, len(raw))+raw[:4]) try: with connection(ca, hostname="wrong-name.invalid"): raise AssertionError("host verification missing") except ssl.SSLCertVerificationError: pass with connection(ca) as tls: begin(tls) time.sleep(0.4) send(tls, 4, struct.pack(">Q", 1)) typ, final = receive(tls) assert typ == 3 and struct.unpack_from(">QQ", final) == (2, 15) send(tls, 4, struct.pack(">Q", 2)) assert receive(tls) == (5, struct.pack(">QI", 15, 0)) print("PASS protocol errors and recovery; slow consumer saw generations 0 and 15")