Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Antoine Pitrou | b7eb563 | 2012-06-24 01:34:13 +0200 | [diff] [blame] | 5 | from unittest.case import _ExpectedFailure |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 6 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 7 | import errno |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 8 | import io |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 9 | import socket |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 10 | import select |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 11 | import tempfile |
| 12 | import _testcapi |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 13 | import time |
| 14 | import traceback |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 15 | import queue |
Jack Jansen | 522e769 | 2002-09-06 21:57:50 +0000 | [diff] [blame] | 16 | import sys |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 17 | import os |
| 18 | import array |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 19 | import platform |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 20 | import contextlib |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 21 | from weakref import proxy |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | import signal |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 23 | import math |
Antoine Pitrou | 6d58d64 | 2011-03-20 23:56:36 +0100 | [diff] [blame] | 24 | import pickle |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 25 | import struct |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 26 | try: |
| 27 | import fcntl |
| 28 | except ImportError: |
| 29 | fcntl = False |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 30 | try: |
| 31 | import multiprocessing |
| 32 | except ImportError: |
| 33 | multiprocessing = False |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 35 | HOST = support.HOST |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 36 | MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') ## test unicode string and carriage return |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 37 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 38 | try: |
| 39 | import _thread as thread |
| 40 | import threading |
| 41 | except ImportError: |
| 42 | thread = None |
| 43 | threading = None |
| 44 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 45 | def _have_socket_can(): |
| 46 | """Check whether CAN sockets are supported on this host.""" |
| 47 | try: |
| 48 | s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) |
| 49 | except (AttributeError, socket.error, OSError): |
| 50 | return False |
| 51 | else: |
| 52 | s.close() |
| 53 | return True |
| 54 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 55 | def _have_socket_rds(): |
| 56 | """Check whether RDS sockets are supported on this host.""" |
| 57 | try: |
| 58 | s = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) |
| 59 | except (AttributeError, OSError): |
| 60 | return False |
| 61 | else: |
| 62 | s.close() |
| 63 | return True |
| 64 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 65 | HAVE_SOCKET_CAN = _have_socket_can() |
| 66 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 67 | HAVE_SOCKET_RDS = _have_socket_rds() |
| 68 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 69 | # Size in bytes of the int type |
| 70 | SIZEOF_INT = array.array("i").itemsize |
| 71 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 72 | class SocketTCPTest(unittest.TestCase): |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 74 | def setUp(self): |
| 75 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 76 | self.port = support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 77 | self.serv.listen(1) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 79 | def tearDown(self): |
| 80 | self.serv.close() |
| 81 | self.serv = None |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 83 | class SocketUDPTest(unittest.TestCase): |
| 84 | |
| 85 | def setUp(self): |
| 86 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 87 | self.port = support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 88 | |
| 89 | def tearDown(self): |
| 90 | self.serv.close() |
| 91 | self.serv = None |
| 92 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 93 | class ThreadSafeCleanupTestCase(unittest.TestCase): |
| 94 | """Subclass of unittest.TestCase with thread-safe cleanup methods. |
| 95 | |
| 96 | This subclass protects the addCleanup() and doCleanups() methods |
| 97 | with a recursive lock. |
| 98 | """ |
| 99 | |
| 100 | if threading: |
| 101 | def __init__(self, *args, **kwargs): |
| 102 | super().__init__(*args, **kwargs) |
| 103 | self._cleanup_lock = threading.RLock() |
| 104 | |
| 105 | def addCleanup(self, *args, **kwargs): |
| 106 | with self._cleanup_lock: |
| 107 | return super().addCleanup(*args, **kwargs) |
| 108 | |
| 109 | def doCleanups(self, *args, **kwargs): |
| 110 | with self._cleanup_lock: |
| 111 | return super().doCleanups(*args, **kwargs) |
| 112 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 113 | class SocketCANTest(unittest.TestCase): |
| 114 | |
| 115 | """To be able to run this test, a `vcan0` CAN interface can be created with |
| 116 | the following commands: |
| 117 | # modprobe vcan |
| 118 | # ip link add dev vcan0 type vcan |
| 119 | # ifconfig vcan0 up |
| 120 | """ |
| 121 | interface = 'vcan0' |
| 122 | bufsize = 128 |
| 123 | |
| 124 | def setUp(self): |
| 125 | self.s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) |
Antoine Pitrou | 86fe860 | 2011-10-10 20:15:59 +0200 | [diff] [blame] | 126 | self.addCleanup(self.s.close) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 127 | try: |
| 128 | self.s.bind((self.interface,)) |
| 129 | except socket.error: |
| 130 | self.skipTest('network interface `%s` does not exist' % |
| 131 | self.interface) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 132 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 133 | |
| 134 | class SocketRDSTest(unittest.TestCase): |
| 135 | |
| 136 | """To be able to run this test, the `rds` kernel module must be loaded: |
| 137 | # modprobe rds |
| 138 | """ |
| 139 | bufsize = 8192 |
| 140 | |
| 141 | def setUp(self): |
| 142 | self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) |
| 143 | self.addCleanup(self.serv.close) |
| 144 | try: |
| 145 | self.port = support.bind_port(self.serv) |
| 146 | except OSError: |
| 147 | self.skipTest('unable to bind RDS socket') |
| 148 | |
| 149 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 150 | class ThreadableTest: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 151 | """Threadable Test class |
| 152 | |
| 153 | The ThreadableTest class makes it easy to create a threaded |
| 154 | client/server pair from an existing unit test. To create a |
| 155 | new threaded class from an existing unit test, use multiple |
| 156 | inheritance: |
| 157 | |
| 158 | class NewClass (OldClass, ThreadableTest): |
| 159 | pass |
| 160 | |
| 161 | This class defines two new fixture functions with obvious |
| 162 | purposes for overriding: |
| 163 | |
| 164 | clientSetUp () |
| 165 | clientTearDown () |
| 166 | |
| 167 | Any new test functions within the class must then define |
| 168 | tests in pairs, where the test name is preceeded with a |
| 169 | '_' to indicate the client portion of the test. Ex: |
| 170 | |
| 171 | def testFoo(self): |
| 172 | # Server portion |
| 173 | |
| 174 | def _testFoo(self): |
| 175 | # Client portion |
| 176 | |
| 177 | Any exceptions raised by the clients during their tests |
| 178 | are caught and transferred to the main thread to alert |
| 179 | the testing framework. |
| 180 | |
| 181 | Note, the server setup function cannot call any blocking |
| 182 | functions that rely on the client thread during setup, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 183 | unless serverExplicitReady() is called just before |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 184 | the blocking call (such as in setting up a client/server |
| 185 | connection and performing the accept() in setUp(). |
| 186 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 187 | |
| 188 | def __init__(self): |
| 189 | # Swap the true setup function |
| 190 | self.__setUp = self.setUp |
| 191 | self.__tearDown = self.tearDown |
| 192 | self.setUp = self._setUp |
| 193 | self.tearDown = self._tearDown |
| 194 | |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 195 | def serverExplicitReady(self): |
| 196 | """This method allows the server to explicitly indicate that |
| 197 | it wants the client thread to proceed. This is useful if the |
| 198 | server is about to execute a blocking routine that is |
| 199 | dependent upon the client thread during its setup routine.""" |
| 200 | self.server_ready.set() |
| 201 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 202 | def _setUp(self): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 203 | self.server_ready = threading.Event() |
| 204 | self.client_ready = threading.Event() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 205 | self.done = threading.Event() |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 206 | self.queue = queue.Queue(1) |
Antoine Pitrou | 86fe860 | 2011-10-10 20:15:59 +0200 | [diff] [blame] | 207 | self.server_crashed = False |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 208 | |
| 209 | # Do some munging to start the client test. |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 210 | methodname = self.id() |
| 211 | i = methodname.rfind('.') |
| 212 | methodname = methodname[i+1:] |
| 213 | test_method = getattr(self, '_' + methodname) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 214 | self.client_thread = thread.start_new_thread( |
| 215 | self.clientRun, (test_method,)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | 86fe860 | 2011-10-10 20:15:59 +0200 | [diff] [blame] | 217 | try: |
| 218 | self.__setUp() |
| 219 | except: |
| 220 | self.server_crashed = True |
| 221 | raise |
| 222 | finally: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 223 | self.server_ready.set() |
| 224 | self.client_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 225 | |
| 226 | def _tearDown(self): |
| 227 | self.__tearDown() |
| 228 | self.done.wait() |
| 229 | |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 230 | if self.queue.qsize(): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 231 | exc = self.queue.get() |
| 232 | raise exc |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 233 | |
| 234 | def clientRun(self, test_func): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 235 | self.server_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 236 | self.clientSetUp() |
Charles-François Natali | a49ed76 | 2011-09-21 22:03:58 +0200 | [diff] [blame] | 237 | self.client_ready.set() |
Antoine Pitrou | 86fe860 | 2011-10-10 20:15:59 +0200 | [diff] [blame] | 238 | if self.server_crashed: |
| 239 | self.clientTearDown() |
| 240 | return |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 241 | if not hasattr(test_func, '__call__'): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 242 | raise TypeError("test_func must be a callable function") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 243 | try: |
| 244 | test_func() |
Antoine Pitrou | b7eb563 | 2012-06-24 01:34:13 +0200 | [diff] [blame] | 245 | except _ExpectedFailure: |
Nick Coghlan | 2496f33 | 2011-09-19 20:26:31 +1000 | [diff] [blame] | 246 | # We deliberately ignore expected failures |
| 247 | pass |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 248 | except BaseException as e: |
| 249 | self.queue.put(e) |
| 250 | finally: |
| 251 | self.clientTearDown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 252 | |
| 253 | def clientSetUp(self): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 254 | raise NotImplementedError("clientSetUp must be implemented.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 255 | |
| 256 | def clientTearDown(self): |
| 257 | self.done.set() |
| 258 | thread.exit() |
| 259 | |
| 260 | class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): |
| 261 | |
| 262 | def __init__(self, methodName='runTest'): |
| 263 | SocketTCPTest.__init__(self, methodName=methodName) |
| 264 | ThreadableTest.__init__(self) |
| 265 | |
| 266 | def clientSetUp(self): |
| 267 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 268 | |
| 269 | def clientTearDown(self): |
| 270 | self.cli.close() |
| 271 | self.cli = None |
| 272 | ThreadableTest.clientTearDown(self) |
| 273 | |
| 274 | class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): |
| 275 | |
| 276 | def __init__(self, methodName='runTest'): |
| 277 | SocketUDPTest.__init__(self, methodName=methodName) |
| 278 | ThreadableTest.__init__(self) |
| 279 | |
| 280 | def clientSetUp(self): |
| 281 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 282 | |
Brian Curtin | 3beb38f | 2010-11-04 03:41:43 +0000 | [diff] [blame] | 283 | def clientTearDown(self): |
| 284 | self.cli.close() |
| 285 | self.cli = None |
| 286 | ThreadableTest.clientTearDown(self) |
| 287 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 288 | class ThreadedCANSocketTest(SocketCANTest, ThreadableTest): |
| 289 | |
| 290 | def __init__(self, methodName='runTest'): |
| 291 | SocketCANTest.__init__(self, methodName=methodName) |
| 292 | ThreadableTest.__init__(self) |
| 293 | |
| 294 | def clientSetUp(self): |
| 295 | self.cli = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) |
| 296 | try: |
| 297 | self.cli.bind((self.interface,)) |
| 298 | except socket.error: |
Antoine Pitrou | 86fe860 | 2011-10-10 20:15:59 +0200 | [diff] [blame] | 299 | # skipTest should not be called here, and will be called in the |
| 300 | # server instead |
| 301 | pass |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 302 | |
| 303 | def clientTearDown(self): |
| 304 | self.cli.close() |
| 305 | self.cli = None |
| 306 | ThreadableTest.clientTearDown(self) |
| 307 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 308 | class ThreadedRDSSocketTest(SocketRDSTest, ThreadableTest): |
| 309 | |
| 310 | def __init__(self, methodName='runTest'): |
| 311 | SocketRDSTest.__init__(self, methodName=methodName) |
| 312 | ThreadableTest.__init__(self) |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 313 | |
| 314 | def clientSetUp(self): |
| 315 | self.cli = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) |
| 316 | try: |
| 317 | # RDS sockets must be bound explicitly to send or receive data |
| 318 | self.cli.bind((HOST, 0)) |
| 319 | self.cli_addr = self.cli.getsockname() |
| 320 | except OSError: |
| 321 | # skipTest should not be called here, and will be called in the |
| 322 | # server instead |
| 323 | pass |
| 324 | |
| 325 | def clientTearDown(self): |
| 326 | self.cli.close() |
| 327 | self.cli = None |
| 328 | ThreadableTest.clientTearDown(self) |
| 329 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 330 | class SocketConnectedTest(ThreadedTCPSocketTest): |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 331 | """Socket tests for client-server connection. |
| 332 | |
| 333 | self.cli_conn is a client socket connected to the server. The |
| 334 | setUp() method guarantees that it is connected to the server. |
| 335 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 336 | |
| 337 | def __init__(self, methodName='runTest'): |
| 338 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 339 | |
| 340 | def setUp(self): |
| 341 | ThreadedTCPSocketTest.setUp(self) |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 342 | # Indicate explicitly we're ready for the client thread to |
| 343 | # proceed and then perform the blocking call to accept |
| 344 | self.serverExplicitReady() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 345 | conn, addr = self.serv.accept() |
| 346 | self.cli_conn = conn |
| 347 | |
| 348 | def tearDown(self): |
| 349 | self.cli_conn.close() |
| 350 | self.cli_conn = None |
| 351 | ThreadedTCPSocketTest.tearDown(self) |
| 352 | |
| 353 | def clientSetUp(self): |
| 354 | ThreadedTCPSocketTest.clientSetUp(self) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 355 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 356 | self.serv_conn = self.cli |
| 357 | |
| 358 | def clientTearDown(self): |
| 359 | self.serv_conn.close() |
| 360 | self.serv_conn = None |
| 361 | ThreadedTCPSocketTest.clientTearDown(self) |
| 362 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 363 | class SocketPairTest(unittest.TestCase, ThreadableTest): |
| 364 | |
| 365 | def __init__(self, methodName='runTest'): |
| 366 | unittest.TestCase.__init__(self, methodName=methodName) |
| 367 | ThreadableTest.__init__(self) |
| 368 | |
| 369 | def setUp(self): |
| 370 | self.serv, self.cli = socket.socketpair() |
| 371 | |
| 372 | def tearDown(self): |
| 373 | self.serv.close() |
| 374 | self.serv = None |
| 375 | |
| 376 | def clientSetUp(self): |
| 377 | pass |
| 378 | |
| 379 | def clientTearDown(self): |
| 380 | self.cli.close() |
| 381 | self.cli = None |
| 382 | ThreadableTest.clientTearDown(self) |
| 383 | |
Tim Peters | 494aaee | 2004-08-09 18:54:11 +0000 | [diff] [blame] | 384 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 385 | # The following classes are used by the sendmsg()/recvmsg() tests. |
| 386 | # Combining, for instance, ConnectedStreamTestMixin and TCPTestBase |
| 387 | # gives a drop-in replacement for SocketConnectedTest, but different |
| 388 | # address families can be used, and the attributes serv_addr and |
| 389 | # cli_addr will be set to the addresses of the endpoints. |
| 390 | |
| 391 | class SocketTestBase(unittest.TestCase): |
| 392 | """A base class for socket tests. |
| 393 | |
| 394 | Subclasses must provide methods newSocket() to return a new socket |
| 395 | and bindSock(sock) to bind it to an unused address. |
| 396 | |
| 397 | Creates a socket self.serv and sets self.serv_addr to its address. |
| 398 | """ |
| 399 | |
| 400 | def setUp(self): |
| 401 | self.serv = self.newSocket() |
| 402 | self.bindServer() |
| 403 | |
| 404 | def bindServer(self): |
| 405 | """Bind server socket and set self.serv_addr to its address.""" |
| 406 | self.bindSock(self.serv) |
| 407 | self.serv_addr = self.serv.getsockname() |
| 408 | |
| 409 | def tearDown(self): |
| 410 | self.serv.close() |
| 411 | self.serv = None |
| 412 | |
| 413 | |
| 414 | class SocketListeningTestMixin(SocketTestBase): |
| 415 | """Mixin to listen on the server socket.""" |
| 416 | |
| 417 | def setUp(self): |
| 418 | super().setUp() |
| 419 | self.serv.listen(1) |
| 420 | |
| 421 | |
| 422 | class ThreadedSocketTestMixin(ThreadSafeCleanupTestCase, SocketTestBase, |
| 423 | ThreadableTest): |
| 424 | """Mixin to add client socket and allow client/server tests. |
| 425 | |
| 426 | Client socket is self.cli and its address is self.cli_addr. See |
| 427 | ThreadableTest for usage information. |
| 428 | """ |
| 429 | |
| 430 | def __init__(self, *args, **kwargs): |
| 431 | super().__init__(*args, **kwargs) |
| 432 | ThreadableTest.__init__(self) |
| 433 | |
| 434 | def clientSetUp(self): |
| 435 | self.cli = self.newClientSocket() |
| 436 | self.bindClient() |
| 437 | |
| 438 | def newClientSocket(self): |
| 439 | """Return a new socket for use as client.""" |
| 440 | return self.newSocket() |
| 441 | |
| 442 | def bindClient(self): |
| 443 | """Bind client socket and set self.cli_addr to its address.""" |
| 444 | self.bindSock(self.cli) |
| 445 | self.cli_addr = self.cli.getsockname() |
| 446 | |
| 447 | def clientTearDown(self): |
| 448 | self.cli.close() |
| 449 | self.cli = None |
| 450 | ThreadableTest.clientTearDown(self) |
| 451 | |
| 452 | |
| 453 | class ConnectedStreamTestMixin(SocketListeningTestMixin, |
| 454 | ThreadedSocketTestMixin): |
| 455 | """Mixin to allow client/server stream tests with connected client. |
| 456 | |
| 457 | Server's socket representing connection to client is self.cli_conn |
| 458 | and client's connection to server is self.serv_conn. (Based on |
| 459 | SocketConnectedTest.) |
| 460 | """ |
| 461 | |
| 462 | def setUp(self): |
| 463 | super().setUp() |
| 464 | # Indicate explicitly we're ready for the client thread to |
| 465 | # proceed and then perform the blocking call to accept |
| 466 | self.serverExplicitReady() |
| 467 | conn, addr = self.serv.accept() |
| 468 | self.cli_conn = conn |
| 469 | |
| 470 | def tearDown(self): |
| 471 | self.cli_conn.close() |
| 472 | self.cli_conn = None |
| 473 | super().tearDown() |
| 474 | |
| 475 | def clientSetUp(self): |
| 476 | super().clientSetUp() |
| 477 | self.cli.connect(self.serv_addr) |
| 478 | self.serv_conn = self.cli |
| 479 | |
| 480 | def clientTearDown(self): |
| 481 | self.serv_conn.close() |
| 482 | self.serv_conn = None |
| 483 | super().clientTearDown() |
| 484 | |
| 485 | |
| 486 | class UnixSocketTestBase(SocketTestBase): |
| 487 | """Base class for Unix-domain socket tests.""" |
| 488 | |
| 489 | # This class is used for file descriptor passing tests, so we |
| 490 | # create the sockets in a private directory so that other users |
| 491 | # can't send anything that might be problematic for a privileged |
| 492 | # user running the tests. |
| 493 | |
| 494 | def setUp(self): |
| 495 | self.dir_path = tempfile.mkdtemp() |
| 496 | self.addCleanup(os.rmdir, self.dir_path) |
| 497 | super().setUp() |
| 498 | |
| 499 | def bindSock(self, sock): |
| 500 | path = tempfile.mktemp(dir=self.dir_path) |
| 501 | sock.bind(path) |
| 502 | self.addCleanup(support.unlink, path) |
| 503 | |
| 504 | class UnixStreamBase(UnixSocketTestBase): |
| 505 | """Base class for Unix-domain SOCK_STREAM tests.""" |
| 506 | |
| 507 | def newSocket(self): |
| 508 | return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 509 | |
| 510 | |
| 511 | class InetTestBase(SocketTestBase): |
| 512 | """Base class for IPv4 socket tests.""" |
| 513 | |
| 514 | host = HOST |
| 515 | |
| 516 | def setUp(self): |
| 517 | super().setUp() |
| 518 | self.port = self.serv_addr[1] |
| 519 | |
| 520 | def bindSock(self, sock): |
| 521 | support.bind_port(sock, host=self.host) |
| 522 | |
| 523 | class TCPTestBase(InetTestBase): |
| 524 | """Base class for TCP-over-IPv4 tests.""" |
| 525 | |
| 526 | def newSocket(self): |
| 527 | return socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 528 | |
| 529 | class UDPTestBase(InetTestBase): |
| 530 | """Base class for UDP-over-IPv4 tests.""" |
| 531 | |
| 532 | def newSocket(self): |
| 533 | return socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 534 | |
| 535 | class SCTPStreamBase(InetTestBase): |
| 536 | """Base class for SCTP tests in one-to-one (SOCK_STREAM) mode.""" |
| 537 | |
| 538 | def newSocket(self): |
| 539 | return socket.socket(socket.AF_INET, socket.SOCK_STREAM, |
| 540 | socket.IPPROTO_SCTP) |
| 541 | |
| 542 | |
| 543 | class Inet6TestBase(InetTestBase): |
| 544 | """Base class for IPv6 socket tests.""" |
| 545 | |
| 546 | # Don't use "localhost" here - it may not have an IPv6 address |
| 547 | # assigned to it by default (e.g. in /etc/hosts), and if someone |
| 548 | # has assigned it an IPv4-mapped address, then it's unlikely to |
| 549 | # work with the full IPv6 API. |
| 550 | host = "::1" |
| 551 | |
| 552 | class UDP6TestBase(Inet6TestBase): |
| 553 | """Base class for UDP-over-IPv6 tests.""" |
| 554 | |
| 555 | def newSocket(self): |
| 556 | return socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) |
| 557 | |
| 558 | |
| 559 | # Test-skipping decorators for use with ThreadableTest. |
| 560 | |
| 561 | def skipWithClientIf(condition, reason): |
| 562 | """Skip decorated test if condition is true, add client_skip decorator. |
| 563 | |
| 564 | If the decorated object is not a class, sets its attribute |
| 565 | "client_skip" to a decorator which will return an empty function |
| 566 | if the test is to be skipped, or the original function if it is |
| 567 | not. This can be used to avoid running the client part of a |
| 568 | skipped test when using ThreadableTest. |
| 569 | """ |
| 570 | def client_pass(*args, **kwargs): |
| 571 | pass |
| 572 | def skipdec(obj): |
| 573 | retval = unittest.skip(reason)(obj) |
| 574 | if not isinstance(obj, type): |
| 575 | retval.client_skip = lambda f: client_pass |
| 576 | return retval |
| 577 | def noskipdec(obj): |
| 578 | if not (isinstance(obj, type) or hasattr(obj, "client_skip")): |
| 579 | obj.client_skip = lambda f: f |
| 580 | return obj |
| 581 | return skipdec if condition else noskipdec |
| 582 | |
| 583 | |
| 584 | def requireAttrs(obj, *attributes): |
| 585 | """Skip decorated test if obj is missing any of the given attributes. |
| 586 | |
| 587 | Sets client_skip attribute as skipWithClientIf() does. |
| 588 | """ |
| 589 | missing = [name for name in attributes if not hasattr(obj, name)] |
| 590 | return skipWithClientIf( |
| 591 | missing, "don't have " + ", ".join(name for name in missing)) |
| 592 | |
| 593 | |
| 594 | def requireSocket(*args): |
| 595 | """Skip decorated test if a socket cannot be created with given arguments. |
| 596 | |
| 597 | When an argument is given as a string, will use the value of that |
| 598 | attribute of the socket module, or skip the test if it doesn't |
| 599 | exist. Sets client_skip attribute as skipWithClientIf() does. |
| 600 | """ |
| 601 | err = None |
| 602 | missing = [obj for obj in args if |
| 603 | isinstance(obj, str) and not hasattr(socket, obj)] |
| 604 | if missing: |
| 605 | err = "don't have " + ", ".join(name for name in missing) |
| 606 | else: |
| 607 | callargs = [getattr(socket, obj) if isinstance(obj, str) else obj |
| 608 | for obj in args] |
| 609 | try: |
| 610 | s = socket.socket(*callargs) |
| 611 | except socket.error as e: |
| 612 | # XXX: check errno? |
| 613 | err = str(e) |
| 614 | else: |
| 615 | s.close() |
| 616 | return skipWithClientIf( |
| 617 | err is not None, |
| 618 | "can't create socket({0}): {1}".format( |
| 619 | ", ".join(str(o) for o in args), err)) |
| 620 | |
| 621 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 622 | ####################################################################### |
| 623 | ## Begin Tests |
| 624 | |
| 625 | class GeneralModuleTests(unittest.TestCase): |
| 626 | |
Walter Dörwald | a7eb93e | 2007-06-05 13:41:53 +0000 | [diff] [blame] | 627 | def test_repr(self): |
| 628 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 629 | self.addCleanup(s.close) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 630 | self.assertTrue(repr(s).startswith("<socket.socket object")) |
Walter Dörwald | a7eb93e | 2007-06-05 13:41:53 +0000 | [diff] [blame] | 631 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 632 | def test_weakref(self): |
| 633 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 634 | p = proxy(s) |
| 635 | self.assertEqual(p.fileno(), s.fileno()) |
| 636 | s.close() |
| 637 | s = None |
| 638 | try: |
| 639 | p.fileno() |
| 640 | except ReferenceError: |
| 641 | pass |
| 642 | else: |
| 643 | self.fail('Socket proxy still exists') |
| 644 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 645 | def testSocketError(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 646 | # Testing socket module exceptions |
Ezio Melotti | 845f152 | 2011-05-08 20:23:57 +0300 | [diff] [blame] | 647 | msg = "Error raising socket exception (%s)." |
| 648 | with self.assertRaises(socket.error, msg=msg % 'socket.error'): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 649 | raise socket.error |
Ezio Melotti | 845f152 | 2011-05-08 20:23:57 +0300 | [diff] [blame] | 650 | with self.assertRaises(socket.error, msg=msg % 'socket.herror'): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 651 | raise socket.herror |
Ezio Melotti | 845f152 | 2011-05-08 20:23:57 +0300 | [diff] [blame] | 652 | with self.assertRaises(socket.error, msg=msg % 'socket.gaierror'): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 653 | raise socket.gaierror |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 654 | |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 655 | def testSendtoErrors(self): |
| 656 | # Testing that sendto doens't masks failures. See #10169. |
| 657 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 658 | self.addCleanup(s.close) |
| 659 | s.bind(('', 0)) |
| 660 | sockname = s.getsockname() |
| 661 | # 2 args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 662 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 663 | s.sendto('\u2620', sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 664 | self.assertEqual(str(cm.exception), |
| 665 | "'str' does not support the buffer interface") |
| 666 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 667 | s.sendto(5j, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 668 | self.assertEqual(str(cm.exception), |
| 669 | "'complex' does not support the buffer interface") |
| 670 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 671 | s.sendto(b'foo', None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 672 | self.assertIn('not NoneType',str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 673 | # 3 args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 674 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 675 | s.sendto('\u2620', 0, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 676 | self.assertEqual(str(cm.exception), |
| 677 | "'str' does not support the buffer interface") |
| 678 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 679 | s.sendto(5j, 0, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 680 | self.assertEqual(str(cm.exception), |
| 681 | "'complex' does not support the buffer interface") |
| 682 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 683 | s.sendto(b'foo', 0, None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 684 | self.assertIn('not NoneType', str(cm.exception)) |
| 685 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 686 | s.sendto(b'foo', 'bar', sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 687 | self.assertIn('an integer is required', str(cm.exception)) |
| 688 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 689 | s.sendto(b'foo', None, None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 690 | self.assertIn('an integer is required', str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 691 | # wrong number of args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 692 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 693 | s.sendto(b'foo') |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 694 | self.assertIn('(1 given)', str(cm.exception)) |
| 695 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 696 | s.sendto(b'foo', 0, sockname, 4) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 697 | self.assertIn('(4 given)', str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 698 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 699 | def testCrucialConstants(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 700 | # Testing for mission critical constants |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 701 | socket.AF_INET |
| 702 | socket.SOCK_STREAM |
| 703 | socket.SOCK_DGRAM |
| 704 | socket.SOCK_RAW |
| 705 | socket.SOCK_RDM |
| 706 | socket.SOCK_SEQPACKET |
| 707 | socket.SOL_SOCKET |
| 708 | socket.SO_REUSEADDR |
| 709 | |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 710 | def testHostnameRes(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 711 | # Testing hostname resolution mechanisms |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 712 | hostname = socket.gethostname() |
Guido van Rossum | 71e0294 | 2002-12-26 16:55:15 +0000 | [diff] [blame] | 713 | try: |
| 714 | ip = socket.gethostbyname(hostname) |
| 715 | except socket.error: |
| 716 | # Probably name lookup wasn't set up right; skip this test |
| 717 | return |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 718 | self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") |
Guido van Rossum | 9647b52 | 2002-12-26 17:04:45 +0000 | [diff] [blame] | 719 | try: |
| 720 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
| 721 | except socket.error: |
| 722 | # Probably a similar problem as above; skip this test |
| 723 | return |
Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame] | 724 | all_host_names = [hostname, hname] + aliases |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 725 | fqhn = socket.getfqdn(ip) |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 726 | if not fqhn in all_host_names: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 727 | self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 728 | |
Antoine Pitrou | 061cfb5 | 2011-02-28 22:25:22 +0000 | [diff] [blame] | 729 | @unittest.skipUnless(hasattr(socket, 'sethostname'), "test needs socket.sethostname()") |
| 730 | @unittest.skipUnless(hasattr(socket, 'gethostname'), "test needs socket.gethostname()") |
| 731 | def test_sethostname(self): |
| 732 | oldhn = socket.gethostname() |
| 733 | try: |
| 734 | socket.sethostname('new') |
| 735 | except socket.error as e: |
| 736 | if e.errno == errno.EPERM: |
| 737 | self.skipTest("test should be run as root") |
| 738 | else: |
| 739 | raise |
| 740 | try: |
| 741 | # running test as root! |
| 742 | self.assertEqual(socket.gethostname(), 'new') |
| 743 | # Should work with bytes objects too |
| 744 | socket.sethostname(b'bar') |
| 745 | self.assertEqual(socket.gethostname(), 'bar') |
| 746 | finally: |
| 747 | socket.sethostname(oldhn) |
| 748 | |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 749 | @unittest.skipUnless(hasattr(socket, 'if_nameindex'), |
| 750 | 'socket.if_nameindex() not available.') |
| 751 | def testInterfaceNameIndex(self): |
| 752 | interfaces = socket.if_nameindex() |
| 753 | for index, name in interfaces: |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 754 | self.assertIsInstance(index, int) |
| 755 | self.assertIsInstance(name, str) |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 756 | # interface indices are non-zero integers |
| 757 | self.assertGreater(index, 0) |
Charles-François Natali | 6071359 | 2011-05-20 16:55:06 +0200 | [diff] [blame] | 758 | _index = socket.if_nametoindex(name) |
| 759 | self.assertIsInstance(_index, int) |
| 760 | self.assertEqual(index, _index) |
| 761 | _name = socket.if_indextoname(index) |
| 762 | self.assertIsInstance(_name, str) |
| 763 | self.assertEqual(name, _name) |
| 764 | |
| 765 | @unittest.skipUnless(hasattr(socket, 'if_nameindex'), |
| 766 | 'socket.if_nameindex() not available.') |
| 767 | def testInvalidInterfaceNameIndex(self): |
| 768 | # test nonexistent interface index/name |
| 769 | self.assertRaises(socket.error, socket.if_indextoname, 0) |
| 770 | self.assertRaises(socket.error, socket.if_nametoindex, '_DEADBEEF') |
| 771 | # test with invalid values |
| 772 | self.assertRaises(TypeError, socket.if_nametoindex, 0) |
| 773 | self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF') |
Gregory P. Smith | 5ed2e77 | 2011-05-15 00:26:45 -0700 | [diff] [blame] | 774 | |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 775 | def testRefCountGetNameInfo(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 776 | # Testing reference count for getnameinfo |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 777 | if hasattr(sys, "getrefcount"): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 778 | try: |
| 779 | # On some versions, this loses a reference |
| 780 | orig = sys.getrefcount(__name__) |
| 781 | socket.getnameinfo(__name__,0) |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 782 | except TypeError: |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 783 | if sys.getrefcount(__name__) != orig: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 784 | self.fail("socket.getnameinfo loses a reference") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 785 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 786 | def testInterpreterCrash(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 787 | # Making sure getnameinfo doesn't crash the interpreter |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 788 | try: |
| 789 | # On some versions, this crashes the interpreter. |
| 790 | socket.getnameinfo(('x', 0, 0, 0), 0) |
| 791 | except socket.error: |
| 792 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 793 | |
Guido van Rossum | c0a0e08 | 2002-09-16 01:30:03 +0000 | [diff] [blame] | 794 | def testNtoH(self): |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 795 | # This just checks that htons etc. are their own inverse, |
| 796 | # when looking at the lower 16 or 32 bits. |
| 797 | sizes = {socket.htonl: 32, socket.ntohl: 32, |
| 798 | socket.htons: 16, socket.ntohs: 16} |
| 799 | for func, size in sizes.items(): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 800 | mask = (1<<size) - 1 |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 801 | for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): |
| 802 | self.assertEqual(i & mask, func(func(i&mask)) & mask) |
Jeremy Hylton | cbd5b89 | 2002-07-31 15:57:39 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 804 | swapped = func(mask) |
| 805 | self.assertEqual(swapped & mask, mask) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 806 | self.assertRaises(OverflowError, func, 1<<34) |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 808 | def testNtoHErrors(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 809 | good_values = [ 1, 2, 3, 1, 2, 3 ] |
| 810 | bad_values = [ -1, -2, -3, -1, -2, -3 ] |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 811 | for k in good_values: |
| 812 | socket.ntohl(k) |
| 813 | socket.ntohs(k) |
| 814 | socket.htonl(k) |
| 815 | socket.htons(k) |
| 816 | for k in bad_values: |
| 817 | self.assertRaises(OverflowError, socket.ntohl, k) |
| 818 | self.assertRaises(OverflowError, socket.ntohs, k) |
| 819 | self.assertRaises(OverflowError, socket.htonl, k) |
| 820 | self.assertRaises(OverflowError, socket.htons, k) |
| 821 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 822 | def testGetServBy(self): |
| 823 | eq = self.assertEqual |
| 824 | # Find one service that exists, then check all the related interfaces. |
| 825 | # I've ordered this by protocols that have both a tcp and udp |
| 826 | # protocol, at least for modern Linuxes. |
Victor Stinner | e674747 | 2011-08-21 00:39:18 +0200 | [diff] [blame] | 827 | if (sys.platform.startswith(('freebsd', 'netbsd')) |
| 828 | or sys.platform in ('linux', 'darwin')): |
Andrew MacIntyre | 18bf43c | 2004-07-12 12:10:30 +0000 | [diff] [blame] | 829 | # avoid the 'echo' service on this platform, as there is an |
| 830 | # assumption breaking non-standard port/protocol entry |
| 831 | services = ('daytime', 'qotd', 'domain') |
| 832 | else: |
| 833 | services = ('echo', 'daytime', 'domain') |
| 834 | for service in services: |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 835 | try: |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 836 | port = socket.getservbyname(service, 'tcp') |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 837 | break |
| 838 | except socket.error: |
| 839 | pass |
Skip Montanaro | 05eb401 | 2004-02-10 15:51:15 +0000 | [diff] [blame] | 840 | else: |
| 841 | raise socket.error |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 842 | # Try same call with optional protocol omitted |
| 843 | port2 = socket.getservbyname(service) |
| 844 | eq(port, port2) |
Terry Jan Reedy | c30b7b1 | 2013-03-11 17:57:08 -0400 | [diff] [blame] | 845 | # Try udp, but don't barf if it doesn't exist |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 846 | try: |
| 847 | udpport = socket.getservbyname(service, 'udp') |
| 848 | except socket.error: |
| 849 | udpport = None |
| 850 | else: |
| 851 | eq(udpport, port) |
| 852 | # Now make sure the lookup by port returns the same service name |
| 853 | eq(socket.getservbyport(port2), service) |
| 854 | eq(socket.getservbyport(port, 'tcp'), service) |
| 855 | if udpport is not None: |
| 856 | eq(socket.getservbyport(udpport, 'udp'), service) |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 857 | # Make sure getservbyport does not accept out of range ports. |
| 858 | self.assertRaises(OverflowError, socket.getservbyport, -1) |
| 859 | self.assertRaises(OverflowError, socket.getservbyport, 65536) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 860 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 861 | def testDefaultTimeout(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 862 | # Testing default timeout |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 863 | # The default timeout should initially be None |
| 864 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 865 | s = socket.socket() |
| 866 | self.assertEqual(s.gettimeout(), None) |
| 867 | s.close() |
| 868 | |
| 869 | # Set the default timeout to 10, and see if it propagates |
| 870 | socket.setdefaulttimeout(10) |
| 871 | self.assertEqual(socket.getdefaulttimeout(), 10) |
| 872 | s = socket.socket() |
| 873 | self.assertEqual(s.gettimeout(), 10) |
| 874 | s.close() |
| 875 | |
| 876 | # Reset the default timeout to None, and see if it propagates |
| 877 | socket.setdefaulttimeout(None) |
| 878 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 879 | s = socket.socket() |
| 880 | self.assertEqual(s.gettimeout(), None) |
| 881 | s.close() |
| 882 | |
| 883 | # Check that setting it to an invalid value raises ValueError |
| 884 | self.assertRaises(ValueError, socket.setdefaulttimeout, -1) |
| 885 | |
| 886 | # Check that setting it to an invalid type raises TypeError |
| 887 | self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") |
| 888 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 889 | def testIPv4_inet_aton_fourbytes(self): |
| 890 | if not hasattr(socket, 'inet_aton'): |
| 891 | return # No inet_aton, nothing to check |
| 892 | # Test that issue1008086 and issue767150 are fixed. |
| 893 | # It must return 4 bytes. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 894 | self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) |
| 895 | self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 896 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 897 | def testIPv4toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 898 | if not hasattr(socket, 'inet_pton'): |
| 899 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 900 | from socket import inet_aton as f, inet_pton, AF_INET |
| 901 | g = lambda a: inet_pton(AF_INET, a) |
| 902 | |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 903 | assertInvalid = lambda func,a: self.assertRaises( |
| 904 | (socket.error, ValueError), func, a |
| 905 | ) |
| 906 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 907 | self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) |
| 908 | self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) |
| 909 | self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
| 910 | self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) |
| 911 | self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 912 | assertInvalid(f, '0.0.0.') |
| 913 | assertInvalid(f, '300.0.0.0') |
| 914 | assertInvalid(f, 'a.0.0.0') |
| 915 | assertInvalid(f, '1.2.3.4.5') |
| 916 | assertInvalid(f, '::1') |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 917 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 918 | self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) |
| 919 | self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) |
| 920 | self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
| 921 | self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 922 | assertInvalid(g, '0.0.0.') |
| 923 | assertInvalid(g, '300.0.0.0') |
| 924 | assertInvalid(g, 'a.0.0.0') |
| 925 | assertInvalid(g, '1.2.3.4.5') |
| 926 | assertInvalid(g, '::1') |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 927 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 928 | def testIPv6toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 929 | if not hasattr(socket, 'inet_pton'): |
| 930 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 931 | try: |
| 932 | from socket import inet_pton, AF_INET6, has_ipv6 |
| 933 | if not has_ipv6: |
| 934 | return |
| 935 | except ImportError: |
| 936 | return |
| 937 | f = lambda a: inet_pton(AF_INET6, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 938 | assertInvalid = lambda a: self.assertRaises( |
| 939 | (socket.error, ValueError), f, a |
| 940 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 941 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 942 | self.assertEqual(b'\x00' * 16, f('::')) |
| 943 | self.assertEqual(b'\x00' * 16, f('0::0')) |
| 944 | self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) |
| 945 | self.assertEqual( |
Guido van Rossum | 6718062 | 2007-07-10 07:29:12 +0000 | [diff] [blame] | 946 | b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 947 | f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') |
| 948 | ) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 949 | self.assertEqual( |
| 950 | b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', |
| 951 | f('ad42:abc::127:0:254:2') |
| 952 | ) |
| 953 | self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) |
| 954 | assertInvalid('0x20::') |
| 955 | assertInvalid(':::') |
| 956 | assertInvalid('::0::') |
| 957 | assertInvalid('1::abc::') |
| 958 | assertInvalid('1::abc::def') |
| 959 | assertInvalid('1:2:3:4:5:6:') |
| 960 | assertInvalid('1:2:3:4:5:6') |
| 961 | assertInvalid('1:2:3:4:5:6:7:8:') |
| 962 | assertInvalid('1:2:3:4:5:6:7:8:0') |
| 963 | |
| 964 | self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', |
| 965 | f('::254.42.23.64') |
| 966 | ) |
| 967 | self.assertEqual( |
| 968 | b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', |
| 969 | f('42::a29b:254.42.23.64') |
| 970 | ) |
| 971 | self.assertEqual( |
| 972 | b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', |
| 973 | f('42:a8b9:0:2:ffff:a29b:254.42.23.64') |
| 974 | ) |
| 975 | assertInvalid('255.254.253.252') |
| 976 | assertInvalid('1::260.2.3.0') |
| 977 | assertInvalid('1::0.be.e.0') |
| 978 | assertInvalid('1:2:3:4:5:6:7:1.2.3.4') |
| 979 | assertInvalid('::1.2.3.4:0') |
| 980 | assertInvalid('0.100.200.0:3:4:5:6:7:8') |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 982 | def testStringToIPv4(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 983 | if not hasattr(socket, 'inet_ntop'): |
| 984 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 985 | from socket import inet_ntoa as f, inet_ntop, AF_INET |
| 986 | g = lambda a: inet_ntop(AF_INET, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 987 | assertInvalid = lambda func,a: self.assertRaises( |
| 988 | (socket.error, ValueError), func, a |
| 989 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 990 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 991 | self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) |
| 992 | self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) |
| 993 | self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) |
| 994 | self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 995 | assertInvalid(f, b'\x00' * 3) |
| 996 | assertInvalid(f, b'\x00' * 5) |
| 997 | assertInvalid(f, b'\x00' * 16) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 998 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 999 | self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) |
| 1000 | self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) |
| 1001 | self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 1002 | assertInvalid(g, b'\x00' * 3) |
| 1003 | assertInvalid(g, b'\x00' * 5) |
| 1004 | assertInvalid(g, b'\x00' * 16) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 1005 | |
| 1006 | def testStringToIPv6(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 1007 | if not hasattr(socket, 'inet_ntop'): |
| 1008 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 1009 | try: |
| 1010 | from socket import inet_ntop, AF_INET6, has_ipv6 |
| 1011 | if not has_ipv6: |
| 1012 | return |
| 1013 | except ImportError: |
| 1014 | return |
| 1015 | f = lambda a: inet_ntop(AF_INET6, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 1016 | assertInvalid = lambda a: self.assertRaises( |
| 1017 | (socket.error, ValueError), f, a |
| 1018 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 1019 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1020 | self.assertEqual('::', f(b'\x00' * 16)) |
| 1021 | self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) |
| 1022 | self.assertEqual( |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 1023 | 'aef:b01:506:1001:ffff:9997:55:170', |
Guido van Rossum | 6718062 | 2007-07-10 07:29:12 +0000 | [diff] [blame] | 1024 | f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 1025 | ) |
| 1026 | |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 1027 | assertInvalid(b'\x12' * 15) |
| 1028 | assertInvalid(b'\x12' * 17) |
| 1029 | assertInvalid(b'\x12' * 4) |
| 1030 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1031 | # XXX The following don't test module-level functionality... |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 1032 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 1033 | def testSockName(self): |
| 1034 | # Testing getsockname() |
Antoine Pitrou | ccc87b5 | 2011-05-10 23:37:11 +0200 | [diff] [blame] | 1035 | port = support.find_unused_port() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1036 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1037 | self.addCleanup(sock.close) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1038 | sock.bind(("0.0.0.0", port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1039 | name = sock.getsockname() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1040 | # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate |
| 1041 | # it reasonable to get the host's addr in addition to 0.0.0.0. |
| 1042 | # At least for eCos. This is required for the S/390 to pass. |
Georg Brandl | 89197fe | 2010-11-20 14:16:17 +0000 | [diff] [blame] | 1043 | try: |
| 1044 | my_ip_addr = socket.gethostbyname(socket.gethostname()) |
| 1045 | except socket.error: |
| 1046 | # Probably name lookup wasn't set up right; skip this test |
| 1047 | return |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1048 | self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1049 | self.assertEqual(name[1], port) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1050 | |
| 1051 | def testGetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1052 | # Testing getsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1053 | # We know a socket should start without reuse==0 |
| 1054 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1055 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1056 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1057 | self.assertFalse(reuse != 0, "initial mode is reuse") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1058 | |
| 1059 | def testSetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1060 | # Testing setsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1061 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1062 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1063 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1064 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1065 | self.assertFalse(reuse == 0, "failed to set reuse mode") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1066 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1067 | def testSendAfterClose(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1068 | # testing send() after close() with timeout |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1069 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1070 | sock.settimeout(1) |
| 1071 | sock.close() |
Guido van Rossum | 8a392d7 | 2007-11-21 22:09:45 +0000 | [diff] [blame] | 1072 | self.assertRaises(socket.error, sock.send, b"spam") |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1073 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1074 | def testNewAttributes(self): |
| 1075 | # testing .family, .type and .protocol |
| 1076 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1077 | self.assertEqual(sock.family, socket.AF_INET) |
| 1078 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 1079 | self.assertEqual(sock.proto, 0) |
| 1080 | sock.close() |
| 1081 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 1082 | def test_getsockaddrarg(self): |
| 1083 | host = '0.0.0.0' |
Antoine Pitrou | ccc87b5 | 2011-05-10 23:37:11 +0200 | [diff] [blame] | 1084 | port = support.find_unused_port() |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 1085 | big_port = port + 65536 |
| 1086 | neg_port = port - 65536 |
| 1087 | sock = socket.socket() |
| 1088 | try: |
| 1089 | self.assertRaises(OverflowError, sock.bind, (host, big_port)) |
| 1090 | self.assertRaises(OverflowError, sock.bind, (host, neg_port)) |
| 1091 | sock.bind((host, port)) |
| 1092 | finally: |
| 1093 | sock.close() |
| 1094 | |
Brian Curtin | 5ad0bd6 | 2010-11-05 15:47:45 +0000 | [diff] [blame] | 1095 | @unittest.skipUnless(os.name == "nt", "Windows specific") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 1096 | def test_sock_ioctl(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1097 | self.assertTrue(hasattr(socket.socket, 'ioctl')) |
| 1098 | self.assertTrue(hasattr(socket, 'SIO_RCVALL')) |
| 1099 | self.assertTrue(hasattr(socket, 'RCVALL_ON')) |
| 1100 | self.assertTrue(hasattr(socket, 'RCVALL_OFF')) |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 1101 | self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) |
| 1102 | s = socket.socket() |
Brian Curtin | 5ad0bd6 | 2010-11-05 15:47:45 +0000 | [diff] [blame] | 1103 | self.addCleanup(s.close) |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 1104 | self.assertRaises(ValueError, s.ioctl, -1, None) |
| 1105 | s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 1106 | |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 1107 | def testGetaddrinfo(self): |
| 1108 | try: |
| 1109 | socket.getaddrinfo('localhost', 80) |
| 1110 | except socket.gaierror as err: |
| 1111 | if err.errno == socket.EAI_SERVICE: |
| 1112 | # see http://bugs.python.org/issue1282647 |
| 1113 | self.skipTest("buggy libc version") |
| 1114 | raise |
| 1115 | # len of every sequence is supposed to be == 5 |
| 1116 | for info in socket.getaddrinfo(HOST, None): |
| 1117 | self.assertEqual(len(info), 5) |
| 1118 | # host can be a domain name, a string representation of an |
| 1119 | # IPv4/v6 address or None |
| 1120 | socket.getaddrinfo('localhost', 80) |
| 1121 | socket.getaddrinfo('127.0.0.1', 80) |
| 1122 | socket.getaddrinfo(None, 80) |
Antoine Pitrou | 9c39f3c | 2011-04-28 19:18:10 +0200 | [diff] [blame] | 1123 | if support.IPV6_ENABLED: |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 1124 | socket.getaddrinfo('::1', 80) |
| 1125 | # port can be a string service name such as "http", a numeric |
| 1126 | # port number or None |
| 1127 | socket.getaddrinfo(HOST, "http") |
| 1128 | socket.getaddrinfo(HOST, 80) |
| 1129 | socket.getaddrinfo(HOST, None) |
| 1130 | # test family and socktype filters |
| 1131 | infos = socket.getaddrinfo(HOST, None, socket.AF_INET) |
| 1132 | for family, _, _, _, _ in infos: |
| 1133 | self.assertEqual(family, socket.AF_INET) |
| 1134 | infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) |
| 1135 | for _, socktype, _, _, _ in infos: |
| 1136 | self.assertEqual(socktype, socket.SOCK_STREAM) |
| 1137 | # test proto and flags arguments |
Giampaolo Rodolà | 677d95c | 2010-08-16 05:08:11 +0000 | [diff] [blame] | 1138 | socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 1139 | socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) |
| 1140 | # a server willing to support both IPv4 and IPv6 will |
| 1141 | # usually do this |
| 1142 | socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
| 1143 | socket.AI_PASSIVE) |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 1144 | # test keyword arguments |
| 1145 | a = socket.getaddrinfo(HOST, None) |
| 1146 | b = socket.getaddrinfo(host=HOST, port=None) |
| 1147 | self.assertEqual(a, b) |
| 1148 | a = socket.getaddrinfo(HOST, None, socket.AF_INET) |
| 1149 | b = socket.getaddrinfo(HOST, None, family=socket.AF_INET) |
| 1150 | self.assertEqual(a, b) |
| 1151 | a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) |
| 1152 | b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM) |
| 1153 | self.assertEqual(a, b) |
| 1154 | a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) |
| 1155 | b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP) |
| 1156 | self.assertEqual(a, b) |
| 1157 | a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) |
| 1158 | b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE) |
| 1159 | self.assertEqual(a, b) |
| 1160 | a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
| 1161 | socket.AI_PASSIVE) |
| 1162 | b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC, |
| 1163 | type=socket.SOCK_STREAM, proto=0, |
| 1164 | flags=socket.AI_PASSIVE) |
| 1165 | self.assertEqual(a, b) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1166 | # Issue #6697. |
| 1167 | self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 1168 | |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 1169 | # Issue 17269 |
Ronald Oussoren | a822d366 | 2013-06-10 10:36:28 +0200 | [diff] [blame] | 1170 | if hasattr(socket, 'AI_NUMERICSERV'): |
| 1171 | socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV) |
Ronald Oussoren | 27a4ac5 | 2013-05-24 13:47:37 +0200 | [diff] [blame] | 1172 | |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 1173 | def test_getnameinfo(self): |
| 1174 | # only IP addresses are allowed |
| 1175 | self.assertRaises(socket.error, socket.getnameinfo, ('mail.python.org',0), 0) |
| 1176 | |
Daniel Stutzbach | 460ff3d | 2010-09-09 21:17:58 +0000 | [diff] [blame] | 1177 | @unittest.skipUnless(support.is_resource_enabled('network'), |
| 1178 | 'network is not enabled') |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 1179 | def test_idna(self): |
Nadeem Vawda | 61baebd | 2012-01-25 08:02:05 +0200 | [diff] [blame] | 1180 | # Check for internet access before running test (issue #12804). |
| 1181 | try: |
| 1182 | socket.gethostbyname('python.org') |
| 1183 | except socket.gaierror as e: |
| 1184 | if e.errno == socket.EAI_NODATA: |
| 1185 | self.skipTest('internet access required for this test') |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 1186 | # these should all be successful |
| 1187 | socket.gethostbyname('испытание.python.org') |
| 1188 | socket.gethostbyname_ex('испытание.python.org') |
Martin v. Löwis | 67e91ad | 2010-08-23 15:27:26 +0000 | [diff] [blame] | 1189 | socket.getaddrinfo('испытание.python.org',0,socket.AF_UNSPEC,socket.SOCK_STREAM) |
| 1190 | # this may not work if the forward lookup choses the IPv6 address, as that doesn't |
| 1191 | # have a reverse entry yet |
| 1192 | # socket.gethostbyaddr('испытание.python.org') |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 1193 | |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 1194 | def check_sendall_interrupted(self, with_timeout): |
| 1195 | # socketpair() is not stricly required, but it makes things easier. |
| 1196 | if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): |
| 1197 | self.skipTest("signal.alarm and socket.socketpair required for this test") |
| 1198 | # Our signal handlers clobber the C errno by calling a math function |
| 1199 | # with an invalid domain value. |
| 1200 | def ok_handler(*args): |
| 1201 | self.assertRaises(ValueError, math.acosh, 0) |
| 1202 | def raising_handler(*args): |
| 1203 | self.assertRaises(ValueError, math.acosh, 0) |
| 1204 | 1 // 0 |
| 1205 | c, s = socket.socketpair() |
| 1206 | old_alarm = signal.signal(signal.SIGALRM, raising_handler) |
| 1207 | try: |
| 1208 | if with_timeout: |
| 1209 | # Just above the one second minimum for signal.alarm |
| 1210 | c.settimeout(1.5) |
| 1211 | with self.assertRaises(ZeroDivisionError): |
| 1212 | signal.alarm(1) |
Charles-François Natali | 5fd2642 | 2013-08-29 19:01:40 +0200 | [diff] [blame] | 1213 | c.sendall(b"x" * support.SOCK_MAX_SIZE) |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 1214 | if with_timeout: |
| 1215 | signal.signal(signal.SIGALRM, ok_handler) |
| 1216 | signal.alarm(1) |
Charles-François Natali | 5fd2642 | 2013-08-29 19:01:40 +0200 | [diff] [blame] | 1217 | self.assertRaises(socket.timeout, c.sendall, |
| 1218 | b"x" * support.SOCK_MAX_SIZE) |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 1219 | finally: |
Victor Stinner | 71fe8c0 | 2013-08-24 15:52:47 +0200 | [diff] [blame] | 1220 | signal.alarm(0) |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 1221 | signal.signal(signal.SIGALRM, old_alarm) |
| 1222 | c.close() |
| 1223 | s.close() |
| 1224 | |
| 1225 | def test_sendall_interrupted(self): |
| 1226 | self.check_sendall_interrupted(False) |
| 1227 | |
| 1228 | def test_sendall_interrupted_with_timeout(self): |
| 1229 | self.check_sendall_interrupted(True) |
| 1230 | |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 1231 | def test_dealloc_warn(self): |
| 1232 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1233 | r = repr(sock) |
| 1234 | with self.assertWarns(ResourceWarning) as cm: |
| 1235 | sock = None |
| 1236 | support.gc_collect() |
| 1237 | self.assertIn(r, str(cm.warning.args[0])) |
| 1238 | # An open socket file object gets dereferenced after the socket |
| 1239 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1240 | f = sock.makefile('rb') |
| 1241 | r = repr(sock) |
| 1242 | sock = None |
| 1243 | support.gc_collect() |
| 1244 | with self.assertWarns(ResourceWarning): |
| 1245 | f = None |
| 1246 | support.gc_collect() |
| 1247 | |
Victor Stinner | c3a51ec | 2011-01-04 11:00:45 +0000 | [diff] [blame] | 1248 | def test_name_closed_socketio(self): |
| 1249 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 1250 | fp = sock.makefile("rb") |
| 1251 | fp.close() |
| 1252 | self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") |
| 1253 | |
Antoine Pitrou | 1e7ee9d | 2012-09-14 17:28:10 +0200 | [diff] [blame] | 1254 | def test_unusable_closed_socketio(self): |
| 1255 | with socket.socket() as sock: |
| 1256 | fp = sock.makefile("rb", buffering=0) |
| 1257 | self.assertTrue(fp.readable()) |
| 1258 | self.assertFalse(fp.writable()) |
| 1259 | self.assertFalse(fp.seekable()) |
| 1260 | fp.close() |
| 1261 | self.assertRaises(ValueError, fp.readable) |
| 1262 | self.assertRaises(ValueError, fp.writable) |
| 1263 | self.assertRaises(ValueError, fp.seekable) |
| 1264 | |
Antoine Pitrou | 6d58d64 | 2011-03-20 23:56:36 +0100 | [diff] [blame] | 1265 | def test_pickle(self): |
| 1266 | sock = socket.socket() |
| 1267 | with sock: |
| 1268 | for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1269 | self.assertRaises(TypeError, pickle.dumps, sock, protocol) |
| 1270 | |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 1271 | def test_listen_backlog(self): |
| 1272 | for backlog in 0, -1: |
| 1273 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1274 | srv.bind((HOST, 0)) |
| 1275 | srv.listen(backlog) |
| 1276 | srv.close() |
| 1277 | |
| 1278 | # Issue 15989 |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 1279 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1280 | srv.bind((HOST, 0)) |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 1281 | self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 1282 | srv.close() |
| 1283 | |
Charles-François Natali | 4266333 | 2012-01-02 15:57:30 +0100 | [diff] [blame] | 1284 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 1285 | def test_flowinfo(self): |
| 1286 | self.assertRaises(OverflowError, socket.getnameinfo, |
| 1287 | ('::1',0, 0xffffffff), 0) |
| 1288 | with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: |
| 1289 | self.assertRaises(OverflowError, s.bind, ('::1', 0, -10)) |
| 1290 | |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 1291 | |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1292 | @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') |
| 1293 | class BasicCANTest(unittest.TestCase): |
| 1294 | |
| 1295 | def testCrucialConstants(self): |
| 1296 | socket.AF_CAN |
| 1297 | socket.PF_CAN |
| 1298 | socket.CAN_RAW |
| 1299 | |
| 1300 | def testCreateSocket(self): |
| 1301 | with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: |
| 1302 | pass |
| 1303 | |
| 1304 | def testBindAny(self): |
| 1305 | with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: |
| 1306 | s.bind(('', )) |
| 1307 | |
| 1308 | def testTooLongInterfaceName(self): |
| 1309 | # most systems limit IFNAMSIZ to 16, take 1024 to be sure |
| 1310 | with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: |
Antoine Pitrou | ffa547e | 2011-10-11 22:43:37 +0200 | [diff] [blame] | 1311 | self.assertRaisesRegex(socket.error, 'interface name too long', |
| 1312 | s.bind, ('x' * 1024,)) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 1313 | |
| 1314 | @unittest.skipUnless(hasattr(socket, "CAN_RAW_LOOPBACK"), |
| 1315 | 'socket.CAN_RAW_LOOPBACK required for this test.') |
| 1316 | def testLoopback(self): |
| 1317 | with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: |
| 1318 | for loopback in (0, 1): |
| 1319 | s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK, |
| 1320 | loopback) |
| 1321 | self.assertEqual(loopback, |
| 1322 | s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_LOOPBACK)) |
| 1323 | |
| 1324 | @unittest.skipUnless(hasattr(socket, "CAN_RAW_FILTER"), |
| 1325 | 'socket.CAN_RAW_FILTER required for this test.') |
| 1326 | def testFilter(self): |
| 1327 | can_id, can_mask = 0x200, 0x700 |
| 1328 | can_filter = struct.pack("=II", can_id, can_mask) |
| 1329 | with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: |
| 1330 | s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, can_filter) |
| 1331 | self.assertEqual(can_filter, |
| 1332 | s.getsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FILTER, 8)) |
| 1333 | |
| 1334 | |
| 1335 | @unittest.skipUnless(HAVE_SOCKET_CAN, 'SocketCan required for this test.') |
| 1336 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 1337 | class CANTest(ThreadedCANSocketTest): |
| 1338 | |
| 1339 | """The CAN frame structure is defined in <linux/can.h>: |
| 1340 | |
| 1341 | struct can_frame { |
| 1342 | canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */ |
| 1343 | __u8 can_dlc; /* data length code: 0 .. 8 */ |
| 1344 | __u8 data[8] __attribute__((aligned(8))); |
| 1345 | }; |
| 1346 | """ |
| 1347 | can_frame_fmt = "=IB3x8s" |
| 1348 | |
| 1349 | def __init__(self, methodName='runTest'): |
| 1350 | ThreadedCANSocketTest.__init__(self, methodName=methodName) |
| 1351 | |
| 1352 | @classmethod |
| 1353 | def build_can_frame(cls, can_id, data): |
| 1354 | """Build a CAN frame.""" |
| 1355 | can_dlc = len(data) |
| 1356 | data = data.ljust(8, b'\x00') |
| 1357 | return struct.pack(cls.can_frame_fmt, can_id, can_dlc, data) |
| 1358 | |
| 1359 | @classmethod |
| 1360 | def dissect_can_frame(cls, frame): |
| 1361 | """Dissect a CAN frame.""" |
| 1362 | can_id, can_dlc, data = struct.unpack(cls.can_frame_fmt, frame) |
| 1363 | return (can_id, can_dlc, data[:can_dlc]) |
| 1364 | |
| 1365 | def testSendFrame(self): |
| 1366 | cf, addr = self.s.recvfrom(self.bufsize) |
| 1367 | self.assertEqual(self.cf, cf) |
| 1368 | self.assertEqual(addr[0], self.interface) |
| 1369 | self.assertEqual(addr[1], socket.AF_CAN) |
| 1370 | |
| 1371 | def _testSendFrame(self): |
| 1372 | self.cf = self.build_can_frame(0x00, b'\x01\x02\x03\x04\x05') |
| 1373 | self.cli.send(self.cf) |
| 1374 | |
| 1375 | def testSendMaxFrame(self): |
| 1376 | cf, addr = self.s.recvfrom(self.bufsize) |
| 1377 | self.assertEqual(self.cf, cf) |
| 1378 | |
| 1379 | def _testSendMaxFrame(self): |
| 1380 | self.cf = self.build_can_frame(0x00, b'\x07' * 8) |
| 1381 | self.cli.send(self.cf) |
| 1382 | |
| 1383 | def testSendMultiFrames(self): |
| 1384 | cf, addr = self.s.recvfrom(self.bufsize) |
| 1385 | self.assertEqual(self.cf1, cf) |
| 1386 | |
| 1387 | cf, addr = self.s.recvfrom(self.bufsize) |
| 1388 | self.assertEqual(self.cf2, cf) |
| 1389 | |
| 1390 | def _testSendMultiFrames(self): |
| 1391 | self.cf1 = self.build_can_frame(0x07, b'\x44\x33\x22\x11') |
| 1392 | self.cli.send(self.cf1) |
| 1393 | |
| 1394 | self.cf2 = self.build_can_frame(0x12, b'\x99\x22\x33') |
| 1395 | self.cli.send(self.cf2) |
| 1396 | |
| 1397 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1398 | @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') |
| 1399 | class BasicRDSTest(unittest.TestCase): |
| 1400 | |
| 1401 | def testCrucialConstants(self): |
| 1402 | socket.AF_RDS |
| 1403 | socket.PF_RDS |
| 1404 | |
| 1405 | def testCreateSocket(self): |
| 1406 | with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: |
| 1407 | pass |
| 1408 | |
| 1409 | def testSocketBufferSize(self): |
| 1410 | bufsize = 16384 |
| 1411 | with socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) as s: |
| 1412 | s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, bufsize) |
| 1413 | s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, bufsize) |
| 1414 | |
| 1415 | |
| 1416 | @unittest.skipUnless(HAVE_SOCKET_RDS, 'RDS sockets required for this test.') |
| 1417 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 1418 | class RDSTest(ThreadedRDSSocketTest): |
| 1419 | |
| 1420 | def __init__(self, methodName='runTest'): |
| 1421 | ThreadedRDSSocketTest.__init__(self, methodName=methodName) |
| 1422 | |
Charles-François Natali | 240c55f | 2011-11-10 20:33:36 +0100 | [diff] [blame] | 1423 | def setUp(self): |
| 1424 | super().setUp() |
| 1425 | self.evt = threading.Event() |
| 1426 | |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 1427 | def testSendAndRecv(self): |
| 1428 | data, addr = self.serv.recvfrom(self.bufsize) |
| 1429 | self.assertEqual(self.data, data) |
| 1430 | self.assertEqual(self.cli_addr, addr) |
| 1431 | |
| 1432 | def _testSendAndRecv(self): |
| 1433 | self.data = b'spam' |
| 1434 | self.cli.sendto(self.data, 0, (HOST, self.port)) |
| 1435 | |
| 1436 | def testPeek(self): |
| 1437 | data, addr = self.serv.recvfrom(self.bufsize, socket.MSG_PEEK) |
| 1438 | self.assertEqual(self.data, data) |
| 1439 | data, addr = self.serv.recvfrom(self.bufsize) |
| 1440 | self.assertEqual(self.data, data) |
| 1441 | |
| 1442 | def _testPeek(self): |
| 1443 | self.data = b'spam' |
| 1444 | self.cli.sendto(self.data, 0, (HOST, self.port)) |
| 1445 | |
| 1446 | @requireAttrs(socket.socket, 'recvmsg') |
| 1447 | def testSendAndRecvMsg(self): |
| 1448 | data, ancdata, msg_flags, addr = self.serv.recvmsg(self.bufsize) |
| 1449 | self.assertEqual(self.data, data) |
| 1450 | |
| 1451 | @requireAttrs(socket.socket, 'sendmsg') |
| 1452 | def _testSendAndRecvMsg(self): |
| 1453 | self.data = b'hello ' * 10 |
| 1454 | self.cli.sendmsg([self.data], (), 0, (HOST, self.port)) |
| 1455 | |
| 1456 | def testSendAndRecvMulti(self): |
| 1457 | data, addr = self.serv.recvfrom(self.bufsize) |
| 1458 | self.assertEqual(self.data1, data) |
| 1459 | |
| 1460 | data, addr = self.serv.recvfrom(self.bufsize) |
| 1461 | self.assertEqual(self.data2, data) |
| 1462 | |
| 1463 | def _testSendAndRecvMulti(self): |
| 1464 | self.data1 = b'bacon' |
| 1465 | self.cli.sendto(self.data1, 0, (HOST, self.port)) |
| 1466 | |
| 1467 | self.data2 = b'egg' |
| 1468 | self.cli.sendto(self.data2, 0, (HOST, self.port)) |
| 1469 | |
| 1470 | def testSelect(self): |
| 1471 | r, w, x = select.select([self.serv], [], [], 3.0) |
| 1472 | self.assertIn(self.serv, r) |
| 1473 | data, addr = self.serv.recvfrom(self.bufsize) |
| 1474 | self.assertEqual(self.data, data) |
| 1475 | |
| 1476 | def _testSelect(self): |
| 1477 | self.data = b'select' |
| 1478 | self.cli.sendto(self.data, 0, (HOST, self.port)) |
| 1479 | |
| 1480 | def testCongestion(self): |
| 1481 | # wait until the sender is done |
| 1482 | self.evt.wait() |
| 1483 | |
| 1484 | def _testCongestion(self): |
| 1485 | # test the behavior in case of congestion |
| 1486 | self.data = b'fill' |
| 1487 | self.cli.setblocking(False) |
| 1488 | try: |
| 1489 | # try to lower the receiver's socket buffer size |
| 1490 | self.cli.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 16384) |
| 1491 | except OSError: |
| 1492 | pass |
| 1493 | with self.assertRaises(OSError) as cm: |
| 1494 | try: |
| 1495 | # fill the receiver's socket buffer |
| 1496 | while True: |
| 1497 | self.cli.sendto(self.data, 0, (HOST, self.port)) |
| 1498 | finally: |
| 1499 | # signal the receiver we're done |
| 1500 | self.evt.set() |
| 1501 | # sendto() should have failed with ENOBUFS |
| 1502 | self.assertEqual(cm.exception.errno, errno.ENOBUFS) |
| 1503 | # and we should have received a congestion notification through poll |
| 1504 | r, w, x = select.select([self.serv], [], [], 3.0) |
| 1505 | self.assertIn(self.serv, r) |
| 1506 | |
| 1507 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1508 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1509 | class BasicTCPTest(SocketConnectedTest): |
| 1510 | |
| 1511 | def __init__(self, methodName='runTest'): |
| 1512 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1513 | |
| 1514 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1515 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1516 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1517 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1518 | |
| 1519 | def _testRecv(self): |
| 1520 | self.serv_conn.send(MSG) |
| 1521 | |
| 1522 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1523 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1524 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 1525 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 1526 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1527 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1528 | |
| 1529 | def _testOverFlowRecv(self): |
| 1530 | self.serv_conn.send(MSG) |
| 1531 | |
| 1532 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1533 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1534 | msg, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1535 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1536 | |
| 1537 | def _testRecvFrom(self): |
| 1538 | self.serv_conn.send(MSG) |
| 1539 | |
| 1540 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1541 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1542 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 1543 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 1544 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1545 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1546 | |
| 1547 | def _testOverFlowRecvFrom(self): |
| 1548 | self.serv_conn.send(MSG) |
| 1549 | |
| 1550 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1551 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1552 | msg = b'' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1553 | while 1: |
| 1554 | read = self.cli_conn.recv(1024) |
| 1555 | if not read: |
| 1556 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 1557 | msg += read |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1558 | self.assertEqual(msg, b'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1559 | |
| 1560 | def _testSendAll(self): |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1561 | big_chunk = b'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1562 | self.serv_conn.sendall(big_chunk) |
| 1563 | |
| 1564 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1565 | # Testing fromfd() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1566 | fd = self.cli_conn.fileno() |
| 1567 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1568 | self.addCleanup(sock.close) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 1569 | self.assertIsInstance(sock, socket.socket) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1570 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1571 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1572 | |
| 1573 | def _testFromFd(self): |
| 1574 | self.serv_conn.send(MSG) |
| 1575 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1576 | def testDup(self): |
| 1577 | # Testing dup() |
| 1578 | sock = self.cli_conn.dup() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1579 | self.addCleanup(sock.close) |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 1580 | msg = sock.recv(1024) |
| 1581 | self.assertEqual(msg, MSG) |
| 1582 | |
| 1583 | def _testDup(self): |
| 1584 | self.serv_conn.send(MSG) |
| 1585 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1586 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1587 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1588 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1589 | self.assertEqual(msg, MSG) |
Mark Dickinson | 29500f6 | 2009-01-15 15:36:10 +0000 | [diff] [blame] | 1590 | # wait for _testShutdown to finish: on OS X, when the server |
| 1591 | # closes the connection the client also becomes disconnected, |
| 1592 | # and the client's shutdown call will fail. (Issue #4397.) |
| 1593 | self.done.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1594 | |
| 1595 | def _testShutdown(self): |
| 1596 | self.serv_conn.send(MSG) |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 1597 | # Issue 15989 |
| 1598 | self.assertRaises(OverflowError, self.serv_conn.shutdown, |
| 1599 | _testcapi.INT_MAX + 1) |
| 1600 | self.assertRaises(OverflowError, self.serv_conn.shutdown, |
| 1601 | 2 + (_testcapi.UINT_MAX + 1)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1602 | self.serv_conn.shutdown(2) |
| 1603 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1604 | def testDetach(self): |
| 1605 | # Testing detach() |
| 1606 | fileno = self.cli_conn.fileno() |
| 1607 | f = self.cli_conn.detach() |
| 1608 | self.assertEqual(f, fileno) |
| 1609 | # cli_conn cannot be used anymore... |
Antoine Pitrou | 70deb3d | 2012-04-01 01:00:17 +0200 | [diff] [blame] | 1610 | self.assertTrue(self.cli_conn._closed) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1611 | self.assertRaises(socket.error, self.cli_conn.recv, 1024) |
| 1612 | self.cli_conn.close() |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1613 | # ...but we can create another socket using the (still open) |
| 1614 | # file descriptor |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1615 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1616 | self.addCleanup(sock.close) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1617 | msg = sock.recv(1024) |
| 1618 | self.assertEqual(msg, MSG) |
| 1619 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 1620 | def _testDetach(self): |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 1621 | self.serv_conn.send(MSG) |
| 1622 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1623 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1624 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 1625 | |
| 1626 | def __init__(self, methodName='runTest'): |
| 1627 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 1628 | |
| 1629 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1630 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1631 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1632 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1633 | |
| 1634 | def _testSendtoAndRecv(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1635 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1636 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 1637 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1638 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1639 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1640 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1641 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 1642 | def _testRecvFrom(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1643 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1644 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1645 | def testRecvFromNegative(self): |
| 1646 | # Negative lengths passed to recvfrom should give ValueError. |
| 1647 | self.assertRaises(ValueError, self.serv.recvfrom, -1) |
| 1648 | |
| 1649 | def _testRecvFromNegative(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1650 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1651 | |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 1652 | # Tests for the sendmsg()/recvmsg() interface. Where possible, the |
| 1653 | # same test code is used with different families and types of socket |
| 1654 | # (e.g. stream, datagram), and tests using recvmsg() are repeated |
| 1655 | # using recvmsg_into(). |
| 1656 | # |
| 1657 | # The generic test classes such as SendmsgTests and |
| 1658 | # RecvmsgGenericTests inherit from SendrecvmsgBase and expect to be |
| 1659 | # supplied with sockets cli_sock and serv_sock representing the |
| 1660 | # client's and the server's end of the connection respectively, and |
| 1661 | # attributes cli_addr and serv_addr holding their (numeric where |
| 1662 | # appropriate) addresses. |
| 1663 | # |
| 1664 | # The final concrete test classes combine these with subclasses of |
| 1665 | # SocketTestBase which set up client and server sockets of a specific |
| 1666 | # type, and with subclasses of SendrecvmsgBase such as |
| 1667 | # SendrecvmsgDgramBase and SendrecvmsgConnectedBase which map these |
| 1668 | # sockets to cli_sock and serv_sock and override the methods and |
| 1669 | # attributes of SendrecvmsgBase to fill in destination addresses if |
| 1670 | # needed when sending, check for specific flags in msg_flags, etc. |
| 1671 | # |
| 1672 | # RecvmsgIntoMixin provides a version of doRecvmsg() implemented using |
| 1673 | # recvmsg_into(). |
| 1674 | |
| 1675 | # XXX: like the other datagram (UDP) tests in this module, the code |
| 1676 | # here assumes that datagram delivery on the local machine will be |
| 1677 | # reliable. |
| 1678 | |
| 1679 | class SendrecvmsgBase(ThreadSafeCleanupTestCase): |
| 1680 | # Base class for sendmsg()/recvmsg() tests. |
| 1681 | |
| 1682 | # Time in seconds to wait before considering a test failed, or |
| 1683 | # None for no timeout. Not all tests actually set a timeout. |
| 1684 | fail_timeout = 3.0 |
| 1685 | |
| 1686 | def setUp(self): |
| 1687 | self.misc_event = threading.Event() |
| 1688 | super().setUp() |
| 1689 | |
| 1690 | def sendToServer(self, msg): |
| 1691 | # Send msg to the server. |
| 1692 | return self.cli_sock.send(msg) |
| 1693 | |
| 1694 | # Tuple of alternative default arguments for sendmsg() when called |
| 1695 | # via sendmsgToServer() (e.g. to include a destination address). |
| 1696 | sendmsg_to_server_defaults = () |
| 1697 | |
| 1698 | def sendmsgToServer(self, *args): |
| 1699 | # Call sendmsg() on self.cli_sock with the given arguments, |
| 1700 | # filling in any arguments which are not supplied with the |
| 1701 | # corresponding items of self.sendmsg_to_server_defaults, if |
| 1702 | # any. |
| 1703 | return self.cli_sock.sendmsg( |
| 1704 | *(args + self.sendmsg_to_server_defaults[len(args):])) |
| 1705 | |
| 1706 | def doRecvmsg(self, sock, bufsize, *args): |
| 1707 | # Call recvmsg() on sock with given arguments and return its |
| 1708 | # result. Should be used for tests which can use either |
| 1709 | # recvmsg() or recvmsg_into() - RecvmsgIntoMixin overrides |
| 1710 | # this method with one which emulates it using recvmsg_into(), |
| 1711 | # thus allowing the same test to be used for both methods. |
| 1712 | result = sock.recvmsg(bufsize, *args) |
| 1713 | self.registerRecvmsgResult(result) |
| 1714 | return result |
| 1715 | |
| 1716 | def registerRecvmsgResult(self, result): |
| 1717 | # Called by doRecvmsg() with the return value of recvmsg() or |
| 1718 | # recvmsg_into(). Can be overridden to arrange cleanup based |
| 1719 | # on the returned ancillary data, for instance. |
| 1720 | pass |
| 1721 | |
| 1722 | def checkRecvmsgAddress(self, addr1, addr2): |
| 1723 | # Called to compare the received address with the address of |
| 1724 | # the peer. |
| 1725 | self.assertEqual(addr1, addr2) |
| 1726 | |
| 1727 | # Flags that are normally unset in msg_flags |
| 1728 | msg_flags_common_unset = 0 |
| 1729 | for name in ("MSG_CTRUNC", "MSG_OOB"): |
| 1730 | msg_flags_common_unset |= getattr(socket, name, 0) |
| 1731 | |
| 1732 | # Flags that are normally set |
| 1733 | msg_flags_common_set = 0 |
| 1734 | |
| 1735 | # Flags set when a complete record has been received (e.g. MSG_EOR |
| 1736 | # for SCTP) |
| 1737 | msg_flags_eor_indicator = 0 |
| 1738 | |
| 1739 | # Flags set when a complete record has not been received |
| 1740 | # (e.g. MSG_TRUNC for datagram sockets) |
| 1741 | msg_flags_non_eor_indicator = 0 |
| 1742 | |
| 1743 | def checkFlags(self, flags, eor=None, checkset=0, checkunset=0, ignore=0): |
| 1744 | # Method to check the value of msg_flags returned by recvmsg[_into](). |
| 1745 | # |
| 1746 | # Checks that all bits in msg_flags_common_set attribute are |
| 1747 | # set in "flags" and all bits in msg_flags_common_unset are |
| 1748 | # unset. |
| 1749 | # |
| 1750 | # The "eor" argument specifies whether the flags should |
| 1751 | # indicate that a full record (or datagram) has been received. |
| 1752 | # If "eor" is None, no checks are done; otherwise, checks |
| 1753 | # that: |
| 1754 | # |
| 1755 | # * if "eor" is true, all bits in msg_flags_eor_indicator are |
| 1756 | # set and all bits in msg_flags_non_eor_indicator are unset |
| 1757 | # |
| 1758 | # * if "eor" is false, all bits in msg_flags_non_eor_indicator |
| 1759 | # are set and all bits in msg_flags_eor_indicator are unset |
| 1760 | # |
| 1761 | # If "checkset" and/or "checkunset" are supplied, they require |
| 1762 | # the given bits to be set or unset respectively, overriding |
| 1763 | # what the attributes require for those bits. |
| 1764 | # |
| 1765 | # If any bits are set in "ignore", they will not be checked, |
| 1766 | # regardless of the other inputs. |
| 1767 | # |
| 1768 | # Will raise Exception if the inputs require a bit to be both |
| 1769 | # set and unset, and it is not ignored. |
| 1770 | |
| 1771 | defaultset = self.msg_flags_common_set |
| 1772 | defaultunset = self.msg_flags_common_unset |
| 1773 | |
| 1774 | if eor: |
| 1775 | defaultset |= self.msg_flags_eor_indicator |
| 1776 | defaultunset |= self.msg_flags_non_eor_indicator |
| 1777 | elif eor is not None: |
| 1778 | defaultset |= self.msg_flags_non_eor_indicator |
| 1779 | defaultunset |= self.msg_flags_eor_indicator |
| 1780 | |
| 1781 | # Function arguments override defaults |
| 1782 | defaultset &= ~checkunset |
| 1783 | defaultunset &= ~checkset |
| 1784 | |
| 1785 | # Merge arguments with remaining defaults, and check for conflicts |
| 1786 | checkset |= defaultset |
| 1787 | checkunset |= defaultunset |
| 1788 | inboth = checkset & checkunset & ~ignore |
| 1789 | if inboth: |
| 1790 | raise Exception("contradictory set, unset requirements for flags " |
| 1791 | "{0:#x}".format(inboth)) |
| 1792 | |
| 1793 | # Compare with given msg_flags value |
| 1794 | mask = (checkset | checkunset) & ~ignore |
| 1795 | self.assertEqual(flags & mask, checkset & mask) |
| 1796 | |
| 1797 | |
| 1798 | class RecvmsgIntoMixin(SendrecvmsgBase): |
| 1799 | # Mixin to implement doRecvmsg() using recvmsg_into(). |
| 1800 | |
| 1801 | def doRecvmsg(self, sock, bufsize, *args): |
| 1802 | buf = bytearray(bufsize) |
| 1803 | result = sock.recvmsg_into([buf], *args) |
| 1804 | self.registerRecvmsgResult(result) |
| 1805 | self.assertGreaterEqual(result[0], 0) |
| 1806 | self.assertLessEqual(result[0], bufsize) |
| 1807 | return (bytes(buf[:result[0]]),) + result[1:] |
| 1808 | |
| 1809 | |
| 1810 | class SendrecvmsgDgramFlagsBase(SendrecvmsgBase): |
| 1811 | # Defines flags to be checked in msg_flags for datagram sockets. |
| 1812 | |
| 1813 | @property |
| 1814 | def msg_flags_non_eor_indicator(self): |
| 1815 | return super().msg_flags_non_eor_indicator | socket.MSG_TRUNC |
| 1816 | |
| 1817 | |
| 1818 | class SendrecvmsgSCTPFlagsBase(SendrecvmsgBase): |
| 1819 | # Defines flags to be checked in msg_flags for SCTP sockets. |
| 1820 | |
| 1821 | @property |
| 1822 | def msg_flags_eor_indicator(self): |
| 1823 | return super().msg_flags_eor_indicator | socket.MSG_EOR |
| 1824 | |
| 1825 | |
| 1826 | class SendrecvmsgConnectionlessBase(SendrecvmsgBase): |
| 1827 | # Base class for tests on connectionless-mode sockets. Users must |
| 1828 | # supply sockets on attributes cli and serv to be mapped to |
| 1829 | # cli_sock and serv_sock respectively. |
| 1830 | |
| 1831 | @property |
| 1832 | def serv_sock(self): |
| 1833 | return self.serv |
| 1834 | |
| 1835 | @property |
| 1836 | def cli_sock(self): |
| 1837 | return self.cli |
| 1838 | |
| 1839 | @property |
| 1840 | def sendmsg_to_server_defaults(self): |
| 1841 | return ([], [], 0, self.serv_addr) |
| 1842 | |
| 1843 | def sendToServer(self, msg): |
| 1844 | return self.cli_sock.sendto(msg, self.serv_addr) |
| 1845 | |
| 1846 | |
| 1847 | class SendrecvmsgConnectedBase(SendrecvmsgBase): |
| 1848 | # Base class for tests on connected sockets. Users must supply |
| 1849 | # sockets on attributes serv_conn and cli_conn (representing the |
| 1850 | # connections *to* the server and the client), to be mapped to |
| 1851 | # cli_sock and serv_sock respectively. |
| 1852 | |
| 1853 | @property |
| 1854 | def serv_sock(self): |
| 1855 | return self.cli_conn |
| 1856 | |
| 1857 | @property |
| 1858 | def cli_sock(self): |
| 1859 | return self.serv_conn |
| 1860 | |
| 1861 | def checkRecvmsgAddress(self, addr1, addr2): |
| 1862 | # Address is currently "unspecified" for a connected socket, |
| 1863 | # so we don't examine it |
| 1864 | pass |
| 1865 | |
| 1866 | |
| 1867 | class SendrecvmsgServerTimeoutBase(SendrecvmsgBase): |
| 1868 | # Base class to set a timeout on server's socket. |
| 1869 | |
| 1870 | def setUp(self): |
| 1871 | super().setUp() |
| 1872 | self.serv_sock.settimeout(self.fail_timeout) |
| 1873 | |
| 1874 | |
| 1875 | class SendmsgTests(SendrecvmsgServerTimeoutBase): |
| 1876 | # Tests for sendmsg() which can use any socket type and do not |
| 1877 | # involve recvmsg() or recvmsg_into(). |
| 1878 | |
| 1879 | def testSendmsg(self): |
| 1880 | # Send a simple message with sendmsg(). |
| 1881 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 1882 | |
| 1883 | def _testSendmsg(self): |
| 1884 | self.assertEqual(self.sendmsgToServer([MSG]), len(MSG)) |
| 1885 | |
| 1886 | def testSendmsgDataGenerator(self): |
| 1887 | # Send from buffer obtained from a generator (not a sequence). |
| 1888 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 1889 | |
| 1890 | def _testSendmsgDataGenerator(self): |
| 1891 | self.assertEqual(self.sendmsgToServer((o for o in [MSG])), |
| 1892 | len(MSG)) |
| 1893 | |
| 1894 | def testSendmsgAncillaryGenerator(self): |
| 1895 | # Gather (empty) ancillary data from a generator. |
| 1896 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 1897 | |
| 1898 | def _testSendmsgAncillaryGenerator(self): |
| 1899 | self.assertEqual(self.sendmsgToServer([MSG], (o for o in [])), |
| 1900 | len(MSG)) |
| 1901 | |
| 1902 | def testSendmsgArray(self): |
| 1903 | # Send data from an array instead of the usual bytes object. |
| 1904 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 1905 | |
| 1906 | def _testSendmsgArray(self): |
| 1907 | self.assertEqual(self.sendmsgToServer([array.array("B", MSG)]), |
| 1908 | len(MSG)) |
| 1909 | |
| 1910 | def testSendmsgGather(self): |
| 1911 | # Send message data from more than one buffer (gather write). |
| 1912 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 1913 | |
| 1914 | def _testSendmsgGather(self): |
| 1915 | self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) |
| 1916 | |
| 1917 | def testSendmsgBadArgs(self): |
| 1918 | # Check that sendmsg() rejects invalid arguments. |
| 1919 | self.assertEqual(self.serv_sock.recv(1000), b"done") |
| 1920 | |
| 1921 | def _testSendmsgBadArgs(self): |
| 1922 | self.assertRaises(TypeError, self.cli_sock.sendmsg) |
| 1923 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1924 | b"not in an iterable") |
| 1925 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1926 | object()) |
| 1927 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1928 | [object()]) |
| 1929 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1930 | [MSG, object()]) |
| 1931 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1932 | [MSG], object()) |
| 1933 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1934 | [MSG], [], object()) |
| 1935 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1936 | [MSG], [], 0, object()) |
| 1937 | self.sendToServer(b"done") |
| 1938 | |
| 1939 | def testSendmsgBadCmsg(self): |
| 1940 | # Check that invalid ancillary data items are rejected. |
| 1941 | self.assertEqual(self.serv_sock.recv(1000), b"done") |
| 1942 | |
| 1943 | def _testSendmsgBadCmsg(self): |
| 1944 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1945 | [MSG], [object()]) |
| 1946 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1947 | [MSG], [(object(), 0, b"data")]) |
| 1948 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1949 | [MSG], [(0, object(), b"data")]) |
| 1950 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1951 | [MSG], [(0, 0, object())]) |
| 1952 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1953 | [MSG], [(0, 0)]) |
| 1954 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1955 | [MSG], [(0, 0, b"data", 42)]) |
| 1956 | self.sendToServer(b"done") |
| 1957 | |
| 1958 | @requireAttrs(socket, "CMSG_SPACE") |
| 1959 | def testSendmsgBadMultiCmsg(self): |
| 1960 | # Check that invalid ancillary data items are rejected when |
| 1961 | # more than one item is present. |
| 1962 | self.assertEqual(self.serv_sock.recv(1000), b"done") |
| 1963 | |
| 1964 | @testSendmsgBadMultiCmsg.client_skip |
| 1965 | def _testSendmsgBadMultiCmsg(self): |
| 1966 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1967 | [MSG], [0, 0, b""]) |
| 1968 | self.assertRaises(TypeError, self.sendmsgToServer, |
| 1969 | [MSG], [(0, 0, b""), object()]) |
| 1970 | self.sendToServer(b"done") |
| 1971 | |
| 1972 | def testSendmsgExcessCmsgReject(self): |
| 1973 | # Check that sendmsg() rejects excess ancillary data items |
| 1974 | # when the number that can be sent is limited. |
| 1975 | self.assertEqual(self.serv_sock.recv(1000), b"done") |
| 1976 | |
| 1977 | def _testSendmsgExcessCmsgReject(self): |
| 1978 | if not hasattr(socket, "CMSG_SPACE"): |
| 1979 | # Can only send one item |
| 1980 | with self.assertRaises(socket.error) as cm: |
| 1981 | self.sendmsgToServer([MSG], [(0, 0, b""), (0, 0, b"")]) |
| 1982 | self.assertIsNone(cm.exception.errno) |
| 1983 | self.sendToServer(b"done") |
| 1984 | |
| 1985 | def testSendmsgAfterClose(self): |
| 1986 | # Check that sendmsg() fails on a closed socket. |
| 1987 | pass |
| 1988 | |
| 1989 | def _testSendmsgAfterClose(self): |
| 1990 | self.cli_sock.close() |
| 1991 | self.assertRaises(socket.error, self.sendmsgToServer, [MSG]) |
| 1992 | |
| 1993 | |
| 1994 | class SendmsgStreamTests(SendmsgTests): |
| 1995 | # Tests for sendmsg() which require a stream socket and do not |
| 1996 | # involve recvmsg() or recvmsg_into(). |
| 1997 | |
| 1998 | def testSendmsgExplicitNoneAddr(self): |
| 1999 | # Check that peer address can be specified as None. |
| 2000 | self.assertEqual(self.serv_sock.recv(len(MSG)), MSG) |
| 2001 | |
| 2002 | def _testSendmsgExplicitNoneAddr(self): |
| 2003 | self.assertEqual(self.sendmsgToServer([MSG], [], 0, None), len(MSG)) |
| 2004 | |
| 2005 | def testSendmsgTimeout(self): |
| 2006 | # Check that timeout works with sendmsg(). |
| 2007 | self.assertEqual(self.serv_sock.recv(512), b"a"*512) |
| 2008 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2009 | |
| 2010 | def _testSendmsgTimeout(self): |
| 2011 | try: |
| 2012 | self.cli_sock.settimeout(0.03) |
| 2013 | with self.assertRaises(socket.timeout): |
| 2014 | while True: |
| 2015 | self.sendmsgToServer([b"a"*512]) |
| 2016 | finally: |
| 2017 | self.misc_event.set() |
| 2018 | |
| 2019 | # XXX: would be nice to have more tests for sendmsg flags argument. |
| 2020 | |
| 2021 | # Linux supports MSG_DONTWAIT when sending, but in general, it |
| 2022 | # only works when receiving. Could add other platforms if they |
| 2023 | # support it too. |
| 2024 | @skipWithClientIf(sys.platform not in {"linux2"}, |
| 2025 | "MSG_DONTWAIT not known to work on this platform when " |
| 2026 | "sending") |
| 2027 | def testSendmsgDontWait(self): |
| 2028 | # Check that MSG_DONTWAIT in flags causes non-blocking behaviour. |
| 2029 | self.assertEqual(self.serv_sock.recv(512), b"a"*512) |
| 2030 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2031 | |
| 2032 | @testSendmsgDontWait.client_skip |
| 2033 | def _testSendmsgDontWait(self): |
| 2034 | try: |
| 2035 | with self.assertRaises(socket.error) as cm: |
| 2036 | while True: |
| 2037 | self.sendmsgToServer([b"a"*512], [], socket.MSG_DONTWAIT) |
| 2038 | self.assertIn(cm.exception.errno, |
| 2039 | (errno.EAGAIN, errno.EWOULDBLOCK)) |
| 2040 | finally: |
| 2041 | self.misc_event.set() |
| 2042 | |
| 2043 | |
| 2044 | class SendmsgConnectionlessTests(SendmsgTests): |
| 2045 | # Tests for sendmsg() which require a connectionless-mode |
| 2046 | # (e.g. datagram) socket, and do not involve recvmsg() or |
| 2047 | # recvmsg_into(). |
| 2048 | |
| 2049 | def testSendmsgNoDestAddr(self): |
| 2050 | # Check that sendmsg() fails when no destination address is |
| 2051 | # given for unconnected socket. |
| 2052 | pass |
| 2053 | |
| 2054 | def _testSendmsgNoDestAddr(self): |
| 2055 | self.assertRaises(socket.error, self.cli_sock.sendmsg, |
| 2056 | [MSG]) |
| 2057 | self.assertRaises(socket.error, self.cli_sock.sendmsg, |
| 2058 | [MSG], [], 0, None) |
| 2059 | |
| 2060 | |
| 2061 | class RecvmsgGenericTests(SendrecvmsgBase): |
| 2062 | # Tests for recvmsg() which can also be emulated using |
| 2063 | # recvmsg_into(), and can use any socket type. |
| 2064 | |
| 2065 | def testRecvmsg(self): |
| 2066 | # Receive a simple message with recvmsg[_into](). |
| 2067 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) |
| 2068 | self.assertEqual(msg, MSG) |
| 2069 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2070 | self.assertEqual(ancdata, []) |
| 2071 | self.checkFlags(flags, eor=True) |
| 2072 | |
| 2073 | def _testRecvmsg(self): |
| 2074 | self.sendToServer(MSG) |
| 2075 | |
| 2076 | def testRecvmsgExplicitDefaults(self): |
| 2077 | # Test recvmsg[_into]() with default arguments provided explicitly. |
| 2078 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2079 | len(MSG), 0, 0) |
| 2080 | self.assertEqual(msg, MSG) |
| 2081 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2082 | self.assertEqual(ancdata, []) |
| 2083 | self.checkFlags(flags, eor=True) |
| 2084 | |
| 2085 | def _testRecvmsgExplicitDefaults(self): |
| 2086 | self.sendToServer(MSG) |
| 2087 | |
| 2088 | def testRecvmsgShorter(self): |
| 2089 | # Receive a message smaller than buffer. |
| 2090 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2091 | len(MSG) + 42) |
| 2092 | self.assertEqual(msg, MSG) |
| 2093 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2094 | self.assertEqual(ancdata, []) |
| 2095 | self.checkFlags(flags, eor=True) |
| 2096 | |
| 2097 | def _testRecvmsgShorter(self): |
| 2098 | self.sendToServer(MSG) |
| 2099 | |
Charles-François Natali | 8619cd7 | 2011-10-03 19:43:15 +0200 | [diff] [blame] | 2100 | # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated |
| 2101 | # datagram is received (issue #13001). |
| 2102 | @support.requires_freebsd_version(8) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2103 | def testRecvmsgTrunc(self): |
| 2104 | # Receive part of message, check for truncation indicators. |
| 2105 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2106 | len(MSG) - 3) |
| 2107 | self.assertEqual(msg, MSG[:-3]) |
| 2108 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2109 | self.assertEqual(ancdata, []) |
| 2110 | self.checkFlags(flags, eor=False) |
| 2111 | |
Charles-François Natali | 8619cd7 | 2011-10-03 19:43:15 +0200 | [diff] [blame] | 2112 | @support.requires_freebsd_version(8) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2113 | def _testRecvmsgTrunc(self): |
| 2114 | self.sendToServer(MSG) |
| 2115 | |
| 2116 | def testRecvmsgShortAncillaryBuf(self): |
| 2117 | # Test ancillary data buffer too small to hold any ancillary data. |
| 2118 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2119 | len(MSG), 1) |
| 2120 | self.assertEqual(msg, MSG) |
| 2121 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2122 | self.assertEqual(ancdata, []) |
| 2123 | self.checkFlags(flags, eor=True) |
| 2124 | |
| 2125 | def _testRecvmsgShortAncillaryBuf(self): |
| 2126 | self.sendToServer(MSG) |
| 2127 | |
| 2128 | def testRecvmsgLongAncillaryBuf(self): |
| 2129 | # Test large ancillary data buffer. |
| 2130 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2131 | len(MSG), 10240) |
| 2132 | self.assertEqual(msg, MSG) |
| 2133 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2134 | self.assertEqual(ancdata, []) |
| 2135 | self.checkFlags(flags, eor=True) |
| 2136 | |
| 2137 | def _testRecvmsgLongAncillaryBuf(self): |
| 2138 | self.sendToServer(MSG) |
| 2139 | |
| 2140 | def testRecvmsgAfterClose(self): |
| 2141 | # Check that recvmsg[_into]() fails on a closed socket. |
| 2142 | self.serv_sock.close() |
| 2143 | self.assertRaises(socket.error, self.doRecvmsg, self.serv_sock, 1024) |
| 2144 | |
| 2145 | def _testRecvmsgAfterClose(self): |
| 2146 | pass |
| 2147 | |
| 2148 | def testRecvmsgTimeout(self): |
| 2149 | # Check that timeout works. |
| 2150 | try: |
| 2151 | self.serv_sock.settimeout(0.03) |
| 2152 | self.assertRaises(socket.timeout, |
| 2153 | self.doRecvmsg, self.serv_sock, len(MSG)) |
| 2154 | finally: |
| 2155 | self.misc_event.set() |
| 2156 | |
| 2157 | def _testRecvmsgTimeout(self): |
| 2158 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2159 | |
| 2160 | @requireAttrs(socket, "MSG_PEEK") |
| 2161 | def testRecvmsgPeek(self): |
| 2162 | # Check that MSG_PEEK in flags enables examination of pending |
| 2163 | # data without consuming it. |
| 2164 | |
| 2165 | # Receive part of data with MSG_PEEK. |
| 2166 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2167 | len(MSG) - 3, 0, |
| 2168 | socket.MSG_PEEK) |
| 2169 | self.assertEqual(msg, MSG[:-3]) |
| 2170 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2171 | self.assertEqual(ancdata, []) |
| 2172 | # Ignoring MSG_TRUNC here (so this test is the same for stream |
| 2173 | # and datagram sockets). Some wording in POSIX seems to |
| 2174 | # suggest that it needn't be set when peeking, but that may |
| 2175 | # just be a slip. |
| 2176 | self.checkFlags(flags, eor=False, |
| 2177 | ignore=getattr(socket, "MSG_TRUNC", 0)) |
| 2178 | |
| 2179 | # Receive all data with MSG_PEEK. |
| 2180 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2181 | len(MSG), 0, |
| 2182 | socket.MSG_PEEK) |
| 2183 | self.assertEqual(msg, MSG) |
| 2184 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2185 | self.assertEqual(ancdata, []) |
| 2186 | self.checkFlags(flags, eor=True) |
| 2187 | |
| 2188 | # Check that the same data can still be received normally. |
| 2189 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) |
| 2190 | self.assertEqual(msg, MSG) |
| 2191 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2192 | self.assertEqual(ancdata, []) |
| 2193 | self.checkFlags(flags, eor=True) |
| 2194 | |
| 2195 | @testRecvmsgPeek.client_skip |
| 2196 | def _testRecvmsgPeek(self): |
| 2197 | self.sendToServer(MSG) |
| 2198 | |
| 2199 | @requireAttrs(socket.socket, "sendmsg") |
| 2200 | def testRecvmsgFromSendmsg(self): |
| 2201 | # Test receiving with recvmsg[_into]() when message is sent |
| 2202 | # using sendmsg(). |
| 2203 | self.serv_sock.settimeout(self.fail_timeout) |
| 2204 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) |
| 2205 | self.assertEqual(msg, MSG) |
| 2206 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2207 | self.assertEqual(ancdata, []) |
| 2208 | self.checkFlags(flags, eor=True) |
| 2209 | |
| 2210 | @testRecvmsgFromSendmsg.client_skip |
| 2211 | def _testRecvmsgFromSendmsg(self): |
| 2212 | self.assertEqual(self.sendmsgToServer([MSG[:3], MSG[3:]]), len(MSG)) |
| 2213 | |
| 2214 | |
| 2215 | class RecvmsgGenericStreamTests(RecvmsgGenericTests): |
| 2216 | # Tests which require a stream socket and can use either recvmsg() |
| 2217 | # or recvmsg_into(). |
| 2218 | |
| 2219 | def testRecvmsgEOF(self): |
| 2220 | # Receive end-of-stream indicator (b"", peer socket closed). |
| 2221 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) |
| 2222 | self.assertEqual(msg, b"") |
| 2223 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2224 | self.assertEqual(ancdata, []) |
| 2225 | self.checkFlags(flags, eor=None) # Might not have end-of-record marker |
| 2226 | |
| 2227 | def _testRecvmsgEOF(self): |
| 2228 | self.cli_sock.close() |
| 2229 | |
| 2230 | def testRecvmsgOverflow(self): |
| 2231 | # Receive a message in more than one chunk. |
| 2232 | seg1, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2233 | len(MSG) - 3) |
| 2234 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2235 | self.assertEqual(ancdata, []) |
| 2236 | self.checkFlags(flags, eor=False) |
| 2237 | |
| 2238 | seg2, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, 1024) |
| 2239 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2240 | self.assertEqual(ancdata, []) |
| 2241 | self.checkFlags(flags, eor=True) |
| 2242 | |
| 2243 | msg = seg1 + seg2 |
| 2244 | self.assertEqual(msg, MSG) |
| 2245 | |
| 2246 | def _testRecvmsgOverflow(self): |
| 2247 | self.sendToServer(MSG) |
| 2248 | |
| 2249 | |
| 2250 | class RecvmsgTests(RecvmsgGenericTests): |
| 2251 | # Tests for recvmsg() which can use any socket type. |
| 2252 | |
| 2253 | def testRecvmsgBadArgs(self): |
| 2254 | # Check that recvmsg() rejects invalid arguments. |
| 2255 | self.assertRaises(TypeError, self.serv_sock.recvmsg) |
| 2256 | self.assertRaises(ValueError, self.serv_sock.recvmsg, |
| 2257 | -1, 0, 0) |
| 2258 | self.assertRaises(ValueError, self.serv_sock.recvmsg, |
| 2259 | len(MSG), -1, 0) |
| 2260 | self.assertRaises(TypeError, self.serv_sock.recvmsg, |
| 2261 | [bytearray(10)], 0, 0) |
| 2262 | self.assertRaises(TypeError, self.serv_sock.recvmsg, |
| 2263 | object(), 0, 0) |
| 2264 | self.assertRaises(TypeError, self.serv_sock.recvmsg, |
| 2265 | len(MSG), object(), 0) |
| 2266 | self.assertRaises(TypeError, self.serv_sock.recvmsg, |
| 2267 | len(MSG), 0, object()) |
| 2268 | |
| 2269 | msg, ancdata, flags, addr = self.serv_sock.recvmsg(len(MSG), 0, 0) |
| 2270 | self.assertEqual(msg, MSG) |
| 2271 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2272 | self.assertEqual(ancdata, []) |
| 2273 | self.checkFlags(flags, eor=True) |
| 2274 | |
| 2275 | def _testRecvmsgBadArgs(self): |
| 2276 | self.sendToServer(MSG) |
| 2277 | |
| 2278 | |
| 2279 | class RecvmsgIntoTests(RecvmsgIntoMixin, RecvmsgGenericTests): |
| 2280 | # Tests for recvmsg_into() which can use any socket type. |
| 2281 | |
| 2282 | def testRecvmsgIntoBadArgs(self): |
| 2283 | # Check that recvmsg_into() rejects invalid arguments. |
| 2284 | buf = bytearray(len(MSG)) |
| 2285 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into) |
| 2286 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2287 | len(MSG), 0, 0) |
| 2288 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2289 | buf, 0, 0) |
| 2290 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2291 | [object()], 0, 0) |
| 2292 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2293 | [b"I'm not writable"], 0, 0) |
| 2294 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2295 | [buf, object()], 0, 0) |
| 2296 | self.assertRaises(ValueError, self.serv_sock.recvmsg_into, |
| 2297 | [buf], -1, 0) |
| 2298 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2299 | [buf], object(), 0) |
| 2300 | self.assertRaises(TypeError, self.serv_sock.recvmsg_into, |
| 2301 | [buf], 0, object()) |
| 2302 | |
| 2303 | nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf], 0, 0) |
| 2304 | self.assertEqual(nbytes, len(MSG)) |
| 2305 | self.assertEqual(buf, bytearray(MSG)) |
| 2306 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2307 | self.assertEqual(ancdata, []) |
| 2308 | self.checkFlags(flags, eor=True) |
| 2309 | |
| 2310 | def _testRecvmsgIntoBadArgs(self): |
| 2311 | self.sendToServer(MSG) |
| 2312 | |
| 2313 | def testRecvmsgIntoGenerator(self): |
| 2314 | # Receive into buffer obtained from a generator (not a sequence). |
| 2315 | buf = bytearray(len(MSG)) |
| 2316 | nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( |
| 2317 | (o for o in [buf])) |
| 2318 | self.assertEqual(nbytes, len(MSG)) |
| 2319 | self.assertEqual(buf, bytearray(MSG)) |
| 2320 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2321 | self.assertEqual(ancdata, []) |
| 2322 | self.checkFlags(flags, eor=True) |
| 2323 | |
| 2324 | def _testRecvmsgIntoGenerator(self): |
| 2325 | self.sendToServer(MSG) |
| 2326 | |
| 2327 | def testRecvmsgIntoArray(self): |
| 2328 | # Receive into an array rather than the usual bytearray. |
| 2329 | buf = array.array("B", [0] * len(MSG)) |
| 2330 | nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into([buf]) |
| 2331 | self.assertEqual(nbytes, len(MSG)) |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2332 | self.assertEqual(buf.tobytes(), MSG) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2333 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2334 | self.assertEqual(ancdata, []) |
| 2335 | self.checkFlags(flags, eor=True) |
| 2336 | |
| 2337 | def _testRecvmsgIntoArray(self): |
| 2338 | self.sendToServer(MSG) |
| 2339 | |
| 2340 | def testRecvmsgIntoScatter(self): |
| 2341 | # Receive into multiple buffers (scatter write). |
| 2342 | b1 = bytearray(b"----") |
| 2343 | b2 = bytearray(b"0123456789") |
| 2344 | b3 = bytearray(b"--------------") |
| 2345 | nbytes, ancdata, flags, addr = self.serv_sock.recvmsg_into( |
| 2346 | [b1, memoryview(b2)[2:9], b3]) |
| 2347 | self.assertEqual(nbytes, len(b"Mary had a little lamb")) |
| 2348 | self.assertEqual(b1, bytearray(b"Mary")) |
| 2349 | self.assertEqual(b2, bytearray(b"01 had a 9")) |
| 2350 | self.assertEqual(b3, bytearray(b"little lamb---")) |
| 2351 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2352 | self.assertEqual(ancdata, []) |
| 2353 | self.checkFlags(flags, eor=True) |
| 2354 | |
| 2355 | def _testRecvmsgIntoScatter(self): |
| 2356 | self.sendToServer(b"Mary had a little lamb") |
| 2357 | |
| 2358 | |
| 2359 | class CmsgMacroTests(unittest.TestCase): |
| 2360 | # Test the functions CMSG_LEN() and CMSG_SPACE(). Tests |
| 2361 | # assumptions used by sendmsg() and recvmsg[_into](), which share |
| 2362 | # code with these functions. |
| 2363 | |
| 2364 | # Match the definition in socketmodule.c |
| 2365 | socklen_t_limit = min(0x7fffffff, _testcapi.INT_MAX) |
| 2366 | |
| 2367 | @requireAttrs(socket, "CMSG_LEN") |
| 2368 | def testCMSG_LEN(self): |
| 2369 | # Test CMSG_LEN() with various valid and invalid values, |
| 2370 | # checking the assumptions used by recvmsg() and sendmsg(). |
| 2371 | toobig = self.socklen_t_limit - socket.CMSG_LEN(0) + 1 |
| 2372 | values = list(range(257)) + list(range(toobig - 257, toobig)) |
| 2373 | |
| 2374 | # struct cmsghdr has at least three members, two of which are ints |
| 2375 | self.assertGreater(socket.CMSG_LEN(0), array.array("i").itemsize * 2) |
| 2376 | for n in values: |
| 2377 | ret = socket.CMSG_LEN(n) |
| 2378 | # This is how recvmsg() calculates the data size |
| 2379 | self.assertEqual(ret - socket.CMSG_LEN(0), n) |
| 2380 | self.assertLessEqual(ret, self.socklen_t_limit) |
| 2381 | |
| 2382 | self.assertRaises(OverflowError, socket.CMSG_LEN, -1) |
| 2383 | # sendmsg() shares code with these functions, and requires |
| 2384 | # that it reject values over the limit. |
| 2385 | self.assertRaises(OverflowError, socket.CMSG_LEN, toobig) |
| 2386 | self.assertRaises(OverflowError, socket.CMSG_LEN, sys.maxsize) |
| 2387 | |
| 2388 | @requireAttrs(socket, "CMSG_SPACE") |
| 2389 | def testCMSG_SPACE(self): |
| 2390 | # Test CMSG_SPACE() with various valid and invalid values, |
| 2391 | # checking the assumptions used by sendmsg(). |
| 2392 | toobig = self.socklen_t_limit - socket.CMSG_SPACE(1) + 1 |
| 2393 | values = list(range(257)) + list(range(toobig - 257, toobig)) |
| 2394 | |
| 2395 | last = socket.CMSG_SPACE(0) |
| 2396 | # struct cmsghdr has at least three members, two of which are ints |
| 2397 | self.assertGreater(last, array.array("i").itemsize * 2) |
| 2398 | for n in values: |
| 2399 | ret = socket.CMSG_SPACE(n) |
| 2400 | self.assertGreaterEqual(ret, last) |
| 2401 | self.assertGreaterEqual(ret, socket.CMSG_LEN(n)) |
| 2402 | self.assertGreaterEqual(ret, n + socket.CMSG_LEN(0)) |
| 2403 | self.assertLessEqual(ret, self.socklen_t_limit) |
| 2404 | last = ret |
| 2405 | |
| 2406 | self.assertRaises(OverflowError, socket.CMSG_SPACE, -1) |
| 2407 | # sendmsg() shares code with these functions, and requires |
| 2408 | # that it reject values over the limit. |
| 2409 | self.assertRaises(OverflowError, socket.CMSG_SPACE, toobig) |
| 2410 | self.assertRaises(OverflowError, socket.CMSG_SPACE, sys.maxsize) |
| 2411 | |
| 2412 | |
| 2413 | class SCMRightsTest(SendrecvmsgServerTimeoutBase): |
| 2414 | # Tests for file descriptor passing on Unix-domain sockets. |
| 2415 | |
| 2416 | # Invalid file descriptor value that's unlikely to evaluate to a |
| 2417 | # real FD even if one of its bytes is replaced with a different |
| 2418 | # value (which shouldn't actually happen). |
| 2419 | badfd = -0x5555 |
| 2420 | |
| 2421 | def newFDs(self, n): |
| 2422 | # Return a list of n file descriptors for newly-created files |
| 2423 | # containing their list indices as ASCII numbers. |
| 2424 | fds = [] |
| 2425 | for i in range(n): |
| 2426 | fd, path = tempfile.mkstemp() |
| 2427 | self.addCleanup(os.unlink, path) |
| 2428 | self.addCleanup(os.close, fd) |
| 2429 | os.write(fd, str(i).encode()) |
| 2430 | fds.append(fd) |
| 2431 | return fds |
| 2432 | |
| 2433 | def checkFDs(self, fds): |
| 2434 | # Check that the file descriptors in the given list contain |
| 2435 | # their correct list indices as ASCII numbers. |
| 2436 | for n, fd in enumerate(fds): |
| 2437 | os.lseek(fd, 0, os.SEEK_SET) |
| 2438 | self.assertEqual(os.read(fd, 1024), str(n).encode()) |
| 2439 | |
| 2440 | def registerRecvmsgResult(self, result): |
| 2441 | self.addCleanup(self.closeRecvmsgFDs, result) |
| 2442 | |
| 2443 | def closeRecvmsgFDs(self, recvmsg_result): |
| 2444 | # Close all file descriptors specified in the ancillary data |
| 2445 | # of the given return value from recvmsg() or recvmsg_into(). |
| 2446 | for cmsg_level, cmsg_type, cmsg_data in recvmsg_result[1]: |
| 2447 | if (cmsg_level == socket.SOL_SOCKET and |
| 2448 | cmsg_type == socket.SCM_RIGHTS): |
| 2449 | fds = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2450 | fds.frombytes(cmsg_data[: |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2451 | len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) |
| 2452 | for fd in fds: |
| 2453 | os.close(fd) |
| 2454 | |
| 2455 | def createAndSendFDs(self, n): |
| 2456 | # Send n new file descriptors created by newFDs() to the |
| 2457 | # server, with the constant MSG as the non-ancillary data. |
| 2458 | self.assertEqual( |
| 2459 | self.sendmsgToServer([MSG], |
| 2460 | [(socket.SOL_SOCKET, |
| 2461 | socket.SCM_RIGHTS, |
| 2462 | array.array("i", self.newFDs(n)))]), |
| 2463 | len(MSG)) |
| 2464 | |
| 2465 | def checkRecvmsgFDs(self, numfds, result, maxcmsgs=1, ignoreflags=0): |
| 2466 | # Check that constant MSG was received with numfds file |
| 2467 | # descriptors in a maximum of maxcmsgs control messages (which |
| 2468 | # must contain only complete integers). By default, check |
| 2469 | # that MSG_CTRUNC is unset, but ignore any flags in |
| 2470 | # ignoreflags. |
| 2471 | msg, ancdata, flags, addr = result |
| 2472 | self.assertEqual(msg, MSG) |
| 2473 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2474 | self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, |
| 2475 | ignore=ignoreflags) |
| 2476 | |
| 2477 | self.assertIsInstance(ancdata, list) |
| 2478 | self.assertLessEqual(len(ancdata), maxcmsgs) |
| 2479 | fds = array.array("i") |
| 2480 | for item in ancdata: |
| 2481 | self.assertIsInstance(item, tuple) |
| 2482 | cmsg_level, cmsg_type, cmsg_data = item |
| 2483 | self.assertEqual(cmsg_level, socket.SOL_SOCKET) |
| 2484 | self.assertEqual(cmsg_type, socket.SCM_RIGHTS) |
| 2485 | self.assertIsInstance(cmsg_data, bytes) |
| 2486 | self.assertEqual(len(cmsg_data) % SIZEOF_INT, 0) |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2487 | fds.frombytes(cmsg_data) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2488 | |
| 2489 | self.assertEqual(len(fds), numfds) |
| 2490 | self.checkFDs(fds) |
| 2491 | |
| 2492 | def testFDPassSimple(self): |
| 2493 | # Pass a single FD (array read from bytes object). |
| 2494 | self.checkRecvmsgFDs(1, self.doRecvmsg(self.serv_sock, |
| 2495 | len(MSG), 10240)) |
| 2496 | |
| 2497 | def _testFDPassSimple(self): |
| 2498 | self.assertEqual( |
| 2499 | self.sendmsgToServer( |
| 2500 | [MSG], |
| 2501 | [(socket.SOL_SOCKET, |
| 2502 | socket.SCM_RIGHTS, |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2503 | array.array("i", self.newFDs(1)).tobytes())]), |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2504 | len(MSG)) |
| 2505 | |
| 2506 | def testMultipleFDPass(self): |
| 2507 | # Pass multiple FDs in a single array. |
| 2508 | self.checkRecvmsgFDs(4, self.doRecvmsg(self.serv_sock, |
| 2509 | len(MSG), 10240)) |
| 2510 | |
| 2511 | def _testMultipleFDPass(self): |
| 2512 | self.createAndSendFDs(4) |
| 2513 | |
| 2514 | @requireAttrs(socket, "CMSG_SPACE") |
| 2515 | def testFDPassCMSG_SPACE(self): |
| 2516 | # Test using CMSG_SPACE() to calculate ancillary buffer size. |
| 2517 | self.checkRecvmsgFDs( |
| 2518 | 4, self.doRecvmsg(self.serv_sock, len(MSG), |
| 2519 | socket.CMSG_SPACE(4 * SIZEOF_INT))) |
| 2520 | |
| 2521 | @testFDPassCMSG_SPACE.client_skip |
| 2522 | def _testFDPassCMSG_SPACE(self): |
| 2523 | self.createAndSendFDs(4) |
| 2524 | |
| 2525 | def testFDPassCMSG_LEN(self): |
| 2526 | # Test using CMSG_LEN() to calculate ancillary buffer size. |
| 2527 | self.checkRecvmsgFDs(1, |
| 2528 | self.doRecvmsg(self.serv_sock, len(MSG), |
| 2529 | socket.CMSG_LEN(4 * SIZEOF_INT)), |
| 2530 | # RFC 3542 says implementations may set |
| 2531 | # MSG_CTRUNC if there isn't enough space |
| 2532 | # for trailing padding. |
| 2533 | ignoreflags=socket.MSG_CTRUNC) |
| 2534 | |
| 2535 | def _testFDPassCMSG_LEN(self): |
| 2536 | self.createAndSendFDs(1) |
| 2537 | |
Charles-Francois Natali | dd225e4 | 2013-05-20 19:08:19 +0200 | [diff] [blame] | 2538 | @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2539 | @requireAttrs(socket, "CMSG_SPACE") |
| 2540 | def testFDPassSeparate(self): |
| 2541 | # Pass two FDs in two separate arrays. Arrays may be combined |
| 2542 | # into a single control message by the OS. |
| 2543 | self.checkRecvmsgFDs(2, |
| 2544 | self.doRecvmsg(self.serv_sock, len(MSG), 10240), |
| 2545 | maxcmsgs=2) |
| 2546 | |
| 2547 | @testFDPassSeparate.client_skip |
Charles-Francois Natali | dd225e4 | 2013-05-20 19:08:19 +0200 | [diff] [blame] | 2548 | @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2549 | def _testFDPassSeparate(self): |
| 2550 | fd0, fd1 = self.newFDs(2) |
| 2551 | self.assertEqual( |
| 2552 | self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, |
| 2553 | socket.SCM_RIGHTS, |
| 2554 | array.array("i", [fd0])), |
| 2555 | (socket.SOL_SOCKET, |
| 2556 | socket.SCM_RIGHTS, |
| 2557 | array.array("i", [fd1]))]), |
| 2558 | len(MSG)) |
| 2559 | |
Charles-Francois Natali | dd225e4 | 2013-05-20 19:08:19 +0200 | [diff] [blame] | 2560 | @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2561 | @requireAttrs(socket, "CMSG_SPACE") |
| 2562 | def testFDPassSeparateMinSpace(self): |
| 2563 | # Pass two FDs in two separate arrays, receiving them into the |
| 2564 | # minimum space for two arrays. |
| 2565 | self.checkRecvmsgFDs(2, |
| 2566 | self.doRecvmsg(self.serv_sock, len(MSG), |
| 2567 | socket.CMSG_SPACE(SIZEOF_INT) + |
| 2568 | socket.CMSG_LEN(SIZEOF_INT)), |
| 2569 | maxcmsgs=2, ignoreflags=socket.MSG_CTRUNC) |
| 2570 | |
| 2571 | @testFDPassSeparateMinSpace.client_skip |
Charles-Francois Natali | dd225e4 | 2013-05-20 19:08:19 +0200 | [diff] [blame] | 2572 | @unittest.skipIf(sys.platform == "darwin", "skipping, see issue #12958") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2573 | def _testFDPassSeparateMinSpace(self): |
| 2574 | fd0, fd1 = self.newFDs(2) |
| 2575 | self.assertEqual( |
| 2576 | self.sendmsgToServer([MSG], [(socket.SOL_SOCKET, |
| 2577 | socket.SCM_RIGHTS, |
| 2578 | array.array("i", [fd0])), |
| 2579 | (socket.SOL_SOCKET, |
| 2580 | socket.SCM_RIGHTS, |
| 2581 | array.array("i", [fd1]))]), |
| 2582 | len(MSG)) |
| 2583 | |
| 2584 | def sendAncillaryIfPossible(self, msg, ancdata): |
| 2585 | # Try to send msg and ancdata to server, but if the system |
| 2586 | # call fails, just send msg with no ancillary data. |
| 2587 | try: |
| 2588 | nbytes = self.sendmsgToServer([msg], ancdata) |
| 2589 | except socket.error as e: |
| 2590 | # Check that it was the system call that failed |
| 2591 | self.assertIsInstance(e.errno, int) |
| 2592 | nbytes = self.sendmsgToServer([msg]) |
| 2593 | self.assertEqual(nbytes, len(msg)) |
| 2594 | |
| 2595 | def testFDPassEmpty(self): |
| 2596 | # Try to pass an empty FD array. Can receive either no array |
| 2597 | # or an empty array. |
| 2598 | self.checkRecvmsgFDs(0, self.doRecvmsg(self.serv_sock, |
| 2599 | len(MSG), 10240), |
| 2600 | ignoreflags=socket.MSG_CTRUNC) |
| 2601 | |
| 2602 | def _testFDPassEmpty(self): |
| 2603 | self.sendAncillaryIfPossible(MSG, [(socket.SOL_SOCKET, |
| 2604 | socket.SCM_RIGHTS, |
| 2605 | b"")]) |
| 2606 | |
| 2607 | def testFDPassPartialInt(self): |
| 2608 | # Try to pass a truncated FD array. |
| 2609 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2610 | len(MSG), 10240) |
| 2611 | self.assertEqual(msg, MSG) |
| 2612 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2613 | self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) |
| 2614 | self.assertLessEqual(len(ancdata), 1) |
| 2615 | for cmsg_level, cmsg_type, cmsg_data in ancdata: |
| 2616 | self.assertEqual(cmsg_level, socket.SOL_SOCKET) |
| 2617 | self.assertEqual(cmsg_type, socket.SCM_RIGHTS) |
| 2618 | self.assertLess(len(cmsg_data), SIZEOF_INT) |
| 2619 | |
| 2620 | def _testFDPassPartialInt(self): |
| 2621 | self.sendAncillaryIfPossible( |
| 2622 | MSG, |
| 2623 | [(socket.SOL_SOCKET, |
| 2624 | socket.SCM_RIGHTS, |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2625 | array.array("i", [self.badfd]).tobytes()[:-1])]) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2626 | |
| 2627 | @requireAttrs(socket, "CMSG_SPACE") |
| 2628 | def testFDPassPartialIntInMiddle(self): |
| 2629 | # Try to pass two FD arrays, the first of which is truncated. |
| 2630 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2631 | len(MSG), 10240) |
| 2632 | self.assertEqual(msg, MSG) |
| 2633 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2634 | self.checkFlags(flags, eor=True, ignore=socket.MSG_CTRUNC) |
| 2635 | self.assertLessEqual(len(ancdata), 2) |
| 2636 | fds = array.array("i") |
| 2637 | # Arrays may have been combined in a single control message |
| 2638 | for cmsg_level, cmsg_type, cmsg_data in ancdata: |
| 2639 | self.assertEqual(cmsg_level, socket.SOL_SOCKET) |
| 2640 | self.assertEqual(cmsg_type, socket.SCM_RIGHTS) |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2641 | fds.frombytes(cmsg_data[: |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2642 | len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) |
| 2643 | self.assertLessEqual(len(fds), 2) |
| 2644 | self.checkFDs(fds) |
| 2645 | |
| 2646 | @testFDPassPartialIntInMiddle.client_skip |
| 2647 | def _testFDPassPartialIntInMiddle(self): |
| 2648 | fd0, fd1 = self.newFDs(2) |
| 2649 | self.sendAncillaryIfPossible( |
| 2650 | MSG, |
| 2651 | [(socket.SOL_SOCKET, |
| 2652 | socket.SCM_RIGHTS, |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2653 | array.array("i", [fd0, self.badfd]).tobytes()[:-1]), |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2654 | (socket.SOL_SOCKET, |
| 2655 | socket.SCM_RIGHTS, |
| 2656 | array.array("i", [fd1]))]) |
| 2657 | |
| 2658 | def checkTruncatedHeader(self, result, ignoreflags=0): |
| 2659 | # Check that no ancillary data items are returned when data is |
| 2660 | # truncated inside the cmsghdr structure. |
| 2661 | msg, ancdata, flags, addr = result |
| 2662 | self.assertEqual(msg, MSG) |
| 2663 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2664 | self.assertEqual(ancdata, []) |
| 2665 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, |
| 2666 | ignore=ignoreflags) |
| 2667 | |
| 2668 | def testCmsgTruncNoBufSize(self): |
| 2669 | # Check that no ancillary data is received when no buffer size |
| 2670 | # is specified. |
| 2671 | self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG)), |
| 2672 | # BSD seems to set MSG_CTRUNC only |
| 2673 | # if an item has been partially |
| 2674 | # received. |
| 2675 | ignoreflags=socket.MSG_CTRUNC) |
| 2676 | |
| 2677 | def _testCmsgTruncNoBufSize(self): |
| 2678 | self.createAndSendFDs(1) |
| 2679 | |
| 2680 | def testCmsgTrunc0(self): |
| 2681 | # Check that no ancillary data is received when buffer size is 0. |
| 2682 | self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 0), |
| 2683 | ignoreflags=socket.MSG_CTRUNC) |
| 2684 | |
| 2685 | def _testCmsgTrunc0(self): |
| 2686 | self.createAndSendFDs(1) |
| 2687 | |
| 2688 | # Check that no ancillary data is returned for various non-zero |
| 2689 | # (but still too small) buffer sizes. |
| 2690 | |
| 2691 | def testCmsgTrunc1(self): |
| 2692 | self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), 1)) |
| 2693 | |
| 2694 | def _testCmsgTrunc1(self): |
| 2695 | self.createAndSendFDs(1) |
| 2696 | |
| 2697 | def testCmsgTrunc2Int(self): |
| 2698 | # The cmsghdr structure has at least three members, two of |
| 2699 | # which are ints, so we still shouldn't see any ancillary |
| 2700 | # data. |
| 2701 | self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), |
| 2702 | SIZEOF_INT * 2)) |
| 2703 | |
| 2704 | def _testCmsgTrunc2Int(self): |
| 2705 | self.createAndSendFDs(1) |
| 2706 | |
| 2707 | def testCmsgTruncLen0Minus1(self): |
| 2708 | self.checkTruncatedHeader(self.doRecvmsg(self.serv_sock, len(MSG), |
| 2709 | socket.CMSG_LEN(0) - 1)) |
| 2710 | |
| 2711 | def _testCmsgTruncLen0Minus1(self): |
| 2712 | self.createAndSendFDs(1) |
| 2713 | |
| 2714 | # The following tests try to truncate the control message in the |
| 2715 | # middle of the FD array. |
| 2716 | |
| 2717 | def checkTruncatedArray(self, ancbuf, maxdata, mindata=0): |
| 2718 | # Check that file descriptor data is truncated to between |
| 2719 | # mindata and maxdata bytes when received with buffer size |
| 2720 | # ancbuf, and that any complete file descriptor numbers are |
| 2721 | # valid. |
| 2722 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2723 | len(MSG), ancbuf) |
| 2724 | self.assertEqual(msg, MSG) |
| 2725 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2726 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) |
| 2727 | |
| 2728 | if mindata == 0 and ancdata == []: |
| 2729 | return |
| 2730 | self.assertEqual(len(ancdata), 1) |
| 2731 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 2732 | self.assertEqual(cmsg_level, socket.SOL_SOCKET) |
| 2733 | self.assertEqual(cmsg_type, socket.SCM_RIGHTS) |
| 2734 | self.assertGreaterEqual(len(cmsg_data), mindata) |
| 2735 | self.assertLessEqual(len(cmsg_data), maxdata) |
| 2736 | fds = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2737 | fds.frombytes(cmsg_data[: |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2738 | len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) |
| 2739 | self.checkFDs(fds) |
| 2740 | |
| 2741 | def testCmsgTruncLen0(self): |
| 2742 | self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0), maxdata=0) |
| 2743 | |
| 2744 | def _testCmsgTruncLen0(self): |
| 2745 | self.createAndSendFDs(1) |
| 2746 | |
| 2747 | def testCmsgTruncLen0Plus1(self): |
| 2748 | self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(0) + 1, maxdata=1) |
| 2749 | |
| 2750 | def _testCmsgTruncLen0Plus1(self): |
| 2751 | self.createAndSendFDs(2) |
| 2752 | |
| 2753 | def testCmsgTruncLen1(self): |
| 2754 | self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(SIZEOF_INT), |
| 2755 | maxdata=SIZEOF_INT) |
| 2756 | |
| 2757 | def _testCmsgTruncLen1(self): |
| 2758 | self.createAndSendFDs(2) |
| 2759 | |
| 2760 | def testCmsgTruncLen2Minus1(self): |
| 2761 | self.checkTruncatedArray(ancbuf=socket.CMSG_LEN(2 * SIZEOF_INT) - 1, |
| 2762 | maxdata=(2 * SIZEOF_INT) - 1) |
| 2763 | |
| 2764 | def _testCmsgTruncLen2Minus1(self): |
| 2765 | self.createAndSendFDs(2) |
| 2766 | |
| 2767 | |
| 2768 | class RFC3542AncillaryTest(SendrecvmsgServerTimeoutBase): |
| 2769 | # Test sendmsg() and recvmsg[_into]() using the ancillary data |
| 2770 | # features of the RFC 3542 Advanced Sockets API for IPv6. |
| 2771 | # Currently we can only handle certain data items (e.g. traffic |
| 2772 | # class, hop limit, MTU discovery and fragmentation settings) |
| 2773 | # without resorting to unportable means such as the struct module, |
| 2774 | # but the tests here are aimed at testing the ancillary data |
| 2775 | # handling in sendmsg() and recvmsg() rather than the IPv6 API |
| 2776 | # itself. |
| 2777 | |
| 2778 | # Test value to use when setting hop limit of packet |
| 2779 | hop_limit = 2 |
| 2780 | |
| 2781 | # Test value to use when setting traffic class of packet. |
| 2782 | # -1 means "use kernel default". |
| 2783 | traffic_class = -1 |
| 2784 | |
| 2785 | def ancillaryMapping(self, ancdata): |
| 2786 | # Given ancillary data list ancdata, return a mapping from |
| 2787 | # pairs (cmsg_level, cmsg_type) to corresponding cmsg_data. |
| 2788 | # Check that no (level, type) pair appears more than once. |
| 2789 | d = {} |
| 2790 | for cmsg_level, cmsg_type, cmsg_data in ancdata: |
| 2791 | self.assertNotIn((cmsg_level, cmsg_type), d) |
| 2792 | d[(cmsg_level, cmsg_type)] = cmsg_data |
| 2793 | return d |
| 2794 | |
| 2795 | def checkHopLimit(self, ancbufsize, maxhop=255, ignoreflags=0): |
| 2796 | # Receive hop limit into ancbufsize bytes of ancillary data |
| 2797 | # space. Check that data is MSG, ancillary data is not |
| 2798 | # truncated (but ignore any flags in ignoreflags), and hop |
| 2799 | # limit is between 0 and maxhop inclusive. |
| 2800 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 2801 | socket.IPV6_RECVHOPLIMIT, 1) |
| 2802 | self.misc_event.set() |
| 2803 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2804 | len(MSG), ancbufsize) |
| 2805 | |
| 2806 | self.assertEqual(msg, MSG) |
| 2807 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2808 | self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, |
| 2809 | ignore=ignoreflags) |
| 2810 | |
| 2811 | self.assertEqual(len(ancdata), 1) |
| 2812 | self.assertIsInstance(ancdata[0], tuple) |
| 2813 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 2814 | self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) |
| 2815 | self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) |
| 2816 | self.assertIsInstance(cmsg_data, bytes) |
| 2817 | self.assertEqual(len(cmsg_data), SIZEOF_INT) |
| 2818 | a = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2819 | a.frombytes(cmsg_data) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2820 | self.assertGreaterEqual(a[0], 0) |
| 2821 | self.assertLessEqual(a[0], maxhop) |
| 2822 | |
| 2823 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 2824 | def testRecvHopLimit(self): |
| 2825 | # Test receiving the packet hop limit as ancillary data. |
| 2826 | self.checkHopLimit(ancbufsize=10240) |
| 2827 | |
| 2828 | @testRecvHopLimit.client_skip |
| 2829 | def _testRecvHopLimit(self): |
| 2830 | # Need to wait until server has asked to receive ancillary |
| 2831 | # data, as implementations are not required to buffer it |
| 2832 | # otherwise. |
| 2833 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2834 | self.sendToServer(MSG) |
| 2835 | |
| 2836 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 2837 | def testRecvHopLimitCMSG_SPACE(self): |
| 2838 | # Test receiving hop limit, using CMSG_SPACE to calculate buffer size. |
| 2839 | self.checkHopLimit(ancbufsize=socket.CMSG_SPACE(SIZEOF_INT)) |
| 2840 | |
| 2841 | @testRecvHopLimitCMSG_SPACE.client_skip |
| 2842 | def _testRecvHopLimitCMSG_SPACE(self): |
| 2843 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2844 | self.sendToServer(MSG) |
| 2845 | |
| 2846 | # Could test receiving into buffer sized using CMSG_LEN, but RFC |
| 2847 | # 3542 says portable applications must provide space for trailing |
| 2848 | # padding. Implementations may set MSG_CTRUNC if there isn't |
| 2849 | # enough space for the padding. |
| 2850 | |
| 2851 | @requireAttrs(socket.socket, "sendmsg") |
| 2852 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 2853 | def testSetHopLimit(self): |
| 2854 | # Test setting hop limit on outgoing packet and receiving it |
| 2855 | # at the other end. |
| 2856 | self.checkHopLimit(ancbufsize=10240, maxhop=self.hop_limit) |
| 2857 | |
| 2858 | @testSetHopLimit.client_skip |
| 2859 | def _testSetHopLimit(self): |
| 2860 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2861 | self.assertEqual( |
| 2862 | self.sendmsgToServer([MSG], |
| 2863 | [(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, |
| 2864 | array.array("i", [self.hop_limit]))]), |
| 2865 | len(MSG)) |
| 2866 | |
| 2867 | def checkTrafficClassAndHopLimit(self, ancbufsize, maxhop=255, |
| 2868 | ignoreflags=0): |
| 2869 | # Receive traffic class and hop limit into ancbufsize bytes of |
| 2870 | # ancillary data space. Check that data is MSG, ancillary |
| 2871 | # data is not truncated (but ignore any flags in ignoreflags), |
| 2872 | # and traffic class and hop limit are in range (hop limit no |
| 2873 | # more than maxhop). |
| 2874 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 2875 | socket.IPV6_RECVHOPLIMIT, 1) |
| 2876 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 2877 | socket.IPV6_RECVTCLASS, 1) |
| 2878 | self.misc_event.set() |
| 2879 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2880 | len(MSG), ancbufsize) |
| 2881 | |
| 2882 | self.assertEqual(msg, MSG) |
| 2883 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2884 | self.checkFlags(flags, eor=True, checkunset=socket.MSG_CTRUNC, |
| 2885 | ignore=ignoreflags) |
| 2886 | self.assertEqual(len(ancdata), 2) |
| 2887 | ancmap = self.ancillaryMapping(ancdata) |
| 2888 | |
| 2889 | tcdata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_TCLASS)] |
| 2890 | self.assertEqual(len(tcdata), SIZEOF_INT) |
| 2891 | a = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2892 | a.frombytes(tcdata) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2893 | self.assertGreaterEqual(a[0], 0) |
| 2894 | self.assertLessEqual(a[0], 255) |
| 2895 | |
| 2896 | hldata = ancmap[(socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT)] |
| 2897 | self.assertEqual(len(hldata), SIZEOF_INT) |
| 2898 | a = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2899 | a.frombytes(hldata) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2900 | self.assertGreaterEqual(a[0], 0) |
| 2901 | self.assertLessEqual(a[0], maxhop) |
| 2902 | |
| 2903 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 2904 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 2905 | def testRecvTrafficClassAndHopLimit(self): |
| 2906 | # Test receiving traffic class and hop limit as ancillary data. |
| 2907 | self.checkTrafficClassAndHopLimit(ancbufsize=10240) |
| 2908 | |
| 2909 | @testRecvTrafficClassAndHopLimit.client_skip |
| 2910 | def _testRecvTrafficClassAndHopLimit(self): |
| 2911 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2912 | self.sendToServer(MSG) |
| 2913 | |
| 2914 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 2915 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 2916 | def testRecvTrafficClassAndHopLimitCMSG_SPACE(self): |
| 2917 | # Test receiving traffic class and hop limit, using |
| 2918 | # CMSG_SPACE() to calculate buffer size. |
| 2919 | self.checkTrafficClassAndHopLimit( |
| 2920 | ancbufsize=socket.CMSG_SPACE(SIZEOF_INT) * 2) |
| 2921 | |
| 2922 | @testRecvTrafficClassAndHopLimitCMSG_SPACE.client_skip |
| 2923 | def _testRecvTrafficClassAndHopLimitCMSG_SPACE(self): |
| 2924 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2925 | self.sendToServer(MSG) |
| 2926 | |
| 2927 | @requireAttrs(socket.socket, "sendmsg") |
| 2928 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 2929 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 2930 | def testSetTrafficClassAndHopLimit(self): |
| 2931 | # Test setting traffic class and hop limit on outgoing packet, |
| 2932 | # and receiving them at the other end. |
| 2933 | self.checkTrafficClassAndHopLimit(ancbufsize=10240, |
| 2934 | maxhop=self.hop_limit) |
| 2935 | |
| 2936 | @testSetTrafficClassAndHopLimit.client_skip |
| 2937 | def _testSetTrafficClassAndHopLimit(self): |
| 2938 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2939 | self.assertEqual( |
| 2940 | self.sendmsgToServer([MSG], |
| 2941 | [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, |
| 2942 | array.array("i", [self.traffic_class])), |
| 2943 | (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, |
| 2944 | array.array("i", [self.hop_limit]))]), |
| 2945 | len(MSG)) |
| 2946 | |
| 2947 | @requireAttrs(socket.socket, "sendmsg") |
| 2948 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 2949 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 2950 | def testOddCmsgSize(self): |
| 2951 | # Try to send ancillary data with first item one byte too |
| 2952 | # long. Fall back to sending with correct size if this fails, |
| 2953 | # and check that second item was handled correctly. |
| 2954 | self.checkTrafficClassAndHopLimit(ancbufsize=10240, |
| 2955 | maxhop=self.hop_limit) |
| 2956 | |
| 2957 | @testOddCmsgSize.client_skip |
| 2958 | def _testOddCmsgSize(self): |
| 2959 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 2960 | try: |
| 2961 | nbytes = self.sendmsgToServer( |
| 2962 | [MSG], |
| 2963 | [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 2964 | array.array("i", [self.traffic_class]).tobytes() + b"\x00"), |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 2965 | (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, |
| 2966 | array.array("i", [self.hop_limit]))]) |
| 2967 | except socket.error as e: |
| 2968 | self.assertIsInstance(e.errno, int) |
| 2969 | nbytes = self.sendmsgToServer( |
| 2970 | [MSG], |
| 2971 | [(socket.IPPROTO_IPV6, socket.IPV6_TCLASS, |
| 2972 | array.array("i", [self.traffic_class])), |
| 2973 | (socket.IPPROTO_IPV6, socket.IPV6_HOPLIMIT, |
| 2974 | array.array("i", [self.hop_limit]))]) |
| 2975 | self.assertEqual(nbytes, len(MSG)) |
| 2976 | |
| 2977 | # Tests for proper handling of truncated ancillary data |
| 2978 | |
| 2979 | def checkHopLimitTruncatedHeader(self, ancbufsize, ignoreflags=0): |
| 2980 | # Receive hop limit into ancbufsize bytes of ancillary data |
| 2981 | # space, which should be too small to contain the ancillary |
| 2982 | # data header (if ancbufsize is None, pass no second argument |
| 2983 | # to recvmsg()). Check that data is MSG, MSG_CTRUNC is set |
| 2984 | # (unless included in ignoreflags), and no ancillary data is |
| 2985 | # returned. |
| 2986 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 2987 | socket.IPV6_RECVHOPLIMIT, 1) |
| 2988 | self.misc_event.set() |
| 2989 | args = () if ancbufsize is None else (ancbufsize,) |
| 2990 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 2991 | len(MSG), *args) |
| 2992 | |
| 2993 | self.assertEqual(msg, MSG) |
| 2994 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 2995 | self.assertEqual(ancdata, []) |
| 2996 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, |
| 2997 | ignore=ignoreflags) |
| 2998 | |
| 2999 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3000 | def testCmsgTruncNoBufSize(self): |
| 3001 | # Check that no ancillary data is received when no ancillary |
| 3002 | # buffer size is provided. |
| 3003 | self.checkHopLimitTruncatedHeader(ancbufsize=None, |
| 3004 | # BSD seems to set |
| 3005 | # MSG_CTRUNC only if an item |
| 3006 | # has been partially |
| 3007 | # received. |
| 3008 | ignoreflags=socket.MSG_CTRUNC) |
| 3009 | |
| 3010 | @testCmsgTruncNoBufSize.client_skip |
| 3011 | def _testCmsgTruncNoBufSize(self): |
| 3012 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3013 | self.sendToServer(MSG) |
| 3014 | |
| 3015 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3016 | def testSingleCmsgTrunc0(self): |
| 3017 | # Check that no ancillary data is received when ancillary |
| 3018 | # buffer size is zero. |
| 3019 | self.checkHopLimitTruncatedHeader(ancbufsize=0, |
| 3020 | ignoreflags=socket.MSG_CTRUNC) |
| 3021 | |
| 3022 | @testSingleCmsgTrunc0.client_skip |
| 3023 | def _testSingleCmsgTrunc0(self): |
| 3024 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3025 | self.sendToServer(MSG) |
| 3026 | |
| 3027 | # Check that no ancillary data is returned for various non-zero |
| 3028 | # (but still too small) buffer sizes. |
| 3029 | |
| 3030 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3031 | def testSingleCmsgTrunc1(self): |
| 3032 | self.checkHopLimitTruncatedHeader(ancbufsize=1) |
| 3033 | |
| 3034 | @testSingleCmsgTrunc1.client_skip |
| 3035 | def _testSingleCmsgTrunc1(self): |
| 3036 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3037 | self.sendToServer(MSG) |
| 3038 | |
| 3039 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3040 | def testSingleCmsgTrunc2Int(self): |
| 3041 | self.checkHopLimitTruncatedHeader(ancbufsize=2 * SIZEOF_INT) |
| 3042 | |
| 3043 | @testSingleCmsgTrunc2Int.client_skip |
| 3044 | def _testSingleCmsgTrunc2Int(self): |
| 3045 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3046 | self.sendToServer(MSG) |
| 3047 | |
| 3048 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3049 | def testSingleCmsgTruncLen0Minus1(self): |
| 3050 | self.checkHopLimitTruncatedHeader(ancbufsize=socket.CMSG_LEN(0) - 1) |
| 3051 | |
| 3052 | @testSingleCmsgTruncLen0Minus1.client_skip |
| 3053 | def _testSingleCmsgTruncLen0Minus1(self): |
| 3054 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3055 | self.sendToServer(MSG) |
| 3056 | |
| 3057 | @requireAttrs(socket, "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT") |
| 3058 | def testSingleCmsgTruncInData(self): |
| 3059 | # Test truncation of a control message inside its associated |
| 3060 | # data. The message may be returned with its data truncated, |
| 3061 | # or not returned at all. |
| 3062 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 3063 | socket.IPV6_RECVHOPLIMIT, 1) |
| 3064 | self.misc_event.set() |
| 3065 | msg, ancdata, flags, addr = self.doRecvmsg( |
| 3066 | self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1) |
| 3067 | |
| 3068 | self.assertEqual(msg, MSG) |
| 3069 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 3070 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) |
| 3071 | |
| 3072 | self.assertLessEqual(len(ancdata), 1) |
| 3073 | if ancdata: |
| 3074 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 3075 | self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) |
| 3076 | self.assertEqual(cmsg_type, socket.IPV6_HOPLIMIT) |
| 3077 | self.assertLess(len(cmsg_data), SIZEOF_INT) |
| 3078 | |
| 3079 | @testSingleCmsgTruncInData.client_skip |
| 3080 | def _testSingleCmsgTruncInData(self): |
| 3081 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3082 | self.sendToServer(MSG) |
| 3083 | |
| 3084 | def checkTruncatedSecondHeader(self, ancbufsize, ignoreflags=0): |
| 3085 | # Receive traffic class and hop limit into ancbufsize bytes of |
| 3086 | # ancillary data space, which should be large enough to |
| 3087 | # contain the first item, but too small to contain the header |
| 3088 | # of the second. Check that data is MSG, MSG_CTRUNC is set |
| 3089 | # (unless included in ignoreflags), and only one ancillary |
| 3090 | # data item is returned. |
| 3091 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 3092 | socket.IPV6_RECVHOPLIMIT, 1) |
| 3093 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 3094 | socket.IPV6_RECVTCLASS, 1) |
| 3095 | self.misc_event.set() |
| 3096 | msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, |
| 3097 | len(MSG), ancbufsize) |
| 3098 | |
| 3099 | self.assertEqual(msg, MSG) |
| 3100 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 3101 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC, |
| 3102 | ignore=ignoreflags) |
| 3103 | |
| 3104 | self.assertEqual(len(ancdata), 1) |
| 3105 | cmsg_level, cmsg_type, cmsg_data = ancdata[0] |
| 3106 | self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) |
| 3107 | self.assertIn(cmsg_type, {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT}) |
| 3108 | self.assertEqual(len(cmsg_data), SIZEOF_INT) |
| 3109 | a = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 3110 | a.frombytes(cmsg_data) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3111 | self.assertGreaterEqual(a[0], 0) |
| 3112 | self.assertLessEqual(a[0], 255) |
| 3113 | |
| 3114 | # Try the above test with various buffer sizes. |
| 3115 | |
| 3116 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 3117 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 3118 | def testSecondCmsgTrunc0(self): |
| 3119 | self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT), |
| 3120 | ignoreflags=socket.MSG_CTRUNC) |
| 3121 | |
| 3122 | @testSecondCmsgTrunc0.client_skip |
| 3123 | def _testSecondCmsgTrunc0(self): |
| 3124 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3125 | self.sendToServer(MSG) |
| 3126 | |
| 3127 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 3128 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 3129 | def testSecondCmsgTrunc1(self): |
| 3130 | self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + 1) |
| 3131 | |
| 3132 | @testSecondCmsgTrunc1.client_skip |
| 3133 | def _testSecondCmsgTrunc1(self): |
| 3134 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3135 | self.sendToServer(MSG) |
| 3136 | |
| 3137 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 3138 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 3139 | def testSecondCmsgTrunc2Int(self): |
| 3140 | self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + |
| 3141 | 2 * SIZEOF_INT) |
| 3142 | |
| 3143 | @testSecondCmsgTrunc2Int.client_skip |
| 3144 | def _testSecondCmsgTrunc2Int(self): |
| 3145 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3146 | self.sendToServer(MSG) |
| 3147 | |
| 3148 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 3149 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 3150 | def testSecondCmsgTruncLen0Minus1(self): |
| 3151 | self.checkTruncatedSecondHeader(socket.CMSG_SPACE(SIZEOF_INT) + |
| 3152 | socket.CMSG_LEN(0) - 1) |
| 3153 | |
| 3154 | @testSecondCmsgTruncLen0Minus1.client_skip |
| 3155 | def _testSecondCmsgTruncLen0Minus1(self): |
| 3156 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3157 | self.sendToServer(MSG) |
| 3158 | |
| 3159 | @requireAttrs(socket, "CMSG_SPACE", "IPV6_RECVHOPLIMIT", "IPV6_HOPLIMIT", |
| 3160 | "IPV6_RECVTCLASS", "IPV6_TCLASS") |
| 3161 | def testSecomdCmsgTruncInData(self): |
| 3162 | # Test truncation of the second of two control messages inside |
| 3163 | # its associated data. |
| 3164 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 3165 | socket.IPV6_RECVHOPLIMIT, 1) |
| 3166 | self.serv_sock.setsockopt(socket.IPPROTO_IPV6, |
| 3167 | socket.IPV6_RECVTCLASS, 1) |
| 3168 | self.misc_event.set() |
| 3169 | msg, ancdata, flags, addr = self.doRecvmsg( |
| 3170 | self.serv_sock, len(MSG), |
| 3171 | socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1) |
| 3172 | |
| 3173 | self.assertEqual(msg, MSG) |
| 3174 | self.checkRecvmsgAddress(addr, self.cli_addr) |
| 3175 | self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC) |
| 3176 | |
| 3177 | cmsg_types = {socket.IPV6_TCLASS, socket.IPV6_HOPLIMIT} |
| 3178 | |
| 3179 | cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) |
| 3180 | self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) |
| 3181 | cmsg_types.remove(cmsg_type) |
| 3182 | self.assertEqual(len(cmsg_data), SIZEOF_INT) |
| 3183 | a = array.array("i") |
Nadeem Vawda | 3d1706f | 2011-08-22 09:46:56 +0200 | [diff] [blame] | 3184 | a.frombytes(cmsg_data) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3185 | self.assertGreaterEqual(a[0], 0) |
| 3186 | self.assertLessEqual(a[0], 255) |
| 3187 | |
| 3188 | if ancdata: |
| 3189 | cmsg_level, cmsg_type, cmsg_data = ancdata.pop(0) |
| 3190 | self.assertEqual(cmsg_level, socket.IPPROTO_IPV6) |
| 3191 | cmsg_types.remove(cmsg_type) |
| 3192 | self.assertLess(len(cmsg_data), SIZEOF_INT) |
| 3193 | |
| 3194 | self.assertEqual(ancdata, []) |
| 3195 | |
| 3196 | @testSecomdCmsgTruncInData.client_skip |
| 3197 | def _testSecomdCmsgTruncInData(self): |
| 3198 | self.assertTrue(self.misc_event.wait(timeout=self.fail_timeout)) |
| 3199 | self.sendToServer(MSG) |
| 3200 | |
| 3201 | |
| 3202 | # Derive concrete test classes for different socket types. |
| 3203 | |
| 3204 | class SendrecvmsgUDPTestBase(SendrecvmsgDgramFlagsBase, |
| 3205 | SendrecvmsgConnectionlessBase, |
| 3206 | ThreadedSocketTestMixin, UDPTestBase): |
| 3207 | pass |
| 3208 | |
| 3209 | @requireAttrs(socket.socket, "sendmsg") |
| 3210 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3211 | class SendmsgUDPTest(SendmsgConnectionlessTests, SendrecvmsgUDPTestBase): |
| 3212 | pass |
| 3213 | |
| 3214 | @requireAttrs(socket.socket, "recvmsg") |
| 3215 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3216 | class RecvmsgUDPTest(RecvmsgTests, SendrecvmsgUDPTestBase): |
| 3217 | pass |
| 3218 | |
| 3219 | @requireAttrs(socket.socket, "recvmsg_into") |
| 3220 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3221 | class RecvmsgIntoUDPTest(RecvmsgIntoTests, SendrecvmsgUDPTestBase): |
| 3222 | pass |
| 3223 | |
| 3224 | |
| 3225 | class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase, |
| 3226 | SendrecvmsgConnectionlessBase, |
| 3227 | ThreadedSocketTestMixin, UDP6TestBase): |
Charles-François Natali | 37114bb | 2013-07-09 19:15:43 +0200 | [diff] [blame] | 3228 | |
| 3229 | def checkRecvmsgAddress(self, addr1, addr2): |
| 3230 | # Called to compare the received address with the address of |
| 3231 | # the peer, ignoring scope ID |
| 3232 | self.assertEqual(addr1[:-1], addr2[:-1]) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3233 | |
| 3234 | @requireAttrs(socket.socket, "sendmsg") |
Ross Lagerwall | 8d459f0 | 2012-07-08 09:53:57 +0200 | [diff] [blame] | 3235 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3236 | @requireSocket("AF_INET6", "SOCK_DGRAM") |
| 3237 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3238 | class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase): |
| 3239 | pass |
| 3240 | |
| 3241 | @requireAttrs(socket.socket, "recvmsg") |
Ross Lagerwall | 8d459f0 | 2012-07-08 09:53:57 +0200 | [diff] [blame] | 3242 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3243 | @requireSocket("AF_INET6", "SOCK_DGRAM") |
| 3244 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3245 | class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase): |
| 3246 | pass |
| 3247 | |
| 3248 | @requireAttrs(socket.socket, "recvmsg_into") |
Ross Lagerwall | 8d459f0 | 2012-07-08 09:53:57 +0200 | [diff] [blame] | 3249 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3250 | @requireSocket("AF_INET6", "SOCK_DGRAM") |
| 3251 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3252 | class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase): |
| 3253 | pass |
| 3254 | |
| 3255 | @requireAttrs(socket.socket, "recvmsg") |
Ross Lagerwall | 8d459f0 | 2012-07-08 09:53:57 +0200 | [diff] [blame] | 3256 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3257 | @requireAttrs(socket, "IPPROTO_IPV6") |
| 3258 | @requireSocket("AF_INET6", "SOCK_DGRAM") |
| 3259 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3260 | class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, |
| 3261 | SendrecvmsgUDP6TestBase): |
| 3262 | pass |
| 3263 | |
| 3264 | @requireAttrs(socket.socket, "recvmsg_into") |
Ross Lagerwall | 8d459f0 | 2012-07-08 09:53:57 +0200 | [diff] [blame] | 3265 | @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3266 | @requireAttrs(socket, "IPPROTO_IPV6") |
| 3267 | @requireSocket("AF_INET6", "SOCK_DGRAM") |
| 3268 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3269 | class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin, |
| 3270 | RFC3542AncillaryTest, |
| 3271 | SendrecvmsgUDP6TestBase): |
| 3272 | pass |
| 3273 | |
| 3274 | |
| 3275 | class SendrecvmsgTCPTestBase(SendrecvmsgConnectedBase, |
| 3276 | ConnectedStreamTestMixin, TCPTestBase): |
| 3277 | pass |
| 3278 | |
| 3279 | @requireAttrs(socket.socket, "sendmsg") |
| 3280 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3281 | class SendmsgTCPTest(SendmsgStreamTests, SendrecvmsgTCPTestBase): |
| 3282 | pass |
| 3283 | |
| 3284 | @requireAttrs(socket.socket, "recvmsg") |
| 3285 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3286 | class RecvmsgTCPTest(RecvmsgTests, RecvmsgGenericStreamTests, |
| 3287 | SendrecvmsgTCPTestBase): |
| 3288 | pass |
| 3289 | |
| 3290 | @requireAttrs(socket.socket, "recvmsg_into") |
| 3291 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3292 | class RecvmsgIntoTCPTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, |
| 3293 | SendrecvmsgTCPTestBase): |
| 3294 | pass |
| 3295 | |
| 3296 | |
| 3297 | class SendrecvmsgSCTPStreamTestBase(SendrecvmsgSCTPFlagsBase, |
| 3298 | SendrecvmsgConnectedBase, |
| 3299 | ConnectedStreamTestMixin, SCTPStreamBase): |
| 3300 | pass |
| 3301 | |
| 3302 | @requireAttrs(socket.socket, "sendmsg") |
| 3303 | @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") |
| 3304 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3305 | class SendmsgSCTPStreamTest(SendmsgStreamTests, SendrecvmsgSCTPStreamTestBase): |
| 3306 | pass |
| 3307 | |
| 3308 | @requireAttrs(socket.socket, "recvmsg") |
| 3309 | @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") |
| 3310 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3311 | class RecvmsgSCTPStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, |
| 3312 | SendrecvmsgSCTPStreamTestBase): |
Antoine Pitrou | 92f8b00 | 2012-12-16 16:40:04 +0100 | [diff] [blame] | 3313 | |
| 3314 | def testRecvmsgEOF(self): |
| 3315 | try: |
| 3316 | super(RecvmsgSCTPStreamTest, self).testRecvmsgEOF() |
| 3317 | except OSError as e: |
| 3318 | if e.errno != errno.ENOTCONN: |
| 3319 | raise |
| 3320 | self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3321 | |
| 3322 | @requireAttrs(socket.socket, "recvmsg_into") |
| 3323 | @requireSocket("AF_INET", "SOCK_STREAM", "IPPROTO_SCTP") |
| 3324 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3325 | class RecvmsgIntoSCTPStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, |
| 3326 | SendrecvmsgSCTPStreamTestBase): |
Antoine Pitrou | d5a070f | 2012-12-18 23:44:36 +0100 | [diff] [blame] | 3327 | |
| 3328 | def testRecvmsgEOF(self): |
| 3329 | try: |
| 3330 | super(RecvmsgIntoSCTPStreamTest, self).testRecvmsgEOF() |
| 3331 | except OSError as e: |
| 3332 | if e.errno != errno.ENOTCONN: |
| 3333 | raise |
| 3334 | self.skipTest("sporadic ENOTCONN (kernel issue?) - see issue #13876") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3335 | |
| 3336 | |
| 3337 | class SendrecvmsgUnixStreamTestBase(SendrecvmsgConnectedBase, |
| 3338 | ConnectedStreamTestMixin, UnixStreamBase): |
| 3339 | pass |
| 3340 | |
| 3341 | @requireAttrs(socket.socket, "sendmsg") |
| 3342 | @requireAttrs(socket, "AF_UNIX") |
| 3343 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3344 | class SendmsgUnixStreamTest(SendmsgStreamTests, SendrecvmsgUnixStreamTestBase): |
| 3345 | pass |
| 3346 | |
| 3347 | @requireAttrs(socket.socket, "recvmsg") |
| 3348 | @requireAttrs(socket, "AF_UNIX") |
| 3349 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3350 | class RecvmsgUnixStreamTest(RecvmsgTests, RecvmsgGenericStreamTests, |
| 3351 | SendrecvmsgUnixStreamTestBase): |
| 3352 | pass |
| 3353 | |
| 3354 | @requireAttrs(socket.socket, "recvmsg_into") |
| 3355 | @requireAttrs(socket, "AF_UNIX") |
| 3356 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3357 | class RecvmsgIntoUnixStreamTest(RecvmsgIntoTests, RecvmsgGenericStreamTests, |
| 3358 | SendrecvmsgUnixStreamTestBase): |
| 3359 | pass |
| 3360 | |
| 3361 | @requireAttrs(socket.socket, "sendmsg", "recvmsg") |
| 3362 | @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") |
| 3363 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3364 | class RecvmsgSCMRightsStreamTest(SCMRightsTest, SendrecvmsgUnixStreamTestBase): |
| 3365 | pass |
| 3366 | |
| 3367 | @requireAttrs(socket.socket, "sendmsg", "recvmsg_into") |
| 3368 | @requireAttrs(socket, "AF_UNIX", "SOL_SOCKET", "SCM_RIGHTS") |
| 3369 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3370 | class RecvmsgIntoSCMRightsStreamTest(RecvmsgIntoMixin, SCMRightsTest, |
| 3371 | SendrecvmsgUnixStreamTestBase): |
| 3372 | pass |
| 3373 | |
| 3374 | |
| 3375 | # Test interrupting the interruptible send/receive methods with a |
| 3376 | # signal when a timeout is set. These tests avoid having multiple |
| 3377 | # threads alive during the test so that the OS cannot deliver the |
| 3378 | # signal to the wrong one. |
| 3379 | |
| 3380 | class InterruptedTimeoutBase(unittest.TestCase): |
| 3381 | # Base class for interrupted send/receive tests. Installs an |
| 3382 | # empty handler for SIGALRM and removes it on teardown, along with |
| 3383 | # any scheduled alarms. |
| 3384 | |
| 3385 | def setUp(self): |
| 3386 | super().setUp() |
| 3387 | orig_alrm_handler = signal.signal(signal.SIGALRM, |
| 3388 | lambda signum, frame: None) |
| 3389 | self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) |
| 3390 | self.addCleanup(self.setAlarm, 0) |
| 3391 | |
| 3392 | # Timeout for socket operations |
| 3393 | timeout = 4.0 |
| 3394 | |
| 3395 | # Provide setAlarm() method to schedule delivery of SIGALRM after |
| 3396 | # given number of seconds, or cancel it if zero, and an |
| 3397 | # appropriate time value to use. Use setitimer() if available. |
| 3398 | if hasattr(signal, "setitimer"): |
| 3399 | alarm_time = 0.05 |
| 3400 | |
| 3401 | def setAlarm(self, seconds): |
| 3402 | signal.setitimer(signal.ITIMER_REAL, seconds) |
| 3403 | else: |
| 3404 | # Old systems may deliver the alarm up to one second early |
| 3405 | alarm_time = 2 |
| 3406 | |
| 3407 | def setAlarm(self, seconds): |
| 3408 | signal.alarm(seconds) |
| 3409 | |
| 3410 | |
| 3411 | # Require siginterrupt() in order to ensure that system calls are |
| 3412 | # interrupted by default. |
| 3413 | @requireAttrs(signal, "siginterrupt") |
| 3414 | @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), |
| 3415 | "Don't have signal.alarm or signal.setitimer") |
| 3416 | class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase): |
| 3417 | # Test interrupting the recv*() methods with signals when a |
| 3418 | # timeout is set. |
| 3419 | |
| 3420 | def setUp(self): |
| 3421 | super().setUp() |
| 3422 | self.serv.settimeout(self.timeout) |
| 3423 | |
| 3424 | def checkInterruptedRecv(self, func, *args, **kwargs): |
| 3425 | # Check that func(*args, **kwargs) raises socket.error with an |
| 3426 | # errno of EINTR when interrupted by a signal. |
| 3427 | self.setAlarm(self.alarm_time) |
| 3428 | with self.assertRaises(socket.error) as cm: |
| 3429 | func(*args, **kwargs) |
| 3430 | self.assertNotIsInstance(cm.exception, socket.timeout) |
| 3431 | self.assertEqual(cm.exception.errno, errno.EINTR) |
| 3432 | |
| 3433 | def testInterruptedRecvTimeout(self): |
| 3434 | self.checkInterruptedRecv(self.serv.recv, 1024) |
| 3435 | |
| 3436 | def testInterruptedRecvIntoTimeout(self): |
| 3437 | self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024)) |
| 3438 | |
| 3439 | def testInterruptedRecvfromTimeout(self): |
| 3440 | self.checkInterruptedRecv(self.serv.recvfrom, 1024) |
| 3441 | |
| 3442 | def testInterruptedRecvfromIntoTimeout(self): |
| 3443 | self.checkInterruptedRecv(self.serv.recvfrom_into, bytearray(1024)) |
| 3444 | |
| 3445 | @requireAttrs(socket.socket, "recvmsg") |
| 3446 | def testInterruptedRecvmsgTimeout(self): |
| 3447 | self.checkInterruptedRecv(self.serv.recvmsg, 1024) |
| 3448 | |
| 3449 | @requireAttrs(socket.socket, "recvmsg_into") |
| 3450 | def testInterruptedRecvmsgIntoTimeout(self): |
| 3451 | self.checkInterruptedRecv(self.serv.recvmsg_into, [bytearray(1024)]) |
| 3452 | |
| 3453 | |
| 3454 | # Require siginterrupt() in order to ensure that system calls are |
| 3455 | # interrupted by default. |
| 3456 | @requireAttrs(signal, "siginterrupt") |
| 3457 | @unittest.skipUnless(hasattr(signal, "alarm") or hasattr(signal, "setitimer"), |
| 3458 | "Don't have signal.alarm or signal.setitimer") |
| 3459 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 3460 | class InterruptedSendTimeoutTest(InterruptedTimeoutBase, |
| 3461 | ThreadSafeCleanupTestCase, |
| 3462 | SocketListeningTestMixin, TCPTestBase): |
| 3463 | # Test interrupting the interruptible send*() methods with signals |
| 3464 | # when a timeout is set. |
| 3465 | |
| 3466 | def setUp(self): |
| 3467 | super().setUp() |
| 3468 | self.serv_conn = self.newSocket() |
| 3469 | self.addCleanup(self.serv_conn.close) |
| 3470 | # Use a thread to complete the connection, but wait for it to |
| 3471 | # terminate before running the test, so that there is only one |
| 3472 | # thread to accept the signal. |
| 3473 | cli_thread = threading.Thread(target=self.doConnect) |
| 3474 | cli_thread.start() |
| 3475 | self.cli_conn, addr = self.serv.accept() |
| 3476 | self.addCleanup(self.cli_conn.close) |
| 3477 | cli_thread.join() |
| 3478 | self.serv_conn.settimeout(self.timeout) |
| 3479 | |
| 3480 | def doConnect(self): |
| 3481 | self.serv_conn.connect(self.serv_addr) |
| 3482 | |
| 3483 | def checkInterruptedSend(self, func, *args, **kwargs): |
| 3484 | # Check that func(*args, **kwargs), run in a loop, raises |
| 3485 | # socket.error with an errno of EINTR when interrupted by a |
| 3486 | # signal. |
| 3487 | with self.assertRaises(socket.error) as cm: |
| 3488 | while True: |
| 3489 | self.setAlarm(self.alarm_time) |
| 3490 | func(*args, **kwargs) |
| 3491 | self.assertNotIsInstance(cm.exception, socket.timeout) |
| 3492 | self.assertEqual(cm.exception.errno, errno.EINTR) |
| 3493 | |
Nick Coghlan | 2496f33 | 2011-09-19 20:26:31 +1000 | [diff] [blame] | 3494 | # Issue #12958: The following tests have problems on Mac OS X |
| 3495 | @support.anticipate_failure(sys.platform == "darwin") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3496 | def testInterruptedSendTimeout(self): |
| 3497 | self.checkInterruptedSend(self.serv_conn.send, b"a"*512) |
| 3498 | |
Nick Coghlan | 2496f33 | 2011-09-19 20:26:31 +1000 | [diff] [blame] | 3499 | @support.anticipate_failure(sys.platform == "darwin") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3500 | def testInterruptedSendtoTimeout(self): |
| 3501 | # Passing an actual address here as Python's wrapper for |
| 3502 | # sendto() doesn't allow passing a zero-length one; POSIX |
| 3503 | # requires that the address is ignored since the socket is |
| 3504 | # connection-mode, however. |
| 3505 | self.checkInterruptedSend(self.serv_conn.sendto, b"a"*512, |
| 3506 | self.serv_addr) |
| 3507 | |
Nick Coghlan | 2496f33 | 2011-09-19 20:26:31 +1000 | [diff] [blame] | 3508 | @support.anticipate_failure(sys.platform == "darwin") |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 3509 | @requireAttrs(socket.socket, "sendmsg") |
| 3510 | def testInterruptedSendmsgTimeout(self): |
| 3511 | self.checkInterruptedSend(self.serv_conn.sendmsg, [b"a"*512]) |
| 3512 | |
| 3513 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 3514 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3515 | class TCPCloserTest(ThreadedTCPSocketTest): |
| 3516 | |
| 3517 | def testClose(self): |
| 3518 | conn, addr = self.serv.accept() |
| 3519 | conn.close() |
| 3520 | |
| 3521 | sd = self.cli |
| 3522 | read, write, err = select.select([sd], [], [], 1.0) |
| 3523 | self.assertEqual(read, [sd]) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 3524 | self.assertEqual(sd.recv(1), b'') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3525 | |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3526 | # Calling close() many times should be safe. |
| 3527 | conn.close() |
| 3528 | conn.close() |
| 3529 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3530 | def _testClose(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 3531 | self.cli.connect((HOST, self.port)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3532 | time.sleep(1.0) |
| 3533 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 3534 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3535 | class BasicSocketPairTest(SocketPairTest): |
| 3536 | |
| 3537 | def __init__(self, methodName='runTest'): |
| 3538 | SocketPairTest.__init__(self, methodName=methodName) |
| 3539 | |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 3540 | def _check_defaults(self, sock): |
| 3541 | self.assertIsInstance(sock, socket.socket) |
| 3542 | if hasattr(socket, 'AF_UNIX'): |
| 3543 | self.assertEqual(sock.family, socket.AF_UNIX) |
| 3544 | else: |
| 3545 | self.assertEqual(sock.family, socket.AF_INET) |
| 3546 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 3547 | self.assertEqual(sock.proto, 0) |
| 3548 | |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 3549 | def _testDefaults(self): |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 3550 | self._check_defaults(self.cli) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 3551 | |
| 3552 | def testDefaults(self): |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 3553 | self._check_defaults(self.serv) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 3554 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 3555 | def testRecv(self): |
| 3556 | msg = self.serv.recv(1024) |
| 3557 | self.assertEqual(msg, MSG) |
| 3558 | |
| 3559 | def _testRecv(self): |
| 3560 | self.cli.send(MSG) |
| 3561 | |
| 3562 | def testSend(self): |
| 3563 | self.serv.send(MSG) |
| 3564 | |
| 3565 | def _testSend(self): |
| 3566 | msg = self.cli.recv(1024) |
| 3567 | self.assertEqual(msg, MSG) |
| 3568 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 3569 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3570 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 3571 | |
| 3572 | def __init__(self, methodName='runTest'): |
| 3573 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 3574 | |
| 3575 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3576 | # Testing whether set blocking works |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 3577 | self.serv.setblocking(True) |
| 3578 | self.assertIsNone(self.serv.gettimeout()) |
| 3579 | self.serv.setblocking(False) |
| 3580 | self.assertEqual(self.serv.gettimeout(), 0.0) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3581 | start = time.time() |
| 3582 | try: |
| 3583 | self.serv.accept() |
| 3584 | except socket.error: |
| 3585 | pass |
| 3586 | end = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 3587 | self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 3588 | # Issue 15989 |
| 3589 | if _testcapi.UINT_MAX < _testcapi.ULONG_MAX: |
| 3590 | self.serv.setblocking(_testcapi.UINT_MAX + 1) |
| 3591 | self.assertIsNone(self.serv.gettimeout()) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3592 | |
| 3593 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 3594 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 3596 | if hasattr(socket, "SOCK_NONBLOCK"): |
Charles-François Natali | 239bb96 | 2011-06-03 12:55:15 +0200 | [diff] [blame] | 3597 | @support.requires_linux_version(2, 6, 28) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 3598 | def testInitNonBlocking(self): |
| 3599 | # reinit server socket |
| 3600 | self.serv.close() |
| 3601 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM | |
Antoine Pitrou | 1cae8b5 | 2010-10-23 17:05:13 +0000 | [diff] [blame] | 3602 | socket.SOCK_NONBLOCK) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 3603 | self.port = support.bind_port(self.serv) |
| 3604 | self.serv.listen(1) |
| 3605 | # actual testing |
| 3606 | start = time.time() |
| 3607 | try: |
| 3608 | self.serv.accept() |
| 3609 | except socket.error: |
| 3610 | pass |
| 3611 | end = time.time() |
| 3612 | self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.") |
| 3613 | |
| 3614 | def _testInitNonBlocking(self): |
| 3615 | pass |
| 3616 | |
Antoine Pitrou | 600232b | 2011-01-05 21:03:42 +0000 | [diff] [blame] | 3617 | def testInheritFlags(self): |
| 3618 | # Issue #7995: when calling accept() on a listening socket with a |
| 3619 | # timeout, the resulting socket should not be non-blocking. |
| 3620 | self.serv.settimeout(10) |
| 3621 | try: |
| 3622 | conn, addr = self.serv.accept() |
| 3623 | message = conn.recv(len(MSG)) |
| 3624 | finally: |
| 3625 | conn.close() |
| 3626 | self.serv.settimeout(None) |
| 3627 | |
| 3628 | def _testInheritFlags(self): |
| 3629 | time.sleep(0.1) |
| 3630 | self.cli.connect((HOST, self.port)) |
| 3631 | time.sleep(0.5) |
| 3632 | self.cli.send(MSG) |
| 3633 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3634 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3635 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3636 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 3637 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3638 | conn, addr = self.serv.accept() |
| 3639 | except socket.error: |
| 3640 | pass |
| 3641 | else: |
| 3642 | self.fail("Error trying to do non-blocking accept.") |
| 3643 | read, write, err = select.select([self.serv], [], []) |
| 3644 | if self.serv in read: |
| 3645 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 3646 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3647 | else: |
| 3648 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 3649 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3650 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 3651 | time.sleep(0.1) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 3652 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3653 | |
| 3654 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3655 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3656 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 3657 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3658 | |
| 3659 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 3660 | self.cli.settimeout(10) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 3661 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3662 | |
| 3663 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3664 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3665 | conn, addr = self.serv.accept() |
| 3666 | conn.setblocking(0) |
| 3667 | try: |
| 3668 | msg = conn.recv(len(MSG)) |
| 3669 | except socket.error: |
| 3670 | pass |
| 3671 | else: |
| 3672 | self.fail("Error trying to do non-blocking recv.") |
| 3673 | read, write, err = select.select([conn], [], []) |
| 3674 | if conn in read: |
| 3675 | msg = conn.recv(len(MSG)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 3676 | conn.close() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 3677 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3678 | else: |
| 3679 | self.fail("Error during select call to non-blocking socket.") |
| 3680 | |
| 3681 | def _testRecv(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 3682 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 3683 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3684 | self.cli.send(MSG) |
| 3685 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 3686 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3687 | class FileObjectClassTestCase(SocketConnectedTest): |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3688 | """Unit tests for the object returned by socket.makefile() |
| 3689 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3690 | self.read_file is the io object returned by makefile() on |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3691 | the client connection. You can read from this file to |
| 3692 | get output from the server. |
| 3693 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3694 | self.write_file is the io object returned by makefile() on the |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3695 | server connection. You can write to this file to send output |
| 3696 | to the client. |
| 3697 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3698 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3699 | bufsize = -1 # Use default buffer size |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 3700 | encoding = 'utf-8' |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3701 | errors = 'strict' |
| 3702 | newline = None |
| 3703 | |
| 3704 | read_mode = 'rb' |
| 3705 | read_msg = MSG |
| 3706 | write_mode = 'wb' |
| 3707 | write_msg = MSG |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3708 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3709 | def __init__(self, methodName='runTest'): |
| 3710 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 3711 | |
| 3712 | def setUp(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 3713 | self.evt1, self.evt2, self.serv_finished, self.cli_finished = [ |
| 3714 | threading.Event() for i in range(4)] |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3715 | SocketConnectedTest.setUp(self) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3716 | self.read_file = self.cli_conn.makefile( |
| 3717 | self.read_mode, self.bufsize, |
| 3718 | encoding = self.encoding, |
| 3719 | errors = self.errors, |
| 3720 | newline = self.newline) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3721 | |
| 3722 | def tearDown(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 3723 | self.serv_finished.set() |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3724 | self.read_file.close() |
| 3725 | self.assertTrue(self.read_file.closed) |
| 3726 | self.read_file = None |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3727 | SocketConnectedTest.tearDown(self) |
| 3728 | |
| 3729 | def clientSetUp(self): |
| 3730 | SocketConnectedTest.clientSetUp(self) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3731 | self.write_file = self.serv_conn.makefile( |
| 3732 | self.write_mode, self.bufsize, |
| 3733 | encoding = self.encoding, |
| 3734 | errors = self.errors, |
| 3735 | newline = self.newline) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3736 | |
| 3737 | def clientTearDown(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 3738 | self.cli_finished.set() |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3739 | self.write_file.close() |
| 3740 | self.assertTrue(self.write_file.closed) |
| 3741 | self.write_file = None |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3742 | SocketConnectedTest.clientTearDown(self) |
| 3743 | |
Antoine Pitrou | 68e5c04 | 2011-02-25 23:07:44 +0000 | [diff] [blame] | 3744 | def testReadAfterTimeout(self): |
| 3745 | # Issue #7322: A file object must disallow further reads |
| 3746 | # after a timeout has occurred. |
| 3747 | self.cli_conn.settimeout(1) |
| 3748 | self.read_file.read(3) |
| 3749 | # First read raises a timeout |
| 3750 | self.assertRaises(socket.timeout, self.read_file.read, 1) |
| 3751 | # Second read is disallowed |
| 3752 | with self.assertRaises(IOError) as ctx: |
| 3753 | self.read_file.read(1) |
| 3754 | self.assertIn("cannot read from timed out object", str(ctx.exception)) |
| 3755 | |
| 3756 | def _testReadAfterTimeout(self): |
| 3757 | self.write_file.write(self.write_msg[0:3]) |
| 3758 | self.write_file.flush() |
| 3759 | self.serv_finished.wait() |
| 3760 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3761 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3762 | # Performing small file read test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3763 | first_seg = self.read_file.read(len(self.read_msg)-3) |
| 3764 | second_seg = self.read_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 3765 | msg = first_seg + second_seg |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3766 | self.assertEqual(msg, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3767 | |
| 3768 | def _testSmallRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3769 | self.write_file.write(self.write_msg) |
| 3770 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3771 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3772 | def testFullRead(self): |
| 3773 | # read until EOF |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3774 | msg = self.read_file.read() |
| 3775 | self.assertEqual(msg, self.read_msg) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3776 | |
| 3777 | def _testFullRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3778 | self.write_file.write(self.write_msg) |
| 3779 | self.write_file.close() |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3780 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3781 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3782 | # Performing unbuffered file read test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3783 | buf = type(self.read_msg)() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3784 | while 1: |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3785 | char = self.read_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3786 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3787 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3788 | buf += char |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3789 | self.assertEqual(buf, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3790 | |
| 3791 | def _testUnbufferedRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3792 | self.write_file.write(self.write_msg) |
| 3793 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3794 | |
| 3795 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3796 | # Performing file readline test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3797 | line = self.read_file.readline() |
| 3798 | self.assertEqual(line, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3799 | |
| 3800 | def _testReadline(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3801 | self.write_file.write(self.write_msg) |
| 3802 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 3803 | |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3804 | def testCloseAfterMakefile(self): |
| 3805 | # The file returned by makefile should keep the socket open. |
| 3806 | self.cli_conn.close() |
| 3807 | # read until EOF |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3808 | msg = self.read_file.read() |
| 3809 | self.assertEqual(msg, self.read_msg) |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3810 | |
| 3811 | def _testCloseAfterMakefile(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3812 | self.write_file.write(self.write_msg) |
| 3813 | self.write_file.flush() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3814 | |
| 3815 | def testMakefileAfterMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3816 | self.read_file.close() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3817 | msg = self.cli_conn.recv(len(MSG)) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3818 | if isinstance(self.read_msg, str): |
| 3819 | msg = msg.decode() |
| 3820 | self.assertEqual(msg, self.read_msg) |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3821 | |
| 3822 | def _testMakefileAfterMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3823 | self.write_file.write(self.write_msg) |
| 3824 | self.write_file.flush() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 3825 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 3826 | def testClosedAttr(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3827 | self.assertTrue(not self.read_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 3828 | |
| 3829 | def _testClosedAttr(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3830 | self.assertTrue(not self.write_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 3831 | |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 3832 | def testAttributes(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3833 | self.assertEqual(self.read_file.mode, self.read_mode) |
| 3834 | self.assertEqual(self.read_file.name, self.cli_conn.fileno()) |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 3835 | |
| 3836 | def _testAttributes(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3837 | self.assertEqual(self.write_file.mode, self.write_mode) |
| 3838 | self.assertEqual(self.write_file.name, self.serv_conn.fileno()) |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 3839 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3840 | def testRealClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3841 | self.read_file.close() |
| 3842 | self.assertRaises(ValueError, self.read_file.fileno) |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3843 | self.cli_conn.close() |
| 3844 | self.assertRaises(socket.error, self.cli_conn.getsockname) |
| 3845 | |
| 3846 | def _testRealClose(self): |
| 3847 | pass |
| 3848 | |
| 3849 | |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3850 | class FileObjectInterruptedTestCase(unittest.TestCase): |
| 3851 | """Test that the file object correctly handles EINTR internally.""" |
| 3852 | |
| 3853 | class MockSocket(object): |
| 3854 | def __init__(self, recv_funcs=()): |
| 3855 | # A generator that returns callables that we'll call for each |
| 3856 | # call to recv(). |
| 3857 | self._recv_step = iter(recv_funcs) |
| 3858 | |
| 3859 | def recv_into(self, buffer): |
| 3860 | data = next(self._recv_step)() |
| 3861 | assert len(buffer) >= len(data) |
| 3862 | buffer[:len(data)] = data |
| 3863 | return len(data) |
| 3864 | |
| 3865 | def _decref_socketios(self): |
| 3866 | pass |
| 3867 | |
| 3868 | def _textiowrap_for_test(self, buffering=-1): |
| 3869 | raw = socket.SocketIO(self, "r") |
| 3870 | if buffering < 0: |
| 3871 | buffering = io.DEFAULT_BUFFER_SIZE |
| 3872 | if buffering == 0: |
| 3873 | return raw |
| 3874 | buffer = io.BufferedReader(raw, buffering) |
| 3875 | text = io.TextIOWrapper(buffer, None, None) |
| 3876 | text.mode = "rb" |
| 3877 | return text |
| 3878 | |
| 3879 | @staticmethod |
| 3880 | def _raise_eintr(): |
Antoine Pitrou | 24d659d | 2011-10-23 23:49:42 +0200 | [diff] [blame] | 3881 | raise socket.error(errno.EINTR, "interrupted") |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3882 | |
| 3883 | def _textiowrap_mock_socket(self, mock, buffering=-1): |
| 3884 | raw = socket.SocketIO(mock, "r") |
| 3885 | if buffering < 0: |
| 3886 | buffering = io.DEFAULT_BUFFER_SIZE |
| 3887 | if buffering == 0: |
| 3888 | return raw |
| 3889 | buffer = io.BufferedReader(raw, buffering) |
| 3890 | text = io.TextIOWrapper(buffer, None, None) |
| 3891 | text.mode = "rb" |
| 3892 | return text |
| 3893 | |
| 3894 | def _test_readline(self, size=-1, buffering=-1): |
| 3895 | mock_sock = self.MockSocket(recv_funcs=[ |
| 3896 | lambda : b"This is the first line\nAnd the sec", |
| 3897 | self._raise_eintr, |
| 3898 | lambda : b"ond line is here\n", |
| 3899 | lambda : b"", |
| 3900 | lambda : b"", # XXX(gps): io library does an extra EOF read |
| 3901 | ]) |
| 3902 | fo = mock_sock._textiowrap_for_test(buffering=buffering) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 3903 | self.assertEqual(fo.readline(size), "This is the first line\n") |
| 3904 | self.assertEqual(fo.readline(size), "And the second line is here\n") |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3905 | |
| 3906 | def _test_read(self, size=-1, buffering=-1): |
| 3907 | mock_sock = self.MockSocket(recv_funcs=[ |
| 3908 | lambda : b"This is the first line\nAnd the sec", |
| 3909 | self._raise_eintr, |
| 3910 | lambda : b"ond line is here\n", |
| 3911 | lambda : b"", |
| 3912 | lambda : b"", # XXX(gps): io library does an extra EOF read |
| 3913 | ]) |
| 3914 | expecting = (b"This is the first line\n" |
| 3915 | b"And the second line is here\n") |
| 3916 | fo = mock_sock._textiowrap_for_test(buffering=buffering) |
| 3917 | if buffering == 0: |
| 3918 | data = b'' |
| 3919 | else: |
| 3920 | data = '' |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 3921 | expecting = expecting.decode('utf-8') |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3922 | while len(data) != len(expecting): |
| 3923 | part = fo.read(size) |
| 3924 | if not part: |
| 3925 | break |
| 3926 | data += part |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 3927 | self.assertEqual(data, expecting) |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3928 | |
| 3929 | def test_default(self): |
| 3930 | self._test_readline() |
| 3931 | self._test_readline(size=100) |
| 3932 | self._test_read() |
| 3933 | self._test_read(size=100) |
| 3934 | |
| 3935 | def test_with_1k_buffer(self): |
| 3936 | self._test_readline(buffering=1024) |
| 3937 | self._test_readline(size=100, buffering=1024) |
| 3938 | self._test_read(buffering=1024) |
| 3939 | self._test_read(size=100, buffering=1024) |
| 3940 | |
| 3941 | def _test_readline_no_buffer(self, size=-1): |
| 3942 | mock_sock = self.MockSocket(recv_funcs=[ |
| 3943 | lambda : b"a", |
| 3944 | lambda : b"\n", |
| 3945 | lambda : b"B", |
| 3946 | self._raise_eintr, |
| 3947 | lambda : b"b", |
| 3948 | lambda : b"", |
| 3949 | ]) |
| 3950 | fo = mock_sock._textiowrap_for_test(buffering=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 3951 | self.assertEqual(fo.readline(size), b"a\n") |
| 3952 | self.assertEqual(fo.readline(size), b"Bb") |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 3953 | |
| 3954 | def test_no_buffer(self): |
| 3955 | self._test_readline_no_buffer() |
| 3956 | self._test_readline_no_buffer(size=4) |
| 3957 | self._test_read(buffering=0) |
| 3958 | self._test_read(size=100, buffering=0) |
| 3959 | |
| 3960 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3961 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 3962 | |
| 3963 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 3964 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3965 | In this case (and in this case only), it should be possible to |
| 3966 | create a file object, read a line from it, create another file |
| 3967 | object, read another line from it, without loss of data in the |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 3968 | first file object's buffer. Note that http.client relies on this |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3969 | when reading multiple requests from the same socket.""" |
| 3970 | |
| 3971 | bufsize = 0 # Use unbuffered mode |
| 3972 | |
| 3973 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 3974 | # Read a line, create a new file object, read another line with it |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3975 | line = self.read_file.readline() # first line |
| 3976 | self.assertEqual(line, b"A. " + self.write_msg) # first line |
| 3977 | self.read_file = self.cli_conn.makefile('rb', 0) |
| 3978 | line = self.read_file.readline() # second line |
| 3979 | self.assertEqual(line, b"B. " + self.write_msg) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3980 | |
| 3981 | def _testUnbufferedReadline(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3982 | self.write_file.write(b"A. " + self.write_msg) |
| 3983 | self.write_file.write(b"B. " + self.write_msg) |
| 3984 | self.write_file.flush() |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 3985 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3986 | def testMakefileClose(self): |
| 3987 | # The file returned by makefile should keep the socket open... |
| 3988 | self.cli_conn.close() |
| 3989 | msg = self.cli_conn.recv(1024) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3990 | self.assertEqual(msg, self.read_msg) |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3991 | # ...until the file is itself closed |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3992 | self.read_file.close() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3993 | self.assertRaises(socket.error, self.cli_conn.recv, 1024) |
| 3994 | |
| 3995 | def _testMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 3996 | self.write_file.write(self.write_msg) |
| 3997 | self.write_file.flush() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 3998 | |
| 3999 | def testMakefileCloseSocketDestroy(self): |
| 4000 | refcount_before = sys.getrefcount(self.cli_conn) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4001 | self.read_file.close() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 4002 | refcount_after = sys.getrefcount(self.cli_conn) |
| 4003 | self.assertEqual(refcount_before - 1, refcount_after) |
| 4004 | |
| 4005 | def _testMakefileCloseSocketDestroy(self): |
| 4006 | pass |
| 4007 | |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4008 | # Non-blocking ops |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4009 | # NOTE: to set `read_file` as non-blocking, we must call |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4010 | # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp). |
| 4011 | |
| 4012 | def testSmallReadNonBlocking(self): |
| 4013 | self.cli_conn.setblocking(False) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4014 | self.assertEqual(self.read_file.readinto(bytearray(10)), None) |
| 4015 | self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4016 | self.evt1.set() |
| 4017 | self.evt2.wait(1.0) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4018 | first_seg = self.read_file.read(len(self.read_msg) - 3) |
Antoine Pitrou | 43bf045 | 2011-04-04 01:21:37 +0200 | [diff] [blame] | 4019 | if first_seg is None: |
| 4020 | # Data not arrived (can happen under Windows), wait a bit |
| 4021 | time.sleep(0.5) |
| 4022 | first_seg = self.read_file.read(len(self.read_msg) - 3) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4023 | buf = bytearray(10) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4024 | n = self.read_file.readinto(buf) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4025 | self.assertEqual(n, 3) |
| 4026 | msg = first_seg + buf[:n] |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4027 | self.assertEqual(msg, self.read_msg) |
| 4028 | self.assertEqual(self.read_file.readinto(bytearray(16)), None) |
| 4029 | self.assertEqual(self.read_file.read(1), None) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4030 | |
| 4031 | def _testSmallReadNonBlocking(self): |
| 4032 | self.evt1.wait(1.0) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4033 | self.write_file.write(self.write_msg) |
| 4034 | self.write_file.flush() |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4035 | self.evt2.set() |
| 4036 | # Avoid cloding the socket before the server test has finished, |
| 4037 | # otherwise system recv() will return 0 instead of EWOULDBLOCK. |
| 4038 | self.serv_finished.wait(5.0) |
| 4039 | |
| 4040 | def testWriteNonBlocking(self): |
| 4041 | self.cli_finished.wait(5.0) |
| 4042 | # The client thread can't skip directly - the SkipTest exception |
| 4043 | # would appear as a failure. |
| 4044 | if self.serv_skipped: |
| 4045 | self.skipTest(self.serv_skipped) |
| 4046 | |
| 4047 | def _testWriteNonBlocking(self): |
| 4048 | self.serv_skipped = None |
| 4049 | self.serv_conn.setblocking(False) |
| 4050 | # Try to saturate the socket buffer pipe with repeated large writes. |
Charles-François Natali | 5fd2642 | 2013-08-29 19:01:40 +0200 | [diff] [blame] | 4051 | BIG = b"x" * support.SOCK_MAX_SIZE |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4052 | LIMIT = 10 |
| 4053 | # The first write() succeeds since a chunk of data can be buffered |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4054 | n = self.write_file.write(BIG) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4055 | self.assertGreater(n, 0) |
| 4056 | for i in range(LIMIT): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4057 | n = self.write_file.write(BIG) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 4058 | if n is None: |
| 4059 | # Succeeded |
| 4060 | break |
| 4061 | self.assertGreater(n, 0) |
| 4062 | else: |
| 4063 | # Let us know that this test didn't manage to establish |
| 4064 | # the expected conditions. This is not a failure in itself but, |
| 4065 | # if it happens repeatedly, the test should be fixed. |
| 4066 | self.serv_skipped = "failed to saturate the socket buffer" |
| 4067 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 4068 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 4069 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 4070 | |
| 4071 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 4072 | |
| 4073 | |
| 4074 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 4075 | |
| 4076 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 4077 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 4078 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4079 | class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): |
| 4080 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 4081 | |
| 4082 | read_mode = 'r' |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 4083 | read_msg = MSG.decode('utf-8') |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4084 | write_mode = 'wb' |
| 4085 | write_msg = MSG |
| 4086 | newline = '' |
| 4087 | |
| 4088 | |
| 4089 | class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): |
| 4090 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 4091 | |
| 4092 | read_mode = 'rb' |
| 4093 | read_msg = MSG |
| 4094 | write_mode = 'w' |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 4095 | write_msg = MSG.decode('utf-8') |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4096 | newline = '' |
| 4097 | |
| 4098 | |
| 4099 | class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): |
| 4100 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 4101 | |
| 4102 | read_mode = 'r' |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 4103 | read_msg = MSG.decode('utf-8') |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4104 | write_mode = 'w' |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 4105 | write_msg = MSG.decode('utf-8') |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4106 | newline = '' |
| 4107 | |
| 4108 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4109 | class NetworkConnectionTest(object): |
| 4110 | """Prove network connection.""" |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4111 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4112 | def clientSetUp(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4113 | # We're inherited below by BasicTCPTest2, which also inherits |
| 4114 | # BasicTCPTest, which defines self.port referenced below. |
| 4115 | self.cli = socket.create_connection((HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4116 | self.serv_conn = self.cli |
| 4117 | |
| 4118 | class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
| 4119 | """Tests that NetworkConnection does not break existing TCP functionality. |
| 4120 | """ |
| 4121 | |
| 4122 | class NetworkConnectionNoServer(unittest.TestCase): |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4123 | |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 4124 | class MockSocket(socket.socket): |
| 4125 | def connect(self, *args): |
| 4126 | raise socket.timeout('timed out') |
| 4127 | |
| 4128 | @contextlib.contextmanager |
| 4129 | def mocked_socket_module(self): |
| 4130 | """Return a socket which times out on connect""" |
| 4131 | old_socket = socket.socket |
| 4132 | socket.socket = self.MockSocket |
| 4133 | try: |
| 4134 | yield |
| 4135 | finally: |
| 4136 | socket.socket = old_socket |
| 4137 | |
| 4138 | def test_connect(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4139 | port = support.find_unused_port() |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 4140 | cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4141 | self.addCleanup(cli.close) |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 4142 | with self.assertRaises(socket.error) as cm: |
| 4143 | cli.connect((HOST, port)) |
| 4144 | self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) |
| 4145 | |
| 4146 | def test_create_connection(self): |
| 4147 | # Issue #9792: errors raised by create_connection() should have |
| 4148 | # a proper errno attribute. |
| 4149 | port = support.find_unused_port() |
| 4150 | with self.assertRaises(socket.error) as cm: |
| 4151 | socket.create_connection((HOST, port)) |
Trent Nelson | 45bb613 | 2012-10-17 06:15:15 -0400 | [diff] [blame] | 4152 | |
| 4153 | # Issue #16257: create_connection() calls getaddrinfo() against |
| 4154 | # 'localhost'. This may result in an IPV6 addr being returned |
| 4155 | # as well as an IPV4 one: |
| 4156 | # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) |
| 4157 | # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), |
| 4158 | # (26, 2, 0, '', ('::1', 41230, 0, 0))] |
| 4159 | # |
| 4160 | # create_connection() enumerates through all the addresses returned |
| 4161 | # and if it doesn't successfully bind to any of them, it propagates |
| 4162 | # the last exception it encountered. |
| 4163 | # |
| 4164 | # On Solaris, ENETUNREACH is returned in this circumstance instead |
| 4165 | # of ECONNREFUSED. So, if that errno exists, add it to our list of |
| 4166 | # expected errnos. |
| 4167 | expected_errnos = [ errno.ECONNREFUSED, ] |
| 4168 | if hasattr(errno, 'ENETUNREACH'): |
| 4169 | expected_errnos.append(errno.ENETUNREACH) |
| 4170 | |
| 4171 | self.assertIn(cm.exception.errno, expected_errnos) |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 4172 | |
| 4173 | def test_create_connection_timeout(self): |
| 4174 | # Issue #9792: create_connection() should not recast timeout errors |
| 4175 | # as generic socket errors. |
| 4176 | with self.mocked_socket_module(): |
| 4177 | with self.assertRaises(socket.timeout): |
| 4178 | socket.create_connection((HOST, 1234)) |
| 4179 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4180 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 4181 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4182 | class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
| 4183 | |
| 4184 | def __init__(self, methodName='runTest'): |
| 4185 | SocketTCPTest.__init__(self, methodName=methodName) |
| 4186 | ThreadableTest.__init__(self) |
| 4187 | |
| 4188 | def clientSetUp(self): |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 4189 | self.source_port = support.find_unused_port() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4190 | |
| 4191 | def clientTearDown(self): |
| 4192 | self.cli.close() |
| 4193 | self.cli = None |
| 4194 | ThreadableTest.clientTearDown(self) |
| 4195 | |
| 4196 | def _justAccept(self): |
| 4197 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4198 | conn.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4199 | |
| 4200 | testFamily = _justAccept |
| 4201 | def _testFamily(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4202 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4203 | self.addCleanup(self.cli.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4204 | self.assertEqual(self.cli.family, 2) |
| 4205 | |
Gregory P. Smith | a81d881 | 2010-01-03 15:09:32 +0000 | [diff] [blame] | 4206 | testSourceAddress = _justAccept |
| 4207 | def _testSourceAddress(self): |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 4208 | self.cli = socket.create_connection((HOST, self.port), timeout=30, |
| 4209 | source_address=('', self.source_port)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4210 | self.addCleanup(self.cli.close) |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 4211 | self.assertEqual(self.cli.getsockname()[1], self.source_port) |
Gregory P. Smith | a81d881 | 2010-01-03 15:09:32 +0000 | [diff] [blame] | 4212 | # The port number being used is sufficient to show that the bind() |
| 4213 | # call happened. |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 4214 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4215 | testTimeoutDefault = _justAccept |
| 4216 | def _testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4217 | # passing no explicit timeout uses socket's global default |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4218 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4219 | socket.setdefaulttimeout(42) |
| 4220 | try: |
| 4221 | self.cli = socket.create_connection((HOST, self.port)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4222 | self.addCleanup(self.cli.close) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4223 | finally: |
| 4224 | socket.setdefaulttimeout(None) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 4225 | self.assertEqual(self.cli.gettimeout(), 42) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4226 | |
| 4227 | testTimeoutNone = _justAccept |
| 4228 | def _testTimeoutNone(self): |
| 4229 | # None timeout means the same as sock.settimeout(None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4230 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4231 | socket.setdefaulttimeout(30) |
| 4232 | try: |
| 4233 | self.cli = socket.create_connection((HOST, self.port), timeout=None) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4234 | self.addCleanup(self.cli.close) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 4235 | finally: |
| 4236 | socket.setdefaulttimeout(None) |
| 4237 | self.assertEqual(self.cli.gettimeout(), None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4238 | |
| 4239 | testTimeoutValueNamed = _justAccept |
| 4240 | def _testTimeoutValueNamed(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4241 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4242 | self.assertEqual(self.cli.gettimeout(), 30) |
| 4243 | |
| 4244 | testTimeoutValueNonamed = _justAccept |
| 4245 | def _testTimeoutValueNonamed(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4246 | self.cli = socket.create_connection((HOST, self.port), 30) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4247 | self.addCleanup(self.cli.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4248 | self.assertEqual(self.cli.gettimeout(), 30) |
| 4249 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 4250 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4251 | class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
| 4252 | |
| 4253 | def __init__(self, methodName='runTest'): |
| 4254 | SocketTCPTest.__init__(self, methodName=methodName) |
| 4255 | ThreadableTest.__init__(self) |
| 4256 | |
| 4257 | def clientSetUp(self): |
| 4258 | pass |
| 4259 | |
| 4260 | def clientTearDown(self): |
| 4261 | self.cli.close() |
| 4262 | self.cli = None |
| 4263 | ThreadableTest.clientTearDown(self) |
| 4264 | |
| 4265 | def testInsideTimeout(self): |
| 4266 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4267 | self.addCleanup(conn.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4268 | time.sleep(3) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4269 | conn.send(b"done!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4270 | testOutsideTimeout = testInsideTimeout |
| 4271 | |
| 4272 | def _testInsideTimeout(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4273 | self.cli = sock = socket.create_connection((HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4274 | data = sock.recv(5) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4275 | self.assertEqual(data, b"done!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4276 | |
| 4277 | def _testOutsideTimeout(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 4278 | self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4279 | self.assertRaises(socket.timeout, lambda: sock.recv(5)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4280 | |
| 4281 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4282 | class TCPTimeoutTest(SocketTCPTest): |
| 4283 | |
| 4284 | def testTCPTimeout(self): |
| 4285 | def raise_timeout(*args, **kwargs): |
| 4286 | self.serv.settimeout(1.0) |
| 4287 | self.serv.accept() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4288 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4289 | "Error generating a timeout exception (TCP)") |
| 4290 | |
| 4291 | def testTimeoutZero(self): |
| 4292 | ok = False |
| 4293 | try: |
| 4294 | self.serv.settimeout(0.0) |
| 4295 | foo = self.serv.accept() |
| 4296 | except socket.timeout: |
| 4297 | self.fail("caught timeout instead of error (TCP)") |
| 4298 | except socket.error: |
| 4299 | ok = True |
| 4300 | except: |
| 4301 | self.fail("caught unexpected exception (TCP)") |
| 4302 | if not ok: |
| 4303 | self.fail("accept() returned success when we did not expect it") |
| 4304 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4305 | def testInterruptedTimeout(self): |
| 4306 | # XXX I don't know how to do this test on MSWindows or any other |
| 4307 | # plaform that doesn't support signal.alarm() or os.kill(), though |
| 4308 | # the bug should have existed on all platforms. |
| 4309 | if not hasattr(signal, "alarm"): |
| 4310 | return # can only test on *nix |
| 4311 | self.serv.settimeout(5.0) # must be longer than alarm |
| 4312 | class Alarm(Exception): |
| 4313 | pass |
| 4314 | def alarm_handler(signal, frame): |
| 4315 | raise Alarm |
| 4316 | old_alarm = signal.signal(signal.SIGALRM, alarm_handler) |
| 4317 | try: |
| 4318 | signal.alarm(2) # POSIX allows alarm to be up to 1 second early |
| 4319 | try: |
| 4320 | foo = self.serv.accept() |
| 4321 | except socket.timeout: |
| 4322 | self.fail("caught timeout instead of Alarm") |
| 4323 | except Alarm: |
| 4324 | pass |
| 4325 | except: |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 4326 | self.fail("caught other exception instead of Alarm:" |
| 4327 | " %s(%s):\n%s" % |
| 4328 | (sys.exc_info()[:2] + (traceback.format_exc(),))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4329 | else: |
| 4330 | self.fail("nothing caught") |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 4331 | finally: |
| 4332 | signal.alarm(0) # shut off alarm |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4333 | except Alarm: |
| 4334 | self.fail("got Alarm in wrong place") |
| 4335 | finally: |
| 4336 | # no alarm can be pending. Safe to restore old handler. |
| 4337 | signal.signal(signal.SIGALRM, old_alarm) |
| 4338 | |
Ezio Melotti | 7f78ddc | 2011-11-18 18:33:14 +0200 | [diff] [blame] | 4339 | class UDPTimeoutTest(SocketUDPTest): |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4340 | |
| 4341 | def testUDPTimeout(self): |
| 4342 | def raise_timeout(*args, **kwargs): |
| 4343 | self.serv.settimeout(1.0) |
| 4344 | self.serv.recv(1024) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4345 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4346 | "Error generating a timeout exception (UDP)") |
| 4347 | |
| 4348 | def testTimeoutZero(self): |
| 4349 | ok = False |
| 4350 | try: |
| 4351 | self.serv.settimeout(0.0) |
| 4352 | foo = self.serv.recv(1024) |
| 4353 | except socket.timeout: |
| 4354 | self.fail("caught timeout instead of error (UDP)") |
| 4355 | except socket.error: |
| 4356 | ok = True |
| 4357 | except: |
| 4358 | self.fail("caught unexpected exception (UDP)") |
| 4359 | if not ok: |
| 4360 | self.fail("recv() returned success when we did not expect it") |
| 4361 | |
| 4362 | class TestExceptions(unittest.TestCase): |
| 4363 | |
| 4364 | def testExceptionTree(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 4365 | self.assertTrue(issubclass(socket.error, Exception)) |
| 4366 | self.assertTrue(issubclass(socket.herror, socket.error)) |
| 4367 | self.assertTrue(issubclass(socket.gaierror, socket.error)) |
| 4368 | self.assertTrue(issubclass(socket.timeout, socket.error)) |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4369 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4370 | class TestLinuxAbstractNamespace(unittest.TestCase): |
| 4371 | |
| 4372 | UNIX_PATH_MAX = 108 |
| 4373 | |
| 4374 | def testLinuxAbstractNamespace(self): |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 4375 | address = b"\x00python-test-hello\x00\xff" |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4376 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1: |
| 4377 | s1.bind(address) |
| 4378 | s1.listen(1) |
| 4379 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2: |
| 4380 | s2.connect(s1.getsockname()) |
| 4381 | with s1.accept()[0] as s3: |
| 4382 | self.assertEqual(s1.getsockname(), address) |
| 4383 | self.assertEqual(s2.getpeername(), address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4384 | |
| 4385 | def testMaxName(self): |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 4386 | address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4387 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 4388 | s.bind(address) |
| 4389 | self.assertEqual(s.getsockname(), address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4390 | |
| 4391 | def testNameOverflow(self): |
| 4392 | address = "\x00" + "h" * self.UNIX_PATH_MAX |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4393 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 4394 | self.assertRaises(socket.error, s.bind, address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4395 | |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4396 | def testStrName(self): |
| 4397 | # Check that an abstract name can be passed as a string. |
| 4398 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 4399 | try: |
| 4400 | s.bind("\x00python\x00test\x00") |
| 4401 | self.assertEqual(s.getsockname(), b"\x00python\x00test\x00") |
| 4402 | finally: |
| 4403 | s.close() |
| 4404 | |
| 4405 | class TestUnixDomain(unittest.TestCase): |
| 4406 | |
| 4407 | def setUp(self): |
| 4408 | self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 4409 | |
| 4410 | def tearDown(self): |
| 4411 | self.sock.close() |
| 4412 | |
| 4413 | def encoded(self, path): |
| 4414 | # Return the given path encoded in the file system encoding, |
| 4415 | # or skip the test if this is not possible. |
| 4416 | try: |
| 4417 | return os.fsencode(path) |
| 4418 | except UnicodeEncodeError: |
| 4419 | self.skipTest( |
| 4420 | "Pathname {0!a} cannot be represented in file " |
| 4421 | "system encoding {1!r}".format( |
| 4422 | path, sys.getfilesystemencoding())) |
| 4423 | |
Antoine Pitrou | 1637487 | 2011-12-16 15:04:12 +0100 | [diff] [blame] | 4424 | def bind(self, sock, path): |
| 4425 | # Bind the socket |
| 4426 | try: |
| 4427 | sock.bind(path) |
| 4428 | except OSError as e: |
| 4429 | if str(e) == "AF_UNIX path too long": |
| 4430 | self.skipTest( |
| 4431 | "Pathname {0!a} is too long to serve as a AF_UNIX path" |
| 4432 | .format(path)) |
| 4433 | else: |
| 4434 | raise |
| 4435 | |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4436 | def testStrAddr(self): |
| 4437 | # Test binding to and retrieving a normal string pathname. |
| 4438 | path = os.path.abspath(support.TESTFN) |
Antoine Pitrou | 1637487 | 2011-12-16 15:04:12 +0100 | [diff] [blame] | 4439 | self.bind(self.sock, path) |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4440 | self.addCleanup(support.unlink, path) |
| 4441 | self.assertEqual(self.sock.getsockname(), path) |
| 4442 | |
| 4443 | def testBytesAddr(self): |
| 4444 | # Test binding to a bytes pathname. |
| 4445 | path = os.path.abspath(support.TESTFN) |
Antoine Pitrou | 1637487 | 2011-12-16 15:04:12 +0100 | [diff] [blame] | 4446 | self.bind(self.sock, self.encoded(path)) |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4447 | self.addCleanup(support.unlink, path) |
| 4448 | self.assertEqual(self.sock.getsockname(), path) |
| 4449 | |
| 4450 | def testSurrogateescapeBind(self): |
| 4451 | # Test binding to a valid non-ASCII pathname, with the |
| 4452 | # non-ASCII bytes supplied using surrogateescape encoding. |
| 4453 | path = os.path.abspath(support.TESTFN_UNICODE) |
| 4454 | b = self.encoded(path) |
Antoine Pitrou | 1637487 | 2011-12-16 15:04:12 +0100 | [diff] [blame] | 4455 | self.bind(self.sock, b.decode("ascii", "surrogateescape")) |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4456 | self.addCleanup(support.unlink, path) |
| 4457 | self.assertEqual(self.sock.getsockname(), path) |
| 4458 | |
| 4459 | def testUnencodableAddr(self): |
| 4460 | # Test binding to a pathname that cannot be encoded in the |
| 4461 | # file system encoding. |
| 4462 | if support.TESTFN_UNENCODABLE is None: |
| 4463 | self.skipTest("No unencodable filename available") |
| 4464 | path = os.path.abspath(support.TESTFN_UNENCODABLE) |
Antoine Pitrou | 1637487 | 2011-12-16 15:04:12 +0100 | [diff] [blame] | 4465 | self.bind(self.sock, path) |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4466 | self.addCleanup(support.unlink, path) |
| 4467 | self.assertEqual(self.sock.getsockname(), path) |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 4468 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 4469 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4470 | class BufferIOTest(SocketConnectedTest): |
| 4471 | """ |
| 4472 | Test the buffer versions of socket.recv() and socket.send(). |
| 4473 | """ |
| 4474 | def __init__(self, methodName='runTest'): |
| 4475 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 4476 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 4477 | def testRecvIntoArray(self): |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 4478 | buf = bytearray(1024) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4479 | nbytes = self.cli_conn.recv_into(buf) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4480 | self.assertEqual(nbytes, len(MSG)) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4481 | msg = buf[:len(MSG)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4482 | self.assertEqual(msg, MSG) |
| 4483 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 4484 | def _testRecvIntoArray(self): |
Guido van Rossum | b5b2270 | 2007-05-18 18:55:53 +0000 | [diff] [blame] | 4485 | buf = bytes(MSG) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4486 | self.serv_conn.send(buf) |
| 4487 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 4488 | def testRecvIntoBytearray(self): |
| 4489 | buf = bytearray(1024) |
| 4490 | nbytes = self.cli_conn.recv_into(buf) |
| 4491 | self.assertEqual(nbytes, len(MSG)) |
| 4492 | msg = buf[:len(MSG)] |
| 4493 | self.assertEqual(msg, MSG) |
| 4494 | |
| 4495 | _testRecvIntoBytearray = _testRecvIntoArray |
| 4496 | |
| 4497 | def testRecvIntoMemoryview(self): |
| 4498 | buf = bytearray(1024) |
| 4499 | nbytes = self.cli_conn.recv_into(memoryview(buf)) |
| 4500 | self.assertEqual(nbytes, len(MSG)) |
| 4501 | msg = buf[:len(MSG)] |
| 4502 | self.assertEqual(msg, MSG) |
| 4503 | |
| 4504 | _testRecvIntoMemoryview = _testRecvIntoArray |
| 4505 | |
| 4506 | def testRecvFromIntoArray(self): |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 4507 | buf = bytearray(1024) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 4508 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4509 | self.assertEqual(nbytes, len(MSG)) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 4510 | msg = buf[:len(MSG)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4511 | self.assertEqual(msg, MSG) |
| 4512 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 4513 | def _testRecvFromIntoArray(self): |
Guido van Rossum | b5b2270 | 2007-05-18 18:55:53 +0000 | [diff] [blame] | 4514 | buf = bytes(MSG) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4515 | self.serv_conn.send(buf) |
| 4516 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 4517 | def testRecvFromIntoBytearray(self): |
| 4518 | buf = bytearray(1024) |
| 4519 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
| 4520 | self.assertEqual(nbytes, len(MSG)) |
| 4521 | msg = buf[:len(MSG)] |
| 4522 | self.assertEqual(msg, MSG) |
| 4523 | |
| 4524 | _testRecvFromIntoBytearray = _testRecvFromIntoArray |
| 4525 | |
| 4526 | def testRecvFromIntoMemoryview(self): |
| 4527 | buf = bytearray(1024) |
| 4528 | nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) |
| 4529 | self.assertEqual(nbytes, len(MSG)) |
| 4530 | msg = buf[:len(MSG)] |
| 4531 | self.assertEqual(msg, MSG) |
| 4532 | |
| 4533 | _testRecvFromIntoMemoryview = _testRecvFromIntoArray |
| 4534 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4535 | |
| 4536 | TIPC_STYPE = 2000 |
| 4537 | TIPC_LOWER = 200 |
| 4538 | TIPC_UPPER = 210 |
| 4539 | |
| 4540 | def isTipcAvailable(): |
| 4541 | """Check if the TIPC module is loaded |
| 4542 | |
| 4543 | The TIPC module is not loaded automatically on Ubuntu and probably |
| 4544 | other Linux distros. |
| 4545 | """ |
| 4546 | if not hasattr(socket, "AF_TIPC"): |
| 4547 | return False |
| 4548 | if not os.path.isfile("/proc/modules"): |
| 4549 | return False |
| 4550 | with open("/proc/modules") as f: |
| 4551 | for line in f: |
| 4552 | if line.startswith("tipc "): |
| 4553 | return True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4554 | if support.verbose: |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4555 | print("TIPC module is not loaded, please 'sudo modprobe tipc'") |
| 4556 | return False |
| 4557 | |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4558 | class TIPCTest(unittest.TestCase): |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4559 | def testRDM(self): |
| 4560 | srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 4561 | cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4562 | self.addCleanup(srv.close) |
| 4563 | self.addCleanup(cli.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4564 | |
| 4565 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 4566 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 4567 | TIPC_LOWER, TIPC_UPPER) |
| 4568 | srv.bind(srvaddr) |
| 4569 | |
| 4570 | sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 4571 | TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) |
| 4572 | cli.sendto(MSG, sendaddr) |
| 4573 | |
| 4574 | msg, recvaddr = srv.recvfrom(1024) |
| 4575 | |
| 4576 | self.assertEqual(cli.getsockname(), recvaddr) |
| 4577 | self.assertEqual(msg, MSG) |
| 4578 | |
| 4579 | |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4580 | class TIPCThreadableTest(unittest.TestCase, ThreadableTest): |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4581 | def __init__(self, methodName = 'runTest'): |
| 4582 | unittest.TestCase.__init__(self, methodName = methodName) |
| 4583 | ThreadableTest.__init__(self) |
| 4584 | |
| 4585 | def setUp(self): |
| 4586 | self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4587 | self.addCleanup(self.srv.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4588 | self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 4589 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 4590 | TIPC_LOWER, TIPC_UPPER) |
| 4591 | self.srv.bind(srvaddr) |
| 4592 | self.srv.listen(5) |
| 4593 | self.serverExplicitReady() |
| 4594 | self.conn, self.connaddr = self.srv.accept() |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4595 | self.addCleanup(self.conn.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4596 | |
| 4597 | def clientSetUp(self): |
| 4598 | # The is a hittable race between serverExplicitReady() and the |
| 4599 | # accept() call; sleep a little while to avoid it, otherwise |
| 4600 | # we could get an exception |
| 4601 | time.sleep(0.1) |
| 4602 | self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 4603 | self.addCleanup(self.cli.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4604 | addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 4605 | TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) |
| 4606 | self.cli.connect(addr) |
| 4607 | self.cliaddr = self.cli.getsockname() |
| 4608 | |
| 4609 | def testStream(self): |
| 4610 | msg = self.conn.recv(1024) |
| 4611 | self.assertEqual(msg, MSG) |
| 4612 | self.assertEqual(self.cliaddr, self.connaddr) |
| 4613 | |
| 4614 | def _testStream(self): |
| 4615 | self.cli.send(MSG) |
| 4616 | self.cli.close() |
| 4617 | |
| 4618 | |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 4619 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 4620 | class ContextManagersTest(ThreadedTCPSocketTest): |
| 4621 | |
| 4622 | def _testSocketClass(self): |
| 4623 | # base test |
| 4624 | with socket.socket() as sock: |
| 4625 | self.assertFalse(sock._closed) |
| 4626 | self.assertTrue(sock._closed) |
| 4627 | # close inside with block |
| 4628 | with socket.socket() as sock: |
| 4629 | sock.close() |
| 4630 | self.assertTrue(sock._closed) |
| 4631 | # exception inside with block |
| 4632 | with socket.socket() as sock: |
| 4633 | self.assertRaises(socket.error, sock.sendall, b'foo') |
| 4634 | self.assertTrue(sock._closed) |
| 4635 | |
| 4636 | def testCreateConnectionBase(self): |
| 4637 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4638 | self.addCleanup(conn.close) |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 4639 | data = conn.recv(1024) |
| 4640 | conn.sendall(data) |
| 4641 | |
| 4642 | def _testCreateConnectionBase(self): |
| 4643 | address = self.serv.getsockname() |
| 4644 | with socket.create_connection(address) as sock: |
| 4645 | self.assertFalse(sock._closed) |
| 4646 | sock.sendall(b'foo') |
| 4647 | self.assertEqual(sock.recv(1024), b'foo') |
| 4648 | self.assertTrue(sock._closed) |
| 4649 | |
| 4650 | def testCreateConnectionClose(self): |
| 4651 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 4652 | self.addCleanup(conn.close) |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 4653 | data = conn.recv(1024) |
| 4654 | conn.sendall(data) |
| 4655 | |
| 4656 | def _testCreateConnectionClose(self): |
| 4657 | address = self.serv.getsockname() |
| 4658 | with socket.create_connection(address) as sock: |
| 4659 | sock.close() |
| 4660 | self.assertTrue(sock._closed) |
| 4661 | self.assertRaises(socket.error, sock.sendall, b'foo') |
| 4662 | |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4664 | @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"), |
| 4665 | "SOCK_CLOEXEC not defined") |
| 4666 | @unittest.skipUnless(fcntl, "module fcntl not available") |
| 4667 | class CloexecConstantTest(unittest.TestCase): |
Charles-François Natali | 239bb96 | 2011-06-03 12:55:15 +0200 | [diff] [blame] | 4668 | @support.requires_linux_version(2, 6, 28) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4669 | def test_SOCK_CLOEXEC(self): |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4670 | with socket.socket(socket.AF_INET, |
| 4671 | socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s: |
| 4672 | self.assertTrue(s.type & socket.SOCK_CLOEXEC) |
| 4673 | self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4674 | |
| 4675 | |
| 4676 | @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), |
| 4677 | "SOCK_NONBLOCK not defined") |
| 4678 | class NonblockConstantTest(unittest.TestCase): |
| 4679 | def checkNonblock(self, s, nonblock=True, timeout=0.0): |
| 4680 | if nonblock: |
| 4681 | self.assertTrue(s.type & socket.SOCK_NONBLOCK) |
| 4682 | self.assertEqual(s.gettimeout(), timeout) |
| 4683 | else: |
| 4684 | self.assertFalse(s.type & socket.SOCK_NONBLOCK) |
| 4685 | self.assertEqual(s.gettimeout(), None) |
| 4686 | |
Charles-François Natali | 239bb96 | 2011-06-03 12:55:15 +0200 | [diff] [blame] | 4687 | @support.requires_linux_version(2, 6, 28) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4688 | def test_SOCK_NONBLOCK(self): |
| 4689 | # a lot of it seems silly and redundant, but I wanted to test that |
| 4690 | # changing back and forth worked ok |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4691 | with socket.socket(socket.AF_INET, |
| 4692 | socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s: |
| 4693 | self.checkNonblock(s) |
| 4694 | s.setblocking(1) |
| 4695 | self.checkNonblock(s, False) |
| 4696 | s.setblocking(0) |
| 4697 | self.checkNonblock(s) |
| 4698 | s.settimeout(None) |
| 4699 | self.checkNonblock(s, False) |
| 4700 | s.settimeout(2.0) |
| 4701 | self.checkNonblock(s, timeout=2.0) |
| 4702 | s.setblocking(1) |
| 4703 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4704 | # defaulttimeout |
| 4705 | t = socket.getdefaulttimeout() |
| 4706 | socket.setdefaulttimeout(0.0) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4707 | with socket.socket() as s: |
| 4708 | self.checkNonblock(s) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4709 | socket.setdefaulttimeout(None) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4710 | with socket.socket() as s: |
| 4711 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4712 | socket.setdefaulttimeout(2.0) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4713 | with socket.socket() as s: |
| 4714 | self.checkNonblock(s, timeout=2.0) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4715 | socket.setdefaulttimeout(None) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 4716 | with socket.socket() as s: |
| 4717 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4718 | socket.setdefaulttimeout(t) |
| 4719 | |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 4720 | |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4721 | @unittest.skipUnless(os.name == "nt", "Windows specific") |
| 4722 | @unittest.skipUnless(multiprocessing, "need multiprocessing") |
| 4723 | class TestSocketSharing(SocketTCPTest): |
| 4724 | # This must be classmethod and not staticmethod or multiprocessing |
| 4725 | # won't be able to bootstrap it. |
| 4726 | @classmethod |
| 4727 | def remoteProcessServer(cls, q): |
| 4728 | # Recreate socket from shared data |
| 4729 | sdata = q.get() |
| 4730 | message = q.get() |
| 4731 | |
| 4732 | s = socket.fromshare(sdata) |
| 4733 | s2, c = s.accept() |
| 4734 | |
| 4735 | # Send the message |
| 4736 | s2.sendall(message) |
| 4737 | s2.close() |
| 4738 | s.close() |
| 4739 | |
| 4740 | def testShare(self): |
| 4741 | # Transfer the listening server socket to another process |
| 4742 | # and service it from there. |
| 4743 | |
| 4744 | # Create process: |
| 4745 | q = multiprocessing.Queue() |
| 4746 | p = multiprocessing.Process(target=self.remoteProcessServer, args=(q,)) |
| 4747 | p.start() |
| 4748 | |
| 4749 | # Get the shared socket data |
| 4750 | data = self.serv.share(p.pid) |
| 4751 | |
| 4752 | # Pass the shared socket to the other process |
| 4753 | addr = self.serv.getsockname() |
| 4754 | self.serv.close() |
| 4755 | q.put(data) |
| 4756 | |
| 4757 | # The data that the server will send us |
| 4758 | message = b"slapmahfro" |
| 4759 | q.put(message) |
| 4760 | |
| 4761 | # Connect |
| 4762 | s = socket.create_connection(addr) |
| 4763 | # listen for the data |
| 4764 | m = [] |
| 4765 | while True: |
| 4766 | data = s.recv(100) |
| 4767 | if not data: |
| 4768 | break |
| 4769 | m.append(data) |
| 4770 | s.close() |
| 4771 | received = b"".join(m) |
| 4772 | self.assertEqual(received, message) |
| 4773 | p.join() |
| 4774 | |
| 4775 | def testShareLength(self): |
| 4776 | data = self.serv.share(os.getpid()) |
| 4777 | self.assertRaises(ValueError, socket.fromshare, data[:-1]) |
| 4778 | self.assertRaises(ValueError, socket.fromshare, data+b"foo") |
| 4779 | |
| 4780 | def compareSockets(self, org, other): |
| 4781 | # socket sharing is expected to work only for blocking socket |
| 4782 | # since the internal python timout value isn't transfered. |
| 4783 | self.assertEqual(org.gettimeout(), None) |
| 4784 | self.assertEqual(org.gettimeout(), other.gettimeout()) |
| 4785 | |
| 4786 | self.assertEqual(org.family, other.family) |
| 4787 | self.assertEqual(org.type, other.type) |
| 4788 | # If the user specified "0" for proto, then |
| 4789 | # internally windows will have picked the correct value. |
| 4790 | # Python introspection on the socket however will still return |
| 4791 | # 0. For the shared socket, the python value is recreated |
| 4792 | # from the actual value, so it may not compare correctly. |
| 4793 | if org.proto != 0: |
| 4794 | self.assertEqual(org.proto, other.proto) |
| 4795 | |
| 4796 | def testShareLocal(self): |
| 4797 | data = self.serv.share(os.getpid()) |
| 4798 | s = socket.fromshare(data) |
| 4799 | try: |
| 4800 | self.compareSockets(self.serv, s) |
| 4801 | finally: |
| 4802 | s.close() |
| 4803 | |
| 4804 | def testTypes(self): |
| 4805 | families = [socket.AF_INET, socket.AF_INET6] |
| 4806 | types = [socket.SOCK_STREAM, socket.SOCK_DGRAM] |
| 4807 | for f in families: |
| 4808 | for t in types: |
Kristján Valur Jónsson | 52a11f1 | 2012-04-07 20:38:44 +0000 | [diff] [blame] | 4809 | try: |
| 4810 | source = socket.socket(f, t) |
| 4811 | except OSError: |
| 4812 | continue # This combination is not supported |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4813 | try: |
| 4814 | data = source.share(os.getpid()) |
| 4815 | shared = socket.fromshare(data) |
| 4816 | try: |
| 4817 | self.compareSockets(source, shared) |
| 4818 | finally: |
| 4819 | shared.close() |
| 4820 | finally: |
| 4821 | source.close() |
| 4822 | |
| 4823 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 4824 | def test_main(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4825 | tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 4826 | TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4827 | |
| 4828 | tests.extend([ |
| 4829 | NonBlockingTCPTests, |
| 4830 | FileObjectClassTestCase, |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 4831 | FileObjectInterruptedTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4832 | UnbufferedFileObjectClassTestCase, |
| 4833 | LineBufferedFileObjectClassTestCase, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 4834 | SmallBufferedFileObjectClassTestCase, |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 4835 | UnicodeReadFileObjectClassTestCase, |
| 4836 | UnicodeWriteFileObjectClassTestCase, |
| 4837 | UnicodeReadWriteFileObjectClassTestCase, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4838 | NetworkConnectionNoServer, |
| 4839 | NetworkConnectionAttributesTest, |
| 4840 | NetworkConnectionBehaviourTest, |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 4841 | ContextManagersTest, |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 4842 | CloexecConstantTest, |
| 4843 | NonblockConstantTest |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4844 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 4845 | if hasattr(socket, "socketpair"): |
| 4846 | tests.append(BasicSocketPairTest) |
Antoine Pitrou | 6ec29e2 | 2011-12-16 14:46:36 +0100 | [diff] [blame] | 4847 | if hasattr(socket, "AF_UNIX"): |
| 4848 | tests.append(TestUnixDomain) |
Victor Stinner | e674747 | 2011-08-21 00:39:18 +0200 | [diff] [blame] | 4849 | if sys.platform == 'linux': |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4850 | tests.append(TestLinuxAbstractNamespace) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 4851 | if isTipcAvailable(): |
| 4852 | tests.append(TIPCTest) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 4853 | tests.append(TIPCThreadableTest) |
Charles-François Natali | 47413c1 | 2011-10-06 19:47:44 +0200 | [diff] [blame] | 4854 | tests.extend([BasicCANTest, CANTest]) |
Charles-François Natali | 10b8cf4 | 2011-11-10 19:21:37 +0100 | [diff] [blame] | 4855 | tests.extend([BasicRDSTest, RDSTest]) |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4856 | tests.extend([ |
| 4857 | CmsgMacroTests, |
| 4858 | SendmsgUDPTest, |
| 4859 | RecvmsgUDPTest, |
| 4860 | RecvmsgIntoUDPTest, |
| 4861 | SendmsgUDP6Test, |
| 4862 | RecvmsgUDP6Test, |
| 4863 | RecvmsgRFC3542AncillaryUDP6Test, |
| 4864 | RecvmsgIntoRFC3542AncillaryUDP6Test, |
| 4865 | RecvmsgIntoUDP6Test, |
| 4866 | SendmsgTCPTest, |
| 4867 | RecvmsgTCPTest, |
| 4868 | RecvmsgIntoTCPTest, |
| 4869 | SendmsgSCTPStreamTest, |
| 4870 | RecvmsgSCTPStreamTest, |
| 4871 | RecvmsgIntoSCTPStreamTest, |
| 4872 | SendmsgUnixStreamTest, |
| 4873 | RecvmsgUnixStreamTest, |
| 4874 | RecvmsgIntoUnixStreamTest, |
| 4875 | RecvmsgSCMRightsStreamTest, |
| 4876 | RecvmsgIntoSCMRightsStreamTest, |
| 4877 | # These are slow when setitimer() is not available |
| 4878 | InterruptedRecvTimeoutTest, |
| 4879 | InterruptedSendTimeoutTest, |
Kristján Valur Jónsson | 10f383a | 2012-04-07 11:23:31 +0000 | [diff] [blame] | 4880 | TestSocketSharing, |
Nick Coghlan | 96fe56a | 2011-08-22 11:55:57 +1000 | [diff] [blame] | 4881 | ]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4882 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4883 | thread_info = support.threading_setup() |
| 4884 | support.run_unittest(*tests) |
| 4885 | support.threading_cleanup(*thread_info) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 4886 | |
| 4887 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 4888 | test_main() |