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