forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_asyncio.py
351 lines (285 loc) · 11.6 KB
/
test_asyncio.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import asyncio
import binascii
import random
import socket
from unittest import TestCase
from unittest.mock import patch
from cryptography.hazmat.primitives import serialization
from aioquic.asyncio.client import connect
from aioquic.asyncio.protocol import QuicConnectionProtocol
from aioquic.asyncio.server import serve
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.logger import QuicLogger
from .utils import (
SERVER_CACERTFILE,
SERVER_CERTFILE,
SERVER_KEYFILE,
generate_ec_certificate,
run,
)
real_sendto = socket.socket.sendto
def sendto_with_loss(self, data, addr=None):
"""
Simulate 25% packet loss.
"""
if random.random() > 0.25:
real_sendto(self, data, addr)
class SessionTicketStore:
def __init__(self):
self.tickets = {}
def add(self, ticket):
self.tickets[ticket.ticket] = ticket
def pop(self, label):
return self.tickets.pop(label, None)
def handle_stream(reader, writer):
async def serve():
data = await reader.read()
writer.write(bytes(reversed(data)))
writer.write_eof()
asyncio.ensure_future(serve())
class HighLevelTest(TestCase):
def setUp(self):
self.server = None
def tearDown(self):
if self.server is not None:
self.server.close()
async def run_client(
self,
host,
port=4433,
cadata=None,
cafile=SERVER_CACERTFILE,
configuration=None,
request=b"ping",
**kwargs
):
if configuration is None:
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cadata=cadata, cafile=cafile)
async with connect(host, port, configuration=configuration, **kwargs) as client:
# waiting for connected when connected returns immediately
await client.wait_connected()
reader, writer = await client.create_stream()
self.assertEqual(writer.can_write_eof(), True)
self.assertEqual(writer.get_extra_info("stream_id"), 0)
writer.write(request)
writer.write_eof()
response = await reader.read()
# waiting for closed when closed returns immediately
await client.wait_closed()
return response
async def run_server(self, configuration=None, **kwargs):
if configuration is None:
configuration = QuicConfiguration(is_client=False)
configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE)
self.server = await serve(
host="::",
port="4433",
configuration=configuration,
stream_handler=handle_stream,
**kwargs
)
return self.server
def test_connect_and_serve(self):
run(self.run_server())
response = run(self.run_client("127.0.0.1"))
self.assertEqual(response, b"gnip")
def test_connect_and_serve_ec_certificate(self):
certificate, private_key = generate_ec_certificate(common_name="localhost")
run(
self.run_server(
configuration=QuicConfiguration(
certificate=certificate, private_key=private_key, is_client=False,
)
)
)
response = run(
self.run_client(
"127.0.0.1",
cadata=certificate.public_bytes(serialization.Encoding.PEM),
cafile=None,
)
)
self.assertEqual(response, b"gnip")
def test_connect_and_serve_large(self):
"""
Transfer enough data to require raising MAX_DATA and MAX_STREAM_DATA.
"""
data = b"Z" * 2097152
run(self.run_server())
response = run(self.run_client("127.0.0.1", request=data))
self.assertEqual(response, data)
def test_connect_and_serve_without_client_configuration(self):
async def run_client_without_config(host, port=4433):
async with connect(host, port) as client:
await client.ping()
run(self.run_server())
with self.assertRaises(ConnectionError):
run(run_client_without_config("127.0.0.1"))
def test_connect_and_serve_writelines(self):
async def run_client_writelines(host, port=4433):
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(host, port, configuration=configuration) as client:
reader, writer = await client.create_stream()
assert writer.can_write_eof() is True
writer.writelines([b"01234567", b"89012345"])
writer.write_eof()
return await reader.read()
run(self.run_server())
response = run(run_client_writelines("127.0.0.1"))
self.assertEqual(response, b"5432109876543210")
@patch("socket.socket.sendto", new_callable=lambda: sendto_with_loss)
def test_connect_and_serve_with_packet_loss(self, mock_sendto):
"""
This test ensures handshake success and stream data is successfully sent
and received in the presence of packet loss (randomized 25% in each direction).
"""
data = b"Z" * 65536
server_configuration = QuicConfiguration(
is_client=False, quic_logger=QuicLogger()
)
server_configuration.load_cert_chain(SERVER_CERTFILE, SERVER_KEYFILE)
run(self.run_server(configuration=server_configuration))
response = run(
self.run_client(
"127.0.0.1",
configuration=QuicConfiguration(
is_client=True, quic_logger=QuicLogger()
),
request=data,
)
)
self.assertEqual(response, data)
def test_connect_and_serve_with_session_ticket(self):
# start server
client_ticket = None
store = SessionTicketStore()
def save_ticket(t):
nonlocal client_ticket
client_ticket = t
run(
self.run_server(
session_ticket_fetcher=store.pop, session_ticket_handler=store.add
)
)
# first request
response = run(
self.run_client("127.0.0.1", session_ticket_handler=save_ticket),
)
self.assertEqual(response, b"gnip")
self.assertIsNotNone(client_ticket)
# second request
run(
self.run_client(
"127.0.0.1",
configuration=QuicConfiguration(
is_client=True, session_ticket=client_ticket
),
)
)
self.assertEqual(response, b"gnip")
def test_connect_and_serve_with_sni(self):
run(self.run_server())
response = run(self.run_client("localhost"))
self.assertEqual(response, b"gnip")
def test_connect_and_serve_with_stateless_retry(self):
run(self.run_server())
response = run(self.run_client("127.0.0.1"))
self.assertEqual(response, b"gnip")
def test_connect_and_serve_with_stateless_retry_bad_original_connection_id(self):
"""
If the server's transport parameters do not have the correct
original_connection_id the connection fail.
"""
def create_protocol(*args, **kwargs):
protocol = QuicConnectionProtocol(*args, **kwargs)
protocol._quic._original_connection_id = None
return protocol
run(self.run_server(create_protocol=create_protocol, stateless_retry=True))
with self.assertRaises(ConnectionError):
run(self.run_client("127.0.0.1"))
@patch("aioquic.quic.retry.QuicRetryTokenHandler.validate_token")
def test_connect_and_serve_with_stateless_retry_bad(self, mock_validate):
mock_validate.side_effect = ValueError("Decryption failed.")
run(self.run_server(stateless_retry=True))
with self.assertRaises(ConnectionError):
run(
self.run_client(
"127.0.0.1",
configuration=QuicConfiguration(is_client=True, idle_timeout=4.0),
)
)
def test_connect_and_serve_with_version_negotiation(self):
run(self.run_server())
# force version negotiation
configuration = QuicConfiguration(is_client=True, quic_logger=QuicLogger())
configuration.supported_versions.insert(0, 0x1A2A3A4A)
response = run(self.run_client("127.0.0.1", configuration=configuration))
self.assertEqual(response, b"gnip")
def test_connect_timeout(self):
with self.assertRaises(ConnectionError):
run(
self.run_client(
"127.0.0.1",
port=4400,
configuration=QuicConfiguration(is_client=True, idle_timeout=5),
)
)
def test_connect_timeout_no_wait_connected(self):
async def run_client_no_wait_connected(host, port, configuration):
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(
host, port, configuration=configuration, wait_connected=False
) as client:
await client.ping()
with self.assertRaises(ConnectionError):
run(
run_client_no_wait_connected(
"127.0.0.1",
port=4400,
configuration=QuicConfiguration(is_client=True, idle_timeout=5),
)
)
def test_change_connection_id(self):
async def run_client_change_connection_id(host, port=4433):
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(host, port, configuration=configuration) as client:
await client.ping()
client.change_connection_id()
await client.ping()
run(self.run_server())
run(run_client_change_connection_id("127.0.0.1"))
def test_key_update(self):
async def run_client_key_update(host, port=4433):
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(host, port, configuration=configuration) as client:
await client.ping()
client.request_key_update()
await client.ping()
run(self.run_server())
run(run_client_key_update("127.0.0.1"))
def test_ping(self):
async def run_client_ping(host, port=4433):
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(host, port, configuration=configuration) as client:
await client.ping()
await client.ping()
run(self.run_server())
run(run_client_ping("127.0.0.1"))
def test_ping_parallel(self):
async def run_client_ping(host, port=4433):
configuration = QuicConfiguration(is_client=True)
configuration.load_verify_locations(cafile=SERVER_CACERTFILE)
async with connect(host, port, configuration=configuration) as client:
coros = [client.ping() for x in range(16)]
await asyncio.gather(*coros)
run(self.run_server())
run(run_client_ping("127.0.0.1"))
def test_server_receives_garbage(self):
server = run(self.run_server())
server.datagram_received(binascii.unhexlify("c00000000080"), ("1.2.3.4", 1234))
server.close()