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