File: test_simple_functions.py

package info (click to toggle)
aioftp 0.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 628 kB
  • sloc: python: 5,574; makefile: 172
file content (261 lines) | stat: -rw-r--r-- 8,154 bytes parent folder | download
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
import asyncio
import datetime
import itertools
import pathlib

import pytest

import aioftp


def test_parse_directory_response():
    s = 'foo "baz "" test nop" """""fdfs """'
    parsed = aioftp.Client.parse_directory_response(s)
    assert parsed == pathlib.PurePosixPath('baz " test nop')


@pytest.mark.asyncio
async def test_connection_del_future():
    loop = asyncio.new_event_loop()
    c = aioftp.Connection(loop=loop)
    c.foo = "bar"
    del c.future.foo


@pytest.mark.asyncio
async def test_connection_not_in_storage():
    loop = asyncio.new_event_loop()
    c = aioftp.Connection(loop=loop)
    with pytest.raises(AttributeError):
        getattr(c, "foo")


def test_available_connections_too_much_acquires():
    ac = aioftp.AvailableConnections(3)
    ac.acquire()
    ac.acquire()
    ac.acquire()
    with pytest.raises(ValueError):
        ac.acquire()


def test_available_connections_too_much_releases():
    ac = aioftp.AvailableConnections(3)
    ac.acquire()
    ac.release()
    with pytest.raises(ValueError):
        ac.release()


def test_parse_pasv_response():
    p = aioftp.Client.parse_pasv_response
    assert p("(192,168,1,0,1,0)") == ("192.168.1.0", 256)


def test_parse_epsv_response():
    p = aioftp.Client.parse_epsv_response
    assert p("some text (ha-ha) (|||665|) ((((666() (|fd667s).") == (None, 666)
    assert p("some text (ha-ha) (|||665|) (6666666).") == (None, 666)


def _c_locale_time(d, format="%b %d %H:%M"):
    with aioftp.common.setlocale("C"):
        return d.strftime(format)


def test_parse_ls_date_of_leap_year():
    def date_to_p(d):
        return d.strftime("%Y%m%d%H%M00")

    p = aioftp.Client.parse_ls_date
    # Leap year date to test
    d = datetime.datetime(year=2000, month=2, day=29)
    current_and_expected_dates = (
        # 2016 (leap)
        (
            datetime.datetime(year=2016, month=2, day=29),
            datetime.datetime(year=2016, month=2, day=29),
        ),
        # 2017
        (
            datetime.datetime(year=2017, month=2, day=28),
            datetime.datetime(year=2016, month=2, day=29),
        ),
        (
            datetime.datetime(year=2017, month=3, day=1),
            datetime.datetime(year=2016, month=2, day=29),
        ),
        # 2018
        (
            datetime.datetime(year=2018, month=2, day=28),
            datetime.datetime(year=2016, month=2, day=29),
        ),
        (
            datetime.datetime(year=2018, month=3, day=1),
            datetime.datetime(year=2020, month=2, day=29),
        ),
        # 2019
        (
            datetime.datetime(year=2019, month=2, day=28),
            datetime.datetime(year=2020, month=2, day=29),
        ),
        (
            datetime.datetime(year=2019, month=3, day=1),
            datetime.datetime(year=2020, month=2, day=29),
        ),
        # 2020 (leap)
        (
            datetime.datetime(year=2020, month=2, day=29),
            datetime.datetime(year=2020, month=2, day=29),
        ),
    )
    for now, expected in current_and_expected_dates:
        assert p(_c_locale_time(d), now=now) == date_to_p(expected)


def test_parse_ls_date_not_older_than_6_month_format():
    def date_to_p(d):
        return d.strftime("%Y%m%d%H%M00")

    p = aioftp.Client.parse_ls_date
    dates = (
        datetime.datetime(year=2002, month=1, day=1),
        datetime.datetime(year=2002, month=12, day=31),
    )
    dt = datetime.timedelta(seconds=15778476 // 2)
    deltas = (datetime.timedelta(), dt, -dt)
    for now, delta in itertools.product(dates, deltas):
        d = now + delta
        assert p(_c_locale_time(d), now=now) == date_to_p(d)


def test_parse_ls_date_older_than_6_month_format():
    def date_to_p(d):
        return d.strftime("%Y%m%d%H%M00")

    p = aioftp.Client.parse_ls_date
    dates = (
        datetime.datetime(year=2002, month=1, day=1),
        datetime.datetime(year=2002, month=12, day=31),
    )
    dt = datetime.timedelta(seconds=15778476, days=30)
    deltas = (dt, -dt)
    for now, delta in itertools.product(dates, deltas):
        d = now + delta
        if delta.total_seconds() > 0:
            expect = date_to_p(d.replace(year=d.year - 1))
        else:
            expect = date_to_p(d.replace(year=d.year + 1))
        assert p(_c_locale_time(d), now=now) == expect


def test_parse_ls_date_short():
    def date_to_p(d):
        return d.strftime("%Y%m%d%H%M00")

    p = aioftp.Client.parse_ls_date
    dates = (
        datetime.datetime(year=2002, month=1, day=1),
        datetime.datetime(year=2002, month=12, day=31),
    )
    for d in dates:
        s = _c_locale_time(d, format="%b %d  %Y")
        assert p(s) == date_to_p(d)


def test_parse_list_line_unix():
    lines = {
        "file": [
            "-rw-rw-r--  1 poh  poh   6595 Feb 27 04:14 history.rst",
            "lrwxrwxrwx  1 poh  poh      6 Mar 23 05:46 link-tmp.py -> tmp.py",
        ],
        "dir": [
            "drw-rw-r--  1 poh  poh   6595 Feb 27 04:14 history.rst",
            "drw-rw-r--  1 poh  poh   6595 Jan 03 2016  changes.rst",
            "drw-rw-r--  1 poh  poh   6595 Mar 10  1996 README.rst",
        ],
        "unknown": [
            "Erw-rw-r--  1 poh  poh   6595 Feb 27 04:14 history.rst",
        ],
    }
    p = aioftp.Client(encoding="utf-8").parse_list_line_unix
    for t, stack in lines.items():
        for line in stack:
            _, parsed = p(line.encode("utf-8"))
            assert parsed["type"] == t
    with pytest.raises(ValueError):
        p(b"-rw-rw-r--  1 poh  poh   6xx5 Feb 27 04:14 history.rst")
    with pytest.raises(ValueError):
        p(b"-rw-rw-r--  xx poh  poh   6595 Feb 27 04:14 history.rst")

    _, parsed = p(b"lrwxrwxrwx  1 poh  poh      6 Mar 23 05:46 link-tmp.py -> tmp.py")
    assert parsed["type"] == "file"
    assert parsed["link_dst"] == "tmp.py"

    _, parsed = p(b"lrwxrwxrwx  1 poh  poh      6 Mar 23 05:46 link-tmp.py -> /tmp.py")
    assert parsed["type"] == "file"
    assert parsed["link_dst"] == "/tmp.py"


@pytest.mark.parametrize("owner", ["s", "x", "-", "E"])
@pytest.mark.parametrize("group", ["s", "x", "-", "E"])
@pytest.mark.parametrize("others", ["t", "x", "-", "E"])
@pytest.mark.parametrize("read", ["r", "-"])
@pytest.mark.parametrize("write", ["w", "-"])
def test_parse_unix_mode(owner, group, others, read, write):
    s = f"{read}{write}{owner}{read}{write}{group}{read}{write}{others}"
    if "E" in {owner, group, others}:
        with pytest.raises(ValueError):
            aioftp.Client.parse_unix_mode(s)
    else:
        assert isinstance(aioftp.Client.parse_unix_mode(s), int)


def test_parse_list_line_failed():
    with pytest.raises(ValueError):
        aioftp.Client(encoding="utf-8").parse_list_line(b"what a hell?!")


def test_reprs_works():
    repr(aioftp.Throttle())
    repr(aioftp.Permission())
    repr(aioftp.User())


def test_throttle_reset():
    t = aioftp.Throttle(limit=1, reset_rate=1)
    t.append(b"-" * 3, 0)
    assert t._start == 0
    assert t._sum == 3
    t.append(b"-" * 3, 2)
    assert t._start == 2
    assert t._sum == 4


def test_permission_is_parent():
    p = aioftp.Permission("/foo/bar")
    assert p.is_parent(pathlib.PurePosixPath("/foo/bar/baz"))
    assert not p.is_parent(pathlib.PurePosixPath("/bar/baz"))


def test_server_mtime_build():
    now = datetime.datetime(year=2002, month=1, day=1).timestamp()
    past = datetime.datetime(year=2001, month=1, day=1).timestamp()
    b = aioftp.Server.build_list_mtime
    assert b(now, now) == "Jan  1 00:00"
    assert b(past, now) == "Jan  1  2001"


@pytest.mark.asyncio
async def test_get_paths_windows_traverse():
    base_path = pathlib.PureWindowsPath("C:\\ftp")
    user = aioftp.User()
    user.base_path = base_path
    connection = aioftp.Connection(current_directory=base_path, user=user)
    virtual_path = pathlib.PurePosixPath("/foo/C:\\windows")
    real_path, resolved_virtual_path = aioftp.Server.get_paths(
        connection,
        virtual_path,
    )
    assert real_path == pathlib.PureWindowsPath("C:/ftp/foo/C:/windows")
    assert resolved_virtual_path == pathlib.PurePosixPath("/foo/C:\\windows")