| 1 |
import asyncio |
| 2 |
import collections |
| 3 |
import contextlib |
| 4 |
import functools |
| 5 |
import math |
| 6 |
import socket |
| 7 |
import ssl |
| 8 |
import tempfile |
| 9 |
import time |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
import pytest |
| 13 |
import pytest_asyncio |
| 14 |
import trustme |
| •15 |
try: |
| •16 |
from asyncio import timeout |
| 17 |
except ImportError: |
| 18 |
from async_timeout import timeout |
| 19 |
try: |
| 20 |
from siosocks.io.asyncio import socks_server_handler |
| 21 |
except ImportError: |
| 22 |
pass |
| 23 |
|
| 24 |
import aioftp |
| 25 |
|
| 26 |
|
| 27 |
ca = trustme.CA() |
| 28 |
server_cert = ca.issue_server_cert("127.0.0.1", "::1") |
| 29 |
|
| 30 |
ssl_server = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) |
| 31 |
server_cert.configure_cert(ssl_server) |
| 32 |
|
| 33 |
ssl_client = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) |
| 34 |
ca.configure_trust(ssl_client) |
| 35 |
|
| 36 |
|
| 37 |
class Container: |
| 38 |
def __init__(self, *args, **kwargs): |
| 39 |
self.args = args |
| 40 |
self.kwargs = kwargs |
| 41 |
|
| 42 |
|
| 43 |
@pytest.fixture |
| 44 |
def Client(): |
| 45 |
return Container |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def Server(): |
| 50 |
return Container |
| 51 |
|
| 52 |
|
| 53 |
def _wrap_with_defaults(kwargs): |
| 54 |
test_defaults = dict( |
| 55 |
path_io_factory=aioftp.MemoryPathIO, |
| 56 |
) |
| 57 |
return collections.ChainMap(kwargs, test_defaults) |
| 58 |
|
| 59 |
|
| 60 |
@pytest.fixture(params=["127.0.0.1", "::1"]) |
| 61 |
def pair_factory(request): |
| 62 |
class Factory: |
| 63 |
def __init__( |
| 64 |
self, |
| 65 |
client=None, |
| 66 |
server=None, |
| 67 |
*, |
| 68 |
connected=True, |
| 69 |
logged=True, |
| 70 |
do_quit=True, |
| 71 |
host=request.param, |
| 72 |
server_factory=aioftp.Server, |
| 73 |
client_factory=aioftp.Client, |
| 74 |
): |
| 75 |
if client is None: |
| 76 |
client = Container() |
| 77 |
self.client = client_factory(*client.args, **_wrap_with_defaults(client.kwargs)) |
| 78 |
if server is None: |
| 79 |
server = Container() |
| 80 |
self.server = server_factory(*server.args, **_wrap_with_defaults(server.kwargs)) |
| 81 |
self.connected = connected |
| 82 |
self.logged = logged |
| 83 |
self.do_quit = do_quit |
| 84 |
self.host = host |
| 85 |
self.timeout = timeout(1) |
| 86 |
|
| 87 |
async def make_server_files(self, *paths, size=None, atom=b"-"): |
| 88 |
if size is None: |
| 89 |
size = aioftp.DEFAULT_BLOCK_SIZE * 3 |
| 90 |
data = atom * size |
| 91 |
for p in paths: |
| 92 |
await self.client.make_directory(Path(p).parent) |
| 93 |
async with self.client.upload_stream(p) as stream: |
| 94 |
await stream.write(data) |
| 95 |
|
| 96 |
async def make_client_files(self, *paths, size=None, atom=b"-"): |
| 97 |
if size is None: |
| 98 |
size = aioftp.DEFAULT_BLOCK_SIZE * 3 |
| 99 |
data = atom * size |
| 100 |
for p in map(Path, paths): |
| 101 |
await self.client.path_io.mkdir( |
| 102 |
p.parent, |
| 103 |
parents=True, |
| 104 |
exist_ok=True, |
| 105 |
) |
| 106 |
async with self.client.path_io.open(p, mode="wb") as f: |
| 107 |
await f.write(data) |
| 108 |
|
| 109 |
async def server_paths_exists(self, *paths): |
| 110 |
values = [] |
| 111 |
for p in paths: |
| 112 |
values.append(await self.client.exists(p)) |
| 113 |
if all(values): |
| 114 |
return True |
| 115 |
if any(values): |
| 116 |
raise ValueError("Mixed exists/not exists list") |
| 117 |
return False |
| 118 |
|
| 119 |
async def client_paths_exists(self, *paths): |
| 120 |
values = [] |
| 121 |
for p in paths: |
| 122 |
values.append(await self.client.path_io.exists(Path(p))) |
| 123 |
if all(values): |
| 124 |
return True |
| 125 |
if any(values): |
| 126 |
raise ValueError("Mixed exists/not exists list") |
| 127 |
return False |
| 128 |
|
| 129 |
async def __aenter__(self): |
| 130 |
await self.timeout.__aenter__() |
| 131 |
await self.server.start(host=self.host) |
| 132 |
if self.connected: |
| 133 |
await self.client.connect( |
| 134 |
self.server.server_host, |
| 135 |
self.server.server_port, |
| 136 |
) |
| 137 |
if self.logged: |
| 138 |
await self.client.login() |
| 139 |
return self |
| 140 |
|
| 141 |
async def __aexit__(self, *exc_info): |
| 142 |
if self.connected and self.do_quit: |
| 143 |
await self.client.quit() |
| 144 |
self.client.close() |
| 145 |
await self.server.close() |
| 146 |
await self.timeout.__aexit__(*exc_info) |
| 147 |
|
| 148 |
return Factory |
| 149 |
|
| 150 |
|
| 151 |
@pytest.fixture |
| 152 |
def expect_codes_in_exception(): |
| 153 |
@contextlib.contextmanager |
| 154 |
def context(*codes): |
| 155 |
try: |
| 156 |
yield |
| 157 |
except aioftp.StatusCodeError as e: |
| 158 |
assert set(e.received_codes) == set(codes) |
| 159 |
else: |
| 160 |
raise RuntimeError("There was no exception") |
| 161 |
|
| 162 |
return context |
| 163 |
|
| 164 |
|
| 165 |
@pytest.fixture( |
| 166 |
params=[ |
| 167 |
aioftp.MemoryPathIO, |
| 168 |
aioftp.PathIO, |
| 169 |
aioftp.AsyncPathIO, |
| 170 |
], |
| 171 |
) |
| 172 |
def path_io(request): |
| 173 |
return request.param() |
| 174 |
|
| 175 |
|
| 176 |
@pytest.fixture |
| 177 |
def temp_dir(path_io): |
| 178 |
if isinstance(path_io, aioftp.MemoryPathIO): |
| 179 |
yield Path("/") |
| 180 |
else: |
| 181 |
with tempfile.TemporaryDirectory() as name: |
| 182 |
yield Path(name) |
| 183 |
|
| 184 |
|
| 185 |
class Sleep: |
| 186 |
def __init__(self): |
| 187 |
self.delay = 0 |
| 188 |
self.first_sleep = None |
| 189 |
|
| 190 |
async def sleep(self, delay, result=None, **kwargs): |
| 191 |
if self.first_sleep is None: |
| 192 |
self.first_sleep = time.monotonic() |
| 193 |
delay = (time.monotonic() - self.first_sleep) + delay |
| 194 |
self.delay = max(self.delay, delay) |
| 195 |
return result |
| 196 |
|
| 197 |
def is_close(self, delay, *, rel_tol=0.05, abs_tol=0.5): |
| 198 |
ok = math.isclose(self.delay, delay, rel_tol=rel_tol, abs_tol=abs_tol) |
| 199 |
if not ok: |
| 200 |
print( |
| 201 |
f"latest sleep: {self.delay}; expected delay: {delay}; rel: {rel_tol}", |
| 202 |
) |
| 203 |
return ok |
| 204 |
|
| 205 |
|
| 206 |
@pytest.fixture |
| 207 |
def skip_sleep(monkeypatch): |
| 208 |
with monkeypatch.context() as m: |
| 209 |
sleeper = Sleep() |
| 210 |
m.setattr(asyncio, "sleep", sleeper.sleep) |
| 211 |
yield sleeper |
| 212 |
|
| 213 |
|
| 214 |
@pytest_asyncio.fixture( |
| 215 |
params=[ |
| 216 |
("127.0.0.1", socket.AF_INET), |
| 217 |
("::1", socket.AF_INET6), |
| 218 |
], |
| 219 |
) |
| 220 |
async def socks(request, unused_tcp_port): |
| 221 |
handler = functools.partial( |
| 222 |
socks_server_handler, |
| 223 |
allowed_versions={5}, |
| 224 |
username="foo", |
| 225 |
password="bar", |
| 226 |
) |
| 227 |
Socks = collections.namedtuple("Socks", "host port server") |
| 228 |
host, family = request.param |
| 229 |
port = unused_tcp_port |
| 230 |
server = await asyncio.start_server( |
| 231 |
handler, |
| 232 |
host=host, |
| 233 |
port=port, |
| 234 |
family=family, |
| 235 |
) |
| 236 |
yield Socks(host, port, server) |
| 237 |
server.close() |
| 238 |
await server.wait_closed() |