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