| 1 | import pytest |
| 2 | |
| 3 | |
| 4 | async def not_implemented(connection, rest): |
| 5 | connection.response("502", ":P") |
| 6 | return True |
| 7 | |
| 8 | |
| 9 | @pytest.mark.asyncio |
| 10 | async def test_client_fallback_to_pasv_at_list(pair_factory): |
| 11 | async with pair_factory(host="127.0.0.1") as pair: |
| 12 | pair.server.epsv = not_implemented |
| 13 | await pair.client.list() |
| 14 | |
| 15 | |
| 16 | @pytest.mark.asyncio |
| 17 | async def test_client_fail_fallback_to_pasv_at_list( |
| 18 | pair_factory, |
| 19 | expect_codes_in_exception, |
| 20 | ): |
| 21 | async with pair_factory(host="127.0.0.1") as pair: |
| 22 | pair.server.commands_mapping["epsv"] = not_implemented |
| 23 | with expect_codes_in_exception("502"): |
| 24 | await pair.client.get_passive_connection(commands=["epsv"]) |
| 25 | with expect_codes_in_exception("502"): |
| 26 | pair.client._passive_commands = ["epsv"] |
| 27 | await pair.client.get_passive_connection() |
| 28 | |
| 29 | |
| 30 | @pytest.mark.asyncio |
| 31 | async def test_client_only_passive_list(pair_factory): |
| 32 | async with pair_factory(host="127.0.0.1") as pair: |
| 33 | pair.client._passive_commands = ["pasv"] |
| 34 | await pair.client.list() |
| 35 | |
| 36 | |
| 37 | @pytest.mark.asyncio |
| 38 | async def test_client_only_enhanced_passive_list(pair_factory): |
| 39 | async with pair_factory(host="127.0.0.1") as pair: |
| 40 | pair.client._passive_commands = ["epsv"] |
| 41 | await pair.client.list() |
| 42 | |
| 43 | |
| 44 | @pytest.mark.asyncio |
| 45 | async def test_passive_no_choices(pair_factory): |
| 46 | async with pair_factory() as pair: |
| 47 | pair.client._passive_commands = [] |
| 48 | with pytest.raises(ValueError): |
| 49 | await pair.client.get_passive_connection(commands=[]) |
| 50 | |
| 51 | |
| 52 | @pytest.mark.asyncio |
| 53 | async def test_passive_bad_choices(pair_factory): |
| 54 | async with pair_factory() as pair: |
| 55 | pair.server.epsv = not_implemented |
| 56 | with pytest.raises(ValueError): |
| 57 | await pair.client.get_passive_connection(commands=["FOO"]) |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize("method", [("pasv", "227"), ("epsv", "229")]) |
| 61 | @pytest.mark.asyncio |
| 62 | async def test_passive_multicall(pair_factory, method): |
| 63 | async with pair_factory(host="127.0.0.1") as pair: |
| 64 | code, info = await pair.client.command(*method) |
| 65 | assert "created" in info[0] |
| 66 | code, info = await pair.client.command(*method) |
| 67 | assert "exists" in info[0] |
| 68 | |
| 69 | |
| 70 | @pytest.mark.parametrize("method", ["pasv", "epsv"]) |
| 71 | @pytest.mark.asyncio |
| 72 | async def test_passive_closed_on_recall(pair_factory, method): |
| 73 | async with pair_factory(host="127.0.0.1") as pair: |
| 74 | r, w = await pair.client.get_passive_connection(commands=[method]) |
| 75 | nr, nw = await pair.client.get_passive_connection(commands=[method]) |
| 76 | with pytest.raises((ConnectionResetError, BrokenPipeError)): |
| 77 | while True: |
| 78 | w.write(b"-") |
| 79 | await w.drain() |