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 |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 5 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 6 | import errno |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 7 | import io |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 8 | import socket |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 9 | import select |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 10 | import _testcapi |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 11 | import time |
| 12 | import traceback |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 13 | import queue |
Jack Jansen | 522e769 | 2002-09-06 21:57:50 +0000 | [diff] [blame] | 14 | import sys |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 15 | import os |
| 16 | import array |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 17 | import platform |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 18 | import contextlib |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 19 | from weakref import proxy |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | import signal |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 21 | import math |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 22 | try: |
| 23 | import fcntl |
| 24 | except ImportError: |
| 25 | fcntl = False |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 26 | |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 27 | def try_address(host, port=0, family=socket.AF_INET): |
| 28 | """Try to bind a socket on the given host:port and return True |
| 29 | if that has been possible.""" |
| 30 | try: |
| 31 | sock = socket.socket(family, socket.SOCK_STREAM) |
| 32 | sock.bind((host, port)) |
| 33 | except (socket.error, socket.gaierror): |
| 34 | return False |
| 35 | else: |
| 36 | sock.close() |
| 37 | return True |
| 38 | |
Antoine Pitrou | 1cae8b5 | 2010-10-23 17:05:13 +0000 | [diff] [blame] | 39 | def linux_version(): |
| 40 | try: |
| 41 | # platform.release() is something like '2.6.33.7-desktop-2mnb' |
| 42 | version_string = platform.release().split('-')[0] |
| 43 | return tuple(map(int, version_string.split('.'))) |
| 44 | except ValueError: |
| 45 | return 0, 0, 0 |
| 46 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 47 | HOST = support.HOST |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 48 | MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf8') ## test unicode string and carriage return |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 49 | SUPPORTS_IPV6 = socket.has_ipv6 and try_address('::1', family=socket.AF_INET6) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 50 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 51 | try: |
| 52 | import _thread as thread |
| 53 | import threading |
| 54 | except ImportError: |
| 55 | thread = None |
| 56 | threading = None |
| 57 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 58 | class SocketTCPTest(unittest.TestCase): |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 60 | def setUp(self): |
| 61 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 62 | self.port = support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 63 | self.serv.listen(1) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 65 | def tearDown(self): |
| 66 | self.serv.close() |
| 67 | self.serv = None |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 69 | class SocketUDPTest(unittest.TestCase): |
| 70 | |
| 71 | def setUp(self): |
| 72 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 73 | self.port = support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 74 | |
| 75 | def tearDown(self): |
| 76 | self.serv.close() |
| 77 | self.serv = None |
| 78 | |
| 79 | class ThreadableTest: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 80 | """Threadable Test class |
| 81 | |
| 82 | The ThreadableTest class makes it easy to create a threaded |
| 83 | client/server pair from an existing unit test. To create a |
| 84 | new threaded class from an existing unit test, use multiple |
| 85 | inheritance: |
| 86 | |
| 87 | class NewClass (OldClass, ThreadableTest): |
| 88 | pass |
| 89 | |
| 90 | This class defines two new fixture functions with obvious |
| 91 | purposes for overriding: |
| 92 | |
| 93 | clientSetUp () |
| 94 | clientTearDown () |
| 95 | |
| 96 | Any new test functions within the class must then define |
| 97 | tests in pairs, where the test name is preceeded with a |
| 98 | '_' to indicate the client portion of the test. Ex: |
| 99 | |
| 100 | def testFoo(self): |
| 101 | # Server portion |
| 102 | |
| 103 | def _testFoo(self): |
| 104 | # Client portion |
| 105 | |
| 106 | Any exceptions raised by the clients during their tests |
| 107 | are caught and transferred to the main thread to alert |
| 108 | the testing framework. |
| 109 | |
| 110 | Note, the server setup function cannot call any blocking |
| 111 | functions that rely on the client thread during setup, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 112 | unless serverExplicitReady() is called just before |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 113 | the blocking call (such as in setting up a client/server |
| 114 | connection and performing the accept() in setUp(). |
| 115 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 116 | |
| 117 | def __init__(self): |
| 118 | # Swap the true setup function |
| 119 | self.__setUp = self.setUp |
| 120 | self.__tearDown = self.tearDown |
| 121 | self.setUp = self._setUp |
| 122 | self.tearDown = self._tearDown |
| 123 | |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 124 | def serverExplicitReady(self): |
| 125 | """This method allows the server to explicitly indicate that |
| 126 | it wants the client thread to proceed. This is useful if the |
| 127 | server is about to execute a blocking routine that is |
| 128 | dependent upon the client thread during its setup routine.""" |
| 129 | self.server_ready.set() |
| 130 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 131 | def _setUp(self): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 132 | self.server_ready = threading.Event() |
| 133 | self.client_ready = threading.Event() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 134 | self.done = threading.Event() |
Alexandre Vassalotti | f260e44 | 2008-05-11 19:59:59 +0000 | [diff] [blame] | 135 | self.queue = queue.Queue(1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 136 | |
| 137 | # Do some munging to start the client test. |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 138 | methodname = self.id() |
| 139 | i = methodname.rfind('.') |
| 140 | methodname = methodname[i+1:] |
| 141 | test_method = getattr(self, '_' + methodname) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 142 | self.client_thread = thread.start_new_thread( |
| 143 | self.clientRun, (test_method,)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 144 | |
| 145 | self.__setUp() |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 146 | if not self.server_ready.is_set(): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 147 | self.server_ready.set() |
| 148 | self.client_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 149 | |
| 150 | def _tearDown(self): |
| 151 | self.__tearDown() |
| 152 | self.done.wait() |
| 153 | |
Raymond Hettinger | da3caed | 2008-01-14 21:39:24 +0000 | [diff] [blame] | 154 | if self.queue.qsize(): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 155 | exc = self.queue.get() |
| 156 | raise exc |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 157 | |
| 158 | def clientRun(self, test_func): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 159 | self.server_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 160 | self.clientSetUp() |
Charles-François Natali | a49ed76 | 2011-09-21 22:03:58 +0200 | [diff] [blame] | 161 | self.client_ready.set() |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 162 | if not hasattr(test_func, '__call__'): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 163 | raise TypeError("test_func must be a callable function") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 164 | try: |
| 165 | test_func() |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 166 | except BaseException as e: |
| 167 | self.queue.put(e) |
| 168 | finally: |
| 169 | self.clientTearDown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 170 | |
| 171 | def clientSetUp(self): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 172 | raise NotImplementedError("clientSetUp must be implemented.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 173 | |
| 174 | def clientTearDown(self): |
| 175 | self.done.set() |
| 176 | thread.exit() |
| 177 | |
| 178 | class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): |
| 179 | |
| 180 | def __init__(self, methodName='runTest'): |
| 181 | SocketTCPTest.__init__(self, methodName=methodName) |
| 182 | ThreadableTest.__init__(self) |
| 183 | |
| 184 | def clientSetUp(self): |
| 185 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 186 | |
| 187 | def clientTearDown(self): |
| 188 | self.cli.close() |
| 189 | self.cli = None |
| 190 | ThreadableTest.clientTearDown(self) |
| 191 | |
| 192 | class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): |
| 193 | |
| 194 | def __init__(self, methodName='runTest'): |
| 195 | SocketUDPTest.__init__(self, methodName=methodName) |
| 196 | ThreadableTest.__init__(self) |
| 197 | |
| 198 | def clientSetUp(self): |
| 199 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 200 | |
Brian Curtin | 3beb38f | 2010-11-04 03:41:43 +0000 | [diff] [blame] | 201 | def clientTearDown(self): |
| 202 | self.cli.close() |
| 203 | self.cli = None |
| 204 | ThreadableTest.clientTearDown(self) |
| 205 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 206 | class SocketConnectedTest(ThreadedTCPSocketTest): |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 207 | """Socket tests for client-server connection. |
| 208 | |
| 209 | self.cli_conn is a client socket connected to the server. The |
| 210 | setUp() method guarantees that it is connected to the server. |
| 211 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 212 | |
| 213 | def __init__(self, methodName='runTest'): |
| 214 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 215 | |
| 216 | def setUp(self): |
| 217 | ThreadedTCPSocketTest.setUp(self) |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 218 | # Indicate explicitly we're ready for the client thread to |
| 219 | # proceed and then perform the blocking call to accept |
| 220 | self.serverExplicitReady() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 221 | conn, addr = self.serv.accept() |
| 222 | self.cli_conn = conn |
| 223 | |
| 224 | def tearDown(self): |
| 225 | self.cli_conn.close() |
| 226 | self.cli_conn = None |
| 227 | ThreadedTCPSocketTest.tearDown(self) |
| 228 | |
| 229 | def clientSetUp(self): |
| 230 | ThreadedTCPSocketTest.clientSetUp(self) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 231 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 232 | self.serv_conn = self.cli |
| 233 | |
| 234 | def clientTearDown(self): |
| 235 | self.serv_conn.close() |
| 236 | self.serv_conn = None |
| 237 | ThreadedTCPSocketTest.clientTearDown(self) |
| 238 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 239 | class SocketPairTest(unittest.TestCase, ThreadableTest): |
| 240 | |
| 241 | def __init__(self, methodName='runTest'): |
| 242 | unittest.TestCase.__init__(self, methodName=methodName) |
| 243 | ThreadableTest.__init__(self) |
| 244 | |
| 245 | def setUp(self): |
| 246 | self.serv, self.cli = socket.socketpair() |
| 247 | |
| 248 | def tearDown(self): |
| 249 | self.serv.close() |
| 250 | self.serv = None |
| 251 | |
| 252 | def clientSetUp(self): |
| 253 | pass |
| 254 | |
| 255 | def clientTearDown(self): |
| 256 | self.cli.close() |
| 257 | self.cli = None |
| 258 | ThreadableTest.clientTearDown(self) |
| 259 | |
Tim Peters | 494aaee | 2004-08-09 18:54:11 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 261 | ####################################################################### |
| 262 | ## Begin Tests |
| 263 | |
| 264 | class GeneralModuleTests(unittest.TestCase): |
| 265 | |
Walter Dörwald | a7eb93e | 2007-06-05 13:41:53 +0000 | [diff] [blame] | 266 | def test_repr(self): |
| 267 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 268 | self.addCleanup(s.close) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 269 | self.assertTrue(repr(s).startswith("<socket.socket object")) |
Walter Dörwald | a7eb93e | 2007-06-05 13:41:53 +0000 | [diff] [blame] | 270 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 271 | def test_weakref(self): |
| 272 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 273 | p = proxy(s) |
| 274 | self.assertEqual(p.fileno(), s.fileno()) |
| 275 | s.close() |
| 276 | s = None |
| 277 | try: |
| 278 | p.fileno() |
| 279 | except ReferenceError: |
| 280 | pass |
| 281 | else: |
| 282 | self.fail('Socket proxy still exists') |
| 283 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 284 | def testSocketError(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 285 | # Testing socket module exceptions |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 286 | def raise_error(*args, **kwargs): |
| 287 | raise socket.error |
| 288 | def raise_herror(*args, **kwargs): |
| 289 | raise socket.herror |
| 290 | def raise_gaierror(*args, **kwargs): |
| 291 | raise socket.gaierror |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 292 | self.assertRaises(socket.error, raise_error, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 293 | "Error raising socket exception.") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 294 | self.assertRaises(socket.error, raise_herror, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 295 | "Error raising socket exception.") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 296 | self.assertRaises(socket.error, raise_gaierror, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 297 | "Error raising socket exception.") |
| 298 | |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 299 | def testSendtoErrors(self): |
| 300 | # Testing that sendto doens't masks failures. See #10169. |
| 301 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 302 | self.addCleanup(s.close) |
| 303 | s.bind(('', 0)) |
| 304 | sockname = s.getsockname() |
| 305 | # 2 args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 306 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 307 | s.sendto('\u2620', sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 308 | self.assertEqual(str(cm.exception), |
| 309 | "'str' does not support the buffer interface") |
| 310 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 311 | s.sendto(5j, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 312 | self.assertEqual(str(cm.exception), |
| 313 | "'complex' does not support the buffer interface") |
| 314 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 315 | s.sendto(b'foo', None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 316 | self.assertIn('not NoneType',str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 317 | # 3 args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 318 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 319 | s.sendto('\u2620', 0, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 320 | self.assertEqual(str(cm.exception), |
| 321 | "'str' does not support the buffer interface") |
| 322 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 323 | s.sendto(5j, 0, sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 324 | self.assertEqual(str(cm.exception), |
| 325 | "'complex' does not support the buffer interface") |
| 326 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 327 | s.sendto(b'foo', 0, None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 328 | self.assertIn('not NoneType', str(cm.exception)) |
| 329 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 330 | s.sendto(b'foo', 'bar', sockname) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 331 | self.assertIn('an integer is required', str(cm.exception)) |
| 332 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 333 | s.sendto(b'foo', None, None) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 334 | self.assertIn('an integer is required', str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 335 | # wrong number of args |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 336 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 337 | s.sendto(b'foo') |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 338 | self.assertIn('(1 given)', str(cm.exception)) |
| 339 | with self.assertRaises(TypeError) as cm: |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 340 | s.sendto(b'foo', 0, sockname, 4) |
Ezio Melotti | 4c1aebd | 2011-05-07 19:50:28 +0300 | [diff] [blame] | 341 | self.assertIn('(4 given)', str(cm.exception)) |
Ezio Melotti | 63e4230 | 2011-05-07 19:47:48 +0300 | [diff] [blame] | 342 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 343 | def testCrucialConstants(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 344 | # Testing for mission critical constants |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 345 | socket.AF_INET |
| 346 | socket.SOCK_STREAM |
| 347 | socket.SOCK_DGRAM |
| 348 | socket.SOCK_RAW |
| 349 | socket.SOCK_RDM |
| 350 | socket.SOCK_SEQPACKET |
| 351 | socket.SOL_SOCKET |
| 352 | socket.SO_REUSEADDR |
| 353 | |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 354 | def testHostnameRes(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 355 | # Testing hostname resolution mechanisms |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 356 | hostname = socket.gethostname() |
Guido van Rossum | 71e0294 | 2002-12-26 16:55:15 +0000 | [diff] [blame] | 357 | try: |
| 358 | ip = socket.gethostbyname(hostname) |
| 359 | except socket.error: |
| 360 | # Probably name lookup wasn't set up right; skip this test |
| 361 | return |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 362 | self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") |
Guido van Rossum | 9647b52 | 2002-12-26 17:04:45 +0000 | [diff] [blame] | 363 | try: |
| 364 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
| 365 | except socket.error: |
| 366 | # Probably a similar problem as above; skip this test |
| 367 | return |
Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame] | 368 | all_host_names = [hostname, hname] + aliases |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 369 | fqhn = socket.getfqdn(ip) |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 370 | if not fqhn in all_host_names: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 371 | 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] | 372 | |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 373 | def testRefCountGetNameInfo(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 374 | # Testing reference count for getnameinfo |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 375 | if hasattr(sys, "getrefcount"): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 376 | try: |
| 377 | # On some versions, this loses a reference |
| 378 | orig = sys.getrefcount(__name__) |
| 379 | socket.getnameinfo(__name__,0) |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 380 | except TypeError: |
Guido van Rossum | b053cd8 | 2006-08-24 03:53:23 +0000 | [diff] [blame] | 381 | if sys.getrefcount(__name__) != orig: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 382 | self.fail("socket.getnameinfo loses a reference") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 384 | def testInterpreterCrash(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 385 | # Making sure getnameinfo doesn't crash the interpreter |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 386 | try: |
| 387 | # On some versions, this crashes the interpreter. |
| 388 | socket.getnameinfo(('x', 0, 0, 0), 0) |
| 389 | except socket.error: |
| 390 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | c0a0e08 | 2002-09-16 01:30:03 +0000 | [diff] [blame] | 392 | def testNtoH(self): |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 393 | # This just checks that htons etc. are their own inverse, |
| 394 | # when looking at the lower 16 or 32 bits. |
| 395 | sizes = {socket.htonl: 32, socket.ntohl: 32, |
| 396 | socket.htons: 16, socket.ntohs: 16} |
| 397 | for func, size in sizes.items(): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 398 | mask = (1<<size) - 1 |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 399 | for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): |
| 400 | self.assertEqual(i & mask, func(func(i&mask)) & mask) |
Jeremy Hylton | cbd5b89 | 2002-07-31 15:57:39 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 402 | swapped = func(mask) |
| 403 | self.assertEqual(swapped & mask, mask) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 404 | self.assertRaises(OverflowError, func, 1<<34) |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 406 | def testNtoHErrors(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 407 | good_values = [ 1, 2, 3, 1, 2, 3 ] |
| 408 | bad_values = [ -1, -2, -3, -1, -2, -3 ] |
Guido van Rossum | 018919a | 2007-01-15 00:07:32 +0000 | [diff] [blame] | 409 | for k in good_values: |
| 410 | socket.ntohl(k) |
| 411 | socket.ntohs(k) |
| 412 | socket.htonl(k) |
| 413 | socket.htons(k) |
| 414 | for k in bad_values: |
| 415 | self.assertRaises(OverflowError, socket.ntohl, k) |
| 416 | self.assertRaises(OverflowError, socket.ntohs, k) |
| 417 | self.assertRaises(OverflowError, socket.htonl, k) |
| 418 | self.assertRaises(OverflowError, socket.htons, k) |
| 419 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 420 | def testGetServBy(self): |
| 421 | eq = self.assertEqual |
| 422 | # Find one service that exists, then check all the related interfaces. |
| 423 | # I've ordered this by protocols that have both a tcp and udp |
| 424 | # protocol, at least for modern Linuxes. |
Gregory P. Smith | b603d03 | 2010-10-17 00:17:24 +0000 | [diff] [blame] | 425 | if (sys.platform.startswith('linux') or |
| 426 | sys.platform.startswith('freebsd') or |
| 427 | sys.platform.startswith('netbsd') or |
| 428 | sys.platform == 'darwin'): |
Andrew MacIntyre | 18bf43c | 2004-07-12 12:10:30 +0000 | [diff] [blame] | 429 | # avoid the 'echo' service on this platform, as there is an |
| 430 | # assumption breaking non-standard port/protocol entry |
| 431 | services = ('daytime', 'qotd', 'domain') |
| 432 | else: |
| 433 | services = ('echo', 'daytime', 'domain') |
| 434 | for service in services: |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 435 | try: |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 436 | port = socket.getservbyname(service, 'tcp') |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 437 | break |
| 438 | except socket.error: |
| 439 | pass |
Skip Montanaro | 05eb401 | 2004-02-10 15:51:15 +0000 | [diff] [blame] | 440 | else: |
| 441 | raise socket.error |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 442 | # Try same call with optional protocol omitted |
| 443 | port2 = socket.getservbyname(service) |
| 444 | eq(port, port2) |
| 445 | # Try udp, but don't barf it it doesn't exist |
| 446 | try: |
| 447 | udpport = socket.getservbyname(service, 'udp') |
| 448 | except socket.error: |
| 449 | udpport = None |
| 450 | else: |
| 451 | eq(udpport, port) |
| 452 | # Now make sure the lookup by port returns the same service name |
| 453 | eq(socket.getservbyport(port2), service) |
| 454 | eq(socket.getservbyport(port, 'tcp'), service) |
| 455 | if udpport is not None: |
| 456 | eq(socket.getservbyport(udpport, 'udp'), service) |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 457 | # Make sure getservbyport does not accept out of range ports. |
| 458 | self.assertRaises(OverflowError, socket.getservbyport, -1) |
| 459 | self.assertRaises(OverflowError, socket.getservbyport, 65536) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 461 | def testDefaultTimeout(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 462 | # Testing default timeout |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 463 | # The default timeout should initially be None |
| 464 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 465 | s = socket.socket() |
| 466 | self.assertEqual(s.gettimeout(), None) |
| 467 | s.close() |
| 468 | |
| 469 | # Set the default timeout to 10, and see if it propagates |
| 470 | socket.setdefaulttimeout(10) |
| 471 | self.assertEqual(socket.getdefaulttimeout(), 10) |
| 472 | s = socket.socket() |
| 473 | self.assertEqual(s.gettimeout(), 10) |
| 474 | s.close() |
| 475 | |
| 476 | # Reset the default timeout to None, and see if it propagates |
| 477 | socket.setdefaulttimeout(None) |
| 478 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 479 | s = socket.socket() |
| 480 | self.assertEqual(s.gettimeout(), None) |
| 481 | s.close() |
| 482 | |
| 483 | # Check that setting it to an invalid value raises ValueError |
| 484 | self.assertRaises(ValueError, socket.setdefaulttimeout, -1) |
| 485 | |
| 486 | # Check that setting it to an invalid type raises TypeError |
| 487 | self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") |
| 488 | |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 489 | def testIPv4_inet_aton_fourbytes(self): |
| 490 | if not hasattr(socket, 'inet_aton'): |
| 491 | return # No inet_aton, nothing to check |
| 492 | # Test that issue1008086 and issue767150 are fixed. |
| 493 | # It must return 4 bytes. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 494 | self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) |
| 495 | self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) |
Benjamin Peterson | f91df04 | 2009-02-13 02:50:59 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 497 | def testIPv4toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 498 | if not hasattr(socket, 'inet_pton'): |
| 499 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 500 | from socket import inet_aton as f, inet_pton, AF_INET |
| 501 | g = lambda a: inet_pton(AF_INET, a) |
| 502 | |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 503 | assertInvalid = lambda func,a: self.assertRaises( |
| 504 | (socket.error, ValueError), func, a |
| 505 | ) |
| 506 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 507 | self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) |
| 508 | self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) |
| 509 | self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
| 510 | self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) |
| 511 | self.assertEqual(b'\xff\xff\xff\xff', f('255.255.255.255')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 512 | assertInvalid(f, '0.0.0.') |
| 513 | assertInvalid(f, '300.0.0.0') |
| 514 | assertInvalid(f, 'a.0.0.0') |
| 515 | assertInvalid(f, '1.2.3.4.5') |
| 516 | assertInvalid(f, '::1') |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 517 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 518 | self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) |
| 519 | self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) |
| 520 | self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
| 521 | self.assertEqual(b'\xff\xff\xff\xff', g('255.255.255.255')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 522 | assertInvalid(g, '0.0.0.') |
| 523 | assertInvalid(g, '300.0.0.0') |
| 524 | assertInvalid(g, 'a.0.0.0') |
| 525 | assertInvalid(g, '1.2.3.4.5') |
| 526 | assertInvalid(g, '::1') |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 527 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 528 | def testIPv6toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 529 | if not hasattr(socket, 'inet_pton'): |
| 530 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 531 | try: |
| 532 | from socket import inet_pton, AF_INET6, has_ipv6 |
| 533 | if not has_ipv6: |
| 534 | return |
| 535 | except ImportError: |
| 536 | return |
| 537 | f = lambda a: inet_pton(AF_INET6, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 538 | assertInvalid = lambda a: self.assertRaises( |
| 539 | (socket.error, ValueError), f, a |
| 540 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 541 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 542 | self.assertEqual(b'\x00' * 16, f('::')) |
| 543 | self.assertEqual(b'\x00' * 16, f('0::0')) |
| 544 | self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) |
| 545 | self.assertEqual( |
Guido van Rossum | 6718062 | 2007-07-10 07:29:12 +0000 | [diff] [blame] | 546 | 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] | 547 | f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') |
| 548 | ) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 549 | self.assertEqual( |
| 550 | b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02', |
| 551 | f('ad42:abc::127:0:254:2') |
| 552 | ) |
| 553 | self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::')) |
| 554 | assertInvalid('0x20::') |
| 555 | assertInvalid(':::') |
| 556 | assertInvalid('::0::') |
| 557 | assertInvalid('1::abc::') |
| 558 | assertInvalid('1::abc::def') |
| 559 | assertInvalid('1:2:3:4:5:6:') |
| 560 | assertInvalid('1:2:3:4:5:6') |
| 561 | assertInvalid('1:2:3:4:5:6:7:8:') |
| 562 | assertInvalid('1:2:3:4:5:6:7:8:0') |
| 563 | |
| 564 | self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40', |
| 565 | f('::254.42.23.64') |
| 566 | ) |
| 567 | self.assertEqual( |
| 568 | b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40', |
| 569 | f('42::a29b:254.42.23.64') |
| 570 | ) |
| 571 | self.assertEqual( |
| 572 | b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40', |
| 573 | f('42:a8b9:0:2:ffff:a29b:254.42.23.64') |
| 574 | ) |
| 575 | assertInvalid('255.254.253.252') |
| 576 | assertInvalid('1::260.2.3.0') |
| 577 | assertInvalid('1::0.be.e.0') |
| 578 | assertInvalid('1:2:3:4:5:6:7:1.2.3.4') |
| 579 | assertInvalid('::1.2.3.4:0') |
| 580 | assertInvalid('0.100.200.0:3:4:5:6:7:8') |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 582 | def testStringToIPv4(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 583 | if not hasattr(socket, 'inet_ntop'): |
| 584 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 585 | from socket import inet_ntoa as f, inet_ntop, AF_INET |
| 586 | g = lambda a: inet_ntop(AF_INET, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 587 | assertInvalid = lambda func,a: self.assertRaises( |
| 588 | (socket.error, ValueError), func, a |
| 589 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 590 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 591 | self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) |
| 592 | self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) |
| 593 | self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) |
| 594 | self.assertEqual('1.2.3.4', f(b'\x01\x02\x03\x04')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 595 | assertInvalid(f, b'\x00' * 3) |
| 596 | assertInvalid(f, b'\x00' * 5) |
| 597 | assertInvalid(f, b'\x00' * 16) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 598 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 599 | self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) |
| 600 | self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) |
| 601 | self.assertEqual('255.255.255.255', g(b'\xff\xff\xff\xff')) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 602 | assertInvalid(g, b'\x00' * 3) |
| 603 | assertInvalid(g, b'\x00' * 5) |
| 604 | assertInvalid(g, b'\x00' * 16) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 605 | |
| 606 | def testStringToIPv6(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 607 | if not hasattr(socket, 'inet_ntop'): |
| 608 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 609 | try: |
| 610 | from socket import inet_ntop, AF_INET6, has_ipv6 |
| 611 | if not has_ipv6: |
| 612 | return |
| 613 | except ImportError: |
| 614 | return |
| 615 | f = lambda a: inet_ntop(AF_INET6, a) |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 616 | assertInvalid = lambda a: self.assertRaises( |
| 617 | (socket.error, ValueError), f, a |
| 618 | ) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 619 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 620 | self.assertEqual('::', f(b'\x00' * 16)) |
| 621 | self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) |
| 622 | self.assertEqual( |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 623 | 'aef:b01:506:1001:ffff:9997:55:170', |
Guido van Rossum | 6718062 | 2007-07-10 07:29:12 +0000 | [diff] [blame] | 624 | 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] | 625 | ) |
| 626 | |
Antoine Pitrou | 7ded21e | 2011-12-19 16:21:21 +0100 | [diff] [blame] | 627 | assertInvalid(b'\x12' * 15) |
| 628 | assertInvalid(b'\x12' * 17) |
| 629 | assertInvalid(b'\x12' * 4) |
| 630 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 631 | # XXX The following don't test module-level functionality... |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 632 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 633 | def testSockName(self): |
| 634 | # Testing getsockname() |
Antoine Pitrou | ccc87b5 | 2011-05-10 23:37:11 +0200 | [diff] [blame] | 635 | port = support.find_unused_port() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 636 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 637 | self.addCleanup(sock.close) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 638 | sock.bind(("0.0.0.0", port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 639 | name = sock.getsockname() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 640 | # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate |
| 641 | # it reasonable to get the host's addr in addition to 0.0.0.0. |
| 642 | # 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] | 643 | try: |
| 644 | my_ip_addr = socket.gethostbyname(socket.gethostname()) |
| 645 | except socket.error: |
| 646 | # Probably name lookup wasn't set up right; skip this test |
| 647 | return |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 648 | 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] | 649 | self.assertEqual(name[1], port) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 650 | |
| 651 | def testGetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 652 | # Testing getsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 653 | # We know a socket should start without reuse==0 |
| 654 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 655 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 656 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 657 | self.assertFalse(reuse != 0, "initial mode is reuse") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 658 | |
| 659 | def testSetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 660 | # Testing setsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 661 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 662 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 663 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 664 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 665 | self.assertFalse(reuse == 0, "failed to set reuse mode") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 666 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 667 | def testSendAfterClose(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 668 | # testing send() after close() with timeout |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 669 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 670 | sock.settimeout(1) |
| 671 | sock.close() |
Guido van Rossum | 8a392d7 | 2007-11-21 22:09:45 +0000 | [diff] [blame] | 672 | self.assertRaises(socket.error, sock.send, b"spam") |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 673 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 674 | def testNewAttributes(self): |
| 675 | # testing .family, .type and .protocol |
| 676 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 677 | self.assertEqual(sock.family, socket.AF_INET) |
| 678 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 679 | self.assertEqual(sock.proto, 0) |
| 680 | sock.close() |
| 681 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 682 | def test_getsockaddrarg(self): |
| 683 | host = '0.0.0.0' |
Antoine Pitrou | ccc87b5 | 2011-05-10 23:37:11 +0200 | [diff] [blame] | 684 | port = support.find_unused_port() |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 685 | big_port = port + 65536 |
| 686 | neg_port = port - 65536 |
| 687 | sock = socket.socket() |
| 688 | try: |
| 689 | self.assertRaises(OverflowError, sock.bind, (host, big_port)) |
| 690 | self.assertRaises(OverflowError, sock.bind, (host, neg_port)) |
| 691 | sock.bind((host, port)) |
| 692 | finally: |
| 693 | sock.close() |
| 694 | |
Brian Curtin | 5ad0bd6 | 2010-11-05 15:47:45 +0000 | [diff] [blame] | 695 | @unittest.skipUnless(os.name == "nt", "Windows specific") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 696 | def test_sock_ioctl(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 697 | self.assertTrue(hasattr(socket.socket, 'ioctl')) |
| 698 | self.assertTrue(hasattr(socket, 'SIO_RCVALL')) |
| 699 | self.assertTrue(hasattr(socket, 'RCVALL_ON')) |
| 700 | self.assertTrue(hasattr(socket, 'RCVALL_OFF')) |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 701 | self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) |
| 702 | s = socket.socket() |
Brian Curtin | 5ad0bd6 | 2010-11-05 15:47:45 +0000 | [diff] [blame] | 703 | self.addCleanup(s.close) |
Kristján Valur Jónsson | 847ec75 | 2009-09-27 21:10:38 +0000 | [diff] [blame] | 704 | self.assertRaises(ValueError, s.ioctl, -1, None) |
| 705 | s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 706 | |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 707 | def testGetaddrinfo(self): |
| 708 | try: |
| 709 | socket.getaddrinfo('localhost', 80) |
| 710 | except socket.gaierror as err: |
| 711 | if err.errno == socket.EAI_SERVICE: |
| 712 | # see http://bugs.python.org/issue1282647 |
| 713 | self.skipTest("buggy libc version") |
| 714 | raise |
| 715 | # len of every sequence is supposed to be == 5 |
| 716 | for info in socket.getaddrinfo(HOST, None): |
| 717 | self.assertEqual(len(info), 5) |
| 718 | # host can be a domain name, a string representation of an |
| 719 | # IPv4/v6 address or None |
| 720 | socket.getaddrinfo('localhost', 80) |
| 721 | socket.getaddrinfo('127.0.0.1', 80) |
| 722 | socket.getaddrinfo(None, 80) |
| 723 | if SUPPORTS_IPV6: |
| 724 | socket.getaddrinfo('::1', 80) |
| 725 | # port can be a string service name such as "http", a numeric |
| 726 | # port number or None |
| 727 | socket.getaddrinfo(HOST, "http") |
| 728 | socket.getaddrinfo(HOST, 80) |
| 729 | socket.getaddrinfo(HOST, None) |
| 730 | # test family and socktype filters |
| 731 | infos = socket.getaddrinfo(HOST, None, socket.AF_INET) |
| 732 | for family, _, _, _, _ in infos: |
| 733 | self.assertEqual(family, socket.AF_INET) |
| 734 | infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) |
| 735 | for _, socktype, _, _, _ in infos: |
| 736 | self.assertEqual(socktype, socket.SOCK_STREAM) |
| 737 | # test proto and flags arguments |
Giampaolo Rodolà | 677d95c | 2010-08-16 05:08:11 +0000 | [diff] [blame] | 738 | socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 739 | socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) |
| 740 | # a server willing to support both IPv4 and IPv6 will |
| 741 | # usually do this |
| 742 | socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
| 743 | socket.AI_PASSIVE) |
Giampaolo Rodolà | ccfb91c | 2010-08-17 15:30:23 +0000 | [diff] [blame] | 744 | # test keyword arguments |
| 745 | a = socket.getaddrinfo(HOST, None) |
| 746 | b = socket.getaddrinfo(host=HOST, port=None) |
| 747 | self.assertEqual(a, b) |
| 748 | a = socket.getaddrinfo(HOST, None, socket.AF_INET) |
| 749 | b = socket.getaddrinfo(HOST, None, family=socket.AF_INET) |
| 750 | self.assertEqual(a, b) |
| 751 | a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) |
| 752 | b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM) |
| 753 | self.assertEqual(a, b) |
| 754 | a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) |
| 755 | b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP) |
| 756 | self.assertEqual(a, b) |
| 757 | a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) |
| 758 | b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE) |
| 759 | self.assertEqual(a, b) |
| 760 | a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
| 761 | socket.AI_PASSIVE) |
| 762 | b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC, |
| 763 | type=socket.SOCK_STREAM, proto=0, |
| 764 | flags=socket.AI_PASSIVE) |
| 765 | self.assertEqual(a, b) |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 766 | # Issue #6697. |
| 767 | self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') |
Giampaolo Rodolà | 419f704 | 2010-08-14 16:45:41 +0000 | [diff] [blame] | 768 | |
Martin v. Löwis | 112c0f3 | 2010-08-25 07:38:15 +0000 | [diff] [blame] | 769 | def test_getnameinfo(self): |
| 770 | # only IP addresses are allowed |
| 771 | self.assertRaises(socket.error, socket.getnameinfo, ('mail.python.org',0), 0) |
| 772 | |
Daniel Stutzbach | 460ff3d | 2010-09-09 21:17:58 +0000 | [diff] [blame] | 773 | @unittest.skipUnless(support.is_resource_enabled('network'), |
| 774 | 'network is not enabled') |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 775 | def test_idna(self): |
Antoine Pitrou | d9107aa | 2010-09-04 18:50:35 +0000 | [diff] [blame] | 776 | support.requires('network') |
Martin v. Löwis | fc0275a | 2010-08-22 19:33:47 +0000 | [diff] [blame] | 777 | # these should all be successful |
| 778 | socket.gethostbyname('испытание.python.org') |
| 779 | socket.gethostbyname_ex('испытание.python.org') |
Martin v. Löwis | 67e91ad | 2010-08-23 15:27:26 +0000 | [diff] [blame] | 780 | socket.getaddrinfo('испытание.python.org',0,socket.AF_UNSPEC,socket.SOCK_STREAM) |
| 781 | # this may not work if the forward lookup choses the IPv6 address, as that doesn't |
| 782 | # have a reverse entry yet |
| 783 | # socket.gethostbyaddr('испытание.python.org') |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 784 | |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 785 | def check_sendall_interrupted(self, with_timeout): |
| 786 | # socketpair() is not stricly required, but it makes things easier. |
| 787 | if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): |
| 788 | self.skipTest("signal.alarm and socket.socketpair required for this test") |
| 789 | # Our signal handlers clobber the C errno by calling a math function |
| 790 | # with an invalid domain value. |
| 791 | def ok_handler(*args): |
| 792 | self.assertRaises(ValueError, math.acosh, 0) |
| 793 | def raising_handler(*args): |
| 794 | self.assertRaises(ValueError, math.acosh, 0) |
| 795 | 1 // 0 |
| 796 | c, s = socket.socketpair() |
| 797 | old_alarm = signal.signal(signal.SIGALRM, raising_handler) |
| 798 | try: |
| 799 | if with_timeout: |
| 800 | # Just above the one second minimum for signal.alarm |
| 801 | c.settimeout(1.5) |
| 802 | with self.assertRaises(ZeroDivisionError): |
| 803 | signal.alarm(1) |
| 804 | c.sendall(b"x" * (1024**2)) |
| 805 | if with_timeout: |
| 806 | signal.signal(signal.SIGALRM, ok_handler) |
| 807 | signal.alarm(1) |
| 808 | self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2)) |
| 809 | finally: |
| 810 | signal.signal(signal.SIGALRM, old_alarm) |
| 811 | c.close() |
| 812 | s.close() |
| 813 | |
| 814 | def test_sendall_interrupted(self): |
| 815 | self.check_sendall_interrupted(False) |
| 816 | |
| 817 | def test_sendall_interrupted_with_timeout(self): |
| 818 | self.check_sendall_interrupted(True) |
| 819 | |
Antoine Pitrou | e033e06 | 2010-10-29 10:38:18 +0000 | [diff] [blame] | 820 | def test_dealloc_warn(self): |
| 821 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 822 | r = repr(sock) |
| 823 | with self.assertWarns(ResourceWarning) as cm: |
| 824 | sock = None |
| 825 | support.gc_collect() |
| 826 | self.assertIn(r, str(cm.warning.args[0])) |
| 827 | # An open socket file object gets dereferenced after the socket |
| 828 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 829 | f = sock.makefile('rb') |
| 830 | r = repr(sock) |
| 831 | sock = None |
| 832 | support.gc_collect() |
| 833 | with self.assertWarns(ResourceWarning): |
| 834 | f = None |
| 835 | support.gc_collect() |
| 836 | |
Victor Stinner | c3a51ec | 2011-01-04 11:00:45 +0000 | [diff] [blame] | 837 | def test_name_closed_socketio(self): |
| 838 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 839 | fp = sock.makefile("rb") |
| 840 | fp.close() |
| 841 | self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") |
| 842 | |
Antoine Pitrou | 1e7ee9d | 2012-09-14 17:28:10 +0200 | [diff] [blame] | 843 | def test_unusable_closed_socketio(self): |
| 844 | with socket.socket() as sock: |
| 845 | fp = sock.makefile("rb", buffering=0) |
| 846 | self.assertTrue(fp.readable()) |
| 847 | self.assertFalse(fp.writable()) |
| 848 | self.assertFalse(fp.seekable()) |
| 849 | fp.close() |
| 850 | self.assertRaises(ValueError, fp.readable) |
| 851 | self.assertRaises(ValueError, fp.writable) |
| 852 | self.assertRaises(ValueError, fp.seekable) |
| 853 | |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 854 | def test_listen_backlog(self): |
| 855 | for backlog in 0, -1: |
| 856 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 857 | srv.bind((HOST, 0)) |
| 858 | srv.listen(backlog) |
| 859 | srv.close() |
| 860 | |
| 861 | # Issue 15989 |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 862 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 863 | srv.bind((HOST, 0)) |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 864 | self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) |
Antoine Pitrou | 1be815a | 2011-05-10 19:16:29 +0200 | [diff] [blame] | 865 | srv.close() |
| 866 | |
Charles-François Natali | 366999a | 2012-01-02 15:47:29 +0100 | [diff] [blame] | 867 | @unittest.skipUnless(SUPPORTS_IPV6, 'IPv6 required for this test.') |
| 868 | def test_flowinfo(self): |
| 869 | self.assertRaises(OverflowError, socket.getnameinfo, |
| 870 | ('::1',0, 0xffffffff), 0) |
| 871 | with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: |
| 872 | self.assertRaises(OverflowError, s.bind, ('::1', 0, -10)) |
| 873 | |
Antoine Pitrou | 6d7df63 | 2010-09-27 17:52:25 +0000 | [diff] [blame] | 874 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 875 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 876 | class BasicTCPTest(SocketConnectedTest): |
| 877 | |
| 878 | def __init__(self, methodName='runTest'): |
| 879 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 880 | |
| 881 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 882 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 883 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 884 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 885 | |
| 886 | def _testRecv(self): |
| 887 | self.serv_conn.send(MSG) |
| 888 | |
| 889 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 890 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 891 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 892 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 893 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 894 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 895 | |
| 896 | def _testOverFlowRecv(self): |
| 897 | self.serv_conn.send(MSG) |
| 898 | |
| 899 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 900 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 901 | msg, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 902 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 903 | |
| 904 | def _testRecvFrom(self): |
| 905 | self.serv_conn.send(MSG) |
| 906 | |
| 907 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 908 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 909 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 910 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 911 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 912 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 913 | |
| 914 | def _testOverFlowRecvFrom(self): |
| 915 | self.serv_conn.send(MSG) |
| 916 | |
| 917 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 918 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 919 | msg = b'' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 920 | while 1: |
| 921 | read = self.cli_conn.recv(1024) |
| 922 | if not read: |
| 923 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 924 | msg += read |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 925 | self.assertEqual(msg, b'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 926 | |
| 927 | def _testSendAll(self): |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 928 | big_chunk = b'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 929 | self.serv_conn.sendall(big_chunk) |
| 930 | |
| 931 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 932 | # Testing fromfd() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 933 | fd = self.cli_conn.fileno() |
| 934 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 935 | self.addCleanup(sock.close) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 936 | self.assertIsInstance(sock, socket.socket) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 937 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 938 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 939 | |
| 940 | def _testFromFd(self): |
| 941 | self.serv_conn.send(MSG) |
| 942 | |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 943 | def testDup(self): |
| 944 | # Testing dup() |
| 945 | sock = self.cli_conn.dup() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 946 | self.addCleanup(sock.close) |
Guido van Rossum | 39eb8fa | 2007-11-16 01:24:05 +0000 | [diff] [blame] | 947 | msg = sock.recv(1024) |
| 948 | self.assertEqual(msg, MSG) |
| 949 | |
| 950 | def _testDup(self): |
| 951 | self.serv_conn.send(MSG) |
| 952 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 953 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 954 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 955 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 956 | self.assertEqual(msg, MSG) |
Mark Dickinson | 29500f6 | 2009-01-15 15:36:10 +0000 | [diff] [blame] | 957 | # wait for _testShutdown to finish: on OS X, when the server |
| 958 | # closes the connection the client also becomes disconnected, |
| 959 | # and the client's shutdown call will fail. (Issue #4397.) |
| 960 | self.done.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 961 | |
| 962 | def _testShutdown(self): |
| 963 | self.serv_conn.send(MSG) |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 964 | # Issue 15989 |
| 965 | self.assertRaises(OverflowError, self.serv_conn.shutdown, |
| 966 | _testcapi.INT_MAX + 1) |
| 967 | self.assertRaises(OverflowError, self.serv_conn.shutdown, |
| 968 | 2 + (_testcapi.UINT_MAX + 1)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 969 | self.serv_conn.shutdown(2) |
| 970 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 971 | def testDetach(self): |
| 972 | # Testing detach() |
| 973 | fileno = self.cli_conn.fileno() |
| 974 | f = self.cli_conn.detach() |
| 975 | self.assertEqual(f, fileno) |
| 976 | # cli_conn cannot be used anymore... |
Antoine Pitrou | 70deb3d | 2012-04-01 01:00:17 +0200 | [diff] [blame] | 977 | self.assertTrue(self.cli_conn._closed) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 978 | self.assertRaises(socket.error, self.cli_conn.recv, 1024) |
| 979 | self.cli_conn.close() |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 980 | # ...but we can create another socket using the (still open) |
| 981 | # file descriptor |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 982 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 983 | self.addCleanup(sock.close) |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 984 | msg = sock.recv(1024) |
| 985 | self.assertEqual(msg, MSG) |
| 986 | |
Antoine Pitrou | 6e451df | 2010-08-09 20:39:54 +0000 | [diff] [blame] | 987 | def _testDetach(self): |
Antoine Pitrou | e43f9d0 | 2010-08-08 23:24:50 +0000 | [diff] [blame] | 988 | self.serv_conn.send(MSG) |
| 989 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 990 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 991 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 992 | |
| 993 | def __init__(self, methodName='runTest'): |
| 994 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 995 | |
| 996 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 997 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 998 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 999 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1000 | |
| 1001 | def _testSendtoAndRecv(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1002 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1003 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 1004 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1005 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1006 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1007 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1008 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 1009 | def _testRecvFrom(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1010 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1011 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1012 | def testRecvFromNegative(self): |
| 1013 | # Negative lengths passed to recvfrom should give ValueError. |
| 1014 | self.assertRaises(ValueError, self.serv.recvfrom, -1) |
| 1015 | |
| 1016 | def _testRecvFromNegative(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1017 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1018 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1019 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1020 | class TCPCloserTest(ThreadedTCPSocketTest): |
| 1021 | |
| 1022 | def testClose(self): |
| 1023 | conn, addr = self.serv.accept() |
| 1024 | conn.close() |
| 1025 | |
| 1026 | sd = self.cli |
| 1027 | read, write, err = select.select([sd], [], [], 1.0) |
| 1028 | self.assertEqual(read, [sd]) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1029 | self.assertEqual(sd.recv(1), b'') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1030 | |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1031 | # Calling close() many times should be safe. |
| 1032 | conn.close() |
| 1033 | conn.close() |
| 1034 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1035 | def _testClose(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1036 | self.cli.connect((HOST, self.port)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1037 | time.sleep(1.0) |
| 1038 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1039 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 1040 | class BasicSocketPairTest(SocketPairTest): |
| 1041 | |
| 1042 | def __init__(self, methodName='runTest'): |
| 1043 | SocketPairTest.__init__(self, methodName=methodName) |
| 1044 | |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 1045 | def _check_defaults(self, sock): |
| 1046 | self.assertIsInstance(sock, socket.socket) |
| 1047 | if hasattr(socket, 'AF_UNIX'): |
| 1048 | self.assertEqual(sock.family, socket.AF_UNIX) |
| 1049 | else: |
| 1050 | self.assertEqual(sock.family, socket.AF_INET) |
| 1051 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 1052 | self.assertEqual(sock.proto, 0) |
| 1053 | |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 1054 | def _testDefaults(self): |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 1055 | self._check_defaults(self.cli) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 1056 | |
| 1057 | def testDefaults(self): |
Antoine Pitrou | 2e55fec | 2010-09-14 21:24:25 +0000 | [diff] [blame] | 1058 | self._check_defaults(self.serv) |
Antoine Pitrou | 9e0b864 | 2010-09-14 18:00:02 +0000 | [diff] [blame] | 1059 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 1060 | def testRecv(self): |
| 1061 | msg = self.serv.recv(1024) |
| 1062 | self.assertEqual(msg, MSG) |
| 1063 | |
| 1064 | def _testRecv(self): |
| 1065 | self.cli.send(MSG) |
| 1066 | |
| 1067 | def testSend(self): |
| 1068 | self.serv.send(MSG) |
| 1069 | |
| 1070 | def _testSend(self): |
| 1071 | msg = self.cli.recv(1024) |
| 1072 | self.assertEqual(msg, MSG) |
| 1073 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1074 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1075 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 1076 | |
| 1077 | def __init__(self, methodName='runTest'): |
| 1078 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 1079 | |
| 1080 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1081 | # Testing whether set blocking works |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 1082 | self.serv.setblocking(True) |
| 1083 | self.assertIsNone(self.serv.gettimeout()) |
| 1084 | self.serv.setblocking(False) |
| 1085 | self.assertEqual(self.serv.gettimeout(), 0.0) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1086 | start = time.time() |
| 1087 | try: |
| 1088 | self.serv.accept() |
| 1089 | except socket.error: |
| 1090 | pass |
| 1091 | end = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1092 | self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 1093 | # Issue 15989 |
| 1094 | if _testcapi.UINT_MAX < _testcapi.ULONG_MAX: |
| 1095 | self.serv.setblocking(_testcapi.UINT_MAX + 1) |
| 1096 | self.assertIsNone(self.serv.gettimeout()) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1097 | |
| 1098 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 1099 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1101 | if hasattr(socket, "SOCK_NONBLOCK"): |
| 1102 | def testInitNonBlocking(self): |
Antoine Pitrou | 1cae8b5 | 2010-10-23 17:05:13 +0000 | [diff] [blame] | 1103 | v = linux_version() |
| 1104 | if v < (2, 6, 28): |
| 1105 | self.skipTest("Linux kernel 2.6.28 or higher required, not %s" |
| 1106 | % ".".join(map(str, v))) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1107 | # reinit server socket |
| 1108 | self.serv.close() |
| 1109 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM | |
Antoine Pitrou | 1cae8b5 | 2010-10-23 17:05:13 +0000 | [diff] [blame] | 1110 | socket.SOCK_NONBLOCK) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 1111 | self.port = support.bind_port(self.serv) |
| 1112 | self.serv.listen(1) |
| 1113 | # actual testing |
| 1114 | start = time.time() |
| 1115 | try: |
| 1116 | self.serv.accept() |
| 1117 | except socket.error: |
| 1118 | pass |
| 1119 | end = time.time() |
| 1120 | self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.") |
| 1121 | |
| 1122 | def _testInitNonBlocking(self): |
| 1123 | pass |
| 1124 | |
Antoine Pitrou | 600232b | 2011-01-05 21:03:42 +0000 | [diff] [blame] | 1125 | def testInheritFlags(self): |
| 1126 | # Issue #7995: when calling accept() on a listening socket with a |
| 1127 | # timeout, the resulting socket should not be non-blocking. |
| 1128 | self.serv.settimeout(10) |
| 1129 | try: |
| 1130 | conn, addr = self.serv.accept() |
| 1131 | message = conn.recv(len(MSG)) |
| 1132 | finally: |
| 1133 | conn.close() |
| 1134 | self.serv.settimeout(None) |
| 1135 | |
| 1136 | def _testInheritFlags(self): |
| 1137 | time.sleep(0.1) |
| 1138 | self.cli.connect((HOST, self.port)) |
| 1139 | time.sleep(0.5) |
| 1140 | self.cli.send(MSG) |
| 1141 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1142 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1143 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1144 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 1145 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1146 | conn, addr = self.serv.accept() |
| 1147 | except socket.error: |
| 1148 | pass |
| 1149 | else: |
| 1150 | self.fail("Error trying to do non-blocking accept.") |
| 1151 | read, write, err = select.select([self.serv], [], []) |
| 1152 | if self.serv in read: |
| 1153 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1154 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1155 | else: |
| 1156 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 1157 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1158 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1159 | time.sleep(0.1) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1160 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1161 | |
| 1162 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1163 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1164 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1165 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1166 | |
| 1167 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 1168 | self.cli.settimeout(10) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1169 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1170 | |
| 1171 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1172 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1173 | conn, addr = self.serv.accept() |
| 1174 | conn.setblocking(0) |
| 1175 | try: |
| 1176 | msg = conn.recv(len(MSG)) |
| 1177 | except socket.error: |
| 1178 | pass |
| 1179 | else: |
| 1180 | self.fail("Error trying to do non-blocking recv.") |
| 1181 | read, write, err = select.select([conn], [], []) |
| 1182 | if conn in read: |
| 1183 | msg = conn.recv(len(MSG)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1184 | conn.close() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1185 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1186 | else: |
| 1187 | self.fail("Error during select call to non-blocking socket.") |
| 1188 | |
| 1189 | def _testRecv(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1190 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 1191 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1192 | self.cli.send(MSG) |
| 1193 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1194 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1195 | class FileObjectClassTestCase(SocketConnectedTest): |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1196 | """Unit tests for the object returned by socket.makefile() |
| 1197 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1198 | self.read_file is the io object returned by makefile() on |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1199 | the client connection. You can read from this file to |
| 1200 | get output from the server. |
| 1201 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1202 | self.write_file is the io object returned by makefile() on the |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1203 | server connection. You can write to this file to send output |
| 1204 | to the client. |
| 1205 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1207 | bufsize = -1 # Use default buffer size |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1208 | encoding = 'utf8' |
| 1209 | errors = 'strict' |
| 1210 | newline = None |
| 1211 | |
| 1212 | read_mode = 'rb' |
| 1213 | read_msg = MSG |
| 1214 | write_mode = 'wb' |
| 1215 | write_msg = MSG |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1216 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1217 | def __init__(self, methodName='runTest'): |
| 1218 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1219 | |
| 1220 | def setUp(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1221 | self.evt1, self.evt2, self.serv_finished, self.cli_finished = [ |
| 1222 | threading.Event() for i in range(4)] |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1223 | SocketConnectedTest.setUp(self) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1224 | self.read_file = self.cli_conn.makefile( |
| 1225 | self.read_mode, self.bufsize, |
| 1226 | encoding = self.encoding, |
| 1227 | errors = self.errors, |
| 1228 | newline = self.newline) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1229 | |
| 1230 | def tearDown(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1231 | self.serv_finished.set() |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1232 | self.read_file.close() |
| 1233 | self.assertTrue(self.read_file.closed) |
| 1234 | self.read_file = None |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1235 | SocketConnectedTest.tearDown(self) |
| 1236 | |
| 1237 | def clientSetUp(self): |
| 1238 | SocketConnectedTest.clientSetUp(self) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1239 | self.write_file = self.serv_conn.makefile( |
| 1240 | self.write_mode, self.bufsize, |
| 1241 | encoding = self.encoding, |
| 1242 | errors = self.errors, |
| 1243 | newline = self.newline) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1244 | |
| 1245 | def clientTearDown(self): |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1246 | self.cli_finished.set() |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1247 | self.write_file.close() |
| 1248 | self.assertTrue(self.write_file.closed) |
| 1249 | self.write_file = None |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1250 | SocketConnectedTest.clientTearDown(self) |
| 1251 | |
Antoine Pitrou | 5d5381e | 2011-02-25 23:14:08 +0000 | [diff] [blame] | 1252 | def testReadAfterTimeout(self): |
| 1253 | # Issue #7322: A file object must disallow further reads |
| 1254 | # after a timeout has occurred. |
| 1255 | self.cli_conn.settimeout(1) |
| 1256 | self.read_file.read(3) |
| 1257 | # First read raises a timeout |
| 1258 | self.assertRaises(socket.timeout, self.read_file.read, 1) |
| 1259 | # Second read is disallowed |
| 1260 | with self.assertRaises(IOError) as ctx: |
| 1261 | self.read_file.read(1) |
| 1262 | self.assertIn("cannot read from timed out object", str(ctx.exception)) |
| 1263 | |
| 1264 | def _testReadAfterTimeout(self): |
| 1265 | self.write_file.write(self.write_msg[0:3]) |
| 1266 | self.write_file.flush() |
| 1267 | self.serv_finished.wait() |
| 1268 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1269 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1270 | # Performing small file read test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1271 | first_seg = self.read_file.read(len(self.read_msg)-3) |
| 1272 | second_seg = self.read_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 1273 | msg = first_seg + second_seg |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1274 | self.assertEqual(msg, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1275 | |
| 1276 | def _testSmallRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1277 | self.write_file.write(self.write_msg) |
| 1278 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1279 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1280 | def testFullRead(self): |
| 1281 | # read until EOF |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1282 | msg = self.read_file.read() |
| 1283 | self.assertEqual(msg, self.read_msg) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1284 | |
| 1285 | def _testFullRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1286 | self.write_file.write(self.write_msg) |
| 1287 | self.write_file.close() |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1288 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1289 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1290 | # Performing unbuffered file read test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1291 | buf = type(self.read_msg)() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1292 | while 1: |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1293 | char = self.read_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1294 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1295 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1296 | buf += char |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1297 | self.assertEqual(buf, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1298 | |
| 1299 | def _testUnbufferedRead(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1300 | self.write_file.write(self.write_msg) |
| 1301 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1302 | |
| 1303 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1304 | # Performing file readline test |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1305 | line = self.read_file.readline() |
| 1306 | self.assertEqual(line, self.read_msg) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1307 | |
| 1308 | def _testReadline(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1309 | self.write_file.write(self.write_msg) |
| 1310 | self.write_file.flush() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1311 | |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1312 | def testCloseAfterMakefile(self): |
| 1313 | # The file returned by makefile should keep the socket open. |
| 1314 | self.cli_conn.close() |
| 1315 | # read until EOF |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1316 | msg = self.read_file.read() |
| 1317 | self.assertEqual(msg, self.read_msg) |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1318 | |
| 1319 | def _testCloseAfterMakefile(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1320 | self.write_file.write(self.write_msg) |
| 1321 | self.write_file.flush() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1322 | |
| 1323 | def testMakefileAfterMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1324 | self.read_file.close() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1325 | msg = self.cli_conn.recv(len(MSG)) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1326 | if isinstance(self.read_msg, str): |
| 1327 | msg = msg.decode() |
| 1328 | self.assertEqual(msg, self.read_msg) |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1329 | |
| 1330 | def _testMakefileAfterMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1331 | self.write_file.write(self.write_msg) |
| 1332 | self.write_file.flush() |
Jeremy Hylton | 5accbdb | 2007-08-03 20:40:09 +0000 | [diff] [blame] | 1333 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1334 | def testClosedAttr(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1335 | self.assertTrue(not self.read_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1336 | |
| 1337 | def _testClosedAttr(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1338 | self.assertTrue(not self.write_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1339 | |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 1340 | def testAttributes(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1341 | self.assertEqual(self.read_file.mode, self.read_mode) |
| 1342 | self.assertEqual(self.read_file.name, self.cli_conn.fileno()) |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 1343 | |
| 1344 | def _testAttributes(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1345 | self.assertEqual(self.write_file.mode, self.write_mode) |
| 1346 | self.assertEqual(self.write_file.name, self.serv_conn.fileno()) |
Amaury Forgeot d'Arc | 9d24ff0 | 2008-11-20 23:15:52 +0000 | [diff] [blame] | 1347 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1348 | def testRealClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1349 | self.read_file.close() |
| 1350 | self.assertRaises(ValueError, self.read_file.fileno) |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1351 | self.cli_conn.close() |
| 1352 | self.assertRaises(socket.error, self.cli_conn.getsockname) |
| 1353 | |
| 1354 | def _testRealClose(self): |
| 1355 | pass |
| 1356 | |
| 1357 | |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 1358 | class FileObjectInterruptedTestCase(unittest.TestCase): |
| 1359 | """Test that the file object correctly handles EINTR internally.""" |
| 1360 | |
| 1361 | class MockSocket(object): |
| 1362 | def __init__(self, recv_funcs=()): |
| 1363 | # A generator that returns callables that we'll call for each |
| 1364 | # call to recv(). |
| 1365 | self._recv_step = iter(recv_funcs) |
| 1366 | |
| 1367 | def recv_into(self, buffer): |
| 1368 | data = next(self._recv_step)() |
| 1369 | assert len(buffer) >= len(data) |
| 1370 | buffer[:len(data)] = data |
| 1371 | return len(data) |
| 1372 | |
| 1373 | def _decref_socketios(self): |
| 1374 | pass |
| 1375 | |
| 1376 | def _textiowrap_for_test(self, buffering=-1): |
| 1377 | raw = socket.SocketIO(self, "r") |
| 1378 | if buffering < 0: |
| 1379 | buffering = io.DEFAULT_BUFFER_SIZE |
| 1380 | if buffering == 0: |
| 1381 | return raw |
| 1382 | buffer = io.BufferedReader(raw, buffering) |
| 1383 | text = io.TextIOWrapper(buffer, None, None) |
| 1384 | text.mode = "rb" |
| 1385 | return text |
| 1386 | |
| 1387 | @staticmethod |
| 1388 | def _raise_eintr(): |
| 1389 | raise socket.error(errno.EINTR) |
| 1390 | |
| 1391 | def _textiowrap_mock_socket(self, mock, buffering=-1): |
| 1392 | raw = socket.SocketIO(mock, "r") |
| 1393 | if buffering < 0: |
| 1394 | buffering = io.DEFAULT_BUFFER_SIZE |
| 1395 | if buffering == 0: |
| 1396 | return raw |
| 1397 | buffer = io.BufferedReader(raw, buffering) |
| 1398 | text = io.TextIOWrapper(buffer, None, None) |
| 1399 | text.mode = "rb" |
| 1400 | return text |
| 1401 | |
| 1402 | def _test_readline(self, size=-1, buffering=-1): |
| 1403 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1404 | lambda : b"This is the first line\nAnd the sec", |
| 1405 | self._raise_eintr, |
| 1406 | lambda : b"ond line is here\n", |
| 1407 | lambda : b"", |
| 1408 | lambda : b"", # XXX(gps): io library does an extra EOF read |
| 1409 | ]) |
| 1410 | fo = mock_sock._textiowrap_for_test(buffering=buffering) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1411 | self.assertEqual(fo.readline(size), "This is the first line\n") |
| 1412 | 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] | 1413 | |
| 1414 | def _test_read(self, size=-1, buffering=-1): |
| 1415 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1416 | lambda : b"This is the first line\nAnd the sec", |
| 1417 | self._raise_eintr, |
| 1418 | lambda : b"ond line is here\n", |
| 1419 | lambda : b"", |
| 1420 | lambda : b"", # XXX(gps): io library does an extra EOF read |
| 1421 | ]) |
| 1422 | expecting = (b"This is the first line\n" |
| 1423 | b"And the second line is here\n") |
| 1424 | fo = mock_sock._textiowrap_for_test(buffering=buffering) |
| 1425 | if buffering == 0: |
| 1426 | data = b'' |
| 1427 | else: |
| 1428 | data = '' |
| 1429 | expecting = expecting.decode('utf8') |
| 1430 | while len(data) != len(expecting): |
| 1431 | part = fo.read(size) |
| 1432 | if not part: |
| 1433 | break |
| 1434 | data += part |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1435 | self.assertEqual(data, expecting) |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 1436 | |
| 1437 | def test_default(self): |
| 1438 | self._test_readline() |
| 1439 | self._test_readline(size=100) |
| 1440 | self._test_read() |
| 1441 | self._test_read(size=100) |
| 1442 | |
| 1443 | def test_with_1k_buffer(self): |
| 1444 | self._test_readline(buffering=1024) |
| 1445 | self._test_readline(size=100, buffering=1024) |
| 1446 | self._test_read(buffering=1024) |
| 1447 | self._test_read(size=100, buffering=1024) |
| 1448 | |
| 1449 | def _test_readline_no_buffer(self, size=-1): |
| 1450 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1451 | lambda : b"a", |
| 1452 | lambda : b"\n", |
| 1453 | lambda : b"B", |
| 1454 | self._raise_eintr, |
| 1455 | lambda : b"b", |
| 1456 | lambda : b"", |
| 1457 | ]) |
| 1458 | fo = mock_sock._textiowrap_for_test(buffering=0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1459 | self.assertEqual(fo.readline(size), b"a\n") |
| 1460 | self.assertEqual(fo.readline(size), b"Bb") |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 1461 | |
| 1462 | def test_no_buffer(self): |
| 1463 | self._test_readline_no_buffer() |
| 1464 | self._test_readline_no_buffer(size=4) |
| 1465 | self._test_read(buffering=0) |
| 1466 | self._test_read(size=100, buffering=0) |
| 1467 | |
| 1468 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1469 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1470 | |
| 1471 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 1472 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1473 | In this case (and in this case only), it should be possible to |
| 1474 | create a file object, read a line from it, create another file |
| 1475 | object, read another line from it, without loss of data in the |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 1476 | 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] | 1477 | when reading multiple requests from the same socket.""" |
| 1478 | |
| 1479 | bufsize = 0 # Use unbuffered mode |
| 1480 | |
| 1481 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1482 | # 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] | 1483 | line = self.read_file.readline() # first line |
| 1484 | self.assertEqual(line, b"A. " + self.write_msg) # first line |
| 1485 | self.read_file = self.cli_conn.makefile('rb', 0) |
| 1486 | line = self.read_file.readline() # second line |
| 1487 | self.assertEqual(line, b"B. " + self.write_msg) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1488 | |
| 1489 | def _testUnbufferedReadline(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1490 | self.write_file.write(b"A. " + self.write_msg) |
| 1491 | self.write_file.write(b"B. " + self.write_msg) |
| 1492 | self.write_file.flush() |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1493 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1494 | def testMakefileClose(self): |
| 1495 | # The file returned by makefile should keep the socket open... |
| 1496 | self.cli_conn.close() |
| 1497 | msg = self.cli_conn.recv(1024) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1498 | self.assertEqual(msg, self.read_msg) |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1499 | # ...until the file is itself closed |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1500 | self.read_file.close() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1501 | self.assertRaises(socket.error, self.cli_conn.recv, 1024) |
| 1502 | |
| 1503 | def _testMakefileClose(self): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1504 | self.write_file.write(self.write_msg) |
| 1505 | self.write_file.flush() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1506 | |
| 1507 | def testMakefileCloseSocketDestroy(self): |
| 1508 | refcount_before = sys.getrefcount(self.cli_conn) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1509 | self.read_file.close() |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1510 | refcount_after = sys.getrefcount(self.cli_conn) |
| 1511 | self.assertEqual(refcount_before - 1, refcount_after) |
| 1512 | |
| 1513 | def _testMakefileCloseSocketDestroy(self): |
| 1514 | pass |
| 1515 | |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1516 | # Non-blocking ops |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1517 | # NOTE: to set `read_file` as non-blocking, we must call |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1518 | # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp). |
| 1519 | |
| 1520 | def testSmallReadNonBlocking(self): |
| 1521 | self.cli_conn.setblocking(False) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1522 | self.assertEqual(self.read_file.readinto(bytearray(10)), None) |
| 1523 | self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1524 | self.evt1.set() |
| 1525 | self.evt2.wait(1.0) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1526 | first_seg = self.read_file.read(len(self.read_msg) - 3) |
Antoine Pitrou | 43bf045 | 2011-04-04 01:21:37 +0200 | [diff] [blame] | 1527 | if first_seg is None: |
| 1528 | # Data not arrived (can happen under Windows), wait a bit |
| 1529 | time.sleep(0.5) |
| 1530 | first_seg = self.read_file.read(len(self.read_msg) - 3) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1531 | buf = bytearray(10) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1532 | n = self.read_file.readinto(buf) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1533 | self.assertEqual(n, 3) |
| 1534 | msg = first_seg + buf[:n] |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1535 | self.assertEqual(msg, self.read_msg) |
| 1536 | self.assertEqual(self.read_file.readinto(bytearray(16)), None) |
| 1537 | self.assertEqual(self.read_file.read(1), None) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1538 | |
| 1539 | def _testSmallReadNonBlocking(self): |
| 1540 | self.evt1.wait(1.0) |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1541 | self.write_file.write(self.write_msg) |
| 1542 | self.write_file.flush() |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1543 | self.evt2.set() |
| 1544 | # Avoid cloding the socket before the server test has finished, |
| 1545 | # otherwise system recv() will return 0 instead of EWOULDBLOCK. |
| 1546 | self.serv_finished.wait(5.0) |
| 1547 | |
| 1548 | def testWriteNonBlocking(self): |
| 1549 | self.cli_finished.wait(5.0) |
| 1550 | # The client thread can't skip directly - the SkipTest exception |
| 1551 | # would appear as a failure. |
| 1552 | if self.serv_skipped: |
| 1553 | self.skipTest(self.serv_skipped) |
| 1554 | |
| 1555 | def _testWriteNonBlocking(self): |
| 1556 | self.serv_skipped = None |
| 1557 | self.serv_conn.setblocking(False) |
| 1558 | # Try to saturate the socket buffer pipe with repeated large writes. |
| 1559 | BIG = b"x" * (1024 ** 2) |
| 1560 | LIMIT = 10 |
| 1561 | # The first write() succeeds since a chunk of data can be buffered |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1562 | n = self.write_file.write(BIG) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1563 | self.assertGreater(n, 0) |
| 1564 | for i in range(LIMIT): |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1565 | n = self.write_file.write(BIG) |
Antoine Pitrou | 98b4670 | 2010-09-18 22:59:00 +0000 | [diff] [blame] | 1566 | if n is None: |
| 1567 | # Succeeded |
| 1568 | break |
| 1569 | self.assertGreater(n, 0) |
| 1570 | else: |
| 1571 | # Let us know that this test didn't manage to establish |
| 1572 | # the expected conditions. This is not a failure in itself but, |
| 1573 | # if it happens repeatedly, the test should be fixed. |
| 1574 | self.serv_skipped = "failed to saturate the socket buffer" |
| 1575 | |
Gregory P. Smith | de3369f | 2009-01-12 04:50:11 +0000 | [diff] [blame] | 1576 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1577 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1578 | |
| 1579 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 1580 | |
| 1581 | |
| 1582 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1583 | |
| 1584 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1585 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 1587 | class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): |
| 1588 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 1589 | |
| 1590 | read_mode = 'r' |
| 1591 | read_msg = MSG.decode('utf8') |
| 1592 | write_mode = 'wb' |
| 1593 | write_msg = MSG |
| 1594 | newline = '' |
| 1595 | |
| 1596 | |
| 1597 | class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): |
| 1598 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 1599 | |
| 1600 | read_mode = 'rb' |
| 1601 | read_msg = MSG |
| 1602 | write_mode = 'w' |
| 1603 | write_msg = MSG.decode('utf8') |
| 1604 | newline = '' |
| 1605 | |
| 1606 | |
| 1607 | class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): |
| 1608 | """Tests for socket.makefile() in text mode (rather than binary)""" |
| 1609 | |
| 1610 | read_mode = 'r' |
| 1611 | read_msg = MSG.decode('utf8') |
| 1612 | write_mode = 'w' |
| 1613 | write_msg = MSG.decode('utf8') |
| 1614 | newline = '' |
| 1615 | |
| 1616 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1617 | class NetworkConnectionTest(object): |
| 1618 | """Prove network connection.""" |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1619 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1620 | def clientSetUp(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1621 | # We're inherited below by BasicTCPTest2, which also inherits |
| 1622 | # BasicTCPTest, which defines self.port referenced below. |
| 1623 | self.cli = socket.create_connection((HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1624 | self.serv_conn = self.cli |
| 1625 | |
| 1626 | class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
| 1627 | """Tests that NetworkConnection does not break existing TCP functionality. |
| 1628 | """ |
| 1629 | |
| 1630 | class NetworkConnectionNoServer(unittest.TestCase): |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 1632 | class MockSocket(socket.socket): |
| 1633 | def connect(self, *args): |
| 1634 | raise socket.timeout('timed out') |
| 1635 | |
| 1636 | @contextlib.contextmanager |
| 1637 | def mocked_socket_module(self): |
| 1638 | """Return a socket which times out on connect""" |
| 1639 | old_socket = socket.socket |
| 1640 | socket.socket = self.MockSocket |
| 1641 | try: |
| 1642 | yield |
| 1643 | finally: |
| 1644 | socket.socket = old_socket |
| 1645 | |
| 1646 | def test_connect(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1647 | port = support.find_unused_port() |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 1648 | cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1649 | self.addCleanup(cli.close) |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 1650 | with self.assertRaises(socket.error) as cm: |
| 1651 | cli.connect((HOST, port)) |
| 1652 | self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) |
| 1653 | |
| 1654 | def test_create_connection(self): |
| 1655 | # Issue #9792: errors raised by create_connection() should have |
| 1656 | # a proper errno attribute. |
| 1657 | port = support.find_unused_port() |
| 1658 | with self.assertRaises(socket.error) as cm: |
| 1659 | socket.create_connection((HOST, port)) |
Trent Nelson | 45bb613 | 2012-10-17 06:15:15 -0400 | [diff] [blame] | 1660 | |
| 1661 | # Issue #16257: create_connection() calls getaddrinfo() against |
| 1662 | # 'localhost'. This may result in an IPV6 addr being returned |
| 1663 | # as well as an IPV4 one: |
| 1664 | # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) |
| 1665 | # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), |
| 1666 | # (26, 2, 0, '', ('::1', 41230, 0, 0))] |
| 1667 | # |
| 1668 | # create_connection() enumerates through all the addresses returned |
| 1669 | # and if it doesn't successfully bind to any of them, it propagates |
| 1670 | # the last exception it encountered. |
| 1671 | # |
| 1672 | # On Solaris, ENETUNREACH is returned in this circumstance instead |
| 1673 | # of ECONNREFUSED. So, if that errno exists, add it to our list of |
| 1674 | # expected errnos. |
| 1675 | expected_errnos = [ errno.ECONNREFUSED, ] |
| 1676 | if hasattr(errno, 'ENETUNREACH'): |
| 1677 | expected_errnos.append(errno.ENETUNREACH) |
| 1678 | |
| 1679 | self.assertIn(cm.exception.errno, expected_errnos) |
Antoine Pitrou | 4b92b5f | 2010-09-07 21:05:49 +0000 | [diff] [blame] | 1680 | |
| 1681 | def test_create_connection_timeout(self): |
| 1682 | # Issue #9792: create_connection() should not recast timeout errors |
| 1683 | # as generic socket errors. |
| 1684 | with self.mocked_socket_module(): |
| 1685 | with self.assertRaises(socket.timeout): |
| 1686 | socket.create_connection((HOST, 1234)) |
| 1687 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1688 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1689 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1690 | class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
| 1691 | |
| 1692 | def __init__(self, methodName='runTest'): |
| 1693 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1694 | ThreadableTest.__init__(self) |
| 1695 | |
| 1696 | def clientSetUp(self): |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 1697 | self.source_port = support.find_unused_port() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1698 | |
| 1699 | def clientTearDown(self): |
| 1700 | self.cli.close() |
| 1701 | self.cli = None |
| 1702 | ThreadableTest.clientTearDown(self) |
| 1703 | |
| 1704 | def _justAccept(self): |
| 1705 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1706 | conn.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1707 | |
| 1708 | testFamily = _justAccept |
| 1709 | def _testFamily(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1710 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1711 | self.addCleanup(self.cli.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1712 | self.assertEqual(self.cli.family, 2) |
| 1713 | |
Gregory P. Smith | a81d881 | 2010-01-03 15:09:32 +0000 | [diff] [blame] | 1714 | testSourceAddress = _justAccept |
| 1715 | def _testSourceAddress(self): |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 1716 | self.cli = socket.create_connection((HOST, self.port), timeout=30, |
| 1717 | source_address=('', self.source_port)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1718 | self.addCleanup(self.cli.close) |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 1719 | self.assertEqual(self.cli.getsockname()[1], self.source_port) |
Gregory P. Smith | a81d881 | 2010-01-03 15:09:32 +0000 | [diff] [blame] | 1720 | # The port number being used is sufficient to show that the bind() |
| 1721 | # call happened. |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1723 | testTimeoutDefault = _justAccept |
| 1724 | def _testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1725 | # passing no explicit timeout uses socket's global default |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1726 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1727 | socket.setdefaulttimeout(42) |
| 1728 | try: |
| 1729 | self.cli = socket.create_connection((HOST, self.port)) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1730 | self.addCleanup(self.cli.close) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1731 | finally: |
| 1732 | socket.setdefaulttimeout(None) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1733 | self.assertEqual(self.cli.gettimeout(), 42) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1734 | |
| 1735 | testTimeoutNone = _justAccept |
| 1736 | def _testTimeoutNone(self): |
| 1737 | # None timeout means the same as sock.settimeout(None) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1738 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1739 | socket.setdefaulttimeout(30) |
| 1740 | try: |
| 1741 | self.cli = socket.create_connection((HOST, self.port), timeout=None) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1742 | self.addCleanup(self.cli.close) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1743 | finally: |
| 1744 | socket.setdefaulttimeout(None) |
| 1745 | self.assertEqual(self.cli.gettimeout(), None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1746 | |
| 1747 | testTimeoutValueNamed = _justAccept |
| 1748 | def _testTimeoutValueNamed(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1749 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1750 | self.assertEqual(self.cli.gettimeout(), 30) |
| 1751 | |
| 1752 | testTimeoutValueNonamed = _justAccept |
| 1753 | def _testTimeoutValueNonamed(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1754 | self.cli = socket.create_connection((HOST, self.port), 30) |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1755 | self.addCleanup(self.cli.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1756 | self.assertEqual(self.cli.gettimeout(), 30) |
| 1757 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1758 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1759 | class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
| 1760 | |
| 1761 | def __init__(self, methodName='runTest'): |
| 1762 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1763 | ThreadableTest.__init__(self) |
| 1764 | |
| 1765 | def clientSetUp(self): |
| 1766 | pass |
| 1767 | |
| 1768 | def clientTearDown(self): |
| 1769 | self.cli.close() |
| 1770 | self.cli = None |
| 1771 | ThreadableTest.clientTearDown(self) |
| 1772 | |
| 1773 | def testInsideTimeout(self): |
| 1774 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 1775 | self.addCleanup(conn.close) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1776 | time.sleep(3) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1777 | conn.send(b"done!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1778 | testOutsideTimeout = testInsideTimeout |
| 1779 | |
| 1780 | def _testInsideTimeout(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1781 | self.cli = sock = socket.create_connection((HOST, self.port)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1782 | data = sock.recv(5) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1783 | self.assertEqual(data, b"done!") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1784 | |
| 1785 | def _testOutsideTimeout(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1786 | self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1787 | self.assertRaises(socket.timeout, lambda: sock.recv(5)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1788 | |
| 1789 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1790 | class TCPTimeoutTest(SocketTCPTest): |
| 1791 | |
| 1792 | def testTCPTimeout(self): |
| 1793 | def raise_timeout(*args, **kwargs): |
| 1794 | self.serv.settimeout(1.0) |
| 1795 | self.serv.accept() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1796 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1797 | "Error generating a timeout exception (TCP)") |
| 1798 | |
| 1799 | def testTimeoutZero(self): |
| 1800 | ok = False |
| 1801 | try: |
| 1802 | self.serv.settimeout(0.0) |
| 1803 | foo = self.serv.accept() |
| 1804 | except socket.timeout: |
| 1805 | self.fail("caught timeout instead of error (TCP)") |
| 1806 | except socket.error: |
| 1807 | ok = True |
| 1808 | except: |
| 1809 | self.fail("caught unexpected exception (TCP)") |
| 1810 | if not ok: |
| 1811 | self.fail("accept() returned success when we did not expect it") |
| 1812 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1813 | def testInterruptedTimeout(self): |
| 1814 | # XXX I don't know how to do this test on MSWindows or any other |
| 1815 | # plaform that doesn't support signal.alarm() or os.kill(), though |
| 1816 | # the bug should have existed on all platforms. |
| 1817 | if not hasattr(signal, "alarm"): |
| 1818 | return # can only test on *nix |
| 1819 | self.serv.settimeout(5.0) # must be longer than alarm |
| 1820 | class Alarm(Exception): |
| 1821 | pass |
| 1822 | def alarm_handler(signal, frame): |
| 1823 | raise Alarm |
| 1824 | old_alarm = signal.signal(signal.SIGALRM, alarm_handler) |
| 1825 | try: |
| 1826 | signal.alarm(2) # POSIX allows alarm to be up to 1 second early |
| 1827 | try: |
| 1828 | foo = self.serv.accept() |
| 1829 | except socket.timeout: |
| 1830 | self.fail("caught timeout instead of Alarm") |
| 1831 | except Alarm: |
| 1832 | pass |
| 1833 | except: |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1834 | self.fail("caught other exception instead of Alarm:" |
| 1835 | " %s(%s):\n%s" % |
| 1836 | (sys.exc_info()[:2] + (traceback.format_exc(),))) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1837 | else: |
| 1838 | self.fail("nothing caught") |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 1839 | finally: |
| 1840 | signal.alarm(0) # shut off alarm |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1841 | except Alarm: |
| 1842 | self.fail("got Alarm in wrong place") |
| 1843 | finally: |
| 1844 | # no alarm can be pending. Safe to restore old handler. |
| 1845 | signal.signal(signal.SIGALRM, old_alarm) |
| 1846 | |
Ezio Melotti | 7f78ddc | 2011-11-18 18:33:14 +0200 | [diff] [blame] | 1847 | class UDPTimeoutTest(SocketUDPTest): |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1848 | |
| 1849 | def testUDPTimeout(self): |
| 1850 | def raise_timeout(*args, **kwargs): |
| 1851 | self.serv.settimeout(1.0) |
| 1852 | self.serv.recv(1024) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1853 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1854 | "Error generating a timeout exception (UDP)") |
| 1855 | |
| 1856 | def testTimeoutZero(self): |
| 1857 | ok = False |
| 1858 | try: |
| 1859 | self.serv.settimeout(0.0) |
| 1860 | foo = self.serv.recv(1024) |
| 1861 | except socket.timeout: |
| 1862 | self.fail("caught timeout instead of error (UDP)") |
| 1863 | except socket.error: |
| 1864 | ok = True |
| 1865 | except: |
| 1866 | self.fail("caught unexpected exception (UDP)") |
| 1867 | if not ok: |
| 1868 | self.fail("recv() returned success when we did not expect it") |
| 1869 | |
| 1870 | class TestExceptions(unittest.TestCase): |
| 1871 | |
| 1872 | def testExceptionTree(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1873 | self.assertTrue(issubclass(socket.error, Exception)) |
| 1874 | self.assertTrue(issubclass(socket.herror, socket.error)) |
| 1875 | self.assertTrue(issubclass(socket.gaierror, socket.error)) |
| 1876 | self.assertTrue(issubclass(socket.timeout, socket.error)) |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1877 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1878 | class TestLinuxAbstractNamespace(unittest.TestCase): |
| 1879 | |
| 1880 | UNIX_PATH_MAX = 108 |
| 1881 | |
| 1882 | def testLinuxAbstractNamespace(self): |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1883 | address = b"\x00python-test-hello\x00\xff" |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 1884 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1: |
| 1885 | s1.bind(address) |
| 1886 | s1.listen(1) |
| 1887 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2: |
| 1888 | s2.connect(s1.getsockname()) |
| 1889 | with s1.accept()[0] as s3: |
| 1890 | self.assertEqual(s1.getsockname(), address) |
| 1891 | self.assertEqual(s2.getpeername(), address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1892 | |
| 1893 | def testMaxName(self): |
Guido van Rossum | 32c4ac0 | 2007-08-15 03:56:40 +0000 | [diff] [blame] | 1894 | address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 1895 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 1896 | s.bind(address) |
| 1897 | self.assertEqual(s.getsockname(), address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1898 | |
| 1899 | def testNameOverflow(self): |
| 1900 | address = "\x00" + "h" * self.UNIX_PATH_MAX |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 1901 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: |
| 1902 | self.assertRaises(socket.error, s.bind, address) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1903 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1904 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 1905 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1906 | class BufferIOTest(SocketConnectedTest): |
| 1907 | """ |
| 1908 | Test the buffer versions of socket.recv() and socket.send(). |
| 1909 | """ |
| 1910 | def __init__(self, methodName='runTest'): |
| 1911 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1912 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 1913 | def testRecvIntoArray(self): |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 1914 | buf = bytearray(1024) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1915 | nbytes = self.cli_conn.recv_into(buf) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1916 | self.assertEqual(nbytes, len(MSG)) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1917 | msg = buf[:len(MSG)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1918 | self.assertEqual(msg, MSG) |
| 1919 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 1920 | def _testRecvIntoArray(self): |
Guido van Rossum | b5b2270 | 2007-05-18 18:55:53 +0000 | [diff] [blame] | 1921 | buf = bytes(MSG) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1922 | self.serv_conn.send(buf) |
| 1923 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 1924 | def testRecvIntoBytearray(self): |
| 1925 | buf = bytearray(1024) |
| 1926 | nbytes = self.cli_conn.recv_into(buf) |
| 1927 | self.assertEqual(nbytes, len(MSG)) |
| 1928 | msg = buf[:len(MSG)] |
| 1929 | self.assertEqual(msg, MSG) |
| 1930 | |
| 1931 | _testRecvIntoBytearray = _testRecvIntoArray |
| 1932 | |
| 1933 | def testRecvIntoMemoryview(self): |
| 1934 | buf = bytearray(1024) |
| 1935 | nbytes = self.cli_conn.recv_into(memoryview(buf)) |
| 1936 | self.assertEqual(nbytes, len(MSG)) |
| 1937 | msg = buf[:len(MSG)] |
| 1938 | self.assertEqual(msg, MSG) |
| 1939 | |
| 1940 | _testRecvIntoMemoryview = _testRecvIntoArray |
| 1941 | |
| 1942 | def testRecvFromIntoArray(self): |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 1943 | buf = bytearray(1024) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1944 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1945 | self.assertEqual(nbytes, len(MSG)) |
Guido van Rossum | 7d0a826 | 2007-05-21 23:13:11 +0000 | [diff] [blame] | 1946 | msg = buf[:len(MSG)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1947 | self.assertEqual(msg, MSG) |
| 1948 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 1949 | def _testRecvFromIntoArray(self): |
Guido van Rossum | b5b2270 | 2007-05-18 18:55:53 +0000 | [diff] [blame] | 1950 | buf = bytes(MSG) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1951 | self.serv_conn.send(buf) |
| 1952 | |
Antoine Pitrou | 2548078 | 2010-03-17 22:50:28 +0000 | [diff] [blame] | 1953 | def testRecvFromIntoBytearray(self): |
| 1954 | buf = bytearray(1024) |
| 1955 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
| 1956 | self.assertEqual(nbytes, len(MSG)) |
| 1957 | msg = buf[:len(MSG)] |
| 1958 | self.assertEqual(msg, MSG) |
| 1959 | |
| 1960 | _testRecvFromIntoBytearray = _testRecvFromIntoArray |
| 1961 | |
| 1962 | def testRecvFromIntoMemoryview(self): |
| 1963 | buf = bytearray(1024) |
| 1964 | nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) |
| 1965 | self.assertEqual(nbytes, len(MSG)) |
| 1966 | msg = buf[:len(MSG)] |
| 1967 | self.assertEqual(msg, MSG) |
| 1968 | |
| 1969 | _testRecvFromIntoMemoryview = _testRecvFromIntoArray |
| 1970 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1971 | |
| 1972 | TIPC_STYPE = 2000 |
| 1973 | TIPC_LOWER = 200 |
| 1974 | TIPC_UPPER = 210 |
| 1975 | |
| 1976 | def isTipcAvailable(): |
| 1977 | """Check if the TIPC module is loaded |
| 1978 | |
| 1979 | The TIPC module is not loaded automatically on Ubuntu and probably |
| 1980 | other Linux distros. |
| 1981 | """ |
| 1982 | if not hasattr(socket, "AF_TIPC"): |
| 1983 | return False |
| 1984 | if not os.path.isfile("/proc/modules"): |
| 1985 | return False |
| 1986 | with open("/proc/modules") as f: |
| 1987 | for line in f: |
| 1988 | if line.startswith("tipc "): |
| 1989 | return True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1990 | if support.verbose: |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1991 | print("TIPC module is not loaded, please 'sudo modprobe tipc'") |
| 1992 | return False |
| 1993 | |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 1994 | class TIPCTest(unittest.TestCase): |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 1995 | def testRDM(self): |
| 1996 | srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1997 | cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 1998 | self.addCleanup(srv.close) |
| 1999 | self.addCleanup(cli.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2000 | |
| 2001 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 2002 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 2003 | TIPC_LOWER, TIPC_UPPER) |
| 2004 | srv.bind(srvaddr) |
| 2005 | |
| 2006 | sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 2007 | TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) |
| 2008 | cli.sendto(MSG, sendaddr) |
| 2009 | |
| 2010 | msg, recvaddr = srv.recvfrom(1024) |
| 2011 | |
| 2012 | self.assertEqual(cli.getsockname(), recvaddr) |
| 2013 | self.assertEqual(msg, MSG) |
| 2014 | |
| 2015 | |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 2016 | class TIPCThreadableTest(unittest.TestCase, ThreadableTest): |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2017 | def __init__(self, methodName = 'runTest'): |
| 2018 | unittest.TestCase.__init__(self, methodName = methodName) |
| 2019 | ThreadableTest.__init__(self) |
| 2020 | |
| 2021 | def setUp(self): |
| 2022 | self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 2023 | self.addCleanup(self.srv.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2024 | self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 2025 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 2026 | TIPC_LOWER, TIPC_UPPER) |
| 2027 | self.srv.bind(srvaddr) |
| 2028 | self.srv.listen(5) |
| 2029 | self.serverExplicitReady() |
| 2030 | self.conn, self.connaddr = self.srv.accept() |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 2031 | self.addCleanup(self.conn.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2032 | |
| 2033 | def clientSetUp(self): |
| 2034 | # The is a hittable race between serverExplicitReady() and the |
| 2035 | # accept() call; sleep a little while to avoid it, otherwise |
| 2036 | # we could get an exception |
| 2037 | time.sleep(0.1) |
| 2038 | self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
Antoine Pitrou | 86aed0b | 2011-10-02 23:33:19 +0200 | [diff] [blame] | 2039 | self.addCleanup(self.cli.close) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2040 | addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 2041 | TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0) |
| 2042 | self.cli.connect(addr) |
| 2043 | self.cliaddr = self.cli.getsockname() |
| 2044 | |
| 2045 | def testStream(self): |
| 2046 | msg = self.conn.recv(1024) |
| 2047 | self.assertEqual(msg, MSG) |
| 2048 | self.assertEqual(self.cliaddr, self.connaddr) |
| 2049 | |
| 2050 | def _testStream(self): |
| 2051 | self.cli.send(MSG) |
| 2052 | self.cli.close() |
| 2053 | |
| 2054 | |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 2055 | @unittest.skipUnless(thread, 'Threading required for this test.') |
| 2056 | class ContextManagersTest(ThreadedTCPSocketTest): |
| 2057 | |
| 2058 | def _testSocketClass(self): |
| 2059 | # base test |
| 2060 | with socket.socket() as sock: |
| 2061 | self.assertFalse(sock._closed) |
| 2062 | self.assertTrue(sock._closed) |
| 2063 | # close inside with block |
| 2064 | with socket.socket() as sock: |
| 2065 | sock.close() |
| 2066 | self.assertTrue(sock._closed) |
| 2067 | # exception inside with block |
| 2068 | with socket.socket() as sock: |
| 2069 | self.assertRaises(socket.error, sock.sendall, b'foo') |
| 2070 | self.assertTrue(sock._closed) |
| 2071 | |
| 2072 | def testCreateConnectionBase(self): |
| 2073 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 2074 | self.addCleanup(conn.close) |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 2075 | data = conn.recv(1024) |
| 2076 | conn.sendall(data) |
| 2077 | |
| 2078 | def _testCreateConnectionBase(self): |
| 2079 | address = self.serv.getsockname() |
| 2080 | with socket.create_connection(address) as sock: |
| 2081 | self.assertFalse(sock._closed) |
| 2082 | sock.sendall(b'foo') |
| 2083 | self.assertEqual(sock.recv(1024), b'foo') |
| 2084 | self.assertTrue(sock._closed) |
| 2085 | |
| 2086 | def testCreateConnectionClose(self): |
| 2087 | conn, addr = self.serv.accept() |
Brian Curtin | 21ebbb7 | 2010-11-02 02:07:09 +0000 | [diff] [blame] | 2088 | self.addCleanup(conn.close) |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 2089 | data = conn.recv(1024) |
| 2090 | conn.sendall(data) |
| 2091 | |
| 2092 | def _testCreateConnectionClose(self): |
| 2093 | address = self.serv.getsockname() |
| 2094 | with socket.create_connection(address) as sock: |
| 2095 | sock.close() |
| 2096 | self.assertTrue(sock._closed) |
| 2097 | self.assertRaises(socket.error, sock.sendall, b'foo') |
| 2098 | |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2100 | @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"), |
| 2101 | "SOCK_CLOEXEC not defined") |
| 2102 | @unittest.skipUnless(fcntl, "module fcntl not available") |
| 2103 | class CloexecConstantTest(unittest.TestCase): |
| 2104 | def test_SOCK_CLOEXEC(self): |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 2105 | v = linux_version() |
| 2106 | if v < (2, 6, 28): |
| 2107 | self.skipTest("Linux kernel 2.6.28 or higher required, not %s" |
| 2108 | % ".".join(map(str, v))) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2109 | with socket.socket(socket.AF_INET, |
| 2110 | socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s: |
| 2111 | self.assertTrue(s.type & socket.SOCK_CLOEXEC) |
| 2112 | self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2113 | |
| 2114 | |
| 2115 | @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), |
| 2116 | "SOCK_NONBLOCK not defined") |
| 2117 | class NonblockConstantTest(unittest.TestCase): |
| 2118 | def checkNonblock(self, s, nonblock=True, timeout=0.0): |
| 2119 | if nonblock: |
| 2120 | self.assertTrue(s.type & socket.SOCK_NONBLOCK) |
| 2121 | self.assertEqual(s.gettimeout(), timeout) |
| 2122 | else: |
| 2123 | self.assertFalse(s.type & socket.SOCK_NONBLOCK) |
| 2124 | self.assertEqual(s.gettimeout(), None) |
| 2125 | |
| 2126 | def test_SOCK_NONBLOCK(self): |
Antoine Pitrou | acd0fda | 2010-10-23 17:01:44 +0000 | [diff] [blame] | 2127 | v = linux_version() |
| 2128 | if v < (2, 6, 28): |
| 2129 | self.skipTest("Linux kernel 2.6.28 or higher required, not %s" |
| 2130 | % ".".join(map(str, v))) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2131 | # a lot of it seems silly and redundant, but I wanted to test that |
| 2132 | # changing back and forth worked ok |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2133 | with socket.socket(socket.AF_INET, |
| 2134 | socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s: |
| 2135 | self.checkNonblock(s) |
| 2136 | s.setblocking(1) |
| 2137 | self.checkNonblock(s, False) |
| 2138 | s.setblocking(0) |
| 2139 | self.checkNonblock(s) |
| 2140 | s.settimeout(None) |
| 2141 | self.checkNonblock(s, False) |
| 2142 | s.settimeout(2.0) |
| 2143 | self.checkNonblock(s, timeout=2.0) |
| 2144 | s.setblocking(1) |
| 2145 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2146 | # defaulttimeout |
| 2147 | t = socket.getdefaulttimeout() |
| 2148 | socket.setdefaulttimeout(0.0) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2149 | with socket.socket() as s: |
| 2150 | self.checkNonblock(s) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2151 | socket.setdefaulttimeout(None) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2152 | with socket.socket() as s: |
| 2153 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2154 | socket.setdefaulttimeout(2.0) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2155 | with socket.socket() as s: |
| 2156 | self.checkNonblock(s, timeout=2.0) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2157 | socket.setdefaulttimeout(None) |
Victor Stinner | 1d5eb34 | 2011-01-03 14:30:46 +0000 | [diff] [blame] | 2158 | with socket.socket() as s: |
| 2159 | self.checkNonblock(s, False) |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2160 | socket.setdefaulttimeout(t) |
| 2161 | |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 2162 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 2163 | def test_main(): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2164 | tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 2165 | TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2166 | |
| 2167 | tests.extend([ |
| 2168 | NonBlockingTCPTests, |
| 2169 | FileObjectClassTestCase, |
Gregory P. Smith | aafdca8 | 2010-01-04 04:50:36 +0000 | [diff] [blame] | 2170 | FileObjectInterruptedTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2171 | UnbufferedFileObjectClassTestCase, |
| 2172 | LineBufferedFileObjectClassTestCase, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 2173 | SmallBufferedFileObjectClassTestCase, |
Antoine Pitrou | 834bd81 | 2010-10-13 16:17:14 +0000 | [diff] [blame] | 2174 | UnicodeReadFileObjectClassTestCase, |
| 2175 | UnicodeWriteFileObjectClassTestCase, |
| 2176 | UnicodeReadWriteFileObjectClassTestCase, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2177 | NetworkConnectionNoServer, |
| 2178 | NetworkConnectionAttributesTest, |
| 2179 | NetworkConnectionBehaviourTest, |
Giampaolo Rodolà | b383dbb | 2010-09-08 22:44:12 +0000 | [diff] [blame] | 2180 | ContextManagersTest, |
Antoine Pitrou | b1c5496 | 2010-10-14 15:05:38 +0000 | [diff] [blame] | 2181 | CloexecConstantTest, |
| 2182 | NonblockConstantTest |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 2183 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 2184 | if hasattr(socket, "socketpair"): |
| 2185 | tests.append(BasicSocketPairTest) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2186 | if sys.platform == 'linux2': |
| 2187 | tests.append(TestLinuxAbstractNamespace) |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 2188 | if isTipcAvailable(): |
| 2189 | tests.append(TIPCTest) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 2190 | tests.append(TIPCThreadableTest) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2191 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2192 | thread_info = support.threading_setup() |
| 2193 | support.run_unittest(*tests) |
| 2194 | support.threading_cleanup(*thread_info) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 2195 | |
| 2196 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 2197 | test_main() |