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 | |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 6 | import socket |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 7 | import select |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 8 | import thread, threading |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 9 | import time |
| 10 | import traceback |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +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 |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 15 | from weakref import proxy |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 16 | import signal |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 17 | |
Amaury Forgeot d'Arc | 5e08e8b | 2008-03-29 13:47:05 +0000 | [diff] [blame] | 18 | # Temporary hack to see why test_socket hangs on one buildbot |
| 19 | if os.environ.get('COMPUTERNAME') == "GRAPE": |
| 20 | def verbose_write(arg): |
| 21 | print >>sys.__stdout__, arg |
| 22 | else: |
| 23 | def verbose_write(arg): |
| 24 | pass |
| 25 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 26 | PORT = 50007 |
| 27 | HOST = 'localhost' |
| 28 | MSG = 'Michael Gilfix was here\n' |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 30 | class SocketTCPTest(unittest.TestCase): |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 32 | def setUp(self): |
Amaury Forgeot d'Arc | 5e08e8b | 2008-03-29 13:47:05 +0000 | [diff] [blame] | 33 | verbose_write(self) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 34 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 35 | self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Amaury Forgeot d'Arc | 672fbf5 | 2008-03-29 14:53:05 +0000 | [diff] [blame] | 36 | verbose_write(str(self) + " socket created") |
Neal Norwitz | 909eb12 | 2006-06-12 02:13:21 +0000 | [diff] [blame] | 37 | global PORT |
| 38 | PORT = test_support.bind_port(self.serv, HOST, PORT) |
Amaury Forgeot d'Arc | 672fbf5 | 2008-03-29 14:53:05 +0000 | [diff] [blame] | 39 | verbose_write(str(self) + " start listening") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 40 | self.serv.listen(1) |
Amaury Forgeot d'Arc | 672fbf5 | 2008-03-29 14:53:05 +0000 | [diff] [blame] | 41 | verbose_write(str(self) + " started") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 43 | def tearDown(self): |
Amaury Forgeot d'Arc | 672fbf5 | 2008-03-29 14:53:05 +0000 | [diff] [blame] | 44 | verbose_write(str(self) + " close") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 45 | self.serv.close() |
| 46 | self.serv = None |
Amaury Forgeot d'Arc | 5e08e8b | 2008-03-29 13:47:05 +0000 | [diff] [blame] | 47 | verbose_write(str(self) + " done") |
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) |
| 53 | self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Neal Norwitz | 909eb12 | 2006-06-12 02:13:21 +0000 | [diff] [blame] | 54 | global PORT |
| 55 | PORT = test_support.bind_port(self.serv, HOST, PORT) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 56 | |
| 57 | def tearDown(self): |
| 58 | self.serv.close() |
| 59 | self.serv = None |
| 60 | |
| 61 | class ThreadableTest: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 62 | """Threadable Test class |
| 63 | |
| 64 | The ThreadableTest class makes it easy to create a threaded |
| 65 | client/server pair from an existing unit test. To create a |
| 66 | new threaded class from an existing unit test, use multiple |
| 67 | inheritance: |
| 68 | |
| 69 | class NewClass (OldClass, ThreadableTest): |
| 70 | pass |
| 71 | |
| 72 | This class defines two new fixture functions with obvious |
| 73 | purposes for overriding: |
| 74 | |
| 75 | clientSetUp () |
| 76 | clientTearDown () |
| 77 | |
| 78 | Any new test functions within the class must then define |
| 79 | tests in pairs, where the test name is preceeded with a |
| 80 | '_' to indicate the client portion of the test. Ex: |
| 81 | |
| 82 | def testFoo(self): |
| 83 | # Server portion |
| 84 | |
| 85 | def _testFoo(self): |
| 86 | # Client portion |
| 87 | |
| 88 | Any exceptions raised by the clients during their tests |
| 89 | are caught and transferred to the main thread to alert |
| 90 | the testing framework. |
| 91 | |
| 92 | Note, the server setup function cannot call any blocking |
| 93 | functions that rely on the client thread during setup, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 94 | unless serverExplicitReady() is called just before |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 95 | the blocking call (such as in setting up a client/server |
| 96 | connection and performing the accept() in setUp(). |
| 97 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 98 | |
| 99 | def __init__(self): |
| 100 | # Swap the true setup function |
| 101 | self.__setUp = self.setUp |
| 102 | self.__tearDown = self.tearDown |
| 103 | self.setUp = self._setUp |
| 104 | self.tearDown = self._tearDown |
| 105 | |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 106 | def serverExplicitReady(self): |
| 107 | """This method allows the server to explicitly indicate that |
| 108 | it wants the client thread to proceed. This is useful if the |
| 109 | server is about to execute a blocking routine that is |
| 110 | dependent upon the client thread during its setup routine.""" |
| 111 | self.server_ready.set() |
| 112 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 113 | def _setUp(self): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 114 | self.server_ready = threading.Event() |
| 115 | self.client_ready = threading.Event() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 116 | self.done = threading.Event() |
| 117 | self.queue = Queue.Queue(1) |
| 118 | |
| 119 | # Do some munging to start the client test. |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 120 | methodname = self.id() |
| 121 | i = methodname.rfind('.') |
| 122 | methodname = methodname[i+1:] |
| 123 | test_method = getattr(self, '_' + methodname) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 124 | self.client_thread = thread.start_new_thread( |
| 125 | self.clientRun, (test_method,)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 126 | |
| 127 | self.__setUp() |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 128 | if not self.server_ready.isSet(): |
| 129 | self.server_ready.set() |
| 130 | self.client_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 131 | |
| 132 | def _tearDown(self): |
| 133 | self.__tearDown() |
| 134 | self.done.wait() |
| 135 | |
| 136 | if not self.queue.empty(): |
| 137 | msg = self.queue.get() |
| 138 | self.fail(msg) |
| 139 | |
| 140 | def clientRun(self, test_func): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 141 | self.server_ready.wait() |
| 142 | self.client_ready.set() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 143 | self.clientSetUp() |
| 144 | if not callable(test_func): |
| 145 | raise TypeError, "test_func must be a callable function" |
| 146 | try: |
| 147 | test_func() |
| 148 | except Exception, strerror: |
| 149 | self.queue.put(strerror) |
| 150 | self.clientTearDown() |
| 151 | |
| 152 | def clientSetUp(self): |
| 153 | raise NotImplementedError, "clientSetUp must be implemented." |
| 154 | |
| 155 | def clientTearDown(self): |
| 156 | self.done.set() |
| 157 | thread.exit() |
| 158 | |
| 159 | class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): |
| 160 | |
| 161 | def __init__(self, methodName='runTest'): |
| 162 | SocketTCPTest.__init__(self, methodName=methodName) |
| 163 | ThreadableTest.__init__(self) |
| 164 | |
| 165 | def clientSetUp(self): |
| 166 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 167 | |
| 168 | def clientTearDown(self): |
| 169 | self.cli.close() |
| 170 | self.cli = None |
| 171 | ThreadableTest.clientTearDown(self) |
| 172 | |
| 173 | class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): |
| 174 | |
| 175 | def __init__(self, methodName='runTest'): |
| 176 | SocketUDPTest.__init__(self, methodName=methodName) |
| 177 | ThreadableTest.__init__(self) |
| 178 | |
| 179 | def clientSetUp(self): |
| 180 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 181 | |
| 182 | class SocketConnectedTest(ThreadedTCPSocketTest): |
| 183 | |
| 184 | def __init__(self, methodName='runTest'): |
| 185 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 186 | |
| 187 | def setUp(self): |
| 188 | ThreadedTCPSocketTest.setUp(self) |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 189 | # Indicate explicitly we're ready for the client thread to |
| 190 | # proceed and then perform the blocking call to accept |
| 191 | self.serverExplicitReady() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 192 | conn, addr = self.serv.accept() |
| 193 | self.cli_conn = conn |
| 194 | |
| 195 | def tearDown(self): |
| 196 | self.cli_conn.close() |
| 197 | self.cli_conn = None |
| 198 | ThreadedTCPSocketTest.tearDown(self) |
| 199 | |
| 200 | def clientSetUp(self): |
| 201 | ThreadedTCPSocketTest.clientSetUp(self) |
| 202 | self.cli.connect((HOST, PORT)) |
| 203 | self.serv_conn = self.cli |
| 204 | |
| 205 | def clientTearDown(self): |
| 206 | self.serv_conn.close() |
| 207 | self.serv_conn = None |
| 208 | ThreadedTCPSocketTest.clientTearDown(self) |
| 209 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 210 | class SocketPairTest(unittest.TestCase, ThreadableTest): |
| 211 | |
| 212 | def __init__(self, methodName='runTest'): |
| 213 | unittest.TestCase.__init__(self, methodName=methodName) |
| 214 | ThreadableTest.__init__(self) |
| 215 | |
| 216 | def setUp(self): |
| 217 | self.serv, self.cli = socket.socketpair() |
| 218 | |
| 219 | def tearDown(self): |
| 220 | self.serv.close() |
| 221 | self.serv = None |
| 222 | |
| 223 | def clientSetUp(self): |
| 224 | pass |
| 225 | |
| 226 | def clientTearDown(self): |
| 227 | self.cli.close() |
| 228 | self.cli = None |
| 229 | ThreadableTest.clientTearDown(self) |
| 230 | |
Tim Peters | 494aaee | 2004-08-09 18:54:11 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 232 | ####################################################################### |
| 233 | ## Begin Tests |
| 234 | |
| 235 | class GeneralModuleTests(unittest.TestCase): |
| 236 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 237 | def test_weakref(self): |
| 238 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 239 | p = proxy(s) |
| 240 | self.assertEqual(p.fileno(), s.fileno()) |
| 241 | s.close() |
| 242 | s = None |
| 243 | try: |
| 244 | p.fileno() |
| 245 | except ReferenceError: |
| 246 | pass |
| 247 | else: |
| 248 | self.fail('Socket proxy still exists') |
| 249 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 250 | def testSocketError(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 251 | # Testing socket module exceptions |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 252 | def raise_error(*args, **kwargs): |
| 253 | raise socket.error |
| 254 | def raise_herror(*args, **kwargs): |
| 255 | raise socket.herror |
| 256 | def raise_gaierror(*args, **kwargs): |
| 257 | raise socket.gaierror |
| 258 | self.failUnlessRaises(socket.error, raise_error, |
| 259 | "Error raising socket exception.") |
| 260 | self.failUnlessRaises(socket.error, raise_herror, |
| 261 | "Error raising socket exception.") |
| 262 | self.failUnlessRaises(socket.error, raise_gaierror, |
| 263 | "Error raising socket exception.") |
| 264 | |
| 265 | def testCrucialConstants(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 266 | # Testing for mission critical constants |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 267 | socket.AF_INET |
| 268 | socket.SOCK_STREAM |
| 269 | socket.SOCK_DGRAM |
| 270 | socket.SOCK_RAW |
| 271 | socket.SOCK_RDM |
| 272 | socket.SOCK_SEQPACKET |
| 273 | socket.SOL_SOCKET |
| 274 | socket.SO_REUSEADDR |
| 275 | |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 276 | def testHostnameRes(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 277 | # Testing hostname resolution mechanisms |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 278 | hostname = socket.gethostname() |
Guido van Rossum | 71e0294 | 2002-12-26 16:55:15 +0000 | [diff] [blame] | 279 | try: |
| 280 | ip = socket.gethostbyname(hostname) |
| 281 | except socket.error: |
| 282 | # Probably name lookup wasn't set up right; skip this test |
| 283 | return |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 284 | self.assert_(ip.find('.') >= 0, "Error resolving host to ip.") |
Guido van Rossum | 9647b52 | 2002-12-26 17:04:45 +0000 | [diff] [blame] | 285 | try: |
| 286 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
| 287 | except socket.error: |
| 288 | # Probably a similar problem as above; skip this test |
| 289 | return |
Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame] | 290 | all_host_names = [hostname, hname] + aliases |
Anthony Baxter | cf0a2a8 | 2006-04-03 08:10:33 +0000 | [diff] [blame] | 291 | fqhn = socket.getfqdn(ip) |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 292 | if not fqhn in all_host_names: |
Martin v. Löwis | 04855cc | 2006-03-26 16:40:47 +0000 | [diff] [blame] | 293 | 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] | 294 | |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 295 | def testRefCountGetNameInfo(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 296 | # Testing reference count for getnameinfo |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 297 | if hasattr(sys, "getrefcount"): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 298 | try: |
| 299 | # On some versions, this loses a reference |
| 300 | orig = sys.getrefcount(__name__) |
| 301 | socket.getnameinfo(__name__,0) |
| 302 | except SystemError: |
| 303 | if sys.getrefcount(__name__) <> orig: |
| 304 | self.fail("socket.getnameinfo loses a reference") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 306 | def testInterpreterCrash(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 307 | # Making sure getnameinfo doesn't crash the interpreter |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 308 | try: |
| 309 | # On some versions, this crashes the interpreter. |
| 310 | socket.getnameinfo(('x', 0, 0, 0), 0) |
| 311 | except socket.error: |
| 312 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | c0a0e08 | 2002-09-16 01:30:03 +0000 | [diff] [blame] | 314 | def testNtoH(self): |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 315 | # This just checks that htons etc. are their own inverse, |
| 316 | # when looking at the lower 16 or 32 bits. |
| 317 | sizes = {socket.htonl: 32, socket.ntohl: 32, |
| 318 | socket.htons: 16, socket.ntohs: 16} |
| 319 | for func, size in sizes.items(): |
| 320 | mask = (1L<<size) - 1 |
| 321 | for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): |
| 322 | self.assertEqual(i & mask, func(func(i&mask)) & mask) |
Jeremy Hylton | cbd5b89 | 2002-07-31 15:57:39 +0000 | [diff] [blame] | 323 | |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 324 | swapped = func(mask) |
| 325 | self.assertEqual(swapped & mask, mask) |
| 326 | self.assertRaises(OverflowError, func, 1L<<34) |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | bb2cc69 | 2007-01-14 17:03:32 +0000 | [diff] [blame] | 328 | def testNtoHErrors(self): |
| 329 | good_values = [ 1, 2, 3, 1L, 2L, 3L ] |
| 330 | bad_values = [ -1, -2, -3, -1L, -2L, -3L ] |
| 331 | for k in good_values: |
| 332 | socket.ntohl(k) |
| 333 | socket.ntohs(k) |
| 334 | socket.htonl(k) |
| 335 | socket.htons(k) |
| 336 | for k in bad_values: |
| 337 | self.assertRaises(OverflowError, socket.ntohl, k) |
| 338 | self.assertRaises(OverflowError, socket.ntohs, k) |
| 339 | self.assertRaises(OverflowError, socket.htonl, k) |
| 340 | self.assertRaises(OverflowError, socket.htons, k) |
| 341 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 342 | def testGetServBy(self): |
| 343 | eq = self.assertEqual |
| 344 | # Find one service that exists, then check all the related interfaces. |
| 345 | # I've ordered this by protocols that have both a tcp and udp |
| 346 | # protocol, at least for modern Linuxes. |
Brett Cannon | 08febeb | 2004-11-20 21:10:07 +0000 | [diff] [blame] | 347 | if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6', |
Hye-Shik Chang | ea68474 | 2007-10-28 12:38:09 +0000 | [diff] [blame] | 348 | 'freebsd7', 'freebsd8', 'darwin'): |
Andrew MacIntyre | 18bf43c | 2004-07-12 12:10:30 +0000 | [diff] [blame] | 349 | # avoid the 'echo' service on this platform, as there is an |
| 350 | # assumption breaking non-standard port/protocol entry |
| 351 | services = ('daytime', 'qotd', 'domain') |
| 352 | else: |
| 353 | services = ('echo', 'daytime', 'domain') |
| 354 | for service in services: |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 355 | try: |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 356 | port = socket.getservbyname(service, 'tcp') |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 357 | break |
| 358 | except socket.error: |
| 359 | pass |
Skip Montanaro | 05eb401 | 2004-02-10 15:51:15 +0000 | [diff] [blame] | 360 | else: |
| 361 | raise socket.error |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 362 | # Try same call with optional protocol omitted |
| 363 | port2 = socket.getservbyname(service) |
| 364 | eq(port, port2) |
| 365 | # Try udp, but don't barf it it doesn't exist |
| 366 | try: |
| 367 | udpport = socket.getservbyname(service, 'udp') |
| 368 | except socket.error: |
| 369 | udpport = None |
| 370 | else: |
| 371 | eq(udpport, port) |
| 372 | # Now make sure the lookup by port returns the same service name |
| 373 | eq(socket.getservbyport(port2), service) |
| 374 | eq(socket.getservbyport(port, 'tcp'), service) |
| 375 | if udpport is not None: |
| 376 | eq(socket.getservbyport(udpport, 'udp'), service) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 378 | def testDefaultTimeout(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 379 | # Testing default timeout |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 380 | # The default timeout should initially be None |
| 381 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 382 | s = socket.socket() |
| 383 | self.assertEqual(s.gettimeout(), None) |
| 384 | s.close() |
| 385 | |
| 386 | # Set the default timeout to 10, and see if it propagates |
| 387 | socket.setdefaulttimeout(10) |
| 388 | self.assertEqual(socket.getdefaulttimeout(), 10) |
| 389 | s = socket.socket() |
| 390 | self.assertEqual(s.gettimeout(), 10) |
| 391 | s.close() |
| 392 | |
| 393 | # Reset the default timeout to None, and see if it propagates |
| 394 | socket.setdefaulttimeout(None) |
| 395 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 396 | s = socket.socket() |
| 397 | self.assertEqual(s.gettimeout(), None) |
| 398 | s.close() |
| 399 | |
| 400 | # Check that setting it to an invalid value raises ValueError |
| 401 | self.assertRaises(ValueError, socket.setdefaulttimeout, -1) |
| 402 | |
| 403 | # Check that setting it to an invalid type raises TypeError |
| 404 | self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") |
| 405 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 406 | def testIPv4toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 407 | if not hasattr(socket, 'inet_pton'): |
| 408 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 409 | from socket import inet_aton as f, inet_pton, AF_INET |
| 410 | g = lambda a: inet_pton(AF_INET, a) |
| 411 | |
| 412 | self.assertEquals('\x00\x00\x00\x00', f('0.0.0.0')) |
| 413 | self.assertEquals('\xff\x00\xff\x00', f('255.0.255.0')) |
| 414 | self.assertEquals('\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
| 415 | self.assertEquals('\x01\x02\x03\x04', f('1.2.3.4')) |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 416 | self.assertEquals('\xff\xff\xff\xff', f('255.255.255.255')) |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 417 | |
| 418 | self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0')) |
| 419 | self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0')) |
| 420 | self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
Georg Brandl | d2e3ba7 | 2005-08-26 08:34:00 +0000 | [diff] [blame] | 421 | self.assertEquals('\xff\xff\xff\xff', g('255.255.255.255')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 423 | def testIPv6toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 424 | if not hasattr(socket, 'inet_pton'): |
| 425 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 426 | try: |
| 427 | from socket import inet_pton, AF_INET6, has_ipv6 |
| 428 | if not has_ipv6: |
| 429 | return |
| 430 | except ImportError: |
| 431 | return |
| 432 | f = lambda a: inet_pton(AF_INET6, a) |
| 433 | |
| 434 | self.assertEquals('\x00' * 16, f('::')) |
| 435 | self.assertEquals('\x00' * 16, f('0::0')) |
| 436 | self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) |
| 437 | self.assertEquals( |
| 438 | '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', |
| 439 | f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') |
| 440 | ) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 442 | def testStringToIPv4(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 443 | if not hasattr(socket, 'inet_ntop'): |
| 444 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 445 | from socket import inet_ntoa as f, inet_ntop, AF_INET |
| 446 | g = lambda a: inet_ntop(AF_INET, a) |
| 447 | |
| 448 | self.assertEquals('1.0.1.0', f('\x01\x00\x01\x00')) |
| 449 | self.assertEquals('170.85.170.85', f('\xaa\x55\xaa\x55')) |
| 450 | self.assertEquals('255.255.255.255', f('\xff\xff\xff\xff')) |
| 451 | self.assertEquals('1.2.3.4', f('\x01\x02\x03\x04')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 453 | self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) |
| 454 | self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) |
| 455 | self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff')) |
| 456 | |
| 457 | def testStringToIPv6(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 458 | if not hasattr(socket, 'inet_ntop'): |
| 459 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 460 | try: |
| 461 | from socket import inet_ntop, AF_INET6, has_ipv6 |
| 462 | if not has_ipv6: |
| 463 | return |
| 464 | except ImportError: |
| 465 | return |
| 466 | f = lambda a: inet_ntop(AF_INET6, a) |
| 467 | |
| 468 | self.assertEquals('::', f('\x00' * 16)) |
| 469 | self.assertEquals('::1', f('\x00' * 15 + '\x01')) |
| 470 | self.assertEquals( |
| 471 | 'aef:b01:506:1001:ffff:9997:55:170', |
| 472 | f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') |
| 473 | ) |
| 474 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 475 | # XXX The following don't test module-level functionality... |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 476 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 477 | def testSockName(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 478 | # Testing getsockname() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 479 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 480 | sock.bind(("0.0.0.0", PORT+1)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 481 | name = sock.getsockname() |
Neal Norwitz | 4a9ff16 | 2006-06-11 21:38:38 +0000 | [diff] [blame] | 482 | # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate |
| 483 | # it reasonable to get the host's addr in addition to 0.0.0.0. |
| 484 | # At least for eCos. This is required for the S/390 to pass. |
| 485 | my_ip_addr = socket.gethostbyname(socket.gethostname()) |
| 486 | self.assert_(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0]) |
| 487 | self.assertEqual(name[1], PORT+1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 488 | |
| 489 | def testGetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 490 | # Testing getsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 491 | # We know a socket should start without reuse==0 |
| 492 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 493 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Guido van Rossum | 733632a | 2002-06-12 20:46:49 +0000 | [diff] [blame] | 494 | self.failIf(reuse != 0, "initial mode is reuse") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 495 | |
| 496 | def testSetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 497 | # Testing setsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 498 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 499 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 500 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Guido van Rossum | 733632a | 2002-06-12 20:46:49 +0000 | [diff] [blame] | 501 | self.failIf(reuse == 0, "failed to set reuse mode") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 503 | def testSendAfterClose(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 504 | # testing send() after close() with timeout |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 505 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 506 | sock.settimeout(1) |
| 507 | sock.close() |
| 508 | self.assertRaises(socket.error, sock.send, "spam") |
| 509 | |
Georg Brandl | bb03ac0 | 2006-03-21 18:17:25 +0000 | [diff] [blame] | 510 | def testNewAttributes(self): |
| 511 | # testing .family, .type and .protocol |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 512 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Georg Brandl | bb03ac0 | 2006-03-21 18:17:25 +0000 | [diff] [blame] | 513 | self.assertEqual(sock.family, socket.AF_INET) |
| 514 | self.assertEqual(sock.type, socket.SOCK_STREAM) |
| 515 | self.assertEqual(sock.proto, 0) |
Georg Brandl | bc45a3f | 2006-03-17 19:17:34 +0000 | [diff] [blame] | 516 | sock.close() |
| 517 | |
Christian Heimes | a47b75b | 2008-01-04 15:48:06 +0000 | [diff] [blame] | 518 | def test_sock_ioctl(self): |
| 519 | if os.name != "nt": |
| 520 | return |
| 521 | self.assert_(hasattr(socket.socket, 'ioctl')) |
| 522 | self.assert_(hasattr(socket, 'SIO_RCVALL')) |
| 523 | self.assert_(hasattr(socket, 'RCVALL_ON')) |
| 524 | self.assert_(hasattr(socket, 'RCVALL_OFF')) |
| 525 | |
| 526 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 527 | class BasicTCPTest(SocketConnectedTest): |
| 528 | |
| 529 | def __init__(self, methodName='runTest'): |
| 530 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 531 | |
| 532 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 533 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 534 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 535 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 536 | |
| 537 | def _testRecv(self): |
| 538 | self.serv_conn.send(MSG) |
| 539 | |
| 540 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 541 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 542 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 543 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 544 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 545 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 546 | |
| 547 | def _testOverFlowRecv(self): |
| 548 | self.serv_conn.send(MSG) |
| 549 | |
| 550 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 551 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 552 | msg, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 553 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 554 | |
| 555 | def _testRecvFrom(self): |
| 556 | self.serv_conn.send(MSG) |
| 557 | |
| 558 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 559 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 560 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 561 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 562 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 563 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 564 | |
| 565 | def _testOverFlowRecvFrom(self): |
| 566 | self.serv_conn.send(MSG) |
| 567 | |
| 568 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 569 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 570 | msg = '' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 571 | while 1: |
| 572 | read = self.cli_conn.recv(1024) |
| 573 | if not read: |
| 574 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 575 | msg += read |
| 576 | self.assertEqual(msg, 'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 577 | |
| 578 | def _testSendAll(self): |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 579 | big_chunk = 'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 580 | self.serv_conn.sendall(big_chunk) |
| 581 | |
| 582 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 583 | # Testing fromfd() |
Guido van Rossum | 8e95ca8 | 2002-06-12 20:55:17 +0000 | [diff] [blame] | 584 | if not hasattr(socket, "fromfd"): |
Guido van Rossum | 6fb3d5e | 2002-06-12 20:48:59 +0000 | [diff] [blame] | 585 | return # On Windows, this doesn't exist |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 586 | fd = self.cli_conn.fileno() |
| 587 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
| 588 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 589 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 590 | |
| 591 | def _testFromFd(self): |
| 592 | self.serv_conn.send(MSG) |
| 593 | |
| 594 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 595 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 596 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 597 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 598 | |
| 599 | def _testShutdown(self): |
| 600 | self.serv_conn.send(MSG) |
| 601 | self.serv_conn.shutdown(2) |
| 602 | |
| 603 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 604 | |
| 605 | def __init__(self, methodName='runTest'): |
| 606 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 607 | |
| 608 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 609 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 610 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 611 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 612 | |
| 613 | def _testSendtoAndRecv(self): |
| 614 | self.cli.sendto(MSG, 0, (HOST, PORT)) |
| 615 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 616 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 617 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 618 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 619 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 621 | def _testRecvFrom(self): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 622 | self.cli.sendto(MSG, 0, (HOST, PORT)) |
| 623 | |
Facundo Batista | 1fe9f96 | 2007-03-28 03:45:20 +0000 | [diff] [blame] | 624 | def testRecvFromNegative(self): |
| 625 | # Negative lengths passed to recvfrom should give ValueError. |
| 626 | self.assertRaises(ValueError, self.serv.recvfrom, -1) |
| 627 | |
| 628 | def _testRecvFromNegative(self): |
| 629 | self.cli.sendto(MSG, 0, (HOST, PORT)) |
| 630 | |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 631 | class TCPCloserTest(ThreadedTCPSocketTest): |
| 632 | |
| 633 | def testClose(self): |
| 634 | conn, addr = self.serv.accept() |
| 635 | conn.close() |
| 636 | |
| 637 | sd = self.cli |
| 638 | read, write, err = select.select([sd], [], [], 1.0) |
| 639 | self.assertEqual(read, [sd]) |
| 640 | self.assertEqual(sd.recv(1), '') |
| 641 | |
| 642 | def _testClose(self): |
| 643 | self.cli.connect((HOST, PORT)) |
| 644 | time.sleep(1.0) |
| 645 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 646 | class BasicSocketPairTest(SocketPairTest): |
| 647 | |
| 648 | def __init__(self, methodName='runTest'): |
| 649 | SocketPairTest.__init__(self, methodName=methodName) |
| 650 | |
| 651 | def testRecv(self): |
| 652 | msg = self.serv.recv(1024) |
| 653 | self.assertEqual(msg, MSG) |
| 654 | |
| 655 | def _testRecv(self): |
| 656 | self.cli.send(MSG) |
| 657 | |
| 658 | def testSend(self): |
| 659 | self.serv.send(MSG) |
| 660 | |
| 661 | def _testSend(self): |
| 662 | msg = self.cli.recv(1024) |
| 663 | self.assertEqual(msg, MSG) |
| 664 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 665 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 666 | |
| 667 | def __init__(self, methodName='runTest'): |
| 668 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 669 | |
| 670 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 671 | # Testing whether set blocking works |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 672 | self.serv.setblocking(0) |
| 673 | start = time.time() |
| 674 | try: |
| 675 | self.serv.accept() |
| 676 | except socket.error: |
| 677 | pass |
| 678 | end = time.time() |
| 679 | self.assert_((end - start) < 1.0, "Error setting non-blocking mode.") |
| 680 | |
| 681 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 682 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 684 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 685 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 686 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 687 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 688 | conn, addr = self.serv.accept() |
| 689 | except socket.error: |
| 690 | pass |
| 691 | else: |
| 692 | self.fail("Error trying to do non-blocking accept.") |
| 693 | read, write, err = select.select([self.serv], [], []) |
| 694 | if self.serv in read: |
| 695 | conn, addr = self.serv.accept() |
| 696 | else: |
| 697 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 699 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 700 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 701 | self.cli.connect((HOST, PORT)) |
| 702 | |
| 703 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 704 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 705 | conn, addr = self.serv.accept() |
| 706 | |
| 707 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 708 | self.cli.settimeout(10) |
| 709 | self.cli.connect((HOST, PORT)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 710 | |
| 711 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 712 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 713 | conn, addr = self.serv.accept() |
| 714 | conn.setblocking(0) |
| 715 | try: |
| 716 | msg = conn.recv(len(MSG)) |
| 717 | except socket.error: |
| 718 | pass |
| 719 | else: |
| 720 | self.fail("Error trying to do non-blocking recv.") |
| 721 | read, write, err = select.select([conn], [], []) |
| 722 | if conn in read: |
| 723 | msg = conn.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 724 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 725 | else: |
| 726 | self.fail("Error during select call to non-blocking socket.") |
| 727 | |
| 728 | def _testRecv(self): |
| 729 | self.cli.connect((HOST, PORT)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 730 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 731 | self.cli.send(MSG) |
| 732 | |
| 733 | class FileObjectClassTestCase(SocketConnectedTest): |
| 734 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 735 | bufsize = -1 # Use default buffer size |
| 736 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 737 | def __init__(self, methodName='runTest'): |
| 738 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 739 | |
| 740 | def setUp(self): |
| 741 | SocketConnectedTest.setUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 742 | self.serv_file = self.cli_conn.makefile('rb', self.bufsize) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 743 | |
| 744 | def tearDown(self): |
| 745 | self.serv_file.close() |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 746 | self.assert_(self.serv_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 747 | self.serv_file = None |
| 748 | SocketConnectedTest.tearDown(self) |
| 749 | |
| 750 | def clientSetUp(self): |
| 751 | SocketConnectedTest.clientSetUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 752 | self.cli_file = self.serv_conn.makefile('wb') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 753 | |
| 754 | def clientTearDown(self): |
| 755 | self.cli_file.close() |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 756 | self.assert_(self.cli_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 757 | self.cli_file = None |
| 758 | SocketConnectedTest.clientTearDown(self) |
| 759 | |
| 760 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 761 | # Performing small file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 762 | first_seg = self.serv_file.read(len(MSG)-3) |
| 763 | second_seg = self.serv_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 764 | msg = first_seg + second_seg |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 765 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 766 | |
| 767 | def _testSmallRead(self): |
| 768 | self.cli_file.write(MSG) |
| 769 | self.cli_file.flush() |
| 770 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 771 | def testFullRead(self): |
| 772 | # read until EOF |
| 773 | msg = self.serv_file.read() |
| 774 | self.assertEqual(msg, MSG) |
| 775 | |
| 776 | def _testFullRead(self): |
| 777 | self.cli_file.write(MSG) |
| 778 | self.cli_file.close() |
| 779 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 780 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 781 | # Performing unbuffered file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 782 | buf = '' |
| 783 | while 1: |
| 784 | char = self.serv_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 785 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 786 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 787 | buf += char |
| 788 | self.assertEqual(buf, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 789 | |
| 790 | def _testUnbufferedRead(self): |
| 791 | self.cli_file.write(MSG) |
| 792 | self.cli_file.flush() |
| 793 | |
| 794 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 795 | # Performing file readline test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 796 | line = self.serv_file.readline() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 797 | self.assertEqual(line, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 798 | |
| 799 | def _testReadline(self): |
| 800 | self.cli_file.write(MSG) |
| 801 | self.cli_file.flush() |
| 802 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 803 | def testClosedAttr(self): |
| 804 | self.assert_(not self.serv_file.closed) |
| 805 | |
| 806 | def _testClosedAttr(self): |
| 807 | self.assert_(not self.cli_file.closed) |
| 808 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 809 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 810 | |
| 811 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 812 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 813 | In this case (and in this case only), it should be possible to |
| 814 | create a file object, read a line from it, create another file |
| 815 | object, read another line from it, without loss of data in the |
| 816 | first file object's buffer. Note that httplib relies on this |
| 817 | when reading multiple requests from the same socket.""" |
| 818 | |
| 819 | bufsize = 0 # Use unbuffered mode |
| 820 | |
| 821 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 822 | # 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] | 823 | line = self.serv_file.readline() # first line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 824 | self.assertEqual(line, "A. " + MSG) # first line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 825 | self.serv_file = self.cli_conn.makefile('rb', 0) |
| 826 | line = self.serv_file.readline() # second line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 827 | self.assertEqual(line, "B. " + MSG) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 828 | |
| 829 | def _testUnbufferedReadline(self): |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 830 | self.cli_file.write("A. " + MSG) |
| 831 | self.cli_file.write("B. " + MSG) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 832 | self.cli_file.flush() |
| 833 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 834 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 835 | |
| 836 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 837 | |
| 838 | |
| 839 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 840 | |
| 841 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 842 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 843 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 844 | class NetworkConnectionTest(object): |
| 845 | """Prove network connection.""" |
| 846 | def clientSetUp(self): |
| 847 | self.cli = socket.create_connection((HOST, PORT)) |
| 848 | self.serv_conn = self.cli |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 849 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 850 | class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
| 851 | """Tests that NetworkConnection does not break existing TCP functionality. |
| 852 | """ |
| 853 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 854 | class NetworkConnectionNoServer(unittest.TestCase): |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 855 | def testWithoutServer(self): |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 856 | self.failUnlessRaises(socket.error, lambda: socket.create_connection((HOST, PORT))) |
| 857 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 858 | class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
| 859 | |
| 860 | def __init__(self, methodName='runTest'): |
| 861 | SocketTCPTest.__init__(self, methodName=methodName) |
| 862 | ThreadableTest.__init__(self) |
| 863 | |
| 864 | def clientSetUp(self): |
| 865 | pass |
| 866 | |
| 867 | def clientTearDown(self): |
| 868 | self.cli.close() |
| 869 | self.cli = None |
| 870 | ThreadableTest.clientTearDown(self) |
| 871 | |
| 872 | def _justAccept(self): |
| 873 | conn, addr = self.serv.accept() |
| 874 | |
| 875 | testFamily = _justAccept |
| 876 | def _testFamily(self): |
| 877 | self.cli = socket.create_connection((HOST, PORT), timeout=30) |
| 878 | self.assertEqual(self.cli.family, 2) |
| 879 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 880 | testTimeoutDefault = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 881 | def _testTimeoutDefault(self): |
| 882 | self.cli = socket.create_connection((HOST, PORT)) |
| 883 | self.assertTrue(self.cli.gettimeout() is None) |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 884 | |
| 885 | testTimeoutValueNamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 886 | def _testTimeoutValueNamed(self): |
| 887 | self.cli = socket.create_connection((HOST, PORT), timeout=30) |
| 888 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 889 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 890 | testTimeoutValueNonamed = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 891 | def _testTimeoutValueNonamed(self): |
| 892 | self.cli = socket.create_connection((HOST, PORT), 30) |
| 893 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 894 | |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 895 | testTimeoutNone = _justAccept |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 896 | def _testTimeoutNone(self): |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 897 | previous = socket.getdefaulttimeout() |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 898 | socket.setdefaulttimeout(30) |
| 899 | try: |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 900 | self.cli = socket.create_connection((HOST, PORT), timeout=None) |
Facundo Batista | 14553b0 | 2007-03-23 20:23:08 +0000 | [diff] [blame] | 901 | finally: |
| 902 | socket.setdefaulttimeout(previous) |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 903 | self.assertEqual(self.cli.gettimeout(), 30) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 904 | |
| 905 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 906 | class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
| 907 | |
| 908 | def __init__(self, methodName='runTest'): |
| 909 | SocketTCPTest.__init__(self, methodName=methodName) |
| 910 | ThreadableTest.__init__(self) |
| 911 | |
| 912 | def clientSetUp(self): |
| 913 | pass |
| 914 | |
| 915 | def clientTearDown(self): |
| 916 | self.cli.close() |
| 917 | self.cli = None |
| 918 | ThreadableTest.clientTearDown(self) |
| 919 | |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 920 | def testInsideTimeout(self): |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 921 | conn, addr = self.serv.accept() |
| 922 | time.sleep(3) |
| 923 | conn.send("done!") |
| 924 | testOutsideTimeout = testInsideTimeout |
| 925 | |
| 926 | def _testInsideTimeout(self): |
| 927 | self.cli = sock = socket.create_connection((HOST, PORT)) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 928 | data = sock.recv(5) |
| 929 | self.assertEqual(data, "done!") |
| 930 | |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 931 | def _testOutsideTimeout(self): |
| 932 | self.cli = sock = socket.create_connection((HOST, PORT), timeout=1) |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 933 | self.failUnlessRaises(socket.timeout, lambda: sock.recv(5)) |
| 934 | |
| 935 | |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 936 | class Urllib2FileobjectTest(unittest.TestCase): |
| 937 | |
| 938 | # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that |
| 939 | # it close the socket if the close c'tor argument is true |
| 940 | |
| 941 | def testClose(self): |
| 942 | class MockSocket: |
| 943 | closed = False |
| 944 | def flush(self): pass |
| 945 | def close(self): self.closed = True |
| 946 | |
| 947 | # must not close unless we request it: the original use of _fileobject |
| 948 | # by module socket requires that the underlying socket not be closed until |
| 949 | # the _socketobject that created the _fileobject is closed |
| 950 | s = MockSocket() |
| 951 | f = socket._fileobject(s) |
| 952 | f.close() |
| 953 | self.assert_(not s.closed) |
| 954 | |
| 955 | s = MockSocket() |
| 956 | f = socket._fileobject(s, close=True) |
| 957 | f.close() |
| 958 | self.assert_(s.closed) |
| 959 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 960 | class TCPTimeoutTest(SocketTCPTest): |
| 961 | |
| 962 | def testTCPTimeout(self): |
| 963 | def raise_timeout(*args, **kwargs): |
| 964 | self.serv.settimeout(1.0) |
| 965 | self.serv.accept() |
| 966 | self.failUnlessRaises(socket.timeout, raise_timeout, |
| 967 | "Error generating a timeout exception (TCP)") |
| 968 | |
| 969 | def testTimeoutZero(self): |
| 970 | ok = False |
| 971 | try: |
| 972 | self.serv.settimeout(0.0) |
| 973 | foo = self.serv.accept() |
| 974 | except socket.timeout: |
| 975 | self.fail("caught timeout instead of error (TCP)") |
| 976 | except socket.error: |
| 977 | ok = True |
| 978 | except: |
| 979 | self.fail("caught unexpected exception (TCP)") |
| 980 | if not ok: |
| 981 | self.fail("accept() returned success when we did not expect it") |
| 982 | |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 983 | def testInterruptedTimeout(self): |
| 984 | # XXX I don't know how to do this test on MSWindows or any other |
| 985 | # plaform that doesn't support signal.alarm() or os.kill(), though |
| 986 | # the bug should have existed on all platforms. |
| 987 | if not hasattr(signal, "alarm"): |
| 988 | return # can only test on *nix |
| 989 | self.serv.settimeout(5.0) # must be longer than alarm |
| 990 | class Alarm(Exception): |
| 991 | pass |
| 992 | def alarm_handler(signal, frame): |
| 993 | raise Alarm |
| 994 | old_alarm = signal.signal(signal.SIGALRM, alarm_handler) |
| 995 | try: |
| 996 | signal.alarm(2) # POSIX allows alarm to be up to 1 second early |
| 997 | try: |
| 998 | foo = self.serv.accept() |
| 999 | except socket.timeout: |
| 1000 | self.fail("caught timeout instead of Alarm") |
| 1001 | except Alarm: |
| 1002 | pass |
| 1003 | except: |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1004 | self.fail("caught other exception instead of Alarm:" |
| 1005 | " %s(%s):\n%s" % |
| 1006 | (sys.exc_info()[:2] + (traceback.format_exc(),))) |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1007 | else: |
| 1008 | self.fail("nothing caught") |
Jeffrey Yasskin | 36550bd | 2008-03-28 04:53:10 +0000 | [diff] [blame] | 1009 | finally: |
| 1010 | signal.alarm(0) # shut off alarm |
Neal Norwitz | 9b0ca79 | 2006-08-02 06:46:21 +0000 | [diff] [blame] | 1011 | except Alarm: |
| 1012 | self.fail("got Alarm in wrong place") |
| 1013 | finally: |
| 1014 | # no alarm can be pending. Safe to restore old handler. |
| 1015 | signal.signal(signal.SIGALRM, old_alarm) |
| 1016 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1017 | class UDPTimeoutTest(SocketTCPTest): |
| 1018 | |
| 1019 | def testUDPTimeout(self): |
| 1020 | def raise_timeout(*args, **kwargs): |
| 1021 | self.serv.settimeout(1.0) |
| 1022 | self.serv.recv(1024) |
| 1023 | self.failUnlessRaises(socket.timeout, raise_timeout, |
| 1024 | "Error generating a timeout exception (UDP)") |
| 1025 | |
| 1026 | def testTimeoutZero(self): |
| 1027 | ok = False |
| 1028 | try: |
| 1029 | self.serv.settimeout(0.0) |
| 1030 | foo = self.serv.recv(1024) |
| 1031 | except socket.timeout: |
| 1032 | self.fail("caught timeout instead of error (UDP)") |
| 1033 | except socket.error: |
| 1034 | ok = True |
| 1035 | except: |
| 1036 | self.fail("caught unexpected exception (UDP)") |
| 1037 | if not ok: |
| 1038 | self.fail("recv() returned success when we did not expect it") |
| 1039 | |
| 1040 | class TestExceptions(unittest.TestCase): |
| 1041 | |
| 1042 | def testExceptionTree(self): |
| 1043 | self.assert_(issubclass(socket.error, Exception)) |
| 1044 | self.assert_(issubclass(socket.herror, socket.error)) |
| 1045 | self.assert_(issubclass(socket.gaierror, socket.error)) |
| 1046 | self.assert_(issubclass(socket.timeout, socket.error)) |
| 1047 | |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1048 | class TestLinuxAbstractNamespace(unittest.TestCase): |
| 1049 | |
| 1050 | UNIX_PATH_MAX = 108 |
| 1051 | |
| 1052 | def testLinuxAbstractNamespace(self): |
| 1053 | address = "\x00python-test-hello\x00\xff" |
| 1054 | s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1055 | s1.bind(address) |
| 1056 | s1.listen(1) |
| 1057 | s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1058 | s2.connect(s1.getsockname()) |
| 1059 | s1.accept() |
| 1060 | self.assertEqual(s1.getsockname(), address) |
| 1061 | self.assertEqual(s2.getpeername(), address) |
| 1062 | |
| 1063 | def testMaxName(self): |
| 1064 | address = "\x00" + "h" * (self.UNIX_PATH_MAX - 1) |
| 1065 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1066 | s.bind(address) |
| 1067 | self.assertEqual(s.getsockname(), address) |
| 1068 | |
| 1069 | def testNameOverflow(self): |
| 1070 | address = "\x00" + "h" * self.UNIX_PATH_MAX |
| 1071 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1072 | self.assertRaises(socket.error, s.bind, address) |
| 1073 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1074 | |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1075 | class BufferIOTest(SocketConnectedTest): |
| 1076 | """ |
| 1077 | Test the buffer versions of socket.recv() and socket.send(). |
| 1078 | """ |
| 1079 | def __init__(self, methodName='runTest'): |
| 1080 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 1081 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1082 | def testRecvInto(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1083 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1084 | nbytes = self.cli_conn.recv_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1085 | self.assertEqual(nbytes, len(MSG)) |
| 1086 | msg = buf.tostring()[:len(MSG)] |
| 1087 | self.assertEqual(msg, MSG) |
| 1088 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1089 | def _testRecvInto(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1090 | buf = buffer(MSG) |
| 1091 | self.serv_conn.send(buf) |
| 1092 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1093 | def testRecvFromInto(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1094 | buf = array.array('c', ' '*1024) |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1095 | nbytes, addr = self.cli_conn.recvfrom_into(buf) |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1096 | self.assertEqual(nbytes, len(MSG)) |
| 1097 | msg = buf.tostring()[:len(MSG)] |
| 1098 | self.assertEqual(msg, MSG) |
| 1099 | |
Martin Blais | af2ae72 | 2006-06-04 13:49:49 +0000 | [diff] [blame] | 1100 | def _testRecvFromInto(self): |
Martin Blais | 2856e5f | 2006-05-26 12:03:27 +0000 | [diff] [blame] | 1101 | buf = buffer(MSG) |
| 1102 | self.serv_conn.send(buf) |
| 1103 | |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1104 | |
| 1105 | TIPC_STYPE = 2000 |
| 1106 | TIPC_LOWER = 200 |
| 1107 | TIPC_UPPER = 210 |
| 1108 | |
| 1109 | def isTipcAvailable(): |
| 1110 | """Check if the TIPC module is loaded |
| 1111 | |
| 1112 | The TIPC module is not loaded automatically on Ubuntu and probably |
| 1113 | other Linux distros. |
| 1114 | """ |
| 1115 | if not hasattr(socket, "AF_TIPC"): |
| 1116 | return False |
| 1117 | if not os.path.isfile("/proc/modules"): |
| 1118 | return False |
| 1119 | with open("/proc/modules") as f: |
| 1120 | for line in f: |
| 1121 | if line.startswith("tipc "): |
| 1122 | return True |
Christian Heimes | f66f95d | 2008-01-08 03:40:04 +0000 | [diff] [blame] | 1123 | if test_support.verbose: |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1124 | print "TIPC module is not loaded, please 'sudo modprobe tipc'" |
| 1125 | return False |
| 1126 | |
| 1127 | class TIPCTest (unittest.TestCase): |
| 1128 | def testRDM(self): |
| 1129 | srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1130 | cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM) |
| 1131 | |
| 1132 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1133 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1134 | TIPC_LOWER, TIPC_UPPER) |
| 1135 | srv.bind(srvaddr) |
| 1136 | |
| 1137 | sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1138 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1139 | cli.sendto(MSG, sendaddr) |
| 1140 | |
| 1141 | msg, recvaddr = srv.recvfrom(1024) |
| 1142 | |
| 1143 | self.assertEqual(cli.getsockname(), recvaddr) |
| 1144 | self.assertEqual(msg, MSG) |
| 1145 | |
| 1146 | |
| 1147 | class TIPCThreadableTest (unittest.TestCase, ThreadableTest): |
| 1148 | def __init__(self, methodName = 'runTest'): |
| 1149 | unittest.TestCase.__init__(self, methodName = methodName) |
| 1150 | ThreadableTest.__init__(self) |
| 1151 | |
| 1152 | def setUp(self): |
| 1153 | self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1154 | self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1155 | srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE, |
| 1156 | TIPC_LOWER, TIPC_UPPER) |
| 1157 | self.srv.bind(srvaddr) |
| 1158 | self.srv.listen(5) |
| 1159 | self.serverExplicitReady() |
| 1160 | self.conn, self.connaddr = self.srv.accept() |
| 1161 | |
| 1162 | def clientSetUp(self): |
| 1163 | # The is a hittable race between serverExplicitReady() and the |
| 1164 | # accept() call; sleep a little while to avoid it, otherwise |
| 1165 | # we could get an exception |
| 1166 | time.sleep(0.1) |
| 1167 | self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM) |
| 1168 | addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE, |
| 1169 | TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0) |
| 1170 | self.cli.connect(addr) |
| 1171 | self.cliaddr = self.cli.getsockname() |
| 1172 | |
| 1173 | def testStream(self): |
| 1174 | msg = self.conn.recv(1024) |
| 1175 | self.assertEqual(msg, MSG) |
| 1176 | self.assertEqual(self.cliaddr, self.connaddr) |
| 1177 | |
| 1178 | def _testStream(self): |
| 1179 | self.cli.send(MSG) |
| 1180 | self.cli.close() |
| 1181 | |
| 1182 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1183 | def test_main(): |
Martin v. Löwis | 7596e83 | 2006-07-01 15:33:37 +0000 | [diff] [blame] | 1184 | tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1185 | TestExceptions, BufferIOTest, BasicTCPTest2] |
Jack Jansen | 522e769 | 2002-09-06 21:57:50 +0000 | [diff] [blame] | 1186 | if sys.platform != 'mac': |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 1187 | tests.extend([ BasicUDPTest, UDPTimeoutTest ]) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1188 | |
| 1189 | tests.extend([ |
| 1190 | NonBlockingTCPTests, |
| 1191 | FileObjectClassTestCase, |
| 1192 | UnbufferedFileObjectClassTestCase, |
| 1193 | LineBufferedFileObjectClassTestCase, |
Georg Brandl | dd7b052 | 2007-01-21 10:35:10 +0000 | [diff] [blame] | 1194 | SmallBufferedFileObjectClassTestCase, |
| 1195 | Urllib2FileobjectTest, |
Facundo Batista | b8af7bc | 2007-03-25 01:53:21 +0000 | [diff] [blame] | 1196 | NetworkConnectionNoServer, |
Facundo Batista | 07c78be | 2007-03-23 18:54:07 +0000 | [diff] [blame] | 1197 | NetworkConnectionAttributesTest, |
| 1198 | NetworkConnectionBehaviourTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1199 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 1200 | if hasattr(socket, "socketpair"): |
| 1201 | tests.append(BasicSocketPairTest) |
Armin Rigo | a9017c3 | 2006-04-19 11:50:27 +0000 | [diff] [blame] | 1202 | if sys.platform == 'linux2': |
| 1203 | tests.append(TestLinuxAbstractNamespace) |
Christian Heimes | fb2d25a | 2008-01-07 16:12:44 +0000 | [diff] [blame] | 1204 | if isTipcAvailable(): |
| 1205 | tests.append(TIPCTest) |
Christian Heimes | 4d7e670 | 2008-01-07 19:58:41 +0000 | [diff] [blame] | 1206 | tests.append(TIPCThreadableTest) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1207 | |
| 1208 | thread_info = test_support.threading_setup() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1209 | test_support.run_unittest(*tests) |
Neal Norwitz | 9602cc2 | 2006-06-18 19:35:01 +0000 | [diff] [blame] | 1210 | test_support.threading_cleanup(*thread_info) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 1211 | |
| 1212 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 1213 | test_main() |