Skip to content

Commit a406fc1

Browse files
committed
gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests
Prefer VMADDR_CID_LOCAL instead of VMADDR_CID_ANY for bind() in the server. Skip the test if bind() fails with EADDRNOTAVAIL.
1 parent d931725 commit a406fc1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Lib/test/test_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ def clientTearDown(self):
563563
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
564564
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
565565
'VSOCK sockets required for this test.')
566-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
567-
"This test can only be run on a virtual guest.")
566+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
567+
"This test can only be run on a virtual guest.")
568568
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
569569

570570
def __init__(self, methodName='runTest'):
@@ -574,7 +574,16 @@ def __init__(self, methodName='runTest'):
574574
def setUp(self):
575575
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
576576
self.addCleanup(self.serv.close)
577-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
577+
cid = get_cid()
578+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
579+
cid = socket.VMADDR_CID_LOCAL
580+
try:
581+
self.serv.bind((cid, VSOCKPORT))
582+
except OSError as exc:
583+
if exc.errno == errno.EADDRNOTAVAIL:
584+
self.skipTest(f"bind() failed with {exc!r}")
585+
else:
586+
raise
578587
self.serv.listen()
579588
self.serverExplicitReady()
580589
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)