| 1 | from pathlib import Path |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import aioftp |
| 6 | |
| 7 | |
| 8 | @pytest.mark.asyncio |
| 9 | async def test_server_side_exception(pair_factory): |
| 10 | class CustomServer(aioftp.Server): |
| 11 | def __init__(self, *args, **kwargs): |
| 12 | super().__init__(*args, **kwargs) |
| 13 | self.commands_mapping["custom"] = self.custom |
| 14 | |
| 15 | async def custom(*args, **kwargs): |
| 16 | raise RuntimeError("Test error") |
| 17 | |
| 18 | factory = pair_factory(server_factory=CustomServer, do_quit=False) |
| 19 | async with factory as pair: |
| 20 | with pytest.raises(ConnectionResetError): |
| 21 | await pair.client.command("custom", "200") |
| 22 | |
| 23 | |
| 24 | @pytest.mark.asyncio |
| 25 | async def test_bad_type_value(pair_factory, expect_codes_in_exception): |
| 26 | async with pair_factory() as pair: |
| 27 | with expect_codes_in_exception("502"): |
| 28 | await pair.client.command("type FOO", "200") |
| 29 | |
| 30 | |
| 31 | @pytest.mark.asyncio |
| 32 | async def test_pbsz(pair_factory): |
| 33 | async with pair_factory() as pair: |
| 34 | await pair.client.command("pbsz", "200") |
| 35 | |
| 36 | |
| 37 | @pytest.mark.asyncio |
| 38 | async def test_prot(pair_factory, expect_codes_in_exception): |
| 39 | async with pair_factory() as pair: |
| 40 | await pair.client.command("prot P", "200") |
| 41 | with expect_codes_in_exception("502"): |
| 42 | await pair.client.command("prot foo", "200") |
| 43 | |
| 44 | |
| 45 | @pytest.mark.asyncio |
| 46 | async def test_server_ipv6_pasv(pair_factory, expect_codes_in_exception): |
| 47 | async with pair_factory(host="::1", do_quit=False) as pair: |
| 48 | with expect_codes_in_exception("503"): |
| 49 | await pair.client.get_passive_connection(commands=["pasv"]) |
| 50 | |
| 51 | |
| 52 | @pytest.mark.asyncio |
| 53 | async def test_epsv_extra_arg(pair_factory, expect_codes_in_exception): |
| 54 | async with pair_factory(do_quit=False) as pair: |
| 55 | with expect_codes_in_exception("522"): |
| 56 | await pair.client.command("epsv foo", "229") |
| 57 | |
| 58 | |
| 59 | @pytest.mark.asyncio |
| 60 | async def test_bad_server_path_io( |
| 61 | pair_factory, |
| 62 | Server, |
| 63 | expect_codes_in_exception, |
| 64 | ): |
| 65 | class BadPathIO(aioftp.MemoryPathIO): |
| 66 | async def is_file(*a, **kw): |
| 67 | return False |
| 68 | |
| 69 | async def is_dir(*a, **kw): |
| 70 | return False |
| 71 | |
| 72 | s = Server(path_io_factory=BadPathIO) |
| 73 | async with pair_factory(None, s) as pair: |
| 74 | pio = pair.server.path_io_factory() |
| 75 | async with pio.open(Path("/foo"), "wb"): |
| 76 | pass |
| 77 | await pair.client.list() |