Skip to content

Commit b1b4e54

Browse files
Merge pull request #3611 from VWS-Python/cleanup-untyped-runtime-boundaries
Narrow local socket addresses before using their ports
2 parents 2f6f4d6 + 7771186 commit b1b4e54

4 files changed

Lines changed: 26 additions & 37 deletions

File tree

tests/mock_vws/test_docker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ def _free_port() -> int:
185185
type=socket.SOCK_STREAM,
186186
) as sock:
187187
sock.bind(("127.0.0.1", 0))
188-
return int(sock.getsockname()[1]) # pyrefly: ignore [unknown-argument-type]
188+
address = sock.getsockname()
189+
assert isinstance(address, tuple)
190+
assert isinstance(address[1], int)
191+
port: int = address[1]
192+
return port
189193

190194

191195
@beartype

tests/mock_vws/test_healthcheck.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ def _unused_port() -> int:
4545
type=socket.SOCK_STREAM,
4646
) as sock:
4747
sock.bind(("localhost", 0))
48-
# The socket stubs cannot specialize this result from the configured
49-
# IPv4 address family, although the second tuple item is always a port.
50-
port: int = sock.getsockname()[1] # ty: ignore[unsound-assignment]
48+
address = sock.getsockname()
49+
assert isinstance(address, tuple)
50+
assert isinstance(address[1], int)
51+
port: int = address[1]
5152
return port
5253

5354

tests/mock_vws/test_httpx2_mock_usage.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ def _unused_local_url() -> str:
7878
Returns:
7979
The URL of a port which was free when this was called.
8080
"""
81-
sock = socket.socket()
82-
sock.bind(("", 0))
83-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
84-
sock.close()
81+
with socket.socket() as sock:
82+
sock.bind(("", 0))
83+
address = sock.getsockname()
84+
assert isinstance(address, tuple)
85+
assert isinstance(address[1], int)
86+
port: int = address[1]
8587
return f"http://localhost:{port}"
8688

8789

tests/mock_vws/test_requests_mock_usage.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ def _bool_matcher(
124124
@beartype
125125
def _unused_local_url() -> str:
126126
"""Return a URL for a local address with nothing listening on it."""
127-
sock = socket.socket()
128-
sock.bind(("", 0))
129-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
130-
sock.close()
127+
with socket.socket() as sock:
128+
sock.bind(("", 0))
129+
address = sock.getsockname()
130+
assert isinstance(address, tuple)
131+
assert isinstance(address[1], int)
132+
port: int = address[1]
131133
return f"http://localhost:{port}"
132134

133135

@@ -142,11 +144,7 @@ def request_unmocked_address() -> None:
142144
context of a ``responses`` mock which does not mock local
143145
addresses.
144146
"""
145-
sock = socket.socket()
146-
sock.bind(("", 0))
147-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
148-
sock.close()
149-
_ = requests.get(url=f"http://localhost:{port}", timeout=30)
147+
_ = requests.get(url=_unused_local_url(), timeout=30)
150148

151149

152150
@beartype
@@ -2075,27 +2073,19 @@ def test_httpx_unmocked_address_blocked() -> None:
20752073
"""``MockVWS`` blocks ``httpx`` requests to non-Vuforia
20762074
addresses.
20772075
"""
2078-
sock = socket.socket()
2079-
sock.bind(("", 0))
2080-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
2081-
sock.close()
20822076
with MockVWS(), pytest.raises(expected_exception=httpx.ConnectError):
2083-
_ = httpx.get(url=f"http://localhost:{port}", timeout=30)
2077+
_ = httpx.get(url=_unused_local_url(), timeout=30)
20842078

20852079
@staticmethod
20862080
def test_httpx_real_http() -> None:
20872081
"""When ``real_http=True``, ``httpx`` requests to non-Vuforia
20882082
addresses are not blocked.
20892083
"""
2090-
sock = socket.socket()
2091-
sock.bind(("", 0))
2092-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
2093-
sock.close()
20942084
with (
20952085
MockVWS(real_http=True),
20962086
pytest.raises(expected_exception=httpx.ConnectError),
20972087
):
2098-
_ = httpx.get(url=f"http://localhost:{port}", timeout=30)
2088+
_ = httpx.get(url=_unused_local_url(), timeout=30)
20992089

21002090

21012091
class TestHttpx2AlsoIntercepted:
@@ -2138,27 +2128,19 @@ def test_httpx2_unmocked_address_blocked() -> None:
21382128
"""``MockVWS`` blocks ``httpx2`` requests to non-Vuforia
21392129
addresses.
21402130
"""
2141-
sock = socket.socket()
2142-
sock.bind(("", 0))
2143-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
2144-
sock.close()
21452131
with MockVWS(), pytest.raises(expected_exception=httpx2.ConnectError):
2146-
_ = httpx2.get(url=f"http://localhost:{port}", timeout=30)
2132+
_ = httpx2.get(url=_unused_local_url(), timeout=30)
21472133

21482134
@staticmethod
21492135
def test_httpx2_real_http() -> None:
21502136
"""When ``real_http=True``, ``httpx2`` requests to non-Vuforia
21512137
addresses are not blocked.
21522138
"""
2153-
sock = socket.socket()
2154-
sock.bind(("", 0))
2155-
port = sock.getsockname()[1] # pyrefly: ignore [unknown-variable-type]
2156-
sock.close()
21572139
with (
21582140
MockVWS(real_http=True),
21592141
pytest.raises(expected_exception=httpx2.ConnectError),
21602142
):
2161-
_ = httpx2.get(url=f"http://localhost:{port}", timeout=30)
2143+
_ = httpx2.get(url=_unused_local_url(), timeout=30)
21622144

21632145

21642146
class TestModelTargetWebAPI:

0 commit comments

Comments
 (0)