2016-04-02 19:17:57 +00:00
|
|
|
import asyncio
|
|
|
|
import os
|
2017-11-17 23:24:45 +00:00
|
|
|
import pathlib
|
2016-04-02 19:17:57 +00:00
|
|
|
import socket
|
|
|
|
import tempfile
|
2017-11-17 23:24:45 +00:00
|
|
|
import time
|
2016-11-09 16:17:08 +00:00
|
|
|
import unittest
|
2016-04-02 19:17:57 +00:00
|
|
|
|
|
|
|
from uvloop import _testbase as tb
|
|
|
|
|
|
|
|
|
|
|
|
class _TestUnix:
|
2016-05-20 16:19:07 +00:00
|
|
|
def test_create_unix_server_1(self):
|
2016-04-02 19:17:57 +00:00
|
|
|
CNT = 0 # number of clients that were successful
|
|
|
|
TOTAL_CNT = 100 # total number of clients that test will create
|
|
|
|
TIMEOUT = 5.0 # timeout for this test
|
|
|
|
|
|
|
|
async def handle_client(reader, writer):
|
|
|
|
nonlocal CNT
|
|
|
|
|
|
|
|
data = await reader.readexactly(4)
|
|
|
|
self.assertEqual(data, b'AAAA')
|
|
|
|
writer.write(b'OK')
|
|
|
|
|
|
|
|
data = await reader.readexactly(4)
|
|
|
|
self.assertEqual(data, b'BBBB')
|
|
|
|
writer.write(b'SPAM')
|
|
|
|
|
|
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
async def test_client(addr):
|
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
with sock:
|
|
|
|
sock.setblocking(False)
|
|
|
|
await self.loop.sock_connect(sock, addr)
|
|
|
|
|
|
|
|
await self.loop.sock_sendall(sock, b'AAAA')
|
2016-04-16 20:27:17 +00:00
|
|
|
|
|
|
|
buf = b''
|
|
|
|
while len(buf) != 2:
|
|
|
|
buf += await self.loop.sock_recv(sock, 1)
|
|
|
|
self.assertEqual(buf, b'OK')
|
2016-04-02 19:17:57 +00:00
|
|
|
|
|
|
|
await self.loop.sock_sendall(sock, b'BBBB')
|
2016-04-16 20:27:17 +00:00
|
|
|
|
|
|
|
buf = b''
|
|
|
|
while len(buf) != 4:
|
|
|
|
buf += await self.loop.sock_recv(sock, 1)
|
|
|
|
self.assertEqual(buf, b'SPAM')
|
2016-04-02 19:17:57 +00:00
|
|
|
|
|
|
|
async def start_server():
|
2016-04-02 19:58:35 +00:00
|
|
|
nonlocal CNT
|
|
|
|
CNT = 0
|
|
|
|
|
2016-04-02 19:17:57 +00:00
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
|
|
sock_name = os.path.join(td, 'sock')
|
2016-04-13 21:56:02 +00:00
|
|
|
srv = await asyncio.start_unix_server(
|
|
|
|
handle_client,
|
|
|
|
sock_name,
|
|
|
|
loop=self.loop)
|
|
|
|
|
2016-04-02 19:17:57 +00:00
|
|
|
try:
|
2016-04-13 21:56:02 +00:00
|
|
|
srv_socks = srv.sockets
|
|
|
|
self.assertTrue(srv_socks)
|
|
|
|
|
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(test_client(sock_name))
|
|
|
|
|
|
|
|
await asyncio.wait_for(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop),
|
|
|
|
TIMEOUT, loop=self.loop)
|
|
|
|
|
|
|
|
finally:
|
2016-05-20 16:19:07 +00:00
|
|
|
self.loop.call_soon(srv.close)
|
|
|
|
await srv.wait_closed()
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
# Check that the server cleaned-up proxy-sockets
|
|
|
|
for srv_sock in srv_socks:
|
|
|
|
self.assertEqual(srv_sock.fileno(), -1)
|
2016-04-02 19:17:57 +00:00
|
|
|
|
2016-05-20 16:19:07 +00:00
|
|
|
# asyncio doesn't cleanup the sock file
|
|
|
|
self.assertTrue(os.path.exists(sock_name))
|
|
|
|
|
2016-11-22 01:00:54 +00:00
|
|
|
async def start_server_sock(start_server):
|
2016-04-02 19:58:35 +00:00
|
|
|
nonlocal CNT
|
|
|
|
CNT = 0
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
|
|
sock_name = os.path.join(td, 'sock')
|
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
sock.bind(sock_name)
|
2016-04-13 21:56:02 +00:00
|
|
|
|
2016-11-22 01:00:54 +00:00
|
|
|
srv = await start_server(sock)
|
2016-04-13 21:56:02 +00:00
|
|
|
|
2016-04-02 19:58:35 +00:00
|
|
|
try:
|
2016-04-13 21:56:02 +00:00
|
|
|
srv_socks = srv.sockets
|
|
|
|
self.assertTrue(srv_socks)
|
|
|
|
|
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(test_client(sock_name))
|
|
|
|
|
|
|
|
await asyncio.wait_for(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop),
|
|
|
|
TIMEOUT, loop=self.loop)
|
|
|
|
|
|
|
|
finally:
|
2016-05-20 16:19:07 +00:00
|
|
|
self.loop.call_soon(srv.close)
|
|
|
|
await srv.wait_closed()
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
# Check that the server cleaned-up proxy-sockets
|
|
|
|
for srv_sock in srv_socks:
|
|
|
|
self.assertEqual(srv_sock.fileno(), -1)
|
|
|
|
|
2016-05-20 16:19:07 +00:00
|
|
|
# asyncio doesn't cleanup the sock file
|
|
|
|
self.assertTrue(os.path.exists(sock_name))
|
|
|
|
|
2016-11-22 01:00:54 +00:00
|
|
|
with self.subTest(func='start_unix_server(host, port)'):
|
|
|
|
self.loop.run_until_complete(start_server())
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
2016-04-02 19:17:57 +00:00
|
|
|
|
2016-11-22 01:00:54 +00:00
|
|
|
with self.subTest(func='start_unix_server(sock)'):
|
|
|
|
self.loop.run_until_complete(start_server_sock(
|
|
|
|
lambda sock: asyncio.start_unix_server(
|
|
|
|
handle_client,
|
|
|
|
None,
|
|
|
|
loop=self.loop,
|
|
|
|
sock=sock)))
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
|
|
|
|
|
|
|
with self.subTest(func='start_server(sock)'):
|
|
|
|
self.loop.run_until_complete(start_server_sock(
|
|
|
|
lambda sock: asyncio.start_server(
|
|
|
|
handle_client,
|
|
|
|
None, None,
|
|
|
|
loop=self.loop,
|
|
|
|
sock=sock)))
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
2016-04-02 19:58:35 +00:00
|
|
|
|
2016-05-20 16:19:07 +00:00
|
|
|
def test_create_unix_server_2(self):
|
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
|
|
sock_name = os.path.join(td, 'sock')
|
|
|
|
with open(sock_name, 'wt') as f:
|
|
|
|
f.write('x')
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(
|
|
|
|
OSError, "Address '{}' is already in use".format(
|
|
|
|
sock_name)):
|
|
|
|
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.loop.create_unix_server(object, sock_name))
|
|
|
|
|
2016-04-11 22:07:55 +00:00
|
|
|
def test_create_unix_connection_1(self):
|
|
|
|
CNT = 0
|
|
|
|
TOTAL_CNT = 100
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
def server(sock):
|
|
|
|
data = sock.recv_all(4)
|
2016-04-11 22:07:55 +00:00
|
|
|
self.assertEqual(data, b'AAAA')
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.send(b'OK')
|
2016-04-11 22:07:55 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
data = sock.recv_all(4)
|
2016-04-11 22:07:55 +00:00
|
|
|
self.assertEqual(data, b'BBBB')
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.send(b'SPAM')
|
2016-04-11 22:07:55 +00:00
|
|
|
|
|
|
|
async def client(addr):
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
addr,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
writer.write(b'AAAA')
|
|
|
|
self.assertEqual(await reader.readexactly(2), b'OK')
|
|
|
|
|
|
|
|
writer.write(b'BBBB')
|
|
|
|
self.assertEqual(await reader.readexactly(4), b'SPAM')
|
|
|
|
|
|
|
|
nonlocal CNT
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
async def client_2(addr):
|
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
sock.connect(addr)
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
sock=sock,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
writer.write(b'AAAA')
|
|
|
|
self.assertEqual(await reader.readexactly(2), b'OK')
|
|
|
|
|
|
|
|
writer.write(b'BBBB')
|
|
|
|
self.assertEqual(await reader.readexactly(4), b'SPAM')
|
|
|
|
|
|
|
|
nonlocal CNT
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
2016-11-22 01:00:54 +00:00
|
|
|
async def client_3(addr):
|
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
|
|
|
sock.connect(addr)
|
|
|
|
reader, writer = await asyncio.open_connection(
|
|
|
|
sock=sock,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
writer.write(b'AAAA')
|
|
|
|
self.assertEqual(await reader.readexactly(2), b'OK')
|
|
|
|
|
|
|
|
writer.write(b'BBBB')
|
|
|
|
self.assertEqual(await reader.readexactly(4), b'SPAM')
|
|
|
|
|
|
|
|
nonlocal CNT
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
2016-04-11 22:07:55 +00:00
|
|
|
def run(coro):
|
|
|
|
nonlocal CNT
|
|
|
|
CNT = 0
|
|
|
|
|
2017-11-17 22:58:38 +00:00
|
|
|
with self.unix_server(server,
|
|
|
|
max_clients=TOTAL_CNT,
|
|
|
|
backlog=TOTAL_CNT) as srv:
|
2017-11-17 22:42:31 +00:00
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(coro(srv.addr))
|
2016-04-11 22:07:55 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop))
|
2016-04-11 22:07:55 +00:00
|
|
|
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
|
|
|
|
|
|
|
run(client)
|
|
|
|
run(client_2)
|
2016-11-22 01:00:54 +00:00
|
|
|
run(client_3)
|
2016-04-11 22:07:55 +00:00
|
|
|
|
|
|
|
def test_create_unix_connection_2(self):
|
|
|
|
with tempfile.NamedTemporaryFile() as tmp:
|
|
|
|
path = tmp.name
|
|
|
|
|
|
|
|
async def client():
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
path,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
async def runner():
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
await client()
|
|
|
|
|
|
|
|
self.loop.run_until_complete(runner())
|
|
|
|
|
|
|
|
def test_create_unix_connection_3(self):
|
|
|
|
CNT = 0
|
|
|
|
TOTAL_CNT = 100
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
def server(sock):
|
|
|
|
data = sock.recv_all(4)
|
2016-04-11 22:07:55 +00:00
|
|
|
self.assertEqual(data, b'AAAA')
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.close()
|
2016-04-11 22:07:55 +00:00
|
|
|
|
|
|
|
async def client(addr):
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
addr,
|
|
|
|
loop=self.loop)
|
|
|
|
|
2016-04-16 17:47:41 +00:00
|
|
|
sock = writer._transport.get_extra_info('socket')
|
|
|
|
self.assertEqual(sock.family, socket.AF_UNIX)
|
|
|
|
|
2016-04-11 22:07:55 +00:00
|
|
|
writer.write(b'AAAA')
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.IncompleteReadError):
|
|
|
|
await reader.readexactly(10)
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
nonlocal CNT
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
def run(coro):
|
|
|
|
nonlocal CNT
|
|
|
|
CNT = 0
|
|
|
|
|
2017-11-17 22:58:38 +00:00
|
|
|
with self.unix_server(server,
|
|
|
|
max_clients=TOTAL_CNT,
|
|
|
|
backlog=TOTAL_CNT) as srv:
|
2017-11-17 22:42:31 +00:00
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(coro(srv.addr))
|
2016-04-11 22:07:55 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop))
|
2016-04-11 22:07:55 +00:00
|
|
|
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
|
|
|
|
|
|
|
run(client)
|
|
|
|
|
|
|
|
def test_create_unix_connection_4(self):
|
2016-04-13 03:13:19 +00:00
|
|
|
sock = socket.socket(socket.AF_UNIX)
|
2016-04-11 22:07:55 +00:00
|
|
|
sock.close()
|
|
|
|
|
|
|
|
async def client():
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
sock=sock,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
async def runner():
|
|
|
|
with self.assertRaisesRegex(OSError, 'Bad file'):
|
|
|
|
await client()
|
|
|
|
|
|
|
|
self.loop.run_until_complete(runner())
|
|
|
|
|
2016-04-12 17:13:58 +00:00
|
|
|
def test_create_unix_connection_5(self):
|
|
|
|
s1, s2 = socket.socketpair(socket.AF_UNIX)
|
|
|
|
|
|
|
|
excs = []
|
|
|
|
|
|
|
|
class Proto(asyncio.Protocol):
|
|
|
|
def connection_lost(self, exc):
|
|
|
|
excs.append(exc)
|
|
|
|
|
|
|
|
proto = Proto()
|
|
|
|
|
|
|
|
async def client():
|
|
|
|
t, _ = await self.loop.create_unix_connection(
|
|
|
|
lambda: proto,
|
|
|
|
None,
|
|
|
|
sock=s2)
|
|
|
|
|
|
|
|
t.write(b'AAAAA')
|
|
|
|
s1.close()
|
|
|
|
t.write(b'AAAAA')
|
2016-04-12 17:26:42 +00:00
|
|
|
await asyncio.sleep(0.1, loop=self.loop)
|
2016-04-12 17:13:58 +00:00
|
|
|
|
|
|
|
self.loop.run_until_complete(client())
|
|
|
|
|
|
|
|
self.assertEqual(len(excs), 1)
|
|
|
|
self.assertIn(excs[0].__class__,
|
2016-05-07 15:05:33 +00:00
|
|
|
(BrokenPipeError, ConnectionResetError))
|
2016-04-12 17:13:58 +00:00
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
def test_transport_unclosed_warning(self):
|
2016-04-13 01:00:32 +00:00
|
|
|
async def test(sock):
|
2016-10-21 23:01:40 +00:00
|
|
|
return await self.loop.create_unix_connection(
|
2016-04-13 01:00:32 +00:00
|
|
|
asyncio.Protocol,
|
|
|
|
None,
|
|
|
|
sock=sock)
|
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
with self.assertWarnsRegex(ResourceWarning, 'unclosed'):
|
|
|
|
s1, s2 = socket.socketpair(socket.AF_UNIX)
|
|
|
|
with s1, s2:
|
|
|
|
self.loop.run_until_complete(test(s1))
|
|
|
|
self.loop.close()
|
2016-07-05 21:10:29 +00:00
|
|
|
|
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
class Test_UV_Unix(_TestUnix, tb.UVTestCase):
|
2016-04-13 01:00:32 +00:00
|
|
|
|
2017-11-17 23:24:45 +00:00
|
|
|
@unittest.skipUnless(hasattr(os, 'fspath'), 'no os.fspath()')
|
|
|
|
def test_create_unix_connection_pathlib(self):
|
|
|
|
async def run(addr):
|
|
|
|
t, _ = await self.loop.create_unix_connection(
|
|
|
|
asyncio.Protocol, addr)
|
|
|
|
t.close()
|
|
|
|
|
|
|
|
with self.unix_server(lambda sock: time.sleep(0.01)) as srv:
|
|
|
|
addr = pathlib.Path(srv.addr)
|
|
|
|
self.loop.run_until_complete(run(addr))
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(os, 'fspath'), 'no os.fspath()')
|
|
|
|
def test_create_unix_server_pathlib(self):
|
|
|
|
with self.unix_sock_name() as srv_path:
|
|
|
|
srv_path = pathlib.Path(srv_path)
|
|
|
|
srv = self.loop.run_until_complete(
|
|
|
|
self.loop.create_unix_server(asyncio.Protocol, srv_path))
|
|
|
|
srv.close()
|
|
|
|
self.loop.run_until_complete(srv.wait_closed())
|
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
def test_transport_fromsock_get_extra_info(self):
|
|
|
|
# This tests is only for uvloop. asyncio should pass it
|
|
|
|
# too in Python 3.6.
|
2016-04-13 01:00:32 +00:00
|
|
|
|
|
|
|
async def test(sock):
|
2016-10-21 23:01:40 +00:00
|
|
|
t, _ = await self.loop.create_unix_connection(
|
2016-04-13 01:00:32 +00:00
|
|
|
asyncio.Protocol,
|
|
|
|
sock=sock)
|
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
sock = t.get_extra_info('socket')
|
|
|
|
self.assertIs(t.get_extra_info('socket'), sock)
|
2016-04-13 01:00:32 +00:00
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
with self.assertRaisesRegex(RuntimeError, 'is used by transport'):
|
|
|
|
self.loop.add_writer(sock.fileno(), lambda: None)
|
|
|
|
with self.assertRaisesRegex(RuntimeError, 'is used by transport'):
|
|
|
|
self.loop.remove_writer(sock.fileno())
|
2016-04-02 19:17:57 +00:00
|
|
|
|
2016-10-21 23:01:40 +00:00
|
|
|
t.close()
|
|
|
|
|
|
|
|
s1, s2 = socket.socketpair(socket.AF_UNIX)
|
|
|
|
with s1, s2:
|
|
|
|
self.loop.run_until_complete(test(s1))
|
2016-04-02 19:17:57 +00:00
|
|
|
|
2016-11-09 16:17:08 +00:00
|
|
|
def test_create_unix_server_path_dgram(self):
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
|
|
|
with sock:
|
2017-11-17 23:24:45 +00:00
|
|
|
coro = self.loop.create_unix_server(lambda: None,
|
2016-11-09 16:17:08 +00:00
|
|
|
sock=sock)
|
|
|
|
with self.assertRaisesRegex(ValueError,
|
|
|
|
'A UNIX Domain Stream.*was expected'):
|
|
|
|
self.loop.run_until_complete(coro)
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'),
|
|
|
|
'no socket.SOCK_NONBLOCK (linux only)')
|
|
|
|
def test_create_unix_server_path_stream_bittype(self):
|
|
|
|
sock = socket.socket(
|
|
|
|
socket.AF_UNIX, socket.SOCK_STREAM | socket.SOCK_NONBLOCK)
|
|
|
|
with tempfile.NamedTemporaryFile() as file:
|
|
|
|
fn = file.name
|
|
|
|
try:
|
|
|
|
with sock:
|
|
|
|
sock.bind(fn)
|
|
|
|
coro = self.loop.create_unix_server(lambda: None, path=None,
|
|
|
|
sock=sock)
|
|
|
|
srv = self.loop.run_until_complete(coro)
|
|
|
|
srv.close()
|
|
|
|
self.loop.run_until_complete(srv.wait_closed())
|
|
|
|
finally:
|
|
|
|
os.unlink(fn)
|
|
|
|
|
2016-04-02 19:17:57 +00:00
|
|
|
|
|
|
|
class Test_AIO_Unix(_TestUnix, tb.AIOTestCase):
|
|
|
|
pass
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _TestSSL(tb.SSLTestCase):
|
|
|
|
|
2017-09-11 23:44:19 +00:00
|
|
|
ONLYCERT = tb._cert_fullname(__file__, 'ssl_cert.pem')
|
|
|
|
ONLYKEY = tb._cert_fullname(__file__, 'ssl_key.pem')
|
|
|
|
|
2016-04-13 21:56:02 +00:00
|
|
|
def test_create_unix_server_ssl_1(self):
|
|
|
|
CNT = 0 # number of clients that were successful
|
|
|
|
TOTAL_CNT = 25 # total number of clients that test will create
|
|
|
|
TIMEOUT = 5.0 # timeout for this test
|
|
|
|
|
|
|
|
A_DATA = b'A' * 1024 * 1024
|
|
|
|
B_DATA = b'B' * 1024 * 1024
|
|
|
|
|
|
|
|
sslctx = self._create_server_ssl_context(self.ONLYCERT, self.ONLYKEY)
|
|
|
|
client_sslctx = self._create_client_ssl_context()
|
|
|
|
|
|
|
|
clients = []
|
|
|
|
|
|
|
|
async def handle_client(reader, writer):
|
|
|
|
nonlocal CNT
|
|
|
|
|
|
|
|
data = await reader.readexactly(len(A_DATA))
|
|
|
|
self.assertEqual(data, A_DATA)
|
|
|
|
writer.write(b'OK')
|
|
|
|
|
|
|
|
data = await reader.readexactly(len(B_DATA))
|
|
|
|
self.assertEqual(data, B_DATA)
|
|
|
|
writer.writelines([b'SP', bytearray(b'A'), memoryview(b'M')])
|
|
|
|
|
|
|
|
await writer.drain()
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
async def test_client(addr):
|
|
|
|
fut = asyncio.Future(loop=self.loop)
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
def prog(sock):
|
2016-04-13 21:56:02 +00:00
|
|
|
try:
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.starttls(client_sslctx)
|
|
|
|
|
|
|
|
sock.connect(addr)
|
|
|
|
sock.send(A_DATA)
|
2016-04-13 21:56:02 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
data = sock.recv_all(2)
|
2016-04-13 21:56:02 +00:00
|
|
|
self.assertEqual(data, b'OK')
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.send(B_DATA)
|
|
|
|
data = sock.recv_all(4)
|
2016-04-13 21:56:02 +00:00
|
|
|
self.assertEqual(data, b'SPAM')
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.close()
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
self.loop.call_soon_threadsafe(fut.set_exception, ex)
|
|
|
|
else:
|
|
|
|
self.loop.call_soon_threadsafe(fut.set_result, None)
|
|
|
|
|
2017-11-17 22:58:38 +00:00
|
|
|
client = self.unix_client(prog)
|
2016-04-13 21:56:02 +00:00
|
|
|
client.start()
|
|
|
|
clients.append(client)
|
|
|
|
|
|
|
|
await fut
|
|
|
|
|
|
|
|
async def start_server():
|
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
|
|
sock_name = os.path.join(td, 'sock')
|
|
|
|
|
|
|
|
srv = await asyncio.start_unix_server(
|
|
|
|
handle_client,
|
|
|
|
sock_name,
|
|
|
|
ssl=sslctx,
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
try:
|
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(test_client(sock_name))
|
|
|
|
|
|
|
|
await asyncio.wait_for(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop),
|
|
|
|
TIMEOUT, loop=self.loop)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
self.loop.call_soon(srv.close)
|
|
|
|
await srv.wait_closed()
|
|
|
|
|
2016-12-24 00:35:08 +00:00
|
|
|
try:
|
|
|
|
with self._silence_eof_received_warning():
|
|
|
|
self.loop.run_until_complete(start_server())
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
if os.environ.get('TRAVIS_OS_NAME') == 'osx':
|
|
|
|
# XXX: figure out why this fails on macOS on Travis
|
|
|
|
raise unittest.SkipTest('unexplained error on Travis macOS')
|
|
|
|
else:
|
|
|
|
raise
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
|
|
|
|
|
|
|
for client in clients:
|
|
|
|
client.stop()
|
|
|
|
|
2016-04-13 23:57:03 +00:00
|
|
|
def test_create_unix_connection_ssl_1(self):
|
|
|
|
CNT = 0
|
|
|
|
TOTAL_CNT = 25
|
|
|
|
|
|
|
|
A_DATA = b'A' * 1024 * 1024
|
|
|
|
B_DATA = b'B' * 1024 * 1024
|
|
|
|
|
|
|
|
sslctx = self._create_server_ssl_context(self.ONLYCERT, self.ONLYKEY)
|
|
|
|
client_sslctx = self._create_client_ssl_context()
|
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
def server(sock):
|
|
|
|
sock.starttls(sslctx, server_side=True)
|
2016-04-13 23:57:03 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
data = sock.recv_all(len(A_DATA))
|
2016-04-13 23:57:03 +00:00
|
|
|
self.assertEqual(data, A_DATA)
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.send(b'OK')
|
2016-04-13 23:57:03 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
data = sock.recv_all(len(B_DATA))
|
2016-04-13 23:57:03 +00:00
|
|
|
self.assertEqual(data, B_DATA)
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.send(b'SPAM')
|
2016-04-13 23:57:03 +00:00
|
|
|
|
2017-11-17 22:42:31 +00:00
|
|
|
sock.close()
|
2016-04-13 23:57:03 +00:00
|
|
|
|
|
|
|
async def client(addr):
|
|
|
|
reader, writer = await asyncio.open_unix_connection(
|
|
|
|
addr,
|
|
|
|
ssl=client_sslctx,
|
|
|
|
server_hostname='',
|
|
|
|
loop=self.loop)
|
|
|
|
|
|
|
|
writer.write(A_DATA)
|
|
|
|
self.assertEqual(await reader.readexactly(2), b'OK')
|
|
|
|
|
|
|
|
writer.write(B_DATA)
|
|
|
|
self.assertEqual(await reader.readexactly(4), b'SPAM')
|
|
|
|
|
|
|
|
nonlocal CNT
|
|
|
|
CNT += 1
|
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
def run(coro):
|
|
|
|
nonlocal CNT
|
|
|
|
CNT = 0
|
|
|
|
|
2017-11-17 22:58:38 +00:00
|
|
|
with self.unix_server(server,
|
2017-11-17 22:42:31 +00:00
|
|
|
max_clients=TOTAL_CNT,
|
2017-11-17 22:58:38 +00:00
|
|
|
backlog=TOTAL_CNT) as srv:
|
|
|
|
tasks = []
|
|
|
|
for _ in range(TOTAL_CNT):
|
|
|
|
tasks.append(coro(srv.addr))
|
2016-04-13 23:57:03 +00:00
|
|
|
|
2017-11-17 22:58:38 +00:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.gather(*tasks, loop=self.loop))
|
2016-04-13 23:57:03 +00:00
|
|
|
|
|
|
|
self.assertEqual(CNT, TOTAL_CNT)
|
|
|
|
|
|
|
|
with self._silence_eof_received_warning():
|
|
|
|
run(client)
|
|
|
|
|
2016-04-13 21:56:02 +00:00
|
|
|
|
|
|
|
class Test_UV_UnixSSL(_TestSSL, tb.UVTestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Test_AIO_UnixSSL(_TestSSL, tb.AIOTestCase):
|
|
|
|
pass
|