1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Bounded raw-response conformance checks; only isolated loopback peers are used."""
import argparse
import base64
import hashlib
import json
import socket
import ssl
import tempfile
import subprocess
import threading
import time
from 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()