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