Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test allow_hosts for unix sockets #93

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _resolve_allow_hosts(item):
hosts = mark_restrictions.args[0]
elif cli_restrictions:
hosts = cli_restrictions
socket_allow_hosts(hosts)
socket_allow_hosts(hosts, allow_unix_socket=item.config.__socket_allow_unix_socket)
return hosts


Expand All @@ -151,7 +151,7 @@ def host_from_connect_args(args):
return host_from_address(address)


def socket_allow_hosts(allowed=None):
def socket_allow_hosts(allowed=None, allow_unix_socket=False):
miketheman marked this conversation as resolved.
Show resolved Hide resolved
miketheman marked this conversation as resolved.
Show resolved Hide resolved
""" disable socket.socket.connect() to disable the Internet. useful in testing.
"""
if isinstance(allowed, str):
Expand All @@ -161,8 +161,17 @@ def socket_allow_hosts(allowed=None):

def guarded_connect(inst, *args):
host = host_from_connect_args(args)
if host and host in allowed:
if host in allowed:
return _true_connect(inst, *args)

try:
is_unix_socket = inst.family == socket.AF_UNIX
except AttributeError:
# AF_UNIX not supported on Windows https://bugs.python.org/issue33408
is_unix_socket = False
if allow_unix_socket and is_unix_socket:
return _true_connect(inst, *args)

raise SocketConnectBlockedError(allowed, host)

socket.socket.connect = guarded_connect
Expand Down
30 changes: 30 additions & 0 deletions tests/test_combinations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test module to express odd combinations of configurations."""
# TODO: Move this to a more common location, like `conftest.py`.
from test_async import unix_sockets_only


def test_parametrize_with_socket_enabled_and_allow_hosts(testdir, httpbin):
Expand Down Expand Up @@ -45,3 +47,31 @@ def test_remote_not_allowed_fails():
result.stdout.fnmatch_lines(
"*SocketConnectBlockedError: A test tried to use socket.socket.connect() with host*"
)


@unix_sockets_only
def test_combine_unix_and_allow_hosts(testdir, httpbin):
"""Test combination of disable, allow-unix and allow-hosts.

From https://github.com/miketheman/pytest-socket/issues/78
"""
testdir.makepyfile(
f"""
import socket

import pytest


def test_unix_connect():
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
with pytest.raises(FileNotFoundError):
sock.connect('/tmp/socket.sock')


def test_inet_connect():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('{httpbin.host}', {httpbin.port}))
"""
)
result = testdir.runpytest("--disable-socket", "--allow-unix-socket", f"--allow-hosts={httpbin.host}")
result.assert_outcomes(passed=2)