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