| 1 |
import asyncio |
| 2 |
import datetime |
| 3 |
import itertools |
| 4 |
import pathlib |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import aioftp |
| 9 |
|
| 10 |
|
| 11 |
def test_parse_directory_response(): |
| 12 |
s = 'foo "baz "" test nop" """""fdfs """' |
| 13 |
parsed = aioftp.Client.parse_directory_response(s) |
| 14 |
assert parsed == pathlib.PurePosixPath('baz " test nop') |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.asyncio |
| 18 |
async def test_connection_del_future(): |
| 19 |
loop = asyncio.new_event_loop() |
| 20 |
c = aioftp.Connection(loop=loop) |
| 21 |
c.foo = "bar" |
| 22 |
del c.future.foo |
| 23 |
|
| 24 |
|
| 25 |
@pytest.mark.asyncio |
| 26 |
async def test_connection_not_in_storage(): |
| 27 |
loop = asyncio.new_event_loop() |
| 28 |
c = aioftp.Connection(loop=loop) |
| 29 |
with pytest.raises(AttributeError): |
| 30 |
getattr(c, "foo") |
| 31 |
|
| 32 |
|
| 33 |
def test_available_connections_too_much_acquires(): |
| 34 |
ac = aioftp.AvailableConnections(3) |
| 35 |
ac.acquire() |
| 36 |
ac.acquire() |
| 37 |
ac.acquire() |
| 38 |
with pytest.raises(ValueError): |
| 39 |
ac.acquire() |
| 40 |
|
| 41 |
|
| 42 |
def test_available_connections_too_much_releases(): |
| 43 |
ac = aioftp.AvailableConnections(3) |
| 44 |
ac.acquire() |
| 45 |
ac.release() |
| 46 |
with pytest.raises(ValueError): |
| 47 |
ac.release() |
| 48 |
|
| 49 |
|
| 50 |
def test_parse_pasv_response(): |
| 51 |
p = aioftp.Client.parse_pasv_response |
| 52 |
assert p("(192,168,1,0,1,0)") == ("192.168.1.0", 256) |
| 53 |
|
| 54 |
|
| 55 |
def test_parse_epsv_response(): |
| 56 |
p = aioftp.Client.parse_epsv_response |
| 57 |
assert p("some text (ha-ha) (|||665|) ((((666() (|fd667s).") == (None, 666) |
| 58 |
assert p("some text (ha-ha) (|||665|) (6666666).") == (None, 666) |
| 59 |
|
| 60 |
|
| 61 |
def _c_locale_time(d, format="%b %d %H:%M"): |
| 62 |
with aioftp.common.setlocale("C"): |
| 63 |
return d.strftime(format) |
| 64 |
|
| 65 |
|
| 66 |
def test_parse_ls_date_of_leap_year(): |
| 67 |
def date_to_p(d): |
| 68 |
return d.strftime("%Y%m%d%H%M00") |
| 69 |
|
| 70 |
p = aioftp.Client.parse_ls_date |
| 71 |
|
| 72 |
d = datetime.datetime(year=2000, month=2, day=29) |
| 73 |
current_and_expected_dates = ( |
| 74 |
|
| 75 |
( |
| 76 |
datetime.datetime(year=2016, month=2, day=29), |
| 77 |
datetime.datetime(year=2016, month=2, day=29), |
| 78 |
), |
| 79 |
|
| 80 |
( |
| 81 |
datetime.datetime(year=2017, month=2, day=28), |
| 82 |
datetime.datetime(year=2016, month=2, day=29), |
| 83 |
), |
| 84 |
( |
| 85 |
datetime.datetime(year=2017, month=3, day=1), |
| 86 |
datetime.datetime(year=2016, month=2, day=29), |
| 87 |
), |
| 88 |
|
| 89 |
( |
| 90 |
datetime.datetime(year=2018, month=2, day=28), |
| 91 |
datetime.datetime(year=2016, month=2, day=29), |
| 92 |
), |
| 93 |
( |
| 94 |
datetime.datetime(year=2018, month=3, day=1), |
| 95 |
datetime.datetime(year=2020, month=2, day=29), |
| 96 |
), |
| 97 |
|
| 98 |
( |
| 99 |
datetime.datetime(year=2019, month=2, day=28), |
| 100 |
datetime.datetime(year=2020, month=2, day=29), |
| 101 |
), |
| 102 |
( |
| 103 |
datetime.datetime(year=2019, month=3, day=1), |
| 104 |
datetime.datetime(year=2020, month=2, day=29), |
| 105 |
), |
| 106 |
|
| 107 |
( |
| 108 |
datetime.datetime(year=2020, month=2, day=29), |
| 109 |
datetime.datetime(year=2020, month=2, day=29), |
| 110 |
), |
| 111 |
) |
| 112 |
for now, expected in current_and_expected_dates: |
| 113 |
assert p(_c_locale_time(d), now=now) == date_to_p(expected) |
| 114 |
|
| 115 |
|
| 116 |
def test_parse_ls_date_not_older_than_6_month_format(): |
| 117 |
def date_to_p(d): |
| 118 |
return d.strftime("%Y%m%d%H%M00") |
| 119 |
|
| 120 |
p = aioftp.Client.parse_ls_date |
| 121 |
dates = ( |
| 122 |
datetime.datetime(year=2002, month=1, day=1), |
| 123 |
datetime.datetime(year=2002, month=12, day=31), |
| 124 |
) |
| 125 |
dt = datetime.timedelta(seconds=15778476 // 2) |
| 126 |
deltas = (datetime.timedelta(), dt, -dt) |
| 127 |
for now, delta in itertools.product(dates, deltas): |
| 128 |
d = now + delta |
| 129 |
assert p(_c_locale_time(d), now=now) == date_to_p(d) |
| 130 |
|
| 131 |
|
| 132 |
def test_parse_ls_date_older_than_6_month_format(): |
| 133 |
def date_to_p(d): |
| 134 |
return d.strftime("%Y%m%d%H%M00") |
| 135 |
|
| 136 |
p = aioftp.Client.parse_ls_date |
| 137 |
dates = ( |
| 138 |
datetime.datetime(year=2002, month=1, day=1), |
| 139 |
datetime.datetime(year=2002, month=12, day=31), |
| 140 |
) |
| 141 |
dt = datetime.timedelta(seconds=15778476, days=30) |
| 142 |
deltas = (dt, -dt) |
| 143 |
for now, delta in itertools.product(dates, deltas): |
| 144 |
d = now + delta |
| 145 |
if delta.total_seconds() > 0: |
| 146 |
expect = date_to_p(d.replace(year=d.year - 1)) |
| 147 |
else: |
| 148 |
expect = date_to_p(d.replace(year=d.year + 1)) |
| 149 |
assert p(_c_locale_time(d), now=now) == expect |
| 150 |
|
| 151 |
|
| 152 |
def test_parse_ls_date_short(): |
| 153 |
def date_to_p(d): |
| 154 |
return d.strftime("%Y%m%d%H%M00") |
| 155 |
|
| 156 |
p = aioftp.Client.parse_ls_date |
| 157 |
dates = ( |
| 158 |
datetime.datetime(year=2002, month=1, day=1), |
| 159 |
datetime.datetime(year=2002, month=12, day=31), |
| 160 |
) |
| 161 |
for d in dates: |
| 162 |
s = _c_locale_time(d, format="%b %d %Y") |
| 163 |
assert p(s) == date_to_p(d) |
| 164 |
|
| 165 |
|
| 166 |
def test_parse_list_line_unix(): |
| 167 |
lines = { |
| 168 |
"file": [ |
| 169 |
"-rw-rw-r-- 1 poh poh 6595 Feb 27 04:14 history.rst", |
| 170 |
"lrwxrwxrwx 1 poh poh 6 Mar 23 05:46 link-tmp.py -> tmp.py", |
| 171 |
], |
| 172 |
"dir": [ |
| 173 |
"drw-rw-r-- 1 poh poh 6595 Feb 27 04:14 history.rst", |
| 174 |
"drw-rw-r-- 1 poh poh 6595 Jan 03 2016 changes.rst", |
| 175 |
"drw-rw-r-- 1 poh poh 6595 Mar 10 1996 README.rst", |
| 176 |
], |
| 177 |
"unknown": [ |
| 178 |
"Erw-rw-r-- 1 poh poh 6595 Feb 27 04:14 history.rst", |
| 179 |
], |
| 180 |
} |
| 181 |
p = aioftp.Client(encoding="utf-8").parse_list_line_unix |
| 182 |
for t, stack in lines.items(): |
| 183 |
for line in stack: |
| 184 |
_, parsed = p(line.encode("utf-8")) |
| 185 |
assert parsed["type"] == t |
| 186 |
with pytest.raises(ValueError): |
| 187 |
p(b"-rw-rw-r-- 1 poh poh 6xx5 Feb 27 04:14 history.rst") |
| 188 |
with pytest.raises(ValueError): |
| 189 |
p(b"-rw-rw-r-- xx poh poh 6595 Feb 27 04:14 history.rst") |
| 190 |
|
| 191 |
_, parsed = p(b"lrwxrwxrwx 1 poh poh 6 Mar 23 05:46 link-tmp.py -> tmp.py") |
| 192 |
assert parsed["type"] == "file" |
| 193 |
assert parsed["link_dst"] == "tmp.py" |
| 194 |
|
| 195 |
_, parsed = p(b"lrwxrwxrwx 1 poh poh 6 Mar 23 05:46 link-tmp.py -> /tmp.py") |
| 196 |
assert parsed["type"] == "file" |
| 197 |
assert parsed["link_dst"] == "/tmp.py" |
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.parametrize("owner", ["s", "x", "-", "E"]) |
| 201 |
@pytest.mark.parametrize("group", ["s", "x", "-", "E"]) |
| 202 |
@pytest.mark.parametrize("others", ["t", "x", "-", "E"]) |
| 203 |
@pytest.mark.parametrize("read", ["r", "-"]) |
| 204 |
@pytest.mark.parametrize("write", ["w", "-"]) |
| 205 |
def test_parse_unix_mode(owner, group, others, read, write): |
| 206 |
s = f"{read}{write}{owner}{read}{write}{group}{read}{write}{others}" |
| 207 |
if "E" in {owner, group, others}: |
| 208 |
with pytest.raises(ValueError): |
| 209 |
aioftp.Client.parse_unix_mode(s) |
| 210 |
else: |
| 211 |
assert isinstance(aioftp.Client.parse_unix_mode(s), int) |
| 212 |
|
| 213 |
|
| 214 |
def test_parse_list_line_failed(): |
| 215 |
with pytest.raises(ValueError): |
| 216 |
aioftp.Client(encoding="utf-8").parse_list_line(b"what a hell?!") |
| 217 |
|
| 218 |
|
| 219 |
def test_reprs_works(): |
| 220 |
repr(aioftp.Throttle()) |
| 221 |
repr(aioftp.Permission()) |
| 222 |
repr(aioftp.User()) |
| 223 |
|
| 224 |
|
| 225 |
def test_throttle_reset(): |
| 226 |
t = aioftp.Throttle(limit=1, reset_rate=1) |
| 227 |
t.append(b"-" * 3, 0) |
| 228 |
assert t._start == 0 |
| 229 |
assert t._sum == 3 |
| 230 |
t.append(b"-" * 3, 2) |
| 231 |
assert t._start == 2 |
| 232 |
assert t._sum == 4 |
| 233 |
|
| 234 |
|
| 235 |
def test_permission_is_parent(): |
| 236 |
p = aioftp.Permission("/foo/bar") |
| 237 |
assert p.is_parent(pathlib.PurePosixPath("/foo/bar/baz")) |
| 238 |
assert not p.is_parent(pathlib.PurePosixPath("/bar/baz")) |
| 239 |
|
| 240 |
|
| 241 |
def test_server_mtime_build(): |
| 242 |
now = datetime.datetime(year=2002, month=1, day=1).timestamp() |
| 243 |
past = datetime.datetime(year=2001, month=1, day=1).timestamp() |
| 244 |
b = aioftp.Server.build_list_mtime |
| 245 |
assert b(now, now) == "Jan 1 00:00" |
| 246 |
assert b(past, now) == "Jan 1 2001" |
| 247 |
|
| 248 |
|
| 249 |
@pytest.mark.asyncio |
| 250 |
async def test_get_paths_windows_traverse(): |
| 251 |
base_path = pathlib.PureWindowsPath("C:\\ftp") |
| 252 |
user = aioftp.User() |
| 253 |
user.base_path = base_path |
| 254 |
connection = aioftp.Connection(current_directory=base_path, user=user) |
| 255 |
virtual_path = pathlib.PurePosixPath("/foo/C:\\windows") |
| 256 |
real_path, resolved_virtual_path = aioftp.Server.get_paths( |
| 257 |
connection, |
| 258 |
virtual_path, |
| 259 |
) |
| 260 |
assert real_path == pathlib.PureWindowsPath("C:/ftp/foo/C:/windows") |
| 261 |
assert resolved_virtual_path == pathlib.PurePosixPath("/foo/C:\\windows") |