From 3550877eecd8b3ce36257d1c9afa005accc23532 Mon Sep 17 00:00:00 2001 From: Nikita Melentev Date: Thu, 14 Mar 2024 13:54:12 +0300 Subject: [PATCH] tests: fix ipv4 override test (#175) * tests: fix ipv4 override test * fix linters --- src/aioftp/__init__.py | 1 + src/aioftp/__main__.py | 1 + tests/test_connection.py | 24 ++++++++++++++---------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/aioftp/__init__.py b/src/aioftp/__init__.py index d992bd8..15235af 100644 --- a/src/aioftp/__init__.py +++ b/src/aioftp/__init__.py @@ -1,4 +1,5 @@ """ftp client/server for asyncio""" + # flake8: noqa import importlib.metadata diff --git a/src/aioftp/__main__.py b/src/aioftp/__main__.py index a09b0b0..3b7db1d 100644 --- a/src/aioftp/__main__.py +++ b/src/aioftp/__main__.py @@ -1,4 +1,5 @@ """Simple aioftp-based server with one user (anonymous or not)""" + import argparse import asyncio import contextlib diff --git a/tests/test_connection.py b/tests/test_connection.py index 27cb9e5..1515a6e 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -129,11 +129,7 @@ async def test_pasv_connection_port_reused( @pytest.mark.asyncio -async def test_pasv_connection_pasv_forced_response_address( - pair_factory, - Server, - unused_tcp_port, -): +async def test_pasv_connection_pasv_forced_response_address(pair_factory, Server): def ipv4_used(): try: ipaddress.IPv4Address(pair.host) @@ -141,21 +137,29 @@ def ipv4_used(): except ValueError: return False + # using TEST-NET-1 address + ipv4_address = "192.0.2.1" async with pair_factory( - server=Server(ipv4_pasv_forced_response_address="127.0.0.2"), + server=Server(ipv4_pasv_forced_response_address=ipv4_address), ) as pair: - assert pair.server.ipv4_pasv_forced_response_address == "127.0.0.2" + assert pair.server.ipv4_pasv_forced_response_address == ipv4_address if ipv4_used(): - # The connection fails here because the server starts to listen for + # The connection should fail here because the server starts to listen for # the passive connections on the host (IPv4 address) that is used # by the control channel. In reality, if the server is behind NAT, # the server is reached with the defined external IPv4 address, # i.e. we can check that the connection to # pair.server.ipv4_pasv_forced_response_address failed to know that # the server returned correct external IP - with pytest.raises(OSError): - await pair.client.get_passive_connection(commands=["pasv"]) + # ... + # but we can't use check like this: + # with pytest.raises(OSError): + # await pair.client.get_passive_connection(commands=["pasv"]) + # because there is no such ipv4 which will be non-routable, so + # we can only check `PASV` response + ip, _ = await pair.client._do_pasv() + assert ip == ipv4_address # With epsv the connection should open as that does not use the # external IPv4 address but just tells the client the port to connect