diff --git a/krater/compute/ssh_reach.py b/krater/compute/ssh_reach.py index 12c4008..636dae9 100644 --- a/krater/compute/ssh_reach.py +++ b/krater/compute/ssh_reach.py @@ -45,6 +45,8 @@ def probe_tcp(host: str, port: int) -> bool | None: """`True` when a TCP connection to `host:port` opens, `False` when it is refused, `None` with no answer.""" + if not 1 <= port <= 65535: + return None # Linux would take it modulo 65536 and test another port try: with socket.create_connection((host, port), timeout=CONNECT_TIMEOUT_SECONDS): return True diff --git a/tests/compute/test_ssh_reach.py b/tests/compute/test_ssh_reach.py index f6d268f..c1a0f16 100644 --- a/tests/compute/test_ssh_reach.py +++ b/tests/compute/test_ssh_reach.py @@ -269,3 +269,12 @@ def test_a_proxy_that_changes_host_keeps_the_box_off_the_dead_port(db_session, m _checked_pass(db_session, client, _Check(True), 12) # quiet runner: untested assert row.ssh_host == "ssh7.vast.ai" + + +def test_a_timeout_is_no_answer(monkeypatch) -> None: + def times_out(*args, **kwargs): + raise TimeoutError + + monkeypatch.setattr(ssh_reach.socket, "create_connection", times_out) + + assert ssh_reach.probe_tcp("38.114.93.198", 20056) is None