-
Notifications
You must be signed in to change notification settings - Fork 243
/
test_network.py
343 lines (274 loc) · 11.7 KB
/
test_network.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
# test_network.py
from os.path import dirname, join
import sys
import os
import ssl
from functools import partial
import pytest
from curio import *
from curio import network
from curio import ssl as curiossl
from curio.socket import *
def test_tcp_echo(kernel):
results = []
async def handler(client, addr):
results.append('handler start')
while True:
results.append('recv wait')
data = await client.recv(100)
if not data:
break
results.append(('handler', data))
await client.sendall(data)
results.append('handler done')
async def client(address, serv):
results.append('client start')
sock = socket(AF_INET, SOCK_STREAM)
await sock.connect(address)
await sock.send(b'Msg1')
await sleep(0.1)
resp = await sock.recv(100)
results.append(('client', resp))
await sock.send(b'Msg2')
await sleep(0.1)
resp = await sock.recv(100)
results.append(('client', resp))
results.append('client close')
await sock.close()
await serv.cancel()
async def main():
async with TaskGroup() as g:
serv = await g.spawn(tcp_server, '', 25000, handler)
await g.spawn(client, ('localhost', 25000), serv)
kernel.run(main())
assert results == [
'client start',
'handler start',
'recv wait',
('handler', b'Msg1'),
'recv wait',
('client', b'Msg1'),
('handler', b'Msg2'),
'recv wait',
('client', b'Msg2'),
'client close',
'handler done'
]
if not sys.platform.startswith('win'):
def test_unix_echo(kernel):
results = []
async def handler(client, addr):
results.append('handler start')
while True:
results.append('recv wait')
data = await client.recv(100)
if not data:
break
results.append(('handler', data))
await client.sendall(data)
results.append('handler done')
async def client(address, serv):
results.append('client start')
sock = await network.open_unix_connection(address)
await sock.send(b'Msg1')
await sleep(0.1)
resp = await sock.recv(100)
results.append(('client', resp))
await sock.send(b'Msg2')
await sleep(0.1)
resp = await sock.recv(100)
results.append(('client', resp))
results.append('client close')
await sock.close()
await serv.cancel()
async def main():
try:
os.remove('/tmp/curionet')
except OSError:
pass
async with TaskGroup() as g:
serv = await g.spawn(unix_server, '/tmp/curionet', handler)
await g.spawn(client, '/tmp/curionet', serv)
kernel.run(main())
assert results == [
'client start',
'handler start',
'recv wait',
('handler', b'Msg1'),
'recv wait',
('client', b'Msg1'),
('handler', b'Msg2'),
'recv wait',
('client', b'Msg2'),
'client close',
'handler done'
]
def test_ssl_server(kernel):
async def client(host, port, context):
sock = await network.open_connection(host, port, ssl=context, server_hostname=host)
await sock.sendall(b'Hello, world!')
resp = await sock.recv(4096)
return resp
async def handler(client_sock, addr):
data = await client_sock.recv(1000)
assert data == b'Hello, world!'
await client_sock.send(b'Back atcha: ' + data)
async def main():
# It might be desirable to move these out of the examples
# directory, as this test are now relying on them being around
file_path = join(dirname(dirname(__file__)), 'examples')
cert_file = join(file_path, 'ssl_test.crt')
key_file = join(file_path, 'ssl_test_rsa')
server_context = curiossl.create_default_context(ssl.Purpose.CLIENT_AUTH)
server_context.load_cert_chain(certfile=cert_file, keyfile=key_file)
stdlib_client_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
curio_client_context = curiossl.create_default_context(ssl.Purpose.SERVER_AUTH)
server_task = await spawn(partial(network.tcp_server, '', 10000, handler, ssl=server_context))
await sleep(0.1)
for test_context in (curio_client_context, stdlib_client_context):
test_context.check_hostname = False
test_context.verify_mode = ssl.CERT_NONE
resp = await client('localhost', 10000, test_context)
assert resp == b'Back atcha: Hello, world!'
await server_task.cancel()
kernel.run(main())
if not sys.platform.startswith('win'):
def test_unix_ssl_server(kernel):
async def client(address, context):
sock = await network.open_unix_connection(address, ssl=context)
await sock.sendall(b'Hello, world!')
resp = await sock.recv(4096)
return resp
async def handler(client_sock, addr):
data = await client_sock.recv(1000)
assert data == b'Hello, world!'
await client_sock.send(b'Back atcha: ' + data)
async def main():
# It might be desirable to move these out of the examples
# directory, as this test are now relying on them being around
file_path = join(dirname(dirname(__file__)), 'examples')
cert_file = join(file_path, 'ssl_test.crt')
key_file = join(file_path, 'ssl_test_rsa')
server_context = curiossl.create_default_context(ssl.Purpose.CLIENT_AUTH)
server_context.load_cert_chain(certfile=cert_file, keyfile=key_file)
stdlib_client_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
curio_client_context = curiossl.create_default_context(ssl.Purpose.SERVER_AUTH)
try:
os.remove('/tmp/curionet')
except OSError:
pass
server_task = await spawn(partial(network.unix_server, '/tmp/curionet', handler, ssl=server_context))
await sleep(0.1)
for test_context in (curio_client_context, stdlib_client_context):
test_context.check_hostname = False
test_context.verify_mode = ssl.CERT_NONE
resp = await client('/tmp/curionet', test_context)
assert resp == b'Back atcha: Hello, world!'
await server_task.cancel()
kernel.run(main())
def test_ssl_wrapping(kernel):
async def client(host, port, context):
sock = await network.open_connection(host, port, ssl=context, server_hostname=host)
await sock.sendall(b'Hello, world!')
resp = await sock.recv(4096)
return resp
async def handler(client_sock, addr):
data = await client_sock.recv(1000)
assert data == b'Hello, world!'
await client_sock.send(b'Back atcha: ' + data)
def server(host, port, context):
sock = socket(AF_INET, SOCK_STREAM)
try:
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
sock.bind((host, port))
sock.listen(5)
return network.run_server(sock, handler, context)
except Exception:
sock._socket.close()
raise
async def main():
# It might be desirable to move these out of the examples
# directory, as this test are now relying on them being around
file_path = join(dirname(dirname(__file__)), 'examples')
cert_file = join(file_path, 'ssl_test.crt')
key_file = join(file_path, 'ssl_test_rsa')
server_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
server_context.load_cert_chain(certfile=cert_file, keyfile=key_file)
stdlib_client_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
curio_client_context = curiossl.create_default_context(ssl.Purpose.SERVER_AUTH)
server_task = await spawn(server, 'localhost', 10000, server_context)
for test_context in (curio_client_context, stdlib_client_context):
test_context.check_hostname = False
test_context.verify_mode = ssl.CERT_NONE
resp = await client('localhost', 10000, test_context)
assert resp == b'Back atcha: Hello, world!'
await server_task.cancel()
kernel.run(main())
@pytest.mark.internet
def test_ssl_outgoing(kernel):
async def main():
c = await network.open_connection('google.com', 443, ssl=True, server_hostname='google.com')
await c.close()
c = await network.open_connection('google.com', 443, ssl=True)
await c.close()
c = await network.open_connection('google.com', 443, ssl=True, alpn_protocols=['h2'])
await c.close()
kernel.run(main)
def test_ssl_manual_wrapping(kernel):
async def client(host, port, context):
sock = socket(AF_INET, SOCK_STREAM)
await sock.connect((host, port))
ssl_sock = await context.wrap_socket(sock, server_hostname=host)
await ssl_sock.sendall(b'Hello, world!')
resp = await ssl_sock.recv(4096)
return resp
async def handler(client_sock, addr):
data = await client_sock.recv(1000)
assert data == b'Hello, world!'
await client_sock.send(b'Back atcha: ' + data)
def server(host, port, context):
sock = socket(AF_INET, SOCK_STREAM)
try:
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
sock.bind((host, port))
sock.listen(5)
return network.run_server(sock, handler, context)
except Exception:
sock._socket.close()
raise
async def main():
# It might be desirable to move these out of the examples
# directory, as this test are now relying on them being around
file_path = join(dirname(dirname(__file__)), 'examples')
cert_file = join(file_path, 'ssl_test.crt')
key_file = join(file_path, 'ssl_test_rsa')
server_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
server_context.load_cert_chain(certfile=cert_file, keyfile=key_file)
curio_client_context = curiossl.create_default_context(ssl.Purpose.SERVER_AUTH)
server_task = await spawn(server, 'localhost', 10000, server_context)
curio_client_context.check_hostname = False
curio_client_context.verify_mode = ssl.CERT_NONE
resp = await client('localhost', 10000, curio_client_context)
assert resp == b'Back atcha: Hello, world!'
await server_task.cancel()
kernel.run(main())
@pytest.mark.internet
def test_errors(kernel):
async def main():
with pytest.raises(ValueError):
c = await network.open_connection('google.com', 443, server_hostname='google.com')
await c.close()
with pytest.raises(Exception):
c = await network.open_connection('google.com', 443, ssl=True, server_hostname='yahoo.com')
await c.close()
with pytest.raises(ValueError):
await network.tcp_server('localhost', 25000, None, ssl=True)
if not sys.platform.startswith('win'):
with pytest.raises(OSError):
await network.tcp_server('localhost', 80, None)
with pytest.raises(OSError):
await network.unix_server('/tmp', None)
with pytest.raises(ValueError):
c = await network.open_unix_connection('/tmp/curionet', server_hostname='google.com')
await c.close()
kernel.run(main)