Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,18 +640,22 @@ def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
# Authenticating avoids using a connection from something else
# able to connect to {host}:{port} instead of us.
# We expect only AF_INET and AF_INET6 families.
try:
if (
ssock.getsockname() != csock.getpeername()
or csock.getsockname() != ssock.getpeername()
):
raise ConnectionError("Unexpected peer connection")
except:
# getsockname() and getpeername() can fail
# if either socket isn't connected.
ssock.close()
csock.close()
raise
#
# Note that we skip this on WASI because on that platorm the client socket
# may not have finished connecting by the time we've reached this point (gh-146139).
if sys.platform != "wasi":
try:
if (
ssock.getsockname() != csock.getpeername()
or csock.getsockname() != ssock.getpeername()
):
raise ConnectionError("Unexpected peer connection")
except:
# getsockname() and getpeername() can fail
# if either socket isn't connected.
ssock.close()
csock.close()
raise

return (ssock, csock)

Expand Down
34 changes: 31 additions & 3 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ def clientTearDown(self):
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
'VSOCK sockets required for this test.')
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
"This test can only be run on a virtual guest.")
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
"This test can only be run on a virtual guest.")
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):

def __init__(self, methodName='runTest'):
Expand All @@ -572,7 +572,16 @@ def __init__(self, methodName='runTest'):
def setUp(self):
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
self.addCleanup(self.serv.close)
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
cid = get_cid()
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
cid = socket.VMADDR_CID_LOCAL
try:
self.serv.bind((cid, VSOCKPORT))
except OSError as exc:
if exc.errno == errno.EADDRNOTAVAIL:
self.skipTest(f"bind() failed with {exc!r}")
else:
raise
self.serv.listen()
self.serverExplicitReady()
self.serv.settimeout(support.LOOPBACK_TIMEOUT)
Expand Down Expand Up @@ -2180,6 +2189,25 @@ def test_addressinfo_enum(self):
source=_socket)
enum._test_simple_enum(CheckedAddressInfo, socket.AddressInfo)

@unittest.expectedFailure # TODO: RUSTPYTHON; TypeError: Expected type 'int' but 'Mut' found
@unittest.skipUnless(hasattr(socket.socket, "sendmsg"),"sendmsg not supported")
def test_sendmsg_reentrant_ancillary_mutation(self):

class Mut:
def __index__(self):
seq.clear()
return socket.SCM_RIGHTS

seq = [
(socket.SOL_SOCKET, Mut(), b'xxxx'),
(socket.SOL_SOCKET, socket.SCM_RIGHTS, b'xxxx'),
]

left, right = socket.socketpair()
self.addCleanup(left.close)
self.addCleanup(right.close)
self.assertRaises(OSError, left.sendmsg, [b'x'], seq)


@unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.')
class BasicCANTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2652,7 +2652,7 @@ mod _socket {
.map_err(|err| vm.new_os_error(err.into_string().unwrap()))
}

#[cfg(all(unix, not(target_os = "redox")))]
#[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[pyfunction]
fn sethostname(hostname: PyUtf8StrRef) -> nix::Result<()> {
nix::unistd::sethostname(hostname.as_str())
Expand Down
Loading