summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socket.py
diff options
context:
space:
mode:
authorcaavery <cavery@redhat.com>2017-09-06 22:18:10 (GMT)
committerChristian Heimes <christian@python.org>2017-09-06 22:18:10 (GMT)
commiteffc12f8e9e20d0951d2ba5883587666bd8218e3 (patch)
tree7c017ddd36f528c98a36c2b08293eb843194deb9 /Lib/test/test_socket.py
parent5d578442ed5ba5025e465b384341cb8646ffd819 (diff)
downloadcpython-effc12f8e9e20d0951d2ba5883587666bd8218e3.zip
cpython-effc12f8e9e20d0951d2ba5883587666bd8218e3.tar.gz
cpython-effc12f8e9e20d0951d2ba5883587666bd8218e3.tar.bz2
bpo-27584: New addition of vSockets to the python socket module (#2489)
* bpo-27584: New addition of vSockets to the python socket module Support for AF_VSOCK on Linux only * bpo-27584: Fixes for V2 Fixed syntax and naming problems. Fixed #ifdef AF_VSOCK checking Restored original aclocal.m4 * bpo-27584: Fixes for V3 Added checking for fcntl and thread modules. * bpo-27584: Fixes for V4 Fixed white space error * bpo-27584: Fixes for V5 Added back comma in (CID, port). * bpo-27584: Fixes for V6 Added news file. socket.rst now reflects first Linux introduction of AF_VSOCK. Fixed get_cid in test_socket.py. Replaced PyLong_FromLong with PyLong_FromUnsignedLong in socketmodule.c Got rid of extra AF_VSOCK #define. Added sockaddr_vm to sock_addr. * bpo-27584: Fixes for V7 Minor cleanup. * bpo-27584: Fixes for V8 Put back #undef AF_VSOCK as it is necessary when vm_sockets.h is not installed.
Diffstat (limited to 'Lib/test/test_socket.py')
-rw-r--r--Lib/test/test_socket.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 8423488..50016ab 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -33,6 +33,8 @@ except ImportError:
HOST = support.HOST
MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return
+VSOCKPORT = 1234
+
try:
import _thread as thread
import threading
@@ -44,6 +46,16 @@ try:
except ImportError:
_socket = None
+def get_cid():
+ if fcntl is None:
+ return None
+ try:
+ with open("/dev/vsock", "rb") as f:
+ r = fcntl.ioctl(f, socket.IOCTL_VM_SOCKETS_GET_LOCAL_CID, " ")
+ except OSError:
+ return None
+ else:
+ return struct.unpack("I", r)[0]
def _have_socket_can():
"""Check whether CAN sockets are supported on this host."""
@@ -85,6 +97,11 @@ def _have_socket_alg():
s.close()
return True
+def _have_socket_vsock():
+ """Check whether AF_VSOCK sockets are supported on this host."""
+ ret = get_cid() is not None
+ return ret
+
HAVE_SOCKET_CAN = _have_socket_can()
HAVE_SOCKET_CAN_ISOTP = _have_socket_can_isotp()
@@ -93,6 +110,8 @@ HAVE_SOCKET_RDS = _have_socket_rds()
HAVE_SOCKET_ALG = _have_socket_alg()
+HAVE_SOCKET_VSOCK = _have_socket_vsock()
+
# Size in bytes of the int type
SIZEOF_INT = array.array("i").itemsize
@@ -387,6 +406,42 @@ class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest):
self.cli = None
ThreadableTest.clientTearDown(self)
+@unittest.skipIf(fcntl is None, "need fcntl")
+@unittest.skipUnless(thread, 'Threading required for this test.')
+@unittest.skipUnless(HAVE_SOCKET_VSOCK,
+ 'VSOCK sockets required for this test.')
+@unittest.skipUnless(get_cid() != 2,
+ "This test can only be run on a virtual guest.")
+class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
+
+ def __init__(self, methodName='runTest'):
+ unittest.TestCase.__init__(self, methodName=methodName)
+ ThreadableTest.__init__(self)
+
+ 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))
+ self.serv.listen()
+ self.serverExplicitReady()
+ self.conn, self.connaddr = self.serv.accept()
+ self.addCleanup(self.conn.close)
+
+ def clientSetUp(self):
+ time.sleep(0.1)
+ self.cli = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
+ self.addCleanup(self.cli.close)
+ cid = get_cid()
+ self.cli.connect((cid, VSOCKPORT))
+
+ def testStream(self):
+ msg = self.conn.recv(1024)
+ self.assertEqual(msg, MSG)
+
+ def _testStream(self):
+ self.cli.send(MSG)
+ self.cli.close()
+
class SocketConnectedTest(ThreadedTCPSocketTest):
"""Socket tests for client-server connection.
@@ -1874,6 +1929,54 @@ class RDSTest(ThreadedRDSSocketTest):
self.assertIn(self.serv, r)
+@unittest.skipIf(fcntl is None, "need fcntl")
+@unittest.skipUnless(HAVE_SOCKET_VSOCK,
+ 'VSOCK sockets required for this test.')
+class BasicVSOCKTest(unittest.TestCase):
+
+ def testCrucialConstants(self):
+ socket.AF_VSOCK
+
+ def testVSOCKConstants(self):
+ socket.SO_VM_SOCKETS_BUFFER_SIZE
+ socket.SO_VM_SOCKETS_BUFFER_MIN_SIZE
+ socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE
+ socket.VMADDR_CID_ANY
+ socket.VMADDR_PORT_ANY
+ socket.VMADDR_CID_HOST
+ socket.VM_SOCKETS_INVALID_VERSION
+ socket.IOCTL_VM_SOCKETS_GET_LOCAL_CID
+
+ def testCreateSocket(self):
+ with socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) as s:
+ pass
+
+ def testSocketBufferSize(self):
+ with socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM) as s:
+ orig_max = s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE)
+ orig = s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_SIZE)
+ orig_min = s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MIN_SIZE)
+
+ s.setsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE, orig_max * 2)
+ s.setsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_SIZE, orig * 2)
+ s.setsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MIN_SIZE, orig_min * 2)
+
+ self.assertEqual(orig_max * 2,
+ s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MAX_SIZE))
+ self.assertEqual(orig * 2,
+ s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_SIZE))
+ self.assertEqual(orig_min * 2,
+ s.getsockopt(socket.AF_VSOCK,
+ socket.SO_VM_SOCKETS_BUFFER_MIN_SIZE))
+
@unittest.skipUnless(thread, 'Threading required for this test.')
class BasicTCPTest(SocketConnectedTest):
@@ -5682,6 +5785,10 @@ def test_main():
tests.extend([BasicRDSTest, RDSTest])
tests.append(LinuxKernelCryptoAPI)
tests.extend([
+ BasicVSOCKTest,
+ ThreadedVSOCKSocketStreamTest,
+ ])
+ tests.extend([
CmsgMacroTests,
SendmsgUDPTest,
RecvmsgUDPTest,