Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 4 | from test import test_support |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 5 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 6 | import errno |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 7 | import socket |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 8 | import select |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 9 | import time |
| 10 | import traceback |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 11 | import Queue |
Jack Jansen | 522e769 | 2002-09-06 21:57:50 +0000 | [diff] [blame] | 12 | import sys |
Christian Heimes | a47b75b | 2008-01-04 15:48:06 +0000 | [diff] [blame] | 13 | import os |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 14 | import array |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 15 | import contextlib |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 16 | from weakref import proxy |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 17 | import signal |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 18 | import math |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 19 | |
Giampaolo Rodolà | 633e474 | 2010-08-14 17:04:46 +0000 | [diff] [blame] | 20 | def try_address(host, port=0, family=socket.AF_INET): |
| 21 | """Try to bind a socket on the given host:port and return True |
| 22 | if that has been possible.""" |
| 23 | try: |
| 24 | sock = socket.socket(family, socket.SOCK_STREAM) |
| 25 | sock.bind((host, port)) |
| 26 | except (socket.error, socket.gaierror): |
| 27 | return False |
| 28 | else: |
| 29 | sock.close() |
| 30 | return True |
| 31 | |
| 32 | HOST = test_support.HOST |
| 33 | MSG = b'Michael Gilfix was here\n' |
| 34 | SUPPORTS_IPV6 = socket.has_ipv6 and try_address('::1', family=socket.AF_INET6) |
| 35 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 36 | try: |
| 37 | import thread |
| 38 | import threading |
| 39 | except ImportError: |
| 40 | thread = None |
| 41 | threading = None |
| 42 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 43 | HOST = test_support.HOST |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 44 | MSG = 'Michael Gilfix was here\n' |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 46 | class SocketTCPTest(unittest.TestCase): |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 48 | def setUp(self): |
| 49 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 50 | self.port = test_support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 51 | self.serv.listen(1) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 53 | def tearDown(self): |
| 54 | self.serv.close() |
| 55 | self.serv = None |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 57 | class SocketUDPTest(unittest.TestCase): |
| 58 | |
| 59 | def setUp(self): |
| 60 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 61 | self.port = test_support.bind_port(self.serv) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 62 | |
| 63 | def tearDown(self): |
| 64 | self.serv.close() |
| 65 | self.serv = None |
| 66 | |
| 67 | class ThreadableTest: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 68 | """Threadable Test class |
| 69 | |
| 70 | The ThreadableTest class makes it easy to create a threaded |
| 71 | client/server pair from an existing unit test. To create a |
| 72 | new threaded class from an existing unit test, use multiple |
| 73 | inheritance: |
| 74 | |
| 75 | class NewClass (OldClass, ThreadableTest): |
| 76 | pass |
| 77 | |
| 78 | This class defines two new fixture functions with obvious |
| 79 | purposes for overriding: |
| 80 | |
| 81 | clientSetUp () |
| 82 | clientTearDown () |
| 83 | |
| 84 | Any new test functions within the class must then define |
| 85 | tests in pairs, where the test name is preceeded with a |
| 86 | '_' to indicate the client portion of the test. Ex: |
| 87 | |
| 88 | def testFoo(self): |
| 89 | # Server portion |
| 90 | |
| 91 | def _testFoo(self): |
| 92 | # Client portion |
| 93 | |
| 94 | Any exceptions raised by the clients during their tests |
| 95 | are caught and transferred to the main thread to alert |
| 96 | the testing framework. |
| 97 | |
| 98 | Note, the server setup function cannot call any blocking |
| 99 | functions that rely on the client thread during setup, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 100 | unless serverExplicitReady() is called just before |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 101 | the blocking call (such as in setting up a client/server |
| 102 | connection and performing the accept() in setUp(). |
| 103 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 104 | |
| 105 | def __init__(self): |
| 106 | # Swap the true setup function |
| 107 | self.__setUp = self.setUp |
| 108 | self.__tearDown = self.tearDown |
| 109 | self.setUp = self._setUp |
| 110 | self.tearDown = self._tearDown |
| 111 | |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 112 | def serverExplicitReady(self): |
| 113 | """This method allows the server to explicitly indicate that |
| 114 | it wants the client thread to proceed. This is useful if the |
| 115 | server is about to execute a blocking routine that is |
| 116 | dependent upon the client thread during its setup routine.""" |
| 117 | self.server_ready.set() |
| 118 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 119 | def _setUp(self): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 120 | self.server_ready = threading.Event() |
| 121 | self.client_ready = threading.Event() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 122 | self.done = threading.Event() |
Georg Brandl | a6168f9 | 2008-05-25 07:20:14 +0000 | [diff] [blame] | 123 | self.queue = Queue.Queue(1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 124 | |
| 125 | # Do some munging to start the client test. |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 126 | methodname = self.id() |
| 127 | i = methodname.rfind('.') |
| 128 | methodname = methodname[i+1:] |
| 129 | test_method = getattr(self, '_' + methodname) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 130 | self.client_thread = thread.start_new_thread( |
| 131 | self.clientRun, (test_method,)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 132 | |
| 133 | self.__setUp() |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 134 | if not self.server_ready.is_set(): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 135 | self.server_ready.set() |
| 136 | self.client_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 137 | |
| 138 | def _tearDown(self): |
| 139 | self.__tearDown() |
| 140 | self.done.wait() |
| 141 | |
| 142 | if not self.queue.empty(): |
| 143 | msg = self.queue.get() |
| 144 | self.fail(msg) |
| 145 | |
| 146 | def clientRun(self, test_func): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 147 | self.server_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 148 | self.clientSetUp() |
Charles-François Natali | 2156594 | 2011-09-21 22:02:27 +0200 | [diff] [blame] | 149 | self.client_ready.set() |
Ezio Melotti | b22c3fe | 2011-11-06 17:50:52 +0200 | [diff] [blame^] | 150 | if not callable(test_func): |
| 151 | raise TypeError("test_func must be a callable function.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 152 | try: |
| 153 | test_func() |
| 154 | except Exception, strerror: |
| 155 | self.queue.put(strerror) |
| 156 | self.clientTearDown() |
| 157 | |
| 158 | def clientSetUp(self): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 159 | raise NotImplementedError("clientSetUp must be implemented.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 160 | |
| 161 | def clientTearDown(self): |
| 162 | self.done.set() |
| 163 | thread.exit() |
| 164 | |
| 165 | class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): |
| 166 | |
| 167 | def __init__(self, methodName='runTest'): |
| 168 | SocketTCPTest.__init__(self, methodName=methodName) |
| 169 | ThreadableTest.__init__(self) |
| 170 | |
| 171 | def clientSetUp(self): |
| 172 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 173 | |
| 174 | def clientTearDown(self): |
| 175 | self.cli.close() |
| 176 | self.cli = None |
| 177 | ThreadableTest.clientTearDown(self) |
| 178 | |
| 179 | class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): |
| 180 | |
| 181 | def __init__(self, methodName='runTest'): |
| 182 | SocketUDPTest.__init__(self, methodName=methodName) |
| 183 | ThreadableTest.__init__(self) |
| 184 | |
| 185 | def clientSetUp(self): |
| 186 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 187 | |
Brian Curtin | dd8564f | 2010-11-04 03:54:23 +0000 | [diff] [blame] | 188 | def clientTearDown(self): |
| 189 | self.cli.close() |
| 190 | self.cli = None |
| 191 | ThreadableTest.clientTearDown(self) |
| 192 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 193 | class SocketConnectedTest(ThreadedTCPSocketTest): |
| 194 | |
| 195 | def __init__(self, methodName='runTest'): |
| 196 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 197 | |
| 198 | def setUp(self): |
| 199 | ThreadedTCPSocketTest.setUp(self) |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 200 | # Indicate explicitly we're ready for the client thread to |
| 201 | # proceed and then perform the blocking call to accept |
| 202 | self.serverExplicitReady() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 203 | conn, addr = self.serv.accept() |
| 204 | self.cli_conn = conn |
| 205 | |
| 206 | def tearDown(self): |
| 207 | self.cli_conn.close() |
| 208 | self.cli_conn = None |
| 209 | ThreadedTCPSocketTest.tearDown(self) |
| 210 | |
| 211 | def clientSetUp(self): |
| 212 | ThreadedTCPSocketTest.clientSetUp(self) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 213 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 214 | self.serv_conn = self.cli |
| 215 | |
| 216 | def clientTearDown(self): |
| 217 | self.serv_conn.close() |
| 218 | self.serv_conn = None |
| 219 | ThreadedTCPSocketTest.clientTearDown(self) |
| 220 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 221 | class SocketPairTest(unittest.TestCase, ThreadableTest): |
| 222 | |
| 223 | def __init__(self, methodName='runTest'): |
| 224 | unittest.TestCase.__init__(self, methodName=methodName) |
| 225 | ThreadableTest.__init__(self) |
| 226 | |
| 227 | def setUp(self): |
| 228 | self.serv, self.cli = socket.socketpair() |
| 229 | |
| 230 | def tearDown(self): |
| 231 | self.serv.close() |
| 232 | self.serv = None |
| 233 | |
| 234 | def clientSetUp(self): |
| 235 | pass |
| 236 | |
| 237 | def clientTearDown(self): |
| 238 | self.cli.close() |
| 239 | self.cli = None |
| 240 | ThreadableTest.clientTearDown(self) |
| 241 | |
Tim Peters | 494aaee | 2004-08-09 18:54:11 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 243 | ####################################################################### |
| 244 | ## Begin Tests |
| 245 | |
| 246 | class GeneralModuleTests(unittest.TestCase): |
| 247 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 248 | def test_weakref(self): |
| 249 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 250 | p = proxy(s) |
| 251 | self.assertEqual(p.fileno(), s.fileno()) |
| 252 | s.close() |
| 253 | s = None |
| 254 | try: |
| 255 | p.fileno() |
| 256 | except ReferenceError: |
| 257 | pass |
| 258 | else: |
| 259 | self.fail('Socket proxy still exists') |
| 260 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 261 | def testSocketError(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 262 | # Testing socket module exceptions |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 263 | def raise_error(*args, **kwargs): |
| 264 | raise socket.error |
| 265 | def raise_herror(*args, **kwargs): |
| 266 | raise socket.herror |
| 267 | def raise_gaierror(*args, **kwargs): |
| 268 | raise socket.gaierror |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 269 | self.assertRaises(socket.error, raise_error, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 270 | "Error raising socket exception.") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 271 | self.assertRaises(socket.error, raise_herror, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 272 | "Error raising socket exception.") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 273 | self.assertRaises(socket.error, raise_gaierror, |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 274 | "Error raising socket exception.") |
| 275 | |
Ezio Melotti | 0639be6 | 2011-05-07 19:21:22 +0300 | [diff] [blame] | 276 | def testSendtoErrors(self): |
| 277 | # Testing that sendto doens't masks failures. See #10169. |
| 278 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 279 | self.addCleanup(s.close) |
| 280 | s.bind(('', 0)) |
| 281 | sockname = s.getsockname() |
| 282 | # 2 args |
| 283 | with self.assertRaises(UnicodeEncodeError): |
| 284 | s.sendto(u'\u2620', sockname) |
| 285 | with self.assertRaises(TypeError) as cm: |
| 286 | s.sendto(5j, sockname) |
| 287 | self.assertIn('not complex', str(cm.exception)) |
| 288 | with self.assertRaises(TypeError) as cm: |
| 289 | s.sendto('foo', None) |
| 290 | self.assertIn('not NoneType', str(cm.exception)) |
| 291 | # 3 args |
| 292 | with self.assertRaises(UnicodeEncodeError): |
| 293 | s.sendto(u'\u2620', 0, sockname) |
| 294 | with self.assertRaises(TypeError) as cm: |
| 295 | s.sendto(5j, 0, sockname) |
| 296 | self.assertIn('not complex', str(cm.exception)) |
| 297 | with self.assertRaises(TypeError) as cm: |
| 298 | s.sendto('foo', 0, None) |
| 299 | self.assertIn('not NoneType', str(cm.exception)) |
| 300 | with self.assertRaises(TypeError) as cm: |
| 301 | s.sendto('foo', 'bar', sockname) |
| 302 | self.assertIn('an integer is required', str(cm.exception)) |
| 303 | with self.assertRaises(TypeError) as cm: |
| 304 | s.sendto('foo', None, None) |
| 305 | self.assertIn('an integer is required', str(cm.exception)) |
| 306 | # wrong number of args |
| 307 | with self.assertRaises(TypeError) as cm: |
| 308 | s.sendto('foo') |
| 309 | self.assertIn('(1 given)', str(cm.exception)) |
| 310 | with self.assertRaises(TypeError) as cm: |
| 311 | s.sendto('foo', 0, sockname, 4) |
| 312 | self.assertIn('(4 given)', str(cm.exception)) |
| 313 | |
| 314 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 315 | def testCrucialConstants(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 316 | # Testing for mission critical constants |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 317 | socket.AF_INET |
| 318 | socket.SOCK_STREAM |
| 319 | socket.SOCK_DGRAM |
| 320 | socket.SOCK_RAW |
| 321 | socket.SOCK_RDM |
| 322 | socket.SOCK_SEQPACKET |
| 323 | socket.SOL_SOCKET |
| 324 | socket.SO_REUSEADDR |
| 325 | |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 326 | def testHostnameRes(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 327 | # Testing hostname resolution mechanisms |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 328 | hostname = socket.gethostname() |
Guido van Rossum | 71e0294 | 2002-12-26 16:55:15 +0000 | [diff] [blame] | 329 | try: |
| 330 | ip = socket.gethostbyname(hostname) |
| 331 | except socket.error: |
| 332 | # Probably name lookup wasn't set up right; skip this test |
| 333 | return |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 334 | self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.") |
Guido van Rossum | 9647b52 | 2002-12-26 17:04:45 +0000 | [diff] [blame] | 335 | try: |
| 336 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
| 337 | except socket.error: |
| 338 | # Probably a similar problem as above; skip this test |
| 339 | return |
Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame] | 340 | all_host_names = [hostname, hname] + aliases |
Anthony Baxter | cf0a2a8 | 2006-04-03 08:10:33 +0000 | [diff] [blame] | 341 | fqhn = socket.getfqdn(ip) |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 342 | if not fqhn in all_host_names: |
Martin v. Löwis | 04855cc | 2006-03-26 16:40:47 +0000 | [diff] [blame] | 343 | 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] | 344 | |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 345 | def testRefCountGetNameInfo(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 346 | # Testing reference count for getnameinfo |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 347 | if hasattr(sys, "getrefcount"): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 348 | try: |
| 349 | # On some versions, this loses a reference |
| 350 | orig = sys.getrefcount(__name__) |
| 351 | socket.getnameinfo(__name__,0) |
Georg Brandl | ab84989 | 2009-09-19 07:35:07 +0000 | [diff] [blame] | 352 | except TypeError: |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 353 | self.assertEqual(sys.getrefcount(__name__), orig, |
| 354 | "socket.getnameinfo loses a reference") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 356 | def testInterpreterCrash(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 357 | # Making sure getnameinfo doesn't crash the interpreter |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 358 | try: |
| 359 | # On some versions, this crashes the interpreter. |
| 360 | socket.getnameinfo(('x', 0, 0, 0), 0) |
| 361 | except socket.error: |
| 362 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | c0a0e08 | 2002-09-16 01:30:03 +0000 | [diff] [blame] | 364 | def testNtoH(self): |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 365 | # This just checks that htons etc. are their own inverse, |
| 366 | # when looking at the lower 16 or 32 bits. |
| 367 | sizes = {socket.htonl: 32, socket.ntohl: 32, |
| 368 | socket.htons: 16, socket.ntohs: 16} |
| 369 | for func, size in sizes.items(): |
| 370 | mask = (1L<<size) - 1 |
| 371 | for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): |
| 372 | self.assertEqual(i & mask, func(func(i&mask)) & mask) |
Jeremy Hylton | cbd5b89 | 2002-07-31 15:57:39 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 374 | swapped = func(mask) |
| 375 | self.assertEqual(swapped & mask, mask) |
| 376 | self.assertRaises(OverflowError, func, 1L<<34) |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 378 | def testNtoHErrors(self): |
| 379 | good_values = [ 1, 2, 3, 1L, 2L, 3L ] |
| 380 | bad_values = [ -1, -2, -3, -1L, -2L, -3L ] |
| 381 | for k in good_values: |
| 382 | socket.ntohl(k) |
| 383 | socket.ntohs(k) |
| 384 | socket.htonl(k) |
| 385 | socket.htons(k) |
| 386 | for k in bad_values: |
| 387 | self.assertRaises(OverflowError, socket.ntohl, k) |
| 388 | self.assertRaises(OverflowError, socket.ntohs, k) |
| 389 | self.assertRaises(OverflowError, socket.htonl, k) |
| 390 | self.assertRaises(OverflowError, socket.htons, k) |
| 391 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 392 | def testGetServBy(self): |
| 393 | eq = self.assertEqual |
| 394 | # Find one service that exists, then check all the related interfaces. |
| 395 | # I've ordered this by protocols that have both a tcp and udp |
| 396 | # protocol, at least for modern Linuxes. |
Gregory P. Smith | 886a1cd | 2010-10-17 04:28:14 +0000 | [diff] [blame] | 397 | if (sys.platform.startswith('linux') or |
| 398 | sys.platform.startswith('freebsd') or |
| 399 | sys.platform.startswith('netbsd') or |
| 400 | sys.platform == 'darwin'): |
Andrew MacIntyre | 18bf43c | 2004-07-12 12:10:30 +0000 | [diff] [blame] | 401 | # avoid the 'echo' service on this platform, as there is an |
| 402 | # assumption breaking non-standard port/protocol entry |
| 403 | services = ('daytime', 'qotd', 'domain') |
| 404 | else: |
| 405 | services = ('echo', 'daytime', 'domain') |
| 406 | for service in services: |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 407 | try: |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 408 | port = socket.getservbyname(service, 'tcp') |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 409 | break |
| 410 | except socket.error: |
| 411 | pass |
Skip Montanaro | 05eb401 | 2004-02-10 15:51:15 +0000 | [diff] [blame] | 412 | else: |
| 413 | raise socket.error |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 414 | # Try same call with optional protocol omitted |
| 415 | port2 = socket.getservbyname(service) |
| 416 | eq(port, port2) |
| 417 | # Try udp, but don't barf it it doesn't exist |
| 418 | try: |
| 419 | udpport = socket.getservbyname(service, 'udp') |
| 420 | except socket.error: |
| 421 | udpport = None |
| 422 | else: |
| 423 | eq(udpport, port) |
| 424 | # Now make sure the lookup by port returns the same service name |
| 425 | eq(socket.getservbyport(port2), service) |
| 426 | eq(socket.getservbyport(port, 'tcp'), service) |
| 427 | if udpport is not None: |
| 428 | eq(socket.getservbyport(udpport, 'udp'), service) |
Gregory P. Smith | de1a8b7 | 2009-01-31 22:57:30 +0000 | [diff] [blame] | 429 | # Make sure getservbyport does not accept out of range ports. |
| 430 | self.assertRaises(OverflowError, socket.getservbyport, -1) |
| 431 | self.assertRaises(OverflowError, socket.getservbyport, 65536) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 433 | def testDefaultTimeout(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 434 | # Testing default timeout |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 435 | # The default timeout should initially be None |
| 436 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 437 | s = socket.socket() |
| 438 | self.assertEqual(s.gettimeout(), None) |
| 439 | s.close() |
| 440 | |
| 441 | # Set the default timeout to 10, and see if it propagates |
| 442 | socket.setdefaulttimeout(10) |
| 443 | self.assertEqual(socket.getdefaulttimeout(), 10) |
| 444 | s = socket.socket() |
| 445 | self.assertEqual(s.gettimeout(), 10) |
| 446 | s.close() |
| 447 | |
| 448 | # Reset the default timeout to None, and see if it propagates |
| 449 | socket.setdefaulttimeout(None) |
| 450 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 451 | s = socket.socket() |
| 452 | self.assertEqual(s.gettimeout(), None) |
| 453 | s.close() |
| 454 | |
| 455 | # Check that setting it to an invalid value raises ValueError |
| 456 | self.assertRaises(ValueError, socket.setdefaulttimeout, -1) |
| 457 | |
| 458 | # Check that setting it to an invalid type raises TypeError |
| 459 | self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") |
| 460 | |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 461 | def testIPv4_inet_aton_fourbytes(self): |
| 462 | if not hasattr(socket, 'inet_aton'): |
| 463 | return # No inet_aton, nothing to check |
| 464 | # Test that issue1008086 and issue767150 are fixed. |
| 465 | # It must return 4 bytes. |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 466 | self.assertEqual('\x00'*4, socket.inet_aton('0.0.0.0')) |
| 467 | self.assertEqual('\xff'*4, socket.inet_aton('255.255.255.255')) |
Gregory P. Smith | 3605b5c | 2009-02-11 23:45:25 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 469 | def testIPv4toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 470 | if not hasattr(socket, 'inet_pton'): |
| 471 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 472 | from socket import inet_aton as f, inet_pton, AF_INET |
| 473 | g = lambda a: inet_pton(AF_INET, a) |
| 474 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 475 | self.assertEqual('\x00\x00\x00\x00', f('0.0.0.0')) |
| 476 | self.assertEqual('\xff\x00\xff\x00', f('255.0.255.0')) |
| 477 | self.assertEqual('\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
| 478 | self.assertEqual('\x01\x02\x03\x04', f('1.2.3.4')) |
| 479 | self.assertEqual('\xff\xff\xff\xff', f('255.255.255.255')) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 480 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 481 | self.assertEqual('\x00\x00\x00\x00', g('0.0.0.0')) |
| 482 | self.assertEqual('\xff\x00\xff\x00', g('255.0.255.0')) |
| 483 | self.assertEqual('\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
| 484 | self.assertEqual('\xff\xff\xff\xff', g('255.255.255.255')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 486 | def testIPv6toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 487 | if not hasattr(socket, 'inet_pton'): |
| 488 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 489 | try: |
| 490 | from socket import inet_pton, AF_INET6, has_ipv6 |
| 491 | if not has_ipv6: |
| 492 | return |
| 493 | except ImportError: |
| 494 | return |
| 495 | f = lambda a: inet_pton(AF_INET6, a) |
| 496 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 497 | self.assertEqual('\x00' * 16, f('::')) |
| 498 | self.assertEqual('\x00' * 16, f('0::0')) |
| 499 | self.assertEqual('\x00\x01' + '\x00' * 14, f('1::')) |
| 500 | self.assertEqual( |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 501 | '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', |
| 502 | f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') |
| 503 | ) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 504 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 505 | def testStringToIPv4(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 506 | if not hasattr(socket, 'inet_ntop'): |
| 507 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 508 | from socket import inet_ntoa as f, inet_ntop, AF_INET |
| 509 | g = lambda a: inet_ntop(AF_INET, a) |
| 510 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 511 | self.assertEqual('1.0.1.0', f('\x01\x00\x01\x00')) |
| 512 | self.assertEqual('170.85.170.85', f('\xaa\x55\xaa\x55')) |
| 513 | self.assertEqual('255.255.255.255', f('\xff\xff\xff\xff')) |
| 514 | self.assertEqual('1.2.3.4', f('\x01\x02\x03\x04')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 515 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 516 | self.assertEqual('1.0.1.0', g('\x01\x00\x01\x00')) |
| 517 | self.assertEqual('170.85.170.85', g('\xaa\x55\xaa\x55')) |
| 518 | self.assertEqual('255.255.255.255', g('\xff\xff\xff\xff')) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 519 | |
| 520 | def testStringToIPv6(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 521 | if not hasattr(socket, 'inet_ntop'): |
| 522 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 523 | try: |
| 524 | from socket import inet_ntop, AF_INET6, has_ipv6 |
| 525 | if not has_ipv6: |
| 526 | return |
| 527 | except ImportError: |
| 528 | return |
| 529 | f = lambda a: inet_ntop(AF_INET6, a) |
| 530 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 531 | self.assertEqual('::', f('\x00' * 16)) |
| 532 | self.assertEqual('::1', f('\x00' * 15 + '\x01')) |
| 533 | self.assertEqual( |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 534 | 'aef:b01:506:1001:ffff:9997:55:170', |
| 535 | f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') |
| 536 | ) |
| 537 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 538 | # XXX The following don't test module-level functionality... |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 539 | |
Gregory P. Smith | de1a8b7 | 2009-01-31 22:57:30 +0000 | [diff] [blame] | 540 | def _get_unused_port(self, bind_address='0.0.0.0'): |
| 541 | """Use a temporary socket to elicit an unused ephemeral port. |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 542 | |
Gregory P. Smith | de1a8b7 | 2009-01-31 22:57:30 +0000 | [diff] [blame] | 543 | Args: |
| 544 | bind_address: Hostname or IP address to search for a port on. |
| 545 | |
| 546 | Returns: A most likely to be unused port. |
| 547 | """ |
| 548 | tempsock = socket.socket() |
| 549 | tempsock.bind((bind_address, 0)) |
| 550 | host, port = tempsock.getsockname() |
| 551 | tempsock.close() |
| 552 | return port |
| 553 | |
| 554 | def testSockName(self): |
| 555 | # Testing getsockname() |
| 556 | port = self._get_unused_port() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 557 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 558 | self.addCleanup(sock.close) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 559 | sock.bind(("0.0.0.0", port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 560 | name = sock.getsockname() |
Neal Norwitz | 4a9ff16 | 2006-06-11 21:38:38 +0000 | [diff] [blame] | 561 | # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate |
| 562 | # it reasonable to get the host's addr in addition to 0.0.0.0. |
| 563 | # At least for eCos. This is required for the S/390 to pass. |
Georg Brandl | b727650 | 2010-11-26 08:28:05 +0000 | [diff] [blame] | 564 | try: |
| 565 | my_ip_addr = socket.gethostbyname(socket.gethostname()) |
| 566 | except socket.error: |
| 567 | # Probably name lookup wasn't set up right; skip this test |
| 568 | return |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 569 | self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 570 | self.assertEqual(name[1], port) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 571 | |
| 572 | def testGetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 573 | # Testing getsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 574 | # We know a socket should start without reuse==0 |
| 575 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 576 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 577 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 578 | self.assertFalse(reuse != 0, "initial mode is reuse") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 579 | |
| 580 | def testSetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 581 | # Testing setsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 582 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 583 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 584 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 585 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 586 | self.assertFalse(reuse == 0, "failed to set reuse mode") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 588 | def testSendAfterClose(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 589 | # testing send() after close() with timeout |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 590 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 591 | sock.settimeout(1) |
| 592 | sock.close() |
| 593 | self.assertRaises(socket.error, sock.send, "spam") |
| 594 | |
Georg Brandl | bb03ac0 | 2006-03-21 18:17:25 +0000 | [diff] [blame] | 595 | def testNewAttributes(self): |
| 596 | # testing .family, .type and .protocol |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 597 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Georg Brandl | bb03ac0 | 2006-03-21 18:17:25 +0000 | [diff] [blame] | 598 | self.assertEqual(sock.family, socket.AF_INET) |
| 599 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 600 | self.assertEqual(sock.proto, 0) |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 601 | sock.close() |
| 602 | |
Gregory P. Smith | de1a8b7 | 2009-01-31 22:57:30 +0000 | [diff] [blame] | 603 | def test_getsockaddrarg(self): |
| 604 | host = '0.0.0.0' |
| 605 | port = self._get_unused_port(bind_address=host) |
| 606 | big_port = port + 65536 |
| 607 | neg_port = port - 65536 |
| 608 | sock = socket.socket() |
| 609 | try: |
| 610 | self.assertRaises(OverflowError, sock.bind, (host, big_port)) |
| 611 | self.assertRaises(OverflowError, sock.bind, (host, neg_port)) |
| 612 | sock.bind((host, port)) |
| 613 | finally: |
| 614 | sock.close() |
| 615 | |
Brian Curtin | 91a5b81 | 2010-11-05 15:52:20 +0000 | [diff] [blame] | 616 | @unittest.skipUnless(os.name == "nt", "Windows specific") |
Christian Heimes | a47b75b | 2008-01-04 15:48:06 +0000 | [diff] [blame] | 617 | def test_sock_ioctl(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 618 | self.assertTrue(hasattr(socket.socket, 'ioctl')) |
| 619 | self.assertTrue(hasattr(socket, 'SIO_RCVALL')) |
| 620 | self.assertTrue(hasattr(socket, 'RCVALL_ON')) |
| 621 | self.assertTrue(hasattr(socket, 'RCVALL_OFF')) |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 622 | self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS')) |
| 623 | s = socket.socket() |
Brian Curtin | 91a5b81 | 2010-11-05 15:52:20 +0000 | [diff] [blame] | 624 | self.addCleanup(s.close) |
Kristján Valur Jónsson | 2fcd03b | 2009-09-25 15:19:51 +0000 | [diff] [blame] | 625 | self.assertRaises(ValueError, s.ioctl, -1, None) |
| 626 | s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100)) |
Christian Heimes | a47b75b | 2008-01-04 15:48:06 +0000 | [diff] [blame] | 627 | |
Giampaolo Rodolà | 633e474 | 2010-08-14 17:04:46 +0000 | [diff] [blame] | 628 | def testGetaddrinfo(self): |
| 629 | try: |
| 630 | socket.getaddrinfo('localhost', 80) |
| 631 | except socket.gaierror as err: |
| 632 | if err.errno == socket.EAI_SERVICE: |
| 633 | # see http://bugs.python.org/issue1282647 |
| 634 | self.skipTest("buggy libc version") |
| 635 | raise |
| 636 | # len of every sequence is supposed to be == 5 |
| 637 | for info in socket.getaddrinfo(HOST, None): |
| 638 | self.assertEqual(len(info), 5) |
| 639 | # host can be a domain name, a string representation of an |
| 640 | # IPv4/v6 address or None |
| 641 | socket.getaddrinfo('localhost', 80) |
| 642 | socket.getaddrinfo('127.0.0.1', 80) |
| 643 | socket.getaddrinfo(None, 80) |
| 644 | if SUPPORTS_IPV6: |
| 645 | socket.getaddrinfo('::1', 80) |
| 646 | # port can be a string service name such as "http", a numeric |
| 647 | # port number or None |
| 648 | socket.getaddrinfo(HOST, "http") |
| 649 | socket.getaddrinfo(HOST, 80) |
| 650 | socket.getaddrinfo(HOST, None) |
| 651 | # test family and socktype filters |
| 652 | infos = socket.getaddrinfo(HOST, None, socket.AF_INET) |
| 653 | for family, _, _, _, _ in infos: |
| 654 | self.assertEqual(family, socket.AF_INET) |
| 655 | infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM) |
| 656 | for _, socktype, _, _, _ in infos: |
| 657 | self.assertEqual(socktype, socket.SOCK_STREAM) |
| 658 | # test proto and flags arguments |
Giampaolo Rodolà | 8a9b222 | 2010-08-16 05:10:30 +0000 | [diff] [blame] | 659 | socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP) |
Giampaolo Rodolà | 633e474 | 2010-08-14 17:04:46 +0000 | [diff] [blame] | 660 | socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE) |
| 661 | # a server willing to support both IPv4 and IPv6 will |
| 662 | # usually do this |
| 663 | socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, |
| 664 | socket.AI_PASSIVE) |
| 665 | |
Christian Heimes | a47b75b | 2008-01-04 15:48:06 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 667 | def check_sendall_interrupted(self, with_timeout): |
| 668 | # socketpair() is not stricly required, but it makes things easier. |
| 669 | if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'): |
| 670 | self.skipTest("signal.alarm and socket.socketpair required for this test") |
| 671 | # Our signal handlers clobber the C errno by calling a math function |
| 672 | # with an invalid domain value. |
| 673 | def ok_handler(*args): |
| 674 | self.assertRaises(ValueError, math.acosh, 0) |
| 675 | def raising_handler(*args): |
| 676 | self.assertRaises(ValueError, math.acosh, 0) |
| 677 | 1 // 0 |
| 678 | c, s = socket.socketpair() |
| 679 | old_alarm = signal.signal(signal.SIGALRM, raising_handler) |
| 680 | try: |
| 681 | if with_timeout: |
| 682 | # Just above the one second minimum for signal.alarm |
| 683 | c.settimeout(1.5) |
| 684 | with self.assertRaises(ZeroDivisionError): |
| 685 | signal.alarm(1) |
| 686 | c.sendall(b"x" * (1024**2)) |
| 687 | if with_timeout: |
| 688 | signal.signal(signal.SIGALRM, ok_handler) |
| 689 | signal.alarm(1) |
| 690 | self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2)) |
| 691 | finally: |
| 692 | signal.signal(signal.SIGALRM, old_alarm) |
| 693 | c.close() |
| 694 | s.close() |
| 695 | |
| 696 | def test_sendall_interrupted(self): |
| 697 | self.check_sendall_interrupted(False) |
| 698 | |
| 699 | def test_sendall_interrupted_with_timeout(self): |
| 700 | self.check_sendall_interrupted(True) |
| 701 | |
Antoine Pitrou | 47d1d0d | 2011-05-10 19:16:03 +0200 | [diff] [blame] | 702 | def testListenBacklog0(self): |
| 703 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 704 | srv.bind((HOST, 0)) |
| 705 | # backlog = 0 |
| 706 | srv.listen(0) |
| 707 | srv.close() |
| 708 | |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 709 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 710 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 711 | class BasicTCPTest(SocketConnectedTest): |
| 712 | |
| 713 | def __init__(self, methodName='runTest'): |
| 714 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 715 | |
| 716 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 717 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 718 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 719 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 720 | |
| 721 | def _testRecv(self): |
| 722 | self.serv_conn.send(MSG) |
| 723 | |
| 724 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 725 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 726 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 727 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 728 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 729 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 730 | |
| 731 | def _testOverFlowRecv(self): |
| 732 | self.serv_conn.send(MSG) |
| 733 | |
| 734 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 735 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 736 | msg, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 737 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 738 | |
| 739 | def _testRecvFrom(self): |
| 740 | self.serv_conn.send(MSG) |
| 741 | |
| 742 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 743 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 744 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 745 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 746 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 747 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 748 | |
| 749 | def _testOverFlowRecvFrom(self): |
| 750 | self.serv_conn.send(MSG) |
| 751 | |
| 752 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 753 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 754 | msg = '' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 755 | while 1: |
| 756 | read = self.cli_conn.recv(1024) |
| 757 | if not read: |
| 758 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 759 | msg += read |
| 760 | self.assertEqual(msg, 'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 761 | |
| 762 | def _testSendAll(self): |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 763 | big_chunk = 'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 764 | self.serv_conn.sendall(big_chunk) |
| 765 | |
| 766 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 767 | # Testing fromfd() |
Guido van Rossum | 8e95ca8 | 2002-06-12 20:55:17 +0000 | [diff] [blame] | 768 | if not hasattr(socket, "fromfd"): |
Guido van Rossum | 6fb3d5e | 2002-06-12 20:48:59 +0000 | [diff] [blame] | 769 | return # On Windows, this doesn't exist |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 770 | fd = self.cli_conn.fileno() |
| 771 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 772 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 773 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 774 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 775 | |
| 776 | def _testFromFd(self): |
| 777 | self.serv_conn.send(MSG) |
| 778 | |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 779 | def testDup(self): |
| 780 | # Testing dup() |
| 781 | sock = self.cli_conn.dup() |
| 782 | self.addCleanup(sock.close) |
| 783 | msg = sock.recv(1024) |
| 784 | self.assertEqual(msg, MSG) |
| 785 | |
| 786 | def _testDup(self): |
| 787 | self.serv_conn.send(MSG) |
| 788 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 789 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 790 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 791 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 792 | self.assertEqual(msg, MSG) |
Mark Dickinson | e82cdae | 2009-01-15 14:54:37 +0000 | [diff] [blame] | 793 | # wait for _testShutdown to finish: on OS X, when the server |
| 794 | # closes the connection the client also becomes disconnected, |
Mark Dickinson | 0e15182 | 2009-01-15 14:58:28 +0000 | [diff] [blame] | 795 | # and the client's shutdown call will fail. (Issue #4397.) |
Mark Dickinson | e82cdae | 2009-01-15 14:54:37 +0000 | [diff] [blame] | 796 | self.done.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 797 | |
| 798 | def _testShutdown(self): |
| 799 | self.serv_conn.send(MSG) |
| 800 | self.serv_conn.shutdown(2) |
| 801 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 802 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 803 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 804 | |
| 805 | def __init__(self, methodName='runTest'): |
| 806 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 807 | |
| 808 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 809 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 810 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 811 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 812 | |
| 813 | def _testSendtoAndRecv(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 814 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 816 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 817 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 818 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 819 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 820 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 821 | def _testRecvFrom(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 822 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 823 | |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 824 | def testRecvFromNegative(self): |
| 825 | # Negative lengths passed to recvfrom should give ValueError. |
| 826 | self.assertRaises(ValueError, self.serv.recvfrom, -1) |
| 827 | |
| 828 | def _testRecvFromNegative(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 829 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 830 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 831 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 832 | class TCPCloserTest(ThreadedTCPSocketTest): |
| 833 | |
| 834 | def testClose(self): |
| 835 | conn, addr = self.serv.accept() |
| 836 | conn.close() |
| 837 | |
| 838 | sd = self.cli |
| 839 | read, write, err = select.select([sd], [], [], 1.0) |
| 840 | self.assertEqual(read, [sd]) |
| 841 | self.assertEqual(sd.recv(1), '') |
| 842 | |
| 843 | def _testClose(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 844 | self.cli.connect((HOST, self.port)) |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 845 | time.sleep(1.0) |
| 846 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 847 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 848 | class BasicSocketPairTest(SocketPairTest): |
| 849 | |
| 850 | def __init__(self, methodName='runTest'): |
| 851 | SocketPairTest.__init__(self, methodName=methodName) |
| 852 | |
| 853 | def testRecv(self): |
| 854 | msg = self.serv.recv(1024) |
| 855 | self.assertEqual(msg, MSG) |
| 856 | |
| 857 | def _testRecv(self): |
| 858 | self.cli.send(MSG) |
| 859 | |
| 860 | def testSend(self): |
| 861 | self.serv.send(MSG) |
| 862 | |
| 863 | def _testSend(self): |
| 864 | msg = self.cli.recv(1024) |
| 865 | self.assertEqual(msg, MSG) |
| 866 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 867 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 868 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 869 | |
| 870 | def __init__(self, methodName='runTest'): |
| 871 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 872 | |
| 873 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 874 | # Testing whether set blocking works |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 875 | self.serv.setblocking(0) |
| 876 | start = time.time() |
| 877 | try: |
| 878 | self.serv.accept() |
| 879 | except socket.error: |
| 880 | pass |
| 881 | end = time.time() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 882 | self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 883 | |
| 884 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 885 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 886 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 887 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 888 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 889 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 890 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 891 | conn, addr = self.serv.accept() |
| 892 | except socket.error: |
| 893 | pass |
| 894 | else: |
| 895 | self.fail("Error trying to do non-blocking accept.") |
| 896 | read, write, err = select.select([self.serv], [], []) |
| 897 | if self.serv in read: |
| 898 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 899 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 900 | else: |
| 901 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 903 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 904 | time.sleep(0.1) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 905 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 906 | |
| 907 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 908 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 909 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 910 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 911 | |
| 912 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 913 | self.cli.settimeout(10) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 914 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 915 | |
| 916 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 917 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 918 | conn, addr = self.serv.accept() |
| 919 | conn.setblocking(0) |
| 920 | try: |
| 921 | msg = conn.recv(len(MSG)) |
| 922 | except socket.error: |
| 923 | pass |
| 924 | else: |
| 925 | self.fail("Error trying to do non-blocking recv.") |
| 926 | read, write, err = select.select([conn], [], []) |
| 927 | if conn in read: |
| 928 | msg = conn.recv(len(MSG)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 929 | conn.close() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 930 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 931 | else: |
| 932 | self.fail("Error during select call to non-blocking socket.") |
| 933 | |
| 934 | def _testRecv(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 935 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 936 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 937 | self.cli.send(MSG) |
| 938 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 939 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 940 | class FileObjectClassTestCase(SocketConnectedTest): |
| 941 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 942 | bufsize = -1 # Use default buffer size |
| 943 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 944 | def __init__(self, methodName='runTest'): |
| 945 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 946 | |
| 947 | def setUp(self): |
| 948 | SocketConnectedTest.setUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 949 | self.serv_file = self.cli_conn.makefile('rb', self.bufsize) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 950 | |
| 951 | def tearDown(self): |
| 952 | self.serv_file.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 953 | self.assertTrue(self.serv_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 954 | self.serv_file = None |
| 955 | SocketConnectedTest.tearDown(self) |
| 956 | |
| 957 | def clientSetUp(self): |
| 958 | SocketConnectedTest.clientSetUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 959 | self.cli_file = self.serv_conn.makefile('wb') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 960 | |
| 961 | def clientTearDown(self): |
| 962 | self.cli_file.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 963 | self.assertTrue(self.cli_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 964 | self.cli_file = None |
| 965 | SocketConnectedTest.clientTearDown(self) |
| 966 | |
| 967 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 968 | # Performing small file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 969 | first_seg = self.serv_file.read(len(MSG)-3) |
| 970 | second_seg = self.serv_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 971 | msg = first_seg + second_seg |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 972 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 973 | |
| 974 | def _testSmallRead(self): |
| 975 | self.cli_file.write(MSG) |
| 976 | self.cli_file.flush() |
| 977 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 978 | def testFullRead(self): |
| 979 | # read until EOF |
| 980 | msg = self.serv_file.read() |
| 981 | self.assertEqual(msg, MSG) |
| 982 | |
| 983 | def _testFullRead(self): |
| 984 | self.cli_file.write(MSG) |
| 985 | self.cli_file.close() |
| 986 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 987 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 988 | # Performing unbuffered file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 989 | buf = '' |
| 990 | while 1: |
| 991 | char = self.serv_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 992 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 993 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 994 | buf += char |
| 995 | self.assertEqual(buf, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 996 | |
| 997 | def _testUnbufferedRead(self): |
| 998 | self.cli_file.write(MSG) |
| 999 | self.cli_file.flush() |
| 1000 | |
| 1001 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1002 | # Performing file readline test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1003 | line = self.serv_file.readline() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1004 | self.assertEqual(line, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1005 | |
| 1006 | def _testReadline(self): |
| 1007 | self.cli_file.write(MSG) |
| 1008 | self.cli_file.flush() |
| 1009 | |
Gregory P. Smith | 24237ea | 2008-05-05 21:53:45 +0000 | [diff] [blame] | 1010 | def testReadlineAfterRead(self): |
| 1011 | a_baloo_is = self.serv_file.read(len("A baloo is")) |
| 1012 | self.assertEqual("A baloo is", a_baloo_is) |
| 1013 | _a_bear = self.serv_file.read(len(" a bear")) |
| 1014 | self.assertEqual(" a bear", _a_bear) |
| 1015 | line = self.serv_file.readline() |
| 1016 | self.assertEqual("\n", line) |
| 1017 | line = self.serv_file.readline() |
| 1018 | self.assertEqual("A BALOO IS A BEAR.\n", line) |
| 1019 | line = self.serv_file.readline() |
| 1020 | self.assertEqual(MSG, line) |
| 1021 | |
| 1022 | def _testReadlineAfterRead(self): |
| 1023 | self.cli_file.write("A baloo is a bear\n") |
| 1024 | self.cli_file.write("A BALOO IS A BEAR.\n") |
| 1025 | self.cli_file.write(MSG) |
| 1026 | self.cli_file.flush() |
| 1027 | |
| 1028 | def testReadlineAfterReadNoNewline(self): |
| 1029 | end_of_ = self.serv_file.read(len("End Of ")) |
| 1030 | self.assertEqual("End Of ", end_of_) |
| 1031 | line = self.serv_file.readline() |
| 1032 | self.assertEqual("Line", line) |
| 1033 | |
| 1034 | def _testReadlineAfterReadNoNewline(self): |
| 1035 | self.cli_file.write("End Of Line") |
| 1036 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1037 | def testClosedAttr(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1038 | self.assertTrue(not self.serv_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1039 | |
| 1040 | def _testClosedAttr(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1041 | self.assertTrue(not self.cli_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1042 | |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1043 | |
| 1044 | class FileObjectInterruptedTestCase(unittest.TestCase): |
| 1045 | """Test that the file object correctly handles EINTR internally.""" |
| 1046 | |
| 1047 | class MockSocket(object): |
| 1048 | def __init__(self, recv_funcs=()): |
| 1049 | # A generator that returns callables that we'll call for each |
| 1050 | # call to recv(). |
| 1051 | self._recv_step = iter(recv_funcs) |
| 1052 | |
| 1053 | def recv(self, size): |
| 1054 | return self._recv_step.next()() |
| 1055 | |
| 1056 | @staticmethod |
| 1057 | def _raise_eintr(): |
| 1058 | raise socket.error(errno.EINTR) |
| 1059 | |
| 1060 | def _test_readline(self, size=-1, **kwargs): |
| 1061 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1062 | lambda : "This is the first line\nAnd the sec", |
| 1063 | self._raise_eintr, |
| 1064 | lambda : "ond line is here\n", |
| 1065 | lambda : "", |
| 1066 | ]) |
| 1067 | fo = socket._fileobject(mock_sock, **kwargs) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1068 | self.assertEqual(fo.readline(size), "This is the first line\n") |
| 1069 | self.assertEqual(fo.readline(size), "And the second line is here\n") |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1070 | |
| 1071 | def _test_read(self, size=-1, **kwargs): |
| 1072 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1073 | lambda : "This is the first line\nAnd the sec", |
| 1074 | self._raise_eintr, |
| 1075 | lambda : "ond line is here\n", |
| 1076 | lambda : "", |
| 1077 | ]) |
| 1078 | fo = socket._fileobject(mock_sock, **kwargs) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1079 | self.assertEqual(fo.read(size), "This is the first line\n" |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1080 | "And the second line is here\n") |
| 1081 | |
| 1082 | def test_default(self): |
| 1083 | self._test_readline() |
| 1084 | self._test_readline(size=100) |
| 1085 | self._test_read() |
| 1086 | self._test_read(size=100) |
| 1087 | |
| 1088 | def test_with_1k_buffer(self): |
| 1089 | self._test_readline(bufsize=1024) |
| 1090 | self._test_readline(size=100, bufsize=1024) |
| 1091 | self._test_read(bufsize=1024) |
| 1092 | self._test_read(size=100, bufsize=1024) |
| 1093 | |
| 1094 | def _test_readline_no_buffer(self, size=-1): |
| 1095 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1096 | lambda : "aa", |
| 1097 | lambda : "\n", |
| 1098 | lambda : "BB", |
| 1099 | self._raise_eintr, |
| 1100 | lambda : "bb", |
| 1101 | lambda : "", |
| 1102 | ]) |
| 1103 | fo = socket._fileobject(mock_sock, bufsize=0) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1104 | self.assertEqual(fo.readline(size), "aa\n") |
| 1105 | self.assertEqual(fo.readline(size), "BBbb") |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1106 | |
| 1107 | def test_no_buffer(self): |
| 1108 | self._test_readline_no_buffer() |
| 1109 | self._test_readline_no_buffer(size=4) |
| 1110 | self._test_read(bufsize=0) |
| 1111 | self._test_read(size=100, bufsize=0) |
| 1112 | |
| 1113 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1114 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1115 | |
| 1116 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 1117 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1118 | In this case (and in this case only), it should be possible to |
| 1119 | create a file object, read a line from it, create another file |
| 1120 | object, read another line from it, without loss of data in the |
| 1121 | first file object's buffer. Note that httplib relies on this |
| 1122 | when reading multiple requests from the same socket.""" |
| 1123 | |
| 1124 | bufsize = 0 # Use unbuffered mode |
| 1125 | |
| 1126 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1127 | # Read a line, create a new file object, read another line with it |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1128 | line = self.serv_file.readline() # first line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1129 | self.assertEqual(line, "A. " + MSG) # first line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1130 | self.serv_file = self.cli_conn.makefile('rb', 0) |
| 1131 | line = self.serv_file.readline() # second line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1132 | self.assertEqual(line, "B. " + MSG) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1133 | |
| 1134 | def _testUnbufferedReadline(self): |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1135 | self.cli_file.write("A. " + MSG) |
| 1136 | self.cli_file.write("B. " + MSG) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1137 | self.cli_file.flush() |
| 1138 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1139 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1140 | |
| 1141 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 1142 | |
| 1143 | |
| 1144 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1145 | |
| 1146 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1147 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1148 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1149 | class NetworkConnectionTest(object): |
| 1150 | """Prove network connection.""" |
| 1151 | def clientSetUp(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1152 | # We're inherited below by BasicTCPTest2, which also inherits |
| 1153 | # BasicTCPTest, which defines self.port referenced below. |
| 1154 | self.cli = socket.create_connection((HOST, self.port)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1155 | self.serv_conn = self.cli |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1156 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1157 | class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
| 1158 | """Tests that NetworkConnection does not break existing TCP functionality. |
| 1159 | """ |
| 1160 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1161 | class NetworkConnectionNoServer(unittest.TestCase): |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1162 | class MockSocket(socket.socket): |
| 1163 | def connect(self, *args): |
| 1164 | raise socket.timeout('timed out') |
| 1165 | |
| 1166 | @contextlib.contextmanager |
| 1167 | def mocked_socket_module(self): |
| 1168 | """Return a socket which times out on connect""" |
| 1169 | old_socket = socket.socket |
| 1170 | socket.socket = self.MockSocket |
| 1171 | try: |
| 1172 | yield |
| 1173 | finally: |
| 1174 | socket.socket = old_socket |
| 1175 | |
| 1176 | def test_connect(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1177 | port = test_support.find_unused_port() |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1178 | cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1179 | self.addCleanup(cli.close) |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1180 | with self.assertRaises(socket.error) as cm: |
| 1181 | cli.connect((HOST, port)) |
| 1182 | self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) |
| 1183 | |
| 1184 | def test_create_connection(self): |
| 1185 | # Issue #9792: errors raised by create_connection() should have |
| 1186 | # a proper errno attribute. |
| 1187 | port = test_support.find_unused_port() |
| 1188 | with self.assertRaises(socket.error) as cm: |
| 1189 | socket.create_connection((HOST, port)) |
| 1190 | self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) |
| 1191 | |
| 1192 | def test_create_connection_timeout(self): |
| 1193 | # Issue #9792: create_connection() should not recast timeout errors |
| 1194 | # as generic socket errors. |
| 1195 | with self.mocked_socket_module(): |
| 1196 | with self.assertRaises(socket.timeout): |
| 1197 | socket.create_connection((HOST, 1234)) |
| 1198 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1199 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1200 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1201 | class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
| 1202 | |
| 1203 | def __init__(self, methodName='runTest'): |
| 1204 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1205 | ThreadableTest.__init__(self) |
| 1206 | |
| 1207 | def clientSetUp(self): |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1208 | self.source_port = test_support.find_unused_port() |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1209 | |
| 1210 | def clientTearDown(self): |
| 1211 | self.cli.close() |
| 1212 | self.cli = None |
| 1213 | ThreadableTest.clientTearDown(self) |
| 1214 | |
| 1215 | def _justAccept(self): |
| 1216 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1217 | conn.close() |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1218 | |
| 1219 | testFamily = _justAccept |
| 1220 | def _testFamily(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1221 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1222 | self.addCleanup(self.cli.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1223 | self.assertEqual(self.cli.family, 2) |
| 1224 | |
Gregory P. Smith | 49d709c | 2010-01-03 15:05:52 +0000 | [diff] [blame] | 1225 | testSourceAddress = _justAccept |
| 1226 | def _testSourceAddress(self): |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1227 | self.cli = socket.create_connection((HOST, self.port), timeout=30, |
| 1228 | source_address=('', self.source_port)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1229 | self.addCleanup(self.cli.close) |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1230 | self.assertEqual(self.cli.getsockname()[1], self.source_port) |
Gregory P. Smith | 49d709c | 2010-01-03 15:05:52 +0000 | [diff] [blame] | 1231 | # The port number being used is sufficient to show that the bind() |
| 1232 | # call happened. |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1233 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1234 | testTimeoutDefault = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1235 | def _testTimeoutDefault(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1236 | # passing no explicit timeout uses socket's global default |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1237 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1238 | socket.setdefaulttimeout(42) |
| 1239 | try: |
| 1240 | self.cli = socket.create_connection((HOST, self.port)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1241 | self.addCleanup(self.cli.close) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1242 | finally: |
| 1243 | socket.setdefaulttimeout(None) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1244 | self.assertEqual(self.cli.gettimeout(), 42) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1245 | |
| 1246 | testTimeoutNone = _justAccept |
| 1247 | def _testTimeoutNone(self): |
| 1248 | # None timeout means the same as sock.settimeout(None) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1249 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1250 | socket.setdefaulttimeout(30) |
| 1251 | try: |
| 1252 | self.cli = socket.create_connection((HOST, self.port), timeout=None) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1253 | self.addCleanup(self.cli.close) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1254 | finally: |
| 1255 | socket.setdefaulttimeout(None) |
| 1256 | self.assertEqual(self.cli.gettimeout(), None) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1257 | |
| 1258 | testTimeoutValueNamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1259 | def _testTimeoutValueNamed(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1260 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1261 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1262 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1263 | testTimeoutValueNonamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1264 | def _testTimeoutValueNonamed(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1265 | self.cli = socket.create_connection((HOST, self.port), 30) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1266 | self.addCleanup(self.cli.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1267 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1268 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1269 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1270 | class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
| 1271 | |
| 1272 | def __init__(self, methodName='runTest'): |
| 1273 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1274 | ThreadableTest.__init__(self) |
| 1275 | |
| 1276 | def clientSetUp(self): |
| 1277 | pass |
| 1278 | |
| 1279 | def clientTearDown(self): |
| 1280 | self.cli.close() |
| 1281 | self.cli = None |
| 1282 | ThreadableTest.clientTearDown(self) |
| 1283 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1284 | def testInsideTimeout(self): |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1285 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1286 | self.addCleanup(conn.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1287 | time.sleep(3) |
| 1288 | conn.send("done!") |
| 1289 | testOutsideTimeout = testInsideTimeout |
| 1290 | |
| 1291 | def _testInsideTimeout(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1292 | self.cli = sock = socket.create_connection((HOST, self.port)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1293 | data = sock.recv(5) |
| 1294 | self.assertEqual(data, "done!") |
| 1295 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1296 | def _testOutsideTimeout(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1297 | self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1298 | self.assertRaises(socket.timeout, lambda: sock.recv(5)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1299 | |
| 1300 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1301 | class Urllib2FileobjectTest(unittest.TestCase): |
| 1302 | |
| 1303 | # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that |
| 1304 | # it close the socket if the close c'tor argument is true |
| 1305 | |
| 1306 | def testClose(self): |
| 1307 | class MockSocket: |
| 1308 | closed = False |
| 1309 | def flush(self): pass |
| 1310 | def close(self): self.closed = True |
| 1311 | |
| 1312 | # must not close unless we request it: the original use of _fileobject |
| 1313 | # by module socket requires that the underlying socket not be closed until |
| 1314 | # the _socketobject that created the _fileobject is closed |
| 1315 | s = MockSocket() |
| 1316 | f = socket._fileobject(s) |
| 1317 | f.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1318 | self.assertTrue(not s.closed) |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1319 | |
| 1320 | s = MockSocket() |
| 1321 | f = socket._fileobject(s, close=True) |
| 1322 | f.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1323 | self.assertTrue(s.closed) |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1324 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1325 | class TCPTimeoutTest(SocketTCPTest): |
| 1326 | |
| 1327 | def testTCPTimeout(self): |
| 1328 | def raise_timeout(*args, **kwargs): |
| 1329 | self.serv.settimeout(1.0) |
| 1330 | self.serv.accept() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1331 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1332 | "Error generating a timeout exception (TCP)") |
| 1333 | |
| 1334 | def testTimeoutZero(self): |
| 1335 | ok = False |
| 1336 | try: |
| 1337 | self.serv.settimeout(0.0) |
| 1338 | foo = self.serv.accept() |
| 1339 | except socket.timeout: |
| 1340 | self.fail("caught timeout instead of error (TCP)") |
| 1341 | except socket.error: |
| 1342 | ok = True |
| 1343 | except: |
| 1344 | self.fail("caught unexpected exception (TCP)") |
| 1345 | if not ok: |
| 1346 | self.fail("accept() returned success when we did not expect it") |
| 1347 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1348 | def testInterruptedTimeout(self): |
| 1349 | # XXX I don't know how to do this test on MSWindows or any other |
| 1350 | # plaform that doesn't support signal.alarm() or os.kill(), though |
| 1351 | # the bug should have existed on all platforms. |
| 1352 | if not hasattr(signal, "alarm"): |
| 1353 | return # can only test on *nix |
| 1354 | self.serv.settimeout(5.0) # must be longer than alarm |
| 1355 | class Alarm(Exception): |
| 1356 | pass |
| 1357 | def alarm_handler(signal, frame): |
| 1358 | raise Alarm |
| 1359 | old_alarm = signal.signal(signal.SIGALRM, alarm_handler) |
| 1360 | try: |
| 1361 | signal.alarm(2) # POSIX allows alarm to be up to 1 second early |
| 1362 | try: |
| 1363 | foo = self.serv.accept() |
| 1364 | except socket.timeout: |
| 1365 | self.fail("caught timeout instead of Alarm") |
| 1366 | except Alarm: |
| 1367 | pass |
| 1368 | except: |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1369 | self.fail("caught other exception instead of Alarm:" |
| 1370 | " %s(%s):\n%s" % |
| 1371 | (sys.exc_info()[:2] + (traceback.format_exc(),))) |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1372 | else: |
| 1373 | self.fail("nothing caught") |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1374 | finally: |
| 1375 | signal.alarm(0) # shut off alarm |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1376 | except Alarm: |
| 1377 | self.fail("got Alarm in wrong place") |
| 1378 | finally: |
| 1379 | # no alarm can be pending. Safe to restore old handler. |
| 1380 | signal.signal(signal.SIGALRM, old_alarm) |
| 1381 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1382 | class UDPTimeoutTest(SocketTCPTest): |
| 1383 | |
| 1384 | def testUDPTimeout(self): |
| 1385 | def raise_timeout(*args, **kwargs): |
| 1386 | self.serv.settimeout(1.0) |
| 1387 | self.serv.recv(1024) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1388 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1389 | "Error generating a timeout exception (UDP)") |
| 1390 | |
| 1391 | def testTimeoutZero(self): |
| 1392 | ok = False |
| 1393 | try: |
| 1394 | self.serv.settimeout(0.0) |
| 1395 | foo = self.serv.recv(1024) |
| 1396 | except socket.timeout: |
| 1397 | self.fail("caught timeout instead of error (UDP)") |
| 1398 | except socket.error: |
| 1399 | ok = True |
| 1400 | except: |
| 1401 | self.fail("caught unexpected exception (UDP)") |
| 1402 | if not ok: |
| 1403 | self.fail("recv() returned success when we did not expect it") |
| 1404 | |
| 1405 | class TestExceptions(unittest.TestCase): |
| 1406 | |
| 1407 | def testExceptionTree(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1408 | self.assertTrue(issubclass(socket.error, Exception)) |
| 1409 | self.assertTrue(issubclass(socket.herror, socket.error)) |
| 1410 | self.assertTrue(issubclass(socket.gaierror, socket.error)) |
| 1411 | self.assertTrue(issubclass(socket.timeout, socket.error)) |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1412 | |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1413 | class TestLinuxAbstractNamespace(unittest.TestCase): |
| 1414 | |
| 1415 | UNIX_PATH_MAX = 108 |
| 1416 | |
| 1417 | def testLinuxAbstractNamespace(self): |
| 1418 | address = "\x00python-test-hello\x00\xff" |
| 1419 | s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1420 | s1.bind(address) |
| 1421 | s1.listen(1) |
| 1422 | s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1423 | s2.connect(s1.getsockname()) |
| 1424 | s1.accept() |
| 1425 | self.assertEqual(s1.getsockname(), address) |
| 1426 | self.assertEqual(s2.getpeername(), address) |
| 1427 | |
| 1428 | def testMaxName(self): |
| 1429 | address = "\x00" + "h" * (self.UNIX_PATH_MAX - 1) |
| 1430 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1431 | s.bind(address) |
| 1432 | self.assertEqual(s.getsockname(), address) |
| 1433 | |
| 1434 | def testNameOverflow(self): |
| 1435 | address = "\x00" + "h" * self.UNIX_PATH_MAX |
| 1436 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1437 | self.assertRaises(socket.error, s.bind, address) |
| 1438 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1439 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1440 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1441 | class BufferIOTest(SocketConnectedTest): |
| 1442 | """ |
| 1443 | Test the buffer versions of socket.recv() and socket.send(). |
| 1444 | """ |
| 1445 | def __init__(self, methodName='runTest'): |
| 1446 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1447 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1448 | def testRecvIntoArray(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1449 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1450 | nbytes = self.cli_conn.recv_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1451 | self.assertEqual(nbytes, len(MSG)) |
| 1452 | msg = buf.tostring()[:len(MSG)] |
| 1453 | self.assertEqual(msg, MSG) |
| 1454 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1455 | def _testRecvIntoArray(self): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 1456 | with test_support.check_py3k_warnings(): |
| 1457 | buf = buffer(MSG) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1458 | self.serv_conn.send(buf) |
| 1459 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1460 | def testRecvIntoBytearray(self): |
| 1461 | buf = bytearray(1024) |
| 1462 | nbytes = self.cli_conn.recv_into(buf) |
| 1463 | self.assertEqual(nbytes, len(MSG)) |
| 1464 | msg = buf[:len(MSG)] |
| 1465 | self.assertEqual(msg, MSG) |
| 1466 | |
| 1467 | _testRecvIntoBytearray = _testRecvIntoArray |
| 1468 | |
| 1469 | def testRecvIntoMemoryview(self): |
| 1470 | buf = bytearray(1024) |
| 1471 | nbytes = self.cli_conn.recv_into(memoryview(buf)) |
| 1472 | self.assertEqual(nbytes, len(MSG)) |
| 1473 | msg = buf[:len(MSG)] |
| 1474 | self.assertEqual(msg, MSG) |
| 1475 | |
| 1476 | _testRecvIntoMemoryview = _testRecvIntoArray |
| 1477 | |
| 1478 | def testRecvFromIntoArray(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1479 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1480 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1481 | self.assertEqual(nbytes, len(MSG)) |
| 1482 | msg = buf.tostring()[:len(MSG)] |
| 1483 | self.assertEqual(msg, MSG) |
| 1484 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1485 | def _testRecvFromIntoArray(self): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 1486 | with test_support.check_py3k_warnings(): |
| 1487 | buf = buffer(MSG) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1488 | self.serv_conn.send(buf) |
| 1489 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1490 | def testRecvFromIntoBytearray(self): |
| 1491 | buf = bytearray(1024) |
| 1492 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
| 1493 | self.assertEqual(nbytes, len(MSG)) |
| 1494 | msg = buf[:len(MSG)] |
| 1495 | self.assertEqual(msg, MSG) |
| 1496 | |
| 1497 | _testRecvFromIntoBytearray = _testRecvFromIntoArray |
| 1498 | |
| 1499 | def testRecvFromIntoMemoryview(self): |
| 1500 | buf = bytearray(1024) |
| 1501 | nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) |
| 1502 | self.assertEqual(nbytes, len(MSG)) |
| 1503 | msg = buf[:len(MSG)] |
| 1504 | self.assertEqual(msg, MSG) |
| 1505 | |
| 1506 | _testRecvFromIntoMemoryview = _testRecvFromIntoArray |
| 1507 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1508 | |
| 1509 | TIPC_STYPE = 2000 |
| 1510 | TIPC_LOWER = 200 |
| 1511 | TIPC_UPPER = 210 |
| 1512 | |
| 1513 | def isTipcAvailable(): |
| 1514 | """Check if the TIPC module is loaded |
| 1515 | |
| 1516 | The TIPC module is not loaded automatically on Ubuntu and probably |
| 1517 | other Linux distros. |
| 1518 | """ |
| 1519 | if not hasattr(socket, "AF_TIPC"): |
| 1520 | return False |
| 1521 | if not os.path.isfile("/proc/modules"): |
| 1522 | return False |
| 1523 | with open("/proc/modules") as f: |
| 1524 | for line in f: |
| 1525 | if line.startswith("tipc "): |
| 1526 | return True |
Christian Heimes | f66f95d | 2008-01-08 03:40:04 +0000 | [diff] [blame] | 1527 | if test_support.verbose: |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1528 | print "TIPC module is not loaded, please 'sudo modprobe tipc'" |
| 1529 | return False |
| 1530 | |
| 1531 | class TIPCTest (unittest.TestCase): |
| 1532 | def testRDM(self): |
| 1533 | srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1534 | cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1535 | |
| 1536 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1537 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1538 | TIPC_LOWER, TIPC_UPPER) |
| 1539 | srv.bind(srvaddr) |
| 1540 | |
| 1541 | sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1542 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1543 | cli.sendto(MSG, sendaddr) |
| 1544 | |
| 1545 | msg, recvaddr = srv.recvfrom(1024) |
| 1546 | |
| 1547 | self.assertEqual(cli.getsockname(), recvaddr) |
| 1548 | self.assertEqual(msg, MSG) |
| 1549 | |
| 1550 | |
| 1551 | class TIPCThreadableTest (unittest.TestCase, ThreadableTest): |
| 1552 | def __init__(self, methodName = 'runTest'): |
| 1553 | unittest.TestCase.__init__(self, methodName = methodName) |
| 1554 | ThreadableTest.__init__(self) |
| 1555 | |
| 1556 | def setUp(self): |
| 1557 | self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1558 | self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1559 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1560 | TIPC_LOWER, TIPC_UPPER) |
| 1561 | self.srv.bind(srvaddr) |
| 1562 | self.srv.listen(5) |
| 1563 | self.serverExplicitReady() |
| 1564 | self.conn, self.connaddr = self.srv.accept() |
| 1565 | |
| 1566 | def clientSetUp(self): |
| 1567 | # The is a hittable race between serverExplicitReady() and the |
| 1568 | # accept() call; sleep a little while to avoid it, otherwise |
| 1569 | # we could get an exception |
| 1570 | time.sleep(0.1) |
| 1571 | self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1572 | addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1573 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1574 | self.cli.connect(addr) |
| 1575 | self.cliaddr = self.cli.getsockname() |
| 1576 | |
| 1577 | def testStream(self): |
| 1578 | msg = self.conn.recv(1024) |
| 1579 | self.assertEqual(msg, MSG) |
| 1580 | self.assertEqual(self.cliaddr, self.connaddr) |
| 1581 | |
| 1582 | def _testStream(self): |
| 1583 | self.cli.send(MSG) |
| 1584 | self.cli.close() |
| 1585 | |
| 1586 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1587 | def test_main(): |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 1588 | tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
Ronald Oussoren | 9545a23 | 2010-05-05 19:09:31 +0000 | [diff] [blame] | 1589 | TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, |
| 1590 | UDPTimeoutTest ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1591 | |
| 1592 | tests.extend([ |
| 1593 | NonBlockingTCPTests, |
| 1594 | FileObjectClassTestCase, |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1595 | FileObjectInterruptedTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1596 | UnbufferedFileObjectClassTestCase, |
| 1597 | LineBufferedFileObjectClassTestCase, |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1598 | SmallBufferedFileObjectClassTestCase, |
| 1599 | Urllib2FileobjectTest, |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1600 | NetworkConnectionNoServer, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1601 | NetworkConnectionAttributesTest, |
| 1602 | NetworkConnectionBehaviourTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1603 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 1604 | if hasattr(socket, "socketpair"): |
| 1605 | tests.append(BasicSocketPairTest) |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1606 | if sys.platform == 'linux2': |
| 1607 | tests.append(TestLinuxAbstractNamespace) |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1608 | if isTipcAvailable(): |
| 1609 | tests.append(TIPCTest) |
Christian Heimes | 4d7e670 | 2008-01-07 19:58:41 +0000 | [diff] [blame] | 1610 | tests.append(TIPCThreadableTest) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1611 | |
| 1612 | thread_info = test_support.threading_setup() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1613 | test_support.run_unittest(*tests) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1614 | test_support.threading_cleanup(*thread_info) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1615 | |
| 1616 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1617 | test_main() |