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 | |
Charles-François Natali | 3aa59e3 | 2012-01-02 15:38:27 +0100 | [diff] [blame] | 709 | @unittest.skipUnless(SUPPORTS_IPV6, 'IPv6 required for this test.') |
| 710 | def test_flowinfo(self): |
| 711 | self.assertRaises(OverflowError, socket.getnameinfo, |
| 712 | ('::1',0, 0xffffffff), 0) |
| 713 | s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) |
| 714 | try: |
| 715 | self.assertRaises(OverflowError, s.bind, ('::1', 0, -10)) |
| 716 | finally: |
| 717 | s.close() |
| 718 | |
Antoine Pitrou | 66b5df6 | 2010-09-27 18:16:46 +0000 | [diff] [blame] | 719 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 720 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 721 | class BasicTCPTest(SocketConnectedTest): |
| 722 | |
| 723 | def __init__(self, methodName='runTest'): |
| 724 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 725 | |
| 726 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 727 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 728 | msg = self.cli_conn.recv(1024) |
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 _testRecv(self): |
| 732 | self.serv_conn.send(MSG) |
| 733 | |
| 734 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 735 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 736 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 737 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 738 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 739 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 740 | |
| 741 | def _testOverFlowRecv(self): |
| 742 | self.serv_conn.send(MSG) |
| 743 | |
| 744 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 745 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 746 | msg, addr = self.cli_conn.recvfrom(1024) |
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 _testRecvFrom(self): |
| 750 | self.serv_conn.send(MSG) |
| 751 | |
| 752 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 753 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 754 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 755 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 756 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 757 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 758 | |
| 759 | def _testOverFlowRecvFrom(self): |
| 760 | self.serv_conn.send(MSG) |
| 761 | |
| 762 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 763 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 764 | msg = '' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 765 | while 1: |
| 766 | read = self.cli_conn.recv(1024) |
| 767 | if not read: |
| 768 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 769 | msg += read |
| 770 | self.assertEqual(msg, 'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 771 | |
| 772 | def _testSendAll(self): |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 773 | big_chunk = 'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 774 | self.serv_conn.sendall(big_chunk) |
| 775 | |
| 776 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 777 | # Testing fromfd() |
Guido van Rossum | 8e95ca8 | 2002-06-12 20:55:17 +0000 | [diff] [blame] | 778 | if not hasattr(socket, "fromfd"): |
Guido van Rossum | 6fb3d5e | 2002-06-12 20:48:59 +0000 | [diff] [blame] | 779 | return # On Windows, this doesn't exist |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 780 | fd = self.cli_conn.fileno() |
| 781 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 782 | self.addCleanup(sock.close) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 783 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 784 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 785 | |
| 786 | def _testFromFd(self): |
| 787 | self.serv_conn.send(MSG) |
| 788 | |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 789 | def testDup(self): |
| 790 | # Testing dup() |
| 791 | sock = self.cli_conn.dup() |
| 792 | self.addCleanup(sock.close) |
| 793 | msg = sock.recv(1024) |
| 794 | self.assertEqual(msg, MSG) |
| 795 | |
| 796 | def _testDup(self): |
| 797 | self.serv_conn.send(MSG) |
| 798 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 799 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 800 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 801 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 802 | self.assertEqual(msg, MSG) |
Mark Dickinson | e82cdae | 2009-01-15 14:54:37 +0000 | [diff] [blame] | 803 | # wait for _testShutdown to finish: on OS X, when the server |
| 804 | # closes the connection the client also becomes disconnected, |
Mark Dickinson | 0e15182 | 2009-01-15 14:58:28 +0000 | [diff] [blame] | 805 | # and the client's shutdown call will fail. (Issue #4397.) |
Mark Dickinson | e82cdae | 2009-01-15 14:54:37 +0000 | [diff] [blame] | 806 | self.done.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 807 | |
| 808 | def _testShutdown(self): |
| 809 | self.serv_conn.send(MSG) |
| 810 | self.serv_conn.shutdown(2) |
| 811 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 812 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 813 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 814 | |
| 815 | def __init__(self, methodName='runTest'): |
| 816 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 817 | |
| 818 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 819 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 820 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 821 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 822 | |
| 823 | def _testSendtoAndRecv(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 824 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 825 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 826 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 827 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 828 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 829 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 831 | def _testRecvFrom(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 832 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 833 | |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 834 | def testRecvFromNegative(self): |
| 835 | # Negative lengths passed to recvfrom should give ValueError. |
| 836 | self.assertRaises(ValueError, self.serv.recvfrom, -1) |
| 837 | |
| 838 | def _testRecvFromNegative(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 839 | self.cli.sendto(MSG, 0, (HOST, self.port)) |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 840 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 841 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 842 | class TCPCloserTest(ThreadedTCPSocketTest): |
| 843 | |
| 844 | def testClose(self): |
| 845 | conn, addr = self.serv.accept() |
| 846 | conn.close() |
| 847 | |
| 848 | sd = self.cli |
| 849 | read, write, err = select.select([sd], [], [], 1.0) |
| 850 | self.assertEqual(read, [sd]) |
| 851 | self.assertEqual(sd.recv(1), '') |
| 852 | |
| 853 | def _testClose(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 854 | self.cli.connect((HOST, self.port)) |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 855 | time.sleep(1.0) |
| 856 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 857 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 858 | class BasicSocketPairTest(SocketPairTest): |
| 859 | |
| 860 | def __init__(self, methodName='runTest'): |
| 861 | SocketPairTest.__init__(self, methodName=methodName) |
| 862 | |
| 863 | def testRecv(self): |
| 864 | msg = self.serv.recv(1024) |
| 865 | self.assertEqual(msg, MSG) |
| 866 | |
| 867 | def _testRecv(self): |
| 868 | self.cli.send(MSG) |
| 869 | |
| 870 | def testSend(self): |
| 871 | self.serv.send(MSG) |
| 872 | |
| 873 | def _testSend(self): |
| 874 | msg = self.cli.recv(1024) |
| 875 | self.assertEqual(msg, MSG) |
| 876 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 877 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 878 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 879 | |
| 880 | def __init__(self, methodName='runTest'): |
| 881 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 882 | |
| 883 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 884 | # Testing whether set blocking works |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 885 | self.serv.setblocking(0) |
| 886 | start = time.time() |
| 887 | try: |
| 888 | self.serv.accept() |
| 889 | except socket.error: |
| 890 | pass |
| 891 | end = time.time() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 892 | self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 893 | |
| 894 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 895 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 896 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 897 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 898 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 899 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 900 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 901 | conn, addr = self.serv.accept() |
| 902 | except socket.error: |
| 903 | pass |
| 904 | else: |
| 905 | self.fail("Error trying to do non-blocking accept.") |
| 906 | read, write, err = select.select([self.serv], [], []) |
| 907 | if self.serv in read: |
| 908 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 909 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 910 | else: |
| 911 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 912 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 913 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 914 | time.sleep(0.1) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 915 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 916 | |
| 917 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 918 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 919 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 920 | conn.close() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 921 | |
| 922 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 923 | self.cli.settimeout(10) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 924 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 925 | |
| 926 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 927 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 928 | conn, addr = self.serv.accept() |
| 929 | conn.setblocking(0) |
| 930 | try: |
| 931 | msg = conn.recv(len(MSG)) |
| 932 | except socket.error: |
| 933 | pass |
| 934 | else: |
| 935 | self.fail("Error trying to do non-blocking recv.") |
| 936 | read, write, err = select.select([conn], [], []) |
| 937 | if conn in read: |
| 938 | msg = conn.recv(len(MSG)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 939 | conn.close() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 940 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 941 | else: |
| 942 | self.fail("Error during select call to non-blocking socket.") |
| 943 | |
| 944 | def _testRecv(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 945 | self.cli.connect((HOST, self.port)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 946 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 947 | self.cli.send(MSG) |
| 948 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 949 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 950 | class FileObjectClassTestCase(SocketConnectedTest): |
| 951 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 952 | bufsize = -1 # Use default buffer size |
| 953 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 954 | def __init__(self, methodName='runTest'): |
| 955 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 956 | |
| 957 | def setUp(self): |
| 958 | SocketConnectedTest.setUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 959 | self.serv_file = self.cli_conn.makefile('rb', self.bufsize) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 960 | |
| 961 | def tearDown(self): |
| 962 | self.serv_file.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 963 | self.assertTrue(self.serv_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 964 | self.serv_file = None |
| 965 | SocketConnectedTest.tearDown(self) |
| 966 | |
| 967 | def clientSetUp(self): |
| 968 | SocketConnectedTest.clientSetUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 969 | self.cli_file = self.serv_conn.makefile('wb') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 970 | |
| 971 | def clientTearDown(self): |
| 972 | self.cli_file.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 973 | self.assertTrue(self.cli_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 974 | self.cli_file = None |
| 975 | SocketConnectedTest.clientTearDown(self) |
| 976 | |
| 977 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 978 | # Performing small file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 979 | first_seg = self.serv_file.read(len(MSG)-3) |
| 980 | second_seg = self.serv_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 981 | msg = first_seg + second_seg |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 982 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 983 | |
| 984 | def _testSmallRead(self): |
| 985 | self.cli_file.write(MSG) |
| 986 | self.cli_file.flush() |
| 987 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 988 | def testFullRead(self): |
| 989 | # read until EOF |
| 990 | msg = self.serv_file.read() |
| 991 | self.assertEqual(msg, MSG) |
| 992 | |
| 993 | def _testFullRead(self): |
| 994 | self.cli_file.write(MSG) |
| 995 | self.cli_file.close() |
| 996 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 997 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 998 | # Performing unbuffered file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 999 | buf = '' |
| 1000 | while 1: |
| 1001 | char = self.serv_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1002 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1003 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1004 | buf += char |
| 1005 | self.assertEqual(buf, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1006 | |
| 1007 | def _testUnbufferedRead(self): |
| 1008 | self.cli_file.write(MSG) |
| 1009 | self.cli_file.flush() |
| 1010 | |
| 1011 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1012 | # Performing file readline test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1013 | line = self.serv_file.readline() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 1014 | self.assertEqual(line, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1015 | |
| 1016 | def _testReadline(self): |
| 1017 | self.cli_file.write(MSG) |
| 1018 | self.cli_file.flush() |
| 1019 | |
Gregory P. Smith | 24237ea | 2008-05-05 21:53:45 +0000 | [diff] [blame] | 1020 | def testReadlineAfterRead(self): |
| 1021 | a_baloo_is = self.serv_file.read(len("A baloo is")) |
| 1022 | self.assertEqual("A baloo is", a_baloo_is) |
| 1023 | _a_bear = self.serv_file.read(len(" a bear")) |
| 1024 | self.assertEqual(" a bear", _a_bear) |
| 1025 | line = self.serv_file.readline() |
| 1026 | self.assertEqual("\n", line) |
| 1027 | line = self.serv_file.readline() |
| 1028 | self.assertEqual("A BALOO IS A BEAR.\n", line) |
| 1029 | line = self.serv_file.readline() |
| 1030 | self.assertEqual(MSG, line) |
| 1031 | |
| 1032 | def _testReadlineAfterRead(self): |
| 1033 | self.cli_file.write("A baloo is a bear\n") |
| 1034 | self.cli_file.write("A BALOO IS A BEAR.\n") |
| 1035 | self.cli_file.write(MSG) |
| 1036 | self.cli_file.flush() |
| 1037 | |
| 1038 | def testReadlineAfterReadNoNewline(self): |
| 1039 | end_of_ = self.serv_file.read(len("End Of ")) |
| 1040 | self.assertEqual("End Of ", end_of_) |
| 1041 | line = self.serv_file.readline() |
| 1042 | self.assertEqual("Line", line) |
| 1043 | |
| 1044 | def _testReadlineAfterReadNoNewline(self): |
| 1045 | self.cli_file.write("End Of Line") |
| 1046 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1047 | def testClosedAttr(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1048 | self.assertTrue(not self.serv_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1049 | |
| 1050 | def _testClosedAttr(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1051 | self.assertTrue(not self.cli_file.closed) |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 1052 | |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1053 | |
| 1054 | class FileObjectInterruptedTestCase(unittest.TestCase): |
| 1055 | """Test that the file object correctly handles EINTR internally.""" |
| 1056 | |
| 1057 | class MockSocket(object): |
| 1058 | def __init__(self, recv_funcs=()): |
| 1059 | # A generator that returns callables that we'll call for each |
| 1060 | # call to recv(). |
| 1061 | self._recv_step = iter(recv_funcs) |
| 1062 | |
| 1063 | def recv(self, size): |
| 1064 | return self._recv_step.next()() |
| 1065 | |
| 1066 | @staticmethod |
| 1067 | def _raise_eintr(): |
| 1068 | raise socket.error(errno.EINTR) |
| 1069 | |
| 1070 | def _test_readline(self, size=-1, **kwargs): |
| 1071 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1072 | lambda : "This is the first line\nAnd the sec", |
| 1073 | self._raise_eintr, |
| 1074 | lambda : "ond line is here\n", |
| 1075 | lambda : "", |
| 1076 | ]) |
| 1077 | fo = socket._fileobject(mock_sock, **kwargs) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1078 | self.assertEqual(fo.readline(size), "This is the first line\n") |
| 1079 | 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] | 1080 | |
| 1081 | def _test_read(self, size=-1, **kwargs): |
| 1082 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1083 | lambda : "This is the first line\nAnd the sec", |
| 1084 | self._raise_eintr, |
| 1085 | lambda : "ond line is here\n", |
| 1086 | lambda : "", |
| 1087 | ]) |
| 1088 | fo = socket._fileobject(mock_sock, **kwargs) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1089 | self.assertEqual(fo.read(size), "This is the first line\n" |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1090 | "And the second line is here\n") |
| 1091 | |
| 1092 | def test_default(self): |
| 1093 | self._test_readline() |
| 1094 | self._test_readline(size=100) |
| 1095 | self._test_read() |
| 1096 | self._test_read(size=100) |
| 1097 | |
| 1098 | def test_with_1k_buffer(self): |
| 1099 | self._test_readline(bufsize=1024) |
| 1100 | self._test_readline(size=100, bufsize=1024) |
| 1101 | self._test_read(bufsize=1024) |
| 1102 | self._test_read(size=100, bufsize=1024) |
| 1103 | |
| 1104 | def _test_readline_no_buffer(self, size=-1): |
| 1105 | mock_sock = self.MockSocket(recv_funcs=[ |
| 1106 | lambda : "aa", |
| 1107 | lambda : "\n", |
| 1108 | lambda : "BB", |
| 1109 | self._raise_eintr, |
| 1110 | lambda : "bb", |
| 1111 | lambda : "", |
| 1112 | ]) |
| 1113 | fo = socket._fileobject(mock_sock, bufsize=0) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1114 | self.assertEqual(fo.readline(size), "aa\n") |
| 1115 | self.assertEqual(fo.readline(size), "BBbb") |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1116 | |
| 1117 | def test_no_buffer(self): |
| 1118 | self._test_readline_no_buffer() |
| 1119 | self._test_readline_no_buffer(size=4) |
| 1120 | self._test_read(bufsize=0) |
| 1121 | self._test_read(size=100, bufsize=0) |
| 1122 | |
| 1123 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1124 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1125 | |
| 1126 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1128 | In this case (and in this case only), it should be possible to |
| 1129 | create a file object, read a line from it, create another file |
| 1130 | object, read another line from it, without loss of data in the |
| 1131 | first file object's buffer. Note that httplib relies on this |
| 1132 | when reading multiple requests from the same socket.""" |
| 1133 | |
| 1134 | bufsize = 0 # Use unbuffered mode |
| 1135 | |
| 1136 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1137 | # 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] | 1138 | line = self.serv_file.readline() # first line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1139 | self.assertEqual(line, "A. " + MSG) # first line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1140 | self.serv_file = self.cli_conn.makefile('rb', 0) |
| 1141 | line = self.serv_file.readline() # second line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1142 | self.assertEqual(line, "B. " + MSG) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1143 | |
| 1144 | def _testUnbufferedReadline(self): |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 1145 | self.cli_file.write("A. " + MSG) |
| 1146 | self.cli_file.write("B. " + MSG) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1147 | self.cli_file.flush() |
| 1148 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 1149 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1150 | |
| 1151 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 1152 | |
| 1153 | |
| 1154 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 1155 | |
| 1156 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 1157 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1158 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1159 | class NetworkConnectionTest(object): |
| 1160 | """Prove network connection.""" |
| 1161 | def clientSetUp(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1162 | # We're inherited below by BasicTCPTest2, which also inherits |
| 1163 | # BasicTCPTest, which defines self.port referenced below. |
| 1164 | self.cli = socket.create_connection((HOST, self.port)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1165 | self.serv_conn = self.cli |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1166 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1167 | class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
| 1168 | """Tests that NetworkConnection does not break existing TCP functionality. |
| 1169 | """ |
| 1170 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1171 | class NetworkConnectionNoServer(unittest.TestCase): |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1172 | class MockSocket(socket.socket): |
| 1173 | def connect(self, *args): |
| 1174 | raise socket.timeout('timed out') |
| 1175 | |
| 1176 | @contextlib.contextmanager |
| 1177 | def mocked_socket_module(self): |
| 1178 | """Return a socket which times out on connect""" |
| 1179 | old_socket = socket.socket |
| 1180 | socket.socket = self.MockSocket |
| 1181 | try: |
| 1182 | yield |
| 1183 | finally: |
| 1184 | socket.socket = old_socket |
| 1185 | |
| 1186 | def test_connect(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1187 | port = test_support.find_unused_port() |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1188 | cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1189 | self.addCleanup(cli.close) |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1190 | with self.assertRaises(socket.error) as cm: |
| 1191 | cli.connect((HOST, port)) |
| 1192 | self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) |
| 1193 | |
| 1194 | def test_create_connection(self): |
| 1195 | # Issue #9792: errors raised by create_connection() should have |
| 1196 | # a proper errno attribute. |
| 1197 | port = test_support.find_unused_port() |
| 1198 | with self.assertRaises(socket.error) as cm: |
| 1199 | socket.create_connection((HOST, port)) |
Trent Nelson | 6a7f867 | 2012-10-18 01:35:32 -0400 | [diff] [blame^] | 1200 | |
| 1201 | # Issue #16257: create_connection() calls getaddrinfo() against |
| 1202 | # 'localhost'. This may result in an IPV6 addr being returned |
| 1203 | # as well as an IPV4 one: |
| 1204 | # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM) |
| 1205 | # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)), |
| 1206 | # (26, 2, 0, '', ('::1', 41230, 0, 0))] |
| 1207 | # |
| 1208 | # create_connection() enumerates through all the addresses returned |
| 1209 | # and if it doesn't successfully bind to any of them, it propagates |
| 1210 | # the last exception it encountered. |
| 1211 | # |
| 1212 | # On Solaris, ENETUNREACH is returned in this circumstance instead |
| 1213 | # of ECONNREFUSED. So, if that errno exists, add it to our list of |
| 1214 | # expected errnos. |
| 1215 | expected_errnos = [ errno.ECONNREFUSED, ] |
| 1216 | if hasattr(errno, 'ENETUNREACH'): |
| 1217 | expected_errnos.append(errno.ENETUNREACH) |
| 1218 | |
| 1219 | self.assertIn(cm.exception.errno, expected_errnos) |
Antoine Pitrou | c818ed4 | 2010-09-07 21:40:25 +0000 | [diff] [blame] | 1220 | |
| 1221 | def test_create_connection_timeout(self): |
| 1222 | # Issue #9792: create_connection() should not recast timeout errors |
| 1223 | # as generic socket errors. |
| 1224 | with self.mocked_socket_module(): |
| 1225 | with self.assertRaises(socket.timeout): |
| 1226 | socket.create_connection((HOST, 1234)) |
| 1227 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1228 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1229 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1230 | class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
| 1231 | |
| 1232 | def __init__(self, methodName='runTest'): |
| 1233 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1234 | ThreadableTest.__init__(self) |
| 1235 | |
| 1236 | def clientSetUp(self): |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1237 | self.source_port = test_support.find_unused_port() |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1238 | |
| 1239 | def clientTearDown(self): |
| 1240 | self.cli.close() |
| 1241 | self.cli = None |
| 1242 | ThreadableTest.clientTearDown(self) |
| 1243 | |
| 1244 | def _justAccept(self): |
| 1245 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1246 | conn.close() |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1247 | |
| 1248 | testFamily = _justAccept |
| 1249 | def _testFamily(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1250 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1251 | self.addCleanup(self.cli.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1252 | self.assertEqual(self.cli.family, 2) |
| 1253 | |
Gregory P. Smith | 49d709c | 2010-01-03 15:05:52 +0000 | [diff] [blame] | 1254 | testSourceAddress = _justAccept |
| 1255 | def _testSourceAddress(self): |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1256 | self.cli = socket.create_connection((HOST, self.port), timeout=30, |
| 1257 | source_address=('', self.source_port)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1258 | self.addCleanup(self.cli.close) |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1259 | self.assertEqual(self.cli.getsockname()[1], self.source_port) |
Gregory P. Smith | 49d709c | 2010-01-03 15:05:52 +0000 | [diff] [blame] | 1260 | # The port number being used is sufficient to show that the bind() |
| 1261 | # call happened. |
Gregory P. Smith | 79a3eb1 | 2010-01-03 01:29:44 +0000 | [diff] [blame] | 1262 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1263 | testTimeoutDefault = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1264 | def _testTimeoutDefault(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1265 | # passing no explicit timeout uses socket's global default |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1266 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1267 | socket.setdefaulttimeout(42) |
| 1268 | try: |
| 1269 | self.cli = socket.create_connection((HOST, self.port)) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1270 | self.addCleanup(self.cli.close) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1271 | finally: |
| 1272 | socket.setdefaulttimeout(None) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 1273 | self.assertEqual(self.cli.gettimeout(), 42) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1274 | |
| 1275 | testTimeoutNone = _justAccept |
| 1276 | def _testTimeoutNone(self): |
| 1277 | # None timeout means the same as sock.settimeout(None) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1278 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1279 | socket.setdefaulttimeout(30) |
| 1280 | try: |
| 1281 | self.cli = socket.create_connection((HOST, self.port), timeout=None) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1282 | self.addCleanup(self.cli.close) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 1283 | finally: |
| 1284 | socket.setdefaulttimeout(None) |
| 1285 | self.assertEqual(self.cli.gettimeout(), None) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1286 | |
| 1287 | testTimeoutValueNamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1288 | def _testTimeoutValueNamed(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1289 | self.cli = socket.create_connection((HOST, self.port), timeout=30) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1290 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1291 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1292 | testTimeoutValueNonamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1293 | def _testTimeoutValueNonamed(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1294 | self.cli = socket.create_connection((HOST, self.port), 30) |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1295 | self.addCleanup(self.cli.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1296 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1297 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1298 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1299 | class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
| 1300 | |
| 1301 | def __init__(self, methodName='runTest'): |
| 1302 | SocketTCPTest.__init__(self, methodName=methodName) |
| 1303 | ThreadableTest.__init__(self) |
| 1304 | |
| 1305 | def clientSetUp(self): |
| 1306 | pass |
| 1307 | |
| 1308 | def clientTearDown(self): |
| 1309 | self.cli.close() |
| 1310 | self.cli = None |
| 1311 | ThreadableTest.clientTearDown(self) |
| 1312 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1313 | def testInsideTimeout(self): |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1314 | conn, addr = self.serv.accept() |
Brian Curtin | 62c20b6 | 2010-11-02 02:59:55 +0000 | [diff] [blame] | 1315 | self.addCleanup(conn.close) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1316 | time.sleep(3) |
| 1317 | conn.send("done!") |
| 1318 | testOutsideTimeout = testInsideTimeout |
| 1319 | |
| 1320 | def _testInsideTimeout(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1321 | self.cli = sock = socket.create_connection((HOST, self.port)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1322 | data = sock.recv(5) |
| 1323 | self.assertEqual(data, "done!") |
| 1324 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1325 | def _testOutsideTimeout(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 1326 | self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1327 | self.assertRaises(socket.timeout, lambda: sock.recv(5)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1328 | |
| 1329 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1330 | class Urllib2FileobjectTest(unittest.TestCase): |
| 1331 | |
| 1332 | # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that |
| 1333 | # it close the socket if the close c'tor argument is true |
| 1334 | |
| 1335 | def testClose(self): |
| 1336 | class MockSocket: |
| 1337 | closed = False |
| 1338 | def flush(self): pass |
| 1339 | def close(self): self.closed = True |
| 1340 | |
| 1341 | # must not close unless we request it: the original use of _fileobject |
| 1342 | # by module socket requires that the underlying socket not be closed until |
| 1343 | # the _socketobject that created the _fileobject is closed |
| 1344 | s = MockSocket() |
| 1345 | f = socket._fileobject(s) |
| 1346 | f.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1347 | self.assertTrue(not s.closed) |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1348 | |
| 1349 | s = MockSocket() |
| 1350 | f = socket._fileobject(s, close=True) |
| 1351 | f.close() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1352 | self.assertTrue(s.closed) |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1353 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1354 | class TCPTimeoutTest(SocketTCPTest): |
| 1355 | |
| 1356 | def testTCPTimeout(self): |
| 1357 | def raise_timeout(*args, **kwargs): |
| 1358 | self.serv.settimeout(1.0) |
| 1359 | self.serv.accept() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1360 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1361 | "Error generating a timeout exception (TCP)") |
| 1362 | |
| 1363 | def testTimeoutZero(self): |
| 1364 | ok = False |
| 1365 | try: |
| 1366 | self.serv.settimeout(0.0) |
| 1367 | foo = self.serv.accept() |
| 1368 | except socket.timeout: |
| 1369 | self.fail("caught timeout instead of error (TCP)") |
| 1370 | except socket.error: |
| 1371 | ok = True |
| 1372 | except: |
| 1373 | self.fail("caught unexpected exception (TCP)") |
| 1374 | if not ok: |
| 1375 | self.fail("accept() returned success when we did not expect it") |
| 1376 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1377 | def testInterruptedTimeout(self): |
| 1378 | # XXX I don't know how to do this test on MSWindows or any other |
| 1379 | # plaform that doesn't support signal.alarm() or os.kill(), though |
| 1380 | # the bug should have existed on all platforms. |
| 1381 | if not hasattr(signal, "alarm"): |
| 1382 | return # can only test on *nix |
| 1383 | self.serv.settimeout(5.0) # must be longer than alarm |
| 1384 | class Alarm(Exception): |
| 1385 | pass |
| 1386 | def alarm_handler(signal, frame): |
| 1387 | raise Alarm |
| 1388 | old_alarm = signal.signal(signal.SIGALRM, alarm_handler) |
| 1389 | try: |
| 1390 | signal.alarm(2) # POSIX allows alarm to be up to 1 second early |
| 1391 | try: |
| 1392 | foo = self.serv.accept() |
| 1393 | except socket.timeout: |
| 1394 | self.fail("caught timeout instead of Alarm") |
| 1395 | except Alarm: |
| 1396 | pass |
| 1397 | except: |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1398 | self.fail("caught other exception instead of Alarm:" |
| 1399 | " %s(%s):\n%s" % |
| 1400 | (sys.exc_info()[:2] + (traceback.format_exc(),))) |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1401 | else: |
| 1402 | self.fail("nothing caught") |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1403 | finally: |
| 1404 | signal.alarm(0) # shut off alarm |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1405 | except Alarm: |
| 1406 | self.fail("got Alarm in wrong place") |
| 1407 | finally: |
| 1408 | # no alarm can be pending. Safe to restore old handler. |
| 1409 | signal.signal(signal.SIGALRM, old_alarm) |
| 1410 | |
Ezio Melotti | 169ed59 | 2011-11-18 18:33:14 +0200 | [diff] [blame] | 1411 | class UDPTimeoutTest(SocketUDPTest): |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1412 | |
| 1413 | def testUDPTimeout(self): |
| 1414 | def raise_timeout(*args, **kwargs): |
| 1415 | self.serv.settimeout(1.0) |
| 1416 | self.serv.recv(1024) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1417 | self.assertRaises(socket.timeout, raise_timeout, |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1418 | "Error generating a timeout exception (UDP)") |
| 1419 | |
| 1420 | def testTimeoutZero(self): |
| 1421 | ok = False |
| 1422 | try: |
| 1423 | self.serv.settimeout(0.0) |
| 1424 | foo = self.serv.recv(1024) |
| 1425 | except socket.timeout: |
| 1426 | self.fail("caught timeout instead of error (UDP)") |
| 1427 | except socket.error: |
| 1428 | ok = True |
| 1429 | except: |
| 1430 | self.fail("caught unexpected exception (UDP)") |
| 1431 | if not ok: |
| 1432 | self.fail("recv() returned success when we did not expect it") |
| 1433 | |
| 1434 | class TestExceptions(unittest.TestCase): |
| 1435 | |
| 1436 | def testExceptionTree(self): |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 1437 | self.assertTrue(issubclass(socket.error, Exception)) |
| 1438 | self.assertTrue(issubclass(socket.herror, socket.error)) |
| 1439 | self.assertTrue(issubclass(socket.gaierror, socket.error)) |
| 1440 | self.assertTrue(issubclass(socket.timeout, socket.error)) |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1441 | |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1442 | class TestLinuxAbstractNamespace(unittest.TestCase): |
| 1443 | |
| 1444 | UNIX_PATH_MAX = 108 |
| 1445 | |
| 1446 | def testLinuxAbstractNamespace(self): |
| 1447 | address = "\x00python-test-hello\x00\xff" |
| 1448 | s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1449 | s1.bind(address) |
| 1450 | s1.listen(1) |
| 1451 | s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1452 | s2.connect(s1.getsockname()) |
| 1453 | s1.accept() |
| 1454 | self.assertEqual(s1.getsockname(), address) |
| 1455 | self.assertEqual(s2.getpeername(), address) |
| 1456 | |
| 1457 | def testMaxName(self): |
| 1458 | address = "\x00" + "h" * (self.UNIX_PATH_MAX - 1) |
| 1459 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1460 | s.bind(address) |
| 1461 | self.assertEqual(s.getsockname(), address) |
| 1462 | |
| 1463 | def testNameOverflow(self): |
| 1464 | address = "\x00" + "h" * self.UNIX_PATH_MAX |
| 1465 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1466 | self.assertRaises(socket.error, s.bind, address) |
| 1467 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1468 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame] | 1469 | @unittest.skipUnless(thread, 'Threading required for this test.') |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1470 | class BufferIOTest(SocketConnectedTest): |
| 1471 | """ |
| 1472 | Test the buffer versions of socket.recv() and socket.send(). |
| 1473 | """ |
| 1474 | def __init__(self, methodName='runTest'): |
| 1475 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1476 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1477 | def testRecvIntoArray(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1478 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1479 | nbytes = self.cli_conn.recv_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1480 | self.assertEqual(nbytes, len(MSG)) |
| 1481 | msg = buf.tostring()[:len(MSG)] |
| 1482 | self.assertEqual(msg, MSG) |
| 1483 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1484 | def _testRecvIntoArray(self): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 1485 | with test_support.check_py3k_warnings(): |
| 1486 | buf = buffer(MSG) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1487 | self.serv_conn.send(buf) |
| 1488 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1489 | def testRecvIntoBytearray(self): |
| 1490 | buf = bytearray(1024) |
| 1491 | nbytes = self.cli_conn.recv_into(buf) |
| 1492 | self.assertEqual(nbytes, len(MSG)) |
| 1493 | msg = buf[:len(MSG)] |
| 1494 | self.assertEqual(msg, MSG) |
| 1495 | |
| 1496 | _testRecvIntoBytearray = _testRecvIntoArray |
| 1497 | |
| 1498 | def testRecvIntoMemoryview(self): |
| 1499 | buf = bytearray(1024) |
| 1500 | nbytes = self.cli_conn.recv_into(memoryview(buf)) |
| 1501 | self.assertEqual(nbytes, len(MSG)) |
| 1502 | msg = buf[:len(MSG)] |
| 1503 | self.assertEqual(msg, MSG) |
| 1504 | |
| 1505 | _testRecvIntoMemoryview = _testRecvIntoArray |
| 1506 | |
| 1507 | def testRecvFromIntoArray(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1508 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1509 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1510 | self.assertEqual(nbytes, len(MSG)) |
| 1511 | msg = buf.tostring()[:len(MSG)] |
| 1512 | self.assertEqual(msg, MSG) |
| 1513 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1514 | def _testRecvFromIntoArray(self): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 1515 | with test_support.check_py3k_warnings(): |
| 1516 | buf = buffer(MSG) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1517 | self.serv_conn.send(buf) |
| 1518 | |
Antoine Pitrou | d7b731d | 2010-03-17 22:45:39 +0000 | [diff] [blame] | 1519 | def testRecvFromIntoBytearray(self): |
| 1520 | buf = bytearray(1024) |
| 1521 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
| 1522 | self.assertEqual(nbytes, len(MSG)) |
| 1523 | msg = buf[:len(MSG)] |
| 1524 | self.assertEqual(msg, MSG) |
| 1525 | |
| 1526 | _testRecvFromIntoBytearray = _testRecvFromIntoArray |
| 1527 | |
| 1528 | def testRecvFromIntoMemoryview(self): |
| 1529 | buf = bytearray(1024) |
| 1530 | nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) |
| 1531 | self.assertEqual(nbytes, len(MSG)) |
| 1532 | msg = buf[:len(MSG)] |
| 1533 | self.assertEqual(msg, MSG) |
| 1534 | |
| 1535 | _testRecvFromIntoMemoryview = _testRecvFromIntoArray |
| 1536 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1537 | |
| 1538 | TIPC_STYPE = 2000 |
| 1539 | TIPC_LOWER = 200 |
| 1540 | TIPC_UPPER = 210 |
| 1541 | |
| 1542 | def isTipcAvailable(): |
| 1543 | """Check if the TIPC module is loaded |
| 1544 | |
| 1545 | The TIPC module is not loaded automatically on Ubuntu and probably |
| 1546 | other Linux distros. |
| 1547 | """ |
| 1548 | if not hasattr(socket, "AF_TIPC"): |
| 1549 | return False |
| 1550 | if not os.path.isfile("/proc/modules"): |
| 1551 | return False |
| 1552 | with open("/proc/modules") as f: |
| 1553 | for line in f: |
| 1554 | if line.startswith("tipc "): |
| 1555 | return True |
Christian Heimes | f66f95d | 2008-01-08 03:40:04 +0000 | [diff] [blame] | 1556 | if test_support.verbose: |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1557 | print "TIPC module is not loaded, please 'sudo modprobe tipc'" |
| 1558 | return False |
| 1559 | |
| 1560 | class TIPCTest (unittest.TestCase): |
| 1561 | def testRDM(self): |
| 1562 | srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1563 | cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1564 | |
| 1565 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1566 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1567 | TIPC_LOWER, TIPC_UPPER) |
| 1568 | srv.bind(srvaddr) |
| 1569 | |
| 1570 | sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1571 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1572 | cli.sendto(MSG, sendaddr) |
| 1573 | |
| 1574 | msg, recvaddr = srv.recvfrom(1024) |
| 1575 | |
| 1576 | self.assertEqual(cli.getsockname(), recvaddr) |
| 1577 | self.assertEqual(msg, MSG) |
| 1578 | |
| 1579 | |
| 1580 | class TIPCThreadableTest (unittest.TestCase, ThreadableTest): |
| 1581 | def __init__(self, methodName = 'runTest'): |
| 1582 | unittest.TestCase.__init__(self, methodName = methodName) |
| 1583 | ThreadableTest.__init__(self) |
| 1584 | |
| 1585 | def setUp(self): |
| 1586 | self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1587 | self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1588 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1589 | TIPC_LOWER, TIPC_UPPER) |
| 1590 | self.srv.bind(srvaddr) |
| 1591 | self.srv.listen(5) |
| 1592 | self.serverExplicitReady() |
| 1593 | self.conn, self.connaddr = self.srv.accept() |
| 1594 | |
| 1595 | def clientSetUp(self): |
| 1596 | # The is a hittable race between serverExplicitReady() and the |
| 1597 | # accept() call; sleep a little while to avoid it, otherwise |
| 1598 | # we could get an exception |
| 1599 | time.sleep(0.1) |
| 1600 | self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1601 | addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1602 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1603 | self.cli.connect(addr) |
| 1604 | self.cliaddr = self.cli.getsockname() |
| 1605 | |
| 1606 | def testStream(self): |
| 1607 | msg = self.conn.recv(1024) |
| 1608 | self.assertEqual(msg, MSG) |
| 1609 | self.assertEqual(self.cliaddr, self.connaddr) |
| 1610 | |
| 1611 | def _testStream(self): |
| 1612 | self.cli.send(MSG) |
| 1613 | self.cli.close() |
| 1614 | |
| 1615 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1616 | def test_main(): |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 1617 | tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
Ronald Oussoren | 9545a23 | 2010-05-05 19:09:31 +0000 | [diff] [blame] | 1618 | TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, |
| 1619 | UDPTimeoutTest ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1620 | |
| 1621 | tests.extend([ |
| 1622 | NonBlockingTCPTests, |
| 1623 | FileObjectClassTestCase, |
Gregory P. Smith | c4ad034 | 2009-08-13 18:54:50 +0000 | [diff] [blame] | 1624 | FileObjectInterruptedTestCase, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1625 | UnbufferedFileObjectClassTestCase, |
| 1626 | LineBufferedFileObjectClassTestCase, |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1627 | SmallBufferedFileObjectClassTestCase, |
| 1628 | Urllib2FileobjectTest, |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1629 | NetworkConnectionNoServer, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1630 | NetworkConnectionAttributesTest, |
| 1631 | NetworkConnectionBehaviourTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1632 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 1633 | if hasattr(socket, "socketpair"): |
| 1634 | tests.append(BasicSocketPairTest) |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1635 | if sys.platform == 'linux2': |
| 1636 | tests.append(TestLinuxAbstractNamespace) |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1637 | if isTipcAvailable(): |
| 1638 | tests.append(TIPCTest) |
Christian Heimes | 4d7e670 | 2008-01-07 19:58:41 +0000 | [diff] [blame] | 1639 | tests.append(TIPCThreadableTest) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1640 | |
| 1641 | thread_info = test_support.threading_setup() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1642 | test_support.run_unittest(*tests) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1643 | test_support.threading_cleanup(*thread_info) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1644 | |
| 1645 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1646 | test_main() |