Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178"""Bounded raw-response conformance checks; only isolated loopback peers are used."""import argparseimport base64import hashlibimport jsonimport socketimport sslimport tempfileimport subprocessimport threadingimport timefrom pathlib import Path
TIMEOUT_FAILURES = {'RequestDeadlineExceeded', 'ResponseHeadTimeout', 'TransferStalled', 'TransferTooSlow'}PROTOCOL_FAILURES = {'HttpHeadersInvalid', 'InvalidContentLength', 'HttpHeaderContinuationsUnsupported', 'HttpTransferEncodingUnsupported', 'HttpContentEncodingUnsupported', 'HttpChunkInvalid', 'HttpChunkTruncated', 'ResponseTruncated', 'HttpHeadersOversize', 'HttpRequestTruncated', 'ResponseTooLarge'}
def observe(probe, backend, case, delivery, tls_fixture): wire = bytes.fromhex(case['wire_hex']) assert len(wire) <= 512 * 1024 listener = socket.socket() listener.bind(('127.0.0.1', 0)) listener.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) listener.listen(2) listener.settimeout(2) url = f"{'https' if case.get('tls') else 'http'}://{'localhost' if case.get('tls') else '127.0.0.1'}:{listener.getsockname()[1]}/case" state = {'connections': 0, 'fixture_error': None}
def accept(): connection, _ = listener.accept() state['connections'] += 1 connection.settimeout(2) if case.get('tls'): connection = tls_fixture['context'].wrap_socket(connection, server_side=True) return connection
def read_head(connection): request = b'' while b'\r\n\r\n' not in request: data = connection.recv(4096) if not data: return False request += data if len(request) > 16384: raise ValueError('fixture request head exceeded bound') return True
def serve(): connection = None try: connection = accept() if not read_head(connection): raise ValueError('client sent no request') if delivery == 'bytes': for byte in wire: connection.sendall(bytes([byte])) time.sleep(0.001) elif delivery == 'boundaries': for offset in range(0, len(wire), 7): connection.sendall(wire[offset:offset+7]) time.sleep(0.001) else: connection.sendall(wire) if case.get('followup'): if case.get('force_eof'): connection.shutdown(socket.SHUT_WR) if not read_head(connection): connection.close() connection = accept() if not read_head(connection): raise ValueError('client sent no followup request') connection.sendall(b'HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ngood') if case.get('tls'): if case.get('graceful_tls'): try: connection.unwrap().close() except ssl.SSLError: pass else: connection.close() else: connection.shutdown(socket.SHUT_WR) except (BrokenPipeError, ConnectionResetError): pass except Exception as error: state['fixture_error'] = type(error).__name__ finally: if connection is not None: connection.close() listener.close()
peer = threading.Thread(target=serve) peer.start() result = {} try: process = subprocess.run([str(probe), backend, url, case['method'], 'followup' if case.get('followup') else 'single', str(tls_fixture['cert']) if case.get('tls') else '-'], capture_output=True, timeout=3, check=False) if process.returncode: result = {'crash': process.returncode, 'detail': process.stderr.decode(errors='replace')[:2000]} else: assert len(process.stdout) <= 2 * 1024 * 1024 result = json.loads(process.stdout) except subprocess.TimeoutExpired: result = {'harness_timeout': True} finally: listener.close() peer.join(timeout=3) if peer.is_alive(): raise RuntimeError('fixture failed to terminate') result.update(state) return result
def matches(observation, outcome): if observation is None: return False failure = observation.get('failure') if outcome.get('reject'): return failure in PROTOCOL_FAILURES if outcome.get('timeout'): return failure in TIMEOUT_FAILURES if outcome.get('failure'): return failure == outcome['failure'] return failure is None and observation['status'] == outcome['status'] and base64.b64decode(observation['body_base64']) == bytes.fromhex(outcome['body_hex'])
def main(): parser = argparse.ArgumentParser() parser.add_argument('--probe', type=Path, required=True) parser.add_argument('--corpus', type=Path, default=Path(__file__).with_name('corpus.json')) parser.add_argument('--output', type=Path, required=True) parser.add_argument('--backend', action='append', choices=['threaded', 'zio', 'zio4', 'zio-native']) parser.add_argument('--filter', default='') args = parser.parse_args() raw = args.corpus.read_bytes() assert len(raw) <= 10 * 1024 * 1024 corpus = json.loads(raw) assert corpus['schema'] == 'zttp-http1-response-v1' assert len({case['id'] for case in corpus['cases']}) == len(corpus['cases']) <= 4096 backends = args.backend or ['threaded', 'zio', 'zio4', 'zio-native'] observations = [] with tempfile.TemporaryDirectory(prefix='zttp-tls-conformance-') as temporary: directory = Path(temporary) cert, key = directory / 'cert.pem', directory / 'key.pem' subprocess.run(['openssl', 'req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', str(key), '-out', str(cert), '-days', '1', '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost'], check=True, capture_output=True, timeout=15) context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) context.load_cert_chain(cert, key) execute(args, corpus, raw, backends, observations, {'context': context, 'cert': cert})
def execute(args, corpus, raw, backends, observations, tls_fixture): for backend in backends: for case in corpus['cases']: if args.filter not in case['id']: continue positive = all('status' in outcome for outcome in case['outcomes']) deliveries = case.get('deliveries', ['whole', 'boundaries', 'bytes'] if positive else ['whole']) for delivery in deliveries: observation = observe(args.probe, backend, case, delivery, tls_fixture) passed = 'crash' not in observation and 'harness_timeout' not in observation and observation['fixture_error'] is None passed = passed and any(matches(observation.get('first'), outcome) for outcome in case['outcomes']) if case.get('followup'): passed = passed and matches(observation.get('followup'), {'status': 200, 'body_hex': b'good'.hex()}) if 'connections' in case: passed = passed and observation['connections'] == case['connections'] record = {'id': case['id'], 'backend': backend, 'delivery': delivery, 'passed': passed, 'observation': observation} observations.append(record) if not passed: print(json.dumps(record), flush=True) report = {'schema': 'zttp-conformance-report-v1', 'upstream': corpus['upstream'], 'selection': {'filter': args.filter, 'backends': backends}, 'available_cases': len(corpus['cases']), 'corpus_sha256': hashlib.sha256(raw).hexdigest(), 'probe_sha256': hashlib.sha256(args.probe.read_bytes()).hexdigest(), 'executed': len(observations), 'passed': sum(record['passed'] for record in observations), 'results': observations} args.output.write_text(json.dumps(report, indent=2) + '\n') print(json.dumps({key: report[key] for key in ('available_cases', 'executed', 'passed', 'corpus_sha256', 'selection')}), flush=True) if not observations or report['passed'] != report['executed']: raise SystemExit(1)
if __name__ == '__main__': main()