| 1 | import contextlib |
| 2 | import pathlib |
| 3 | import textwrap |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | import aioftp |
| 8 | |
| 9 | |
| 10 | async def not_implemented(connection, rest): |
| 11 | connection.response("502", ":P") |
| 12 | return True |
| 13 | |
| 14 | |
| 15 | @pytest.mark.asyncio |
| 16 | async def test_client_fallback_to_list_at_list(pair_factory): |
| 17 | async with pair_factory() as pair: |
| 18 | pair.server.commands_mapping["mlst"] = not_implemented |
| 19 | pair.server.commands_mapping["mlsd"] = not_implemented |
| 20 | await pair.make_server_files("bar/foo") |
| 21 | (path, stat), *_ = files = await pair.client.list() |
| 22 | assert len(files) == 1 |
| 23 | assert path == pathlib.PurePosixPath("bar") |
| 24 | assert stat["type"] == "dir" |
| 25 | (path, stat), *_ = files = await pair.client.list("bar") |
| 26 | assert len(files) == 1 |
| 27 | assert path == pathlib.PurePosixPath("bar/foo") |
| 28 | assert stat["type"] == "file" |
| 29 | result = await pair.client.list("bar/foo") |
| 30 | assert len(result) == 0 |
| 31 | |
| 32 | |
| 33 | async def implemented_badly(connection, rest): |
| 34 | assert False, "should not be called" |
| 35 | |
| 36 | |
| 37 | @pytest.mark.asyncio |
| 38 | async def test_client_list_override(pair_factory): |
| 39 | async with pair_factory() as pair: |
| 40 | pair.server.commands_mapping["mlsd"] = implemented_badly |
| 41 | await pair.client.make_directory("bar") |
| 42 | (path, stat), *_ = files = await pair.client.list(raw_command="LIST") |
| 43 | assert len(files) == 1 |
| 44 | assert path == pathlib.PurePosixPath("bar") |
| 45 | assert stat["type"] == "dir" |
| 46 | |
| 47 | |
| 48 | @pytest.mark.asyncio |
| 49 | async def test_client_list_override_invalid_raw_command(pair_factory): |
| 50 | async with pair_factory() as pair: |
| 51 | with pytest.raises(ValueError): |
| 52 | await pair.client.list(raw_command="FOO") |
| 53 | |
| 54 | |
| 55 | def test_client_list_windows(): |
| 56 | test_str = textwrap.dedent( |
| 57 | """\ |
| 58 | 11/4/2018 9:09 PM <DIR> . |
| 59 | 8/10/2018 1:02 PM <DIR> .. |
| 60 | 9/23/2018 2:16 PM <DIR> bin |
| 61 | 10/16/2018 10:25 PM <DIR> Desktop |
| 62 | 11/4/2018 3:31 PM <DIR> dow |
| 63 | 10/16/2018 8:21 PM <DIR> Downloads |
| 64 | 10/14/2018 5:34 PM <DIR> msc |
| 65 | 9/9/2018 9:32 AM <DIR> opt |
| 66 | 10/3/2018 2:58 PM 34,359,738,368 win10.img |
| 67 | 6/30/2018 8:36 AM 3,939,237,888 win10.iso |
| 68 | 7/26/2018 1:11 PM 189 win10.sh |
| 69 | 10/29/2018 11:46 AM 34,359,738,368 win7.img |
| 70 | 6/30/2018 8:35 AM 3,319,791,616 win7.iso |
| 71 | 10/29/2018 10:55 AM 219 win7.sh |
| 72 | 6 files 75,978,506,648 bytes |
| 73 | 3 directories 22,198,362,112 bytes free |
| 74 | """, |
| 75 | ) |
| 76 | test_str = test_str.strip().split("\n") |
| 77 | entities = {} |
| 78 | parse = aioftp.Client(encoding="utf-8").parse_list_line_windows |
| 79 | for x in test_str: |
| 80 | with contextlib.suppress(ValueError): |
| 81 | path, stat = parse(x.encode("utf-8")) |
| 82 | entities[path] = stat |
| 83 | dirs = ["bin", "Desktop", "dow", "Downloads", "msc", "opt"] |
| 84 | files = [ |
| 85 | "win10.img", |
| 86 | "win10.iso", |
| 87 | "win10.sh", |
| 88 | "win7.img", |
| 89 | "win7.iso", |
| 90 | "win7.sh", |
| 91 | ] |
| 92 | assert len(entities) == len(dirs + files) |
| 93 | for d in dirs: |
| 94 | p = pathlib.PurePosixPath(d) |
| 95 | assert p in entities |
| 96 | assert entities[p]["type"] == "dir" |
| 97 | for f in files: |
| 98 | p = pathlib.PurePosixPath(f) |
| 99 | assert p in entities |
| 100 | assert entities[p]["type"] == "file" |
| 101 | with pytest.raises(ValueError): |
| 102 | parse(b" 10/3/2018 2:58 PM 34,35xxx38,368 win10.img") |
| 103 | |
| 104 | |
| 105 | @pytest.mark.asyncio |
| 106 | async def test_client_list_override_with_custom(pair_factory, Client): |
| 107 | meta = {"type": "file", "works": True} |
| 108 | |
| 109 | def parser(b): |
| 110 | import pickle |
| 111 | |
| 112 | return pickle.loads(bytes.fromhex(b.decode().rstrip("\r\n"))) |
| 113 | |
| 114 | async def builder(_, path): |
| 115 | import pickle |
| 116 | |
| 117 | return pickle.dumps((path, meta)).hex() |
| 118 | |
| 119 | async with pair_factory(Client(parse_list_line_custom=parser)) as pair: |
| 120 | pair.server.commands_mapping["mlst"] = not_implemented |
| 121 | pair.server.commands_mapping["mlsd"] = not_implemented |
| 122 | pair.server.build_list_string = builder |
| 123 | await pair.client.make_directory("bar") |
| 124 | (path, stat), *_ = files = await pair.client.list() |
| 125 | assert len(files) == 1 |
| 126 | assert path == pathlib.PurePosixPath("bar") |
| 127 | assert stat == meta |
| 128 | |
| 129 | |
| 130 | @pytest.mark.asyncio |
| 131 | async def test_client_list_override_with_custom_last(pair_factory, Client): |
| 132 | meta = {"type": "file", "works": True} |
| 133 | |
| 134 | def parser(b): |
| 135 | import pickle |
| 136 | |
| 137 | return pickle.loads(bytes.fromhex(b.decode().rstrip("\r\n"))) |
| 138 | |
| 139 | async def builder(_, path): |
| 140 | import pickle |
| 141 | |
| 142 | return pickle.dumps((path, meta)).hex() |
| 143 | |
| 144 | client = Client( |
| 145 | parse_list_line_custom=parser, |
| 146 | parse_list_line_custom_first=False, |
| 147 | ) |
| 148 | async with pair_factory(client) as pair: |
| 149 | pair.server.commands_mapping["mlst"] = not_implemented |
| 150 | pair.server.commands_mapping["mlsd"] = not_implemented |
| 151 | pair.server.build_list_string = builder |
| 152 | await pair.client.make_directory("bar") |
| 153 | (path, stat), *_ = files = await pair.client.list() |
| 154 | assert len(files) == 1 |
| 155 | assert path == pathlib.PurePosixPath("bar") |
| 156 | assert stat == meta |