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 |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 12 | from weakref import proxy |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 14 | PORT = 50007 |
| 15 | HOST = 'localhost' |
| 16 | MSG = 'Michael Gilfix was here\n' |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 18 | class SocketTCPTest(unittest.TestCase): |
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 | def setUp(self): |
| 21 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 22 | self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 23 | self.serv.bind((HOST, PORT)) |
| 24 | self.serv.listen(1) |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 26 | def tearDown(self): |
| 27 | self.serv.close() |
| 28 | self.serv = None |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 30 | class SocketUDPTest(unittest.TestCase): |
| 31 | |
| 32 | def setUp(self): |
| 33 | self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 34 | self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 35 | self.serv.bind((HOST, PORT)) |
| 36 | |
| 37 | def tearDown(self): |
| 38 | self.serv.close() |
| 39 | self.serv = None |
| 40 | |
| 41 | class ThreadableTest: |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 42 | """Threadable Test class |
| 43 | |
| 44 | The ThreadableTest class makes it easy to create a threaded |
| 45 | client/server pair from an existing unit test. To create a |
| 46 | new threaded class from an existing unit test, use multiple |
| 47 | inheritance: |
| 48 | |
| 49 | class NewClass (OldClass, ThreadableTest): |
| 50 | pass |
| 51 | |
| 52 | This class defines two new fixture functions with obvious |
| 53 | purposes for overriding: |
| 54 | |
| 55 | clientSetUp () |
| 56 | clientTearDown () |
| 57 | |
| 58 | Any new test functions within the class must then define |
| 59 | tests in pairs, where the test name is preceeded with a |
| 60 | '_' to indicate the client portion of the test. Ex: |
| 61 | |
| 62 | def testFoo(self): |
| 63 | # Server portion |
| 64 | |
| 65 | def _testFoo(self): |
| 66 | # Client portion |
| 67 | |
| 68 | Any exceptions raised by the clients during their tests |
| 69 | are caught and transferred to the main thread to alert |
| 70 | the testing framework. |
| 71 | |
| 72 | Note, the server setup function cannot call any blocking |
| 73 | functions that rely on the client thread during setup, |
| 74 | unless serverExplicityReady() is called just before |
| 75 | the blocking call (such as in setting up a client/server |
| 76 | connection and performing the accept() in setUp(). |
| 77 | """ |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 78 | |
| 79 | def __init__(self): |
| 80 | # Swap the true setup function |
| 81 | self.__setUp = self.setUp |
| 82 | self.__tearDown = self.tearDown |
| 83 | self.setUp = self._setUp |
| 84 | self.tearDown = self._tearDown |
| 85 | |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 86 | def serverExplicitReady(self): |
| 87 | """This method allows the server to explicitly indicate that |
| 88 | it wants the client thread to proceed. This is useful if the |
| 89 | server is about to execute a blocking routine that is |
| 90 | dependent upon the client thread during its setup routine.""" |
| 91 | self.server_ready.set() |
| 92 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 93 | def _setUp(self): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 94 | self.server_ready = threading.Event() |
| 95 | self.client_ready = threading.Event() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 96 | self.done = threading.Event() |
| 97 | self.queue = Queue.Queue(1) |
| 98 | |
| 99 | # Do some munging to start the client test. |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 100 | methodname = self.id() |
| 101 | i = methodname.rfind('.') |
| 102 | methodname = methodname[i+1:] |
| 103 | test_method = getattr(self, '_' + methodname) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 104 | self.client_thread = thread.start_new_thread( |
| 105 | self.clientRun, (test_method,)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 106 | |
| 107 | self.__setUp() |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 108 | if not self.server_ready.isSet(): |
| 109 | self.server_ready.set() |
| 110 | self.client_ready.wait() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 111 | |
| 112 | def _tearDown(self): |
| 113 | self.__tearDown() |
| 114 | self.done.wait() |
| 115 | |
| 116 | if not self.queue.empty(): |
| 117 | msg = self.queue.get() |
| 118 | self.fail(msg) |
| 119 | |
| 120 | def clientRun(self, test_func): |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 121 | self.server_ready.wait() |
| 122 | self.client_ready.set() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 123 | self.clientSetUp() |
| 124 | if not callable(test_func): |
| 125 | raise TypeError, "test_func must be a callable function" |
| 126 | try: |
| 127 | test_func() |
| 128 | except Exception, strerror: |
| 129 | self.queue.put(strerror) |
| 130 | self.clientTearDown() |
| 131 | |
| 132 | def clientSetUp(self): |
| 133 | raise NotImplementedError, "clientSetUp must be implemented." |
| 134 | |
| 135 | def clientTearDown(self): |
| 136 | self.done.set() |
| 137 | thread.exit() |
| 138 | |
| 139 | class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): |
| 140 | |
| 141 | def __init__(self, methodName='runTest'): |
| 142 | SocketTCPTest.__init__(self, methodName=methodName) |
| 143 | ThreadableTest.__init__(self) |
| 144 | |
| 145 | def clientSetUp(self): |
| 146 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 147 | |
| 148 | def clientTearDown(self): |
| 149 | self.cli.close() |
| 150 | self.cli = None |
| 151 | ThreadableTest.clientTearDown(self) |
| 152 | |
| 153 | class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): |
| 154 | |
| 155 | def __init__(self, methodName='runTest'): |
| 156 | SocketUDPTest.__init__(self, methodName=methodName) |
| 157 | ThreadableTest.__init__(self) |
| 158 | |
| 159 | def clientSetUp(self): |
| 160 | self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 161 | |
| 162 | class SocketConnectedTest(ThreadedTCPSocketTest): |
| 163 | |
| 164 | def __init__(self, methodName='runTest'): |
| 165 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 166 | |
| 167 | def setUp(self): |
| 168 | ThreadedTCPSocketTest.setUp(self) |
Guido van Rossum | 83ccb4e | 2002-06-18 18:35:13 +0000 | [diff] [blame] | 169 | # Indicate explicitly we're ready for the client thread to |
| 170 | # proceed and then perform the blocking call to accept |
| 171 | self.serverExplicitReady() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 172 | conn, addr = self.serv.accept() |
| 173 | self.cli_conn = conn |
| 174 | |
| 175 | def tearDown(self): |
| 176 | self.cli_conn.close() |
| 177 | self.cli_conn = None |
| 178 | ThreadedTCPSocketTest.tearDown(self) |
| 179 | |
| 180 | def clientSetUp(self): |
| 181 | ThreadedTCPSocketTest.clientSetUp(self) |
| 182 | self.cli.connect((HOST, PORT)) |
| 183 | self.serv_conn = self.cli |
| 184 | |
| 185 | def clientTearDown(self): |
| 186 | self.serv_conn.close() |
| 187 | self.serv_conn = None |
| 188 | ThreadedTCPSocketTest.clientTearDown(self) |
| 189 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 190 | class SocketPairTest(unittest.TestCase, ThreadableTest): |
| 191 | |
| 192 | def __init__(self, methodName='runTest'): |
| 193 | unittest.TestCase.__init__(self, methodName=methodName) |
| 194 | ThreadableTest.__init__(self) |
| 195 | |
| 196 | def setUp(self): |
| 197 | self.serv, self.cli = socket.socketpair() |
| 198 | |
| 199 | def tearDown(self): |
| 200 | self.serv.close() |
| 201 | self.serv = None |
| 202 | |
| 203 | def clientSetUp(self): |
| 204 | pass |
| 205 | |
| 206 | def clientTearDown(self): |
| 207 | self.cli.close() |
| 208 | self.cli = None |
| 209 | ThreadableTest.clientTearDown(self) |
| 210 | |
Tim Peters | 494aaee | 2004-08-09 18:54:11 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 212 | ####################################################################### |
| 213 | ## Begin Tests |
| 214 | |
| 215 | class GeneralModuleTests(unittest.TestCase): |
| 216 | |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 217 | def test_weakref(self): |
| 218 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 219 | p = proxy(s) |
| 220 | self.assertEqual(p.fileno(), s.fileno()) |
| 221 | s.close() |
| 222 | s = None |
| 223 | try: |
| 224 | p.fileno() |
| 225 | except ReferenceError: |
| 226 | pass |
| 227 | else: |
| 228 | self.fail('Socket proxy still exists') |
| 229 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 230 | def testSocketError(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 231 | # Testing socket module exceptions |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 232 | def raise_error(*args, **kwargs): |
| 233 | raise socket.error |
| 234 | def raise_herror(*args, **kwargs): |
| 235 | raise socket.herror |
| 236 | def raise_gaierror(*args, **kwargs): |
| 237 | raise socket.gaierror |
| 238 | self.failUnlessRaises(socket.error, raise_error, |
| 239 | "Error raising socket exception.") |
| 240 | self.failUnlessRaises(socket.error, raise_herror, |
| 241 | "Error raising socket exception.") |
| 242 | self.failUnlessRaises(socket.error, raise_gaierror, |
| 243 | "Error raising socket exception.") |
| 244 | |
| 245 | def testCrucialConstants(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 246 | # Testing for mission critical constants |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 247 | socket.AF_INET |
| 248 | socket.SOCK_STREAM |
| 249 | socket.SOCK_DGRAM |
| 250 | socket.SOCK_RAW |
| 251 | socket.SOCK_RDM |
| 252 | socket.SOCK_SEQPACKET |
| 253 | socket.SOL_SOCKET |
| 254 | socket.SO_REUSEADDR |
| 255 | |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 256 | def testHostnameRes(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 257 | # Testing hostname resolution mechanisms |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 258 | hostname = socket.gethostname() |
Guido van Rossum | 71e0294 | 2002-12-26 16:55:15 +0000 | [diff] [blame] | 259 | try: |
| 260 | ip = socket.gethostbyname(hostname) |
| 261 | except socket.error: |
| 262 | # Probably name lookup wasn't set up right; skip this test |
| 263 | return |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 264 | self.assert_(ip.find('.') >= 0, "Error resolving host to ip.") |
Guido van Rossum | 9647b52 | 2002-12-26 17:04:45 +0000 | [diff] [blame] | 265 | try: |
| 266 | hname, aliases, ipaddrs = socket.gethostbyaddr(ip) |
| 267 | except socket.error: |
| 268 | # Probably a similar problem as above; skip this test |
| 269 | return |
Brett Cannon | 01668a1 | 2005-03-11 00:04:17 +0000 | [diff] [blame^] | 270 | all_host_names = [hostname, hname] + aliases |
Guido van Rossum | 654c11e | 2002-06-13 20:24:17 +0000 | [diff] [blame] | 271 | fqhn = socket.getfqdn() |
| 272 | if not fqhn in all_host_names: |
| 273 | self.fail("Error testing host resolution mechanisms.") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 275 | def testRefCountGetNameInfo(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 276 | # Testing reference count for getnameinfo |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 277 | import sys |
Guido van Rossum | 284a2cf | 2002-06-12 21:19:40 +0000 | [diff] [blame] | 278 | if hasattr(sys, "getrefcount"): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 279 | try: |
| 280 | # On some versions, this loses a reference |
| 281 | orig = sys.getrefcount(__name__) |
| 282 | socket.getnameinfo(__name__,0) |
| 283 | except SystemError: |
| 284 | if sys.getrefcount(__name__) <> orig: |
| 285 | self.fail("socket.getnameinfo loses a reference") |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 287 | def testInterpreterCrash(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 288 | # Making sure getnameinfo doesn't crash the interpreter |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 289 | try: |
| 290 | # On some versions, this crashes the interpreter. |
| 291 | socket.getnameinfo(('x', 0, 0, 0), 0) |
| 292 | except socket.error: |
| 293 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | c0a0e08 | 2002-09-16 01:30:03 +0000 | [diff] [blame] | 295 | def testNtoH(self): |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 296 | # This just checks that htons etc. are their own inverse, |
| 297 | # when looking at the lower 16 or 32 bits. |
| 298 | sizes = {socket.htonl: 32, socket.ntohl: 32, |
| 299 | socket.htons: 16, socket.ntohs: 16} |
| 300 | for func, size in sizes.items(): |
| 301 | mask = (1L<<size) - 1 |
| 302 | for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): |
| 303 | self.assertEqual(i & mask, func(func(i&mask)) & mask) |
Jeremy Hylton | cbd5b89 | 2002-07-31 15:57:39 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | a2627af | 2002-09-14 00:58:46 +0000 | [diff] [blame] | 305 | swapped = func(mask) |
| 306 | self.assertEqual(swapped & mask, mask) |
| 307 | self.assertRaises(OverflowError, func, 1L<<34) |
Jeremy Hylton | c075e19 | 2002-07-25 16:01:12 +0000 | [diff] [blame] | 308 | |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 309 | def testGetServBy(self): |
| 310 | eq = self.assertEqual |
| 311 | # Find one service that exists, then check all the related interfaces. |
| 312 | # I've ordered this by protocols that have both a tcp and udp |
| 313 | # protocol, at least for modern Linuxes. |
Brett Cannon | 08febeb | 2004-11-20 21:10:07 +0000 | [diff] [blame] | 314 | if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6', |
| 315 | 'darwin'): |
Andrew MacIntyre | 18bf43c | 2004-07-12 12:10:30 +0000 | [diff] [blame] | 316 | # avoid the 'echo' service on this platform, as there is an |
| 317 | # assumption breaking non-standard port/protocol entry |
| 318 | services = ('daytime', 'qotd', 'domain') |
| 319 | else: |
| 320 | services = ('echo', 'daytime', 'domain') |
| 321 | for service in services: |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 322 | try: |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 323 | port = socket.getservbyname(service, 'tcp') |
Skip Montanaro | f443330 | 2002-08-02 15:52:30 +0000 | [diff] [blame] | 324 | break |
| 325 | except socket.error: |
| 326 | pass |
Skip Montanaro | 05eb401 | 2004-02-10 15:51:15 +0000 | [diff] [blame] | 327 | else: |
| 328 | raise socket.error |
Barry Warsaw | 11b91a0 | 2004-06-28 00:50:43 +0000 | [diff] [blame] | 329 | # Try same call with optional protocol omitted |
| 330 | port2 = socket.getservbyname(service) |
| 331 | eq(port, port2) |
| 332 | # Try udp, but don't barf it it doesn't exist |
| 333 | try: |
| 334 | udpport = socket.getservbyname(service, 'udp') |
| 335 | except socket.error: |
| 336 | udpport = None |
| 337 | else: |
| 338 | eq(udpport, port) |
| 339 | # Now make sure the lookup by port returns the same service name |
| 340 | eq(socket.getservbyport(port2), service) |
| 341 | eq(socket.getservbyport(port, 'tcp'), service) |
| 342 | if udpport is not None: |
| 343 | eq(socket.getservbyport(udpport, 'udp'), service) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 345 | def testDefaultTimeout(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 346 | # Testing default timeout |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 347 | # The default timeout should initially be None |
| 348 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 349 | s = socket.socket() |
| 350 | self.assertEqual(s.gettimeout(), None) |
| 351 | s.close() |
| 352 | |
| 353 | # Set the default timeout to 10, and see if it propagates |
| 354 | socket.setdefaulttimeout(10) |
| 355 | self.assertEqual(socket.getdefaulttimeout(), 10) |
| 356 | s = socket.socket() |
| 357 | self.assertEqual(s.gettimeout(), 10) |
| 358 | s.close() |
| 359 | |
| 360 | # Reset the default timeout to None, and see if it propagates |
| 361 | socket.setdefaulttimeout(None) |
| 362 | self.assertEqual(socket.getdefaulttimeout(), None) |
| 363 | s = socket.socket() |
| 364 | self.assertEqual(s.gettimeout(), None) |
| 365 | s.close() |
| 366 | |
| 367 | # Check that setting it to an invalid value raises ValueError |
| 368 | self.assertRaises(ValueError, socket.setdefaulttimeout, -1) |
| 369 | |
| 370 | # Check that setting it to an invalid type raises TypeError |
| 371 | self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") |
| 372 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 373 | def testIPv4toString(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 374 | if not hasattr(socket, 'inet_pton'): |
| 375 | return # No inet_pton() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 376 | from socket import inet_aton as f, inet_pton, AF_INET |
| 377 | g = lambda a: inet_pton(AF_INET, a) |
| 378 | |
| 379 | self.assertEquals('\x00\x00\x00\x00', f('0.0.0.0')) |
| 380 | self.assertEquals('\xff\x00\xff\x00', f('255.0.255.0')) |
| 381 | self.assertEquals('\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
| 382 | self.assertEquals('\x01\x02\x03\x04', f('1.2.3.4')) |
| 383 | |
| 384 | self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0')) |
| 385 | self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0')) |
| 386 | self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 388 | def testIPv6toString(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 | try: |
| 392 | from socket import inet_pton, AF_INET6, has_ipv6 |
| 393 | if not has_ipv6: |
| 394 | return |
| 395 | except ImportError: |
| 396 | return |
| 397 | f = lambda a: inet_pton(AF_INET6, a) |
| 398 | |
| 399 | self.assertEquals('\x00' * 16, f('::')) |
| 400 | self.assertEquals('\x00' * 16, f('0::0')) |
| 401 | self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) |
| 402 | self.assertEquals( |
| 403 | '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', |
| 404 | f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') |
| 405 | ) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 407 | def testStringToIPv4(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 408 | if not hasattr(socket, 'inet_ntop'): |
| 409 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 410 | from socket import inet_ntoa as f, inet_ntop, AF_INET |
| 411 | g = lambda a: inet_ntop(AF_INET, a) |
| 412 | |
| 413 | self.assertEquals('1.0.1.0', f('\x01\x00\x01\x00')) |
| 414 | self.assertEquals('170.85.170.85', f('\xaa\x55\xaa\x55')) |
| 415 | self.assertEquals('255.255.255.255', f('\xff\xff\xff\xff')) |
| 416 | self.assertEquals('1.2.3.4', f('\x01\x02\x03\x04')) |
Tim Peters | c2659cf | 2003-05-12 20:19:37 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 418 | self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) |
| 419 | self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) |
| 420 | self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff')) |
| 421 | |
| 422 | def testStringToIPv6(self): |
Guido van Rossum | f4001ee | 2003-04-25 15:11:23 +0000 | [diff] [blame] | 423 | if not hasattr(socket, 'inet_ntop'): |
| 424 | return # No inet_ntop() on this platform |
Guido van Rossum | 47dfa4a | 2003-04-25 05:48:32 +0000 | [diff] [blame] | 425 | try: |
| 426 | from socket import inet_ntop, AF_INET6, has_ipv6 |
| 427 | if not has_ipv6: |
| 428 | return |
| 429 | except ImportError: |
| 430 | return |
| 431 | f = lambda a: inet_ntop(AF_INET6, a) |
| 432 | |
| 433 | self.assertEquals('::', f('\x00' * 16)) |
| 434 | self.assertEquals('::1', f('\x00' * 15 + '\x01')) |
| 435 | self.assertEquals( |
| 436 | 'aef:b01:506:1001:ffff:9997:55:170', |
| 437 | f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') |
| 438 | ) |
| 439 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 440 | # XXX The following don't test module-level functionality... |
Guido van Rossum | 9d0c8ce | 2002-07-18 17:08:35 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 442 | def testSockName(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 443 | # Testing getsockname() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 444 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 445 | sock.bind(("0.0.0.0", PORT+1)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 446 | name = sock.getsockname() |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 447 | self.assertEqual(name, ("0.0.0.0", PORT+1)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 448 | |
| 449 | def testGetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 450 | # Testing getsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 451 | # We know a socket should start without reuse==0 |
| 452 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 453 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Guido van Rossum | 733632a | 2002-06-12 20:46:49 +0000 | [diff] [blame] | 454 | self.failIf(reuse != 0, "initial mode is reuse") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 455 | |
| 456 | def testSetSockOpt(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 457 | # Testing setsockopt() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 458 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 459 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 460 | reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) |
Guido van Rossum | 733632a | 2002-06-12 20:46:49 +0000 | [diff] [blame] | 461 | self.failIf(reuse == 0, "failed to set reuse mode") |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 462 | |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 463 | def testSendAfterClose(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 464 | # testing send() after close() with timeout |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 465 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 466 | sock.settimeout(1) |
| 467 | sock.close() |
| 468 | self.assertRaises(socket.error, sock.send, "spam") |
| 469 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 470 | class BasicTCPTest(SocketConnectedTest): |
| 471 | |
| 472 | def __init__(self, methodName='runTest'): |
| 473 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 474 | |
| 475 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 476 | # Testing large receive over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 477 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 478 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 479 | |
| 480 | def _testRecv(self): |
| 481 | self.serv_conn.send(MSG) |
| 482 | |
| 483 | def testOverFlowRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 484 | # Testing receive in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 485 | seg1 = self.cli_conn.recv(len(MSG) - 3) |
| 486 | seg2 = self.cli_conn.recv(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 487 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 488 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 489 | |
| 490 | def _testOverFlowRecv(self): |
| 491 | self.serv_conn.send(MSG) |
| 492 | |
| 493 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 494 | # Testing large recvfrom() over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 495 | msg, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 496 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 497 | |
| 498 | def _testRecvFrom(self): |
| 499 | self.serv_conn.send(MSG) |
| 500 | |
| 501 | def testOverFlowRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 502 | # Testing recvfrom() in chunks over TCP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 503 | seg1, addr = self.cli_conn.recvfrom(len(MSG)-3) |
| 504 | seg2, addr = self.cli_conn.recvfrom(1024) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 505 | msg = seg1 + seg2 |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 506 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 507 | |
| 508 | def _testOverFlowRecvFrom(self): |
| 509 | self.serv_conn.send(MSG) |
| 510 | |
| 511 | def testSendAll(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 512 | # Testing sendall() with a 2048 byte string over TCP |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 513 | msg = '' |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 514 | while 1: |
| 515 | read = self.cli_conn.recv(1024) |
| 516 | if not read: |
| 517 | break |
Guido van Rossum | e531e29 | 2002-08-08 20:28:34 +0000 | [diff] [blame] | 518 | msg += read |
| 519 | self.assertEqual(msg, 'f' * 2048) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 520 | |
| 521 | def _testSendAll(self): |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 522 | big_chunk = 'f' * 2048 |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 523 | self.serv_conn.sendall(big_chunk) |
| 524 | |
| 525 | def testFromFd(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 526 | # Testing fromfd() |
Guido van Rossum | 8e95ca8 | 2002-06-12 20:55:17 +0000 | [diff] [blame] | 527 | if not hasattr(socket, "fromfd"): |
Guido van Rossum | 6fb3d5e | 2002-06-12 20:48:59 +0000 | [diff] [blame] | 528 | return # On Windows, this doesn't exist |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 529 | fd = self.cli_conn.fileno() |
| 530 | sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
| 531 | msg = sock.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 532 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 533 | |
| 534 | def _testFromFd(self): |
| 535 | self.serv_conn.send(MSG) |
| 536 | |
| 537 | def testShutdown(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 538 | # Testing shutdown() |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 539 | msg = self.cli_conn.recv(1024) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 540 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 541 | |
| 542 | def _testShutdown(self): |
| 543 | self.serv_conn.send(MSG) |
| 544 | self.serv_conn.shutdown(2) |
| 545 | |
| 546 | class BasicUDPTest(ThreadedUDPSocketTest): |
| 547 | |
| 548 | def __init__(self, methodName='runTest'): |
| 549 | ThreadedUDPSocketTest.__init__(self, methodName=methodName) |
| 550 | |
| 551 | def testSendtoAndRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 552 | # Testing sendto() and Recv() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 553 | msg = self.serv.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 554 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 555 | |
| 556 | def _testSendtoAndRecv(self): |
| 557 | self.cli.sendto(MSG, 0, (HOST, PORT)) |
| 558 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 559 | def testRecvFrom(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 560 | # Testing recvfrom() over UDP |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 561 | msg, addr = self.serv.recvfrom(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 562 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 1c93801 | 2002-06-12 21:17:20 +0000 | [diff] [blame] | 564 | def _testRecvFrom(self): |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 565 | self.cli.sendto(MSG, 0, (HOST, PORT)) |
| 566 | |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 567 | class BasicSocketPairTest(SocketPairTest): |
| 568 | |
| 569 | def __init__(self, methodName='runTest'): |
| 570 | SocketPairTest.__init__(self, methodName=methodName) |
| 571 | |
| 572 | def testRecv(self): |
| 573 | msg = self.serv.recv(1024) |
| 574 | self.assertEqual(msg, MSG) |
| 575 | |
| 576 | def _testRecv(self): |
| 577 | self.cli.send(MSG) |
| 578 | |
| 579 | def testSend(self): |
| 580 | self.serv.send(MSG) |
| 581 | |
| 582 | def _testSend(self): |
| 583 | msg = self.cli.recv(1024) |
| 584 | self.assertEqual(msg, MSG) |
| 585 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 586 | class NonBlockingTCPTests(ThreadedTCPSocketTest): |
| 587 | |
| 588 | def __init__(self, methodName='runTest'): |
| 589 | ThreadedTCPSocketTest.__init__(self, methodName=methodName) |
| 590 | |
| 591 | def testSetBlocking(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 592 | # Testing whether set blocking works |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 593 | self.serv.setblocking(0) |
| 594 | start = time.time() |
| 595 | try: |
| 596 | self.serv.accept() |
| 597 | except socket.error: |
| 598 | pass |
| 599 | end = time.time() |
| 600 | self.assert_((end - start) < 1.0, "Error setting non-blocking mode.") |
| 601 | |
| 602 | def _testSetBlocking(self): |
Barry Warsaw | 6870bba | 2001-03-23 17:40:16 +0000 | [diff] [blame] | 603 | pass |
Barry Warsaw | cf3d4b5 | 1997-01-03 20:03:32 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 605 | def testAccept(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 606 | # Testing non-blocking accept |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 607 | self.serv.setblocking(0) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 608 | try: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 609 | conn, addr = self.serv.accept() |
| 610 | except socket.error: |
| 611 | pass |
| 612 | else: |
| 613 | self.fail("Error trying to do non-blocking accept.") |
| 614 | read, write, err = select.select([self.serv], [], []) |
| 615 | if self.serv in read: |
| 616 | conn, addr = self.serv.accept() |
| 617 | else: |
| 618 | self.fail("Error trying to do accept after select.") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 620 | def _testAccept(self): |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 621 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 622 | self.cli.connect((HOST, PORT)) |
| 623 | |
| 624 | def testConnect(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 625 | # Testing non-blocking connect |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 626 | conn, addr = self.serv.accept() |
| 627 | |
| 628 | def _testConnect(self): |
Guido van Rossum | 7b8bac1 | 2002-06-13 16:07:04 +0000 | [diff] [blame] | 629 | self.cli.settimeout(10) |
| 630 | self.cli.connect((HOST, PORT)) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 631 | |
| 632 | def testRecv(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 633 | # Testing non-blocking recv |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 634 | conn, addr = self.serv.accept() |
| 635 | conn.setblocking(0) |
| 636 | try: |
| 637 | msg = conn.recv(len(MSG)) |
| 638 | except socket.error: |
| 639 | pass |
| 640 | else: |
| 641 | self.fail("Error trying to do non-blocking recv.") |
| 642 | read, write, err = select.select([conn], [], []) |
| 643 | if conn in read: |
| 644 | msg = conn.recv(len(MSG)) |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 645 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 646 | else: |
| 647 | self.fail("Error during select call to non-blocking socket.") |
| 648 | |
| 649 | def _testRecv(self): |
| 650 | self.cli.connect((HOST, PORT)) |
Guido van Rossum | b6cc7d2 | 2002-07-19 12:46:46 +0000 | [diff] [blame] | 651 | time.sleep(0.1) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 652 | self.cli.send(MSG) |
| 653 | |
| 654 | class FileObjectClassTestCase(SocketConnectedTest): |
| 655 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 656 | bufsize = -1 # Use default buffer size |
| 657 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 658 | def __init__(self, methodName='runTest'): |
| 659 | SocketConnectedTest.__init__(self, methodName=methodName) |
| 660 | |
| 661 | def setUp(self): |
| 662 | SocketConnectedTest.setUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 663 | self.serv_file = self.cli_conn.makefile('rb', self.bufsize) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 664 | |
| 665 | def tearDown(self): |
| 666 | self.serv_file.close() |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 667 | self.assert_(self.serv_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 668 | self.serv_file = None |
| 669 | SocketConnectedTest.tearDown(self) |
| 670 | |
| 671 | def clientSetUp(self): |
| 672 | SocketConnectedTest.clientSetUp(self) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 673 | self.cli_file = self.serv_conn.makefile('wb') |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 674 | |
| 675 | def clientTearDown(self): |
| 676 | self.cli_file.close() |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 677 | self.assert_(self.cli_file.closed) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 678 | self.cli_file = None |
| 679 | SocketConnectedTest.clientTearDown(self) |
| 680 | |
| 681 | def testSmallRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 682 | # Performing small file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 683 | first_seg = self.serv_file.read(len(MSG)-3) |
| 684 | second_seg = self.serv_file.read(3) |
Guido van Rossum | ab65996 | 2002-06-12 21:29:43 +0000 | [diff] [blame] | 685 | msg = first_seg + second_seg |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 686 | self.assertEqual(msg, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 687 | |
| 688 | def _testSmallRead(self): |
| 689 | self.cli_file.write(MSG) |
| 690 | self.cli_file.flush() |
| 691 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 692 | def testFullRead(self): |
| 693 | # read until EOF |
| 694 | msg = self.serv_file.read() |
| 695 | self.assertEqual(msg, MSG) |
| 696 | |
| 697 | def _testFullRead(self): |
| 698 | self.cli_file.write(MSG) |
| 699 | self.cli_file.close() |
| 700 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 701 | def testUnbufferedRead(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 702 | # Performing unbuffered file read test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 703 | buf = '' |
| 704 | while 1: |
| 705 | char = self.serv_file.read(1) |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 706 | if not char: |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 707 | break |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 708 | buf += char |
| 709 | self.assertEqual(buf, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 710 | |
| 711 | def _testUnbufferedRead(self): |
| 712 | self.cli_file.write(MSG) |
| 713 | self.cli_file.flush() |
| 714 | |
| 715 | def testReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 716 | # Performing file readline test |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 717 | line = self.serv_file.readline() |
Guido van Rossum | 7648968 | 2002-06-12 20:38:30 +0000 | [diff] [blame] | 718 | self.assertEqual(line, MSG) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 719 | |
| 720 | def _testReadline(self): |
| 721 | self.cli_file.write(MSG) |
| 722 | self.cli_file.flush() |
| 723 | |
Tim Peters | 116d83c | 2004-03-28 02:20:45 +0000 | [diff] [blame] | 724 | def testClosedAttr(self): |
| 725 | self.assert_(not self.serv_file.closed) |
| 726 | |
| 727 | def _testClosedAttr(self): |
| 728 | self.assert_(not self.cli_file.closed) |
| 729 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 730 | class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 731 | |
| 732 | """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 734 | In this case (and in this case only), it should be possible to |
| 735 | create a file object, read a line from it, create another file |
| 736 | object, read another line from it, without loss of data in the |
| 737 | first file object's buffer. Note that httplib relies on this |
| 738 | when reading multiple requests from the same socket.""" |
| 739 | |
| 740 | bufsize = 0 # Use unbuffered mode |
| 741 | |
| 742 | def testUnbufferedReadline(self): |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 743 | # 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] | 744 | line = self.serv_file.readline() # first line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 745 | self.assertEqual(line, "A. " + MSG) # first line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 746 | self.serv_file = self.cli_conn.makefile('rb', 0) |
| 747 | line = self.serv_file.readline() # second line |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 748 | self.assertEqual(line, "B. " + MSG) # second line |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 749 | |
| 750 | def _testUnbufferedReadline(self): |
Guido van Rossum | 10e3f41 | 2002-08-07 19:02:49 +0000 | [diff] [blame] | 751 | self.cli_file.write("A. " + MSG) |
| 752 | self.cli_file.write("B. " + MSG) |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 753 | self.cli_file.flush() |
| 754 | |
Guido van Rossum | 8c94383 | 2002-08-08 01:00:28 +0000 | [diff] [blame] | 755 | class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 756 | |
| 757 | bufsize = 1 # Default-buffered for reading; line-buffered for writing |
| 758 | |
| 759 | |
| 760 | class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
| 761 | |
| 762 | bufsize = 2 # Exercise the buffering code |
Guido van Rossum | e9f6614 | 2002-08-07 15:46:19 +0000 | [diff] [blame] | 763 | |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 764 | class TCPTimeoutTest(SocketTCPTest): |
| 765 | |
| 766 | def testTCPTimeout(self): |
| 767 | def raise_timeout(*args, **kwargs): |
| 768 | self.serv.settimeout(1.0) |
| 769 | self.serv.accept() |
| 770 | self.failUnlessRaises(socket.timeout, raise_timeout, |
| 771 | "Error generating a timeout exception (TCP)") |
| 772 | |
| 773 | def testTimeoutZero(self): |
| 774 | ok = False |
| 775 | try: |
| 776 | self.serv.settimeout(0.0) |
| 777 | foo = self.serv.accept() |
| 778 | except socket.timeout: |
| 779 | self.fail("caught timeout instead of error (TCP)") |
| 780 | except socket.error: |
| 781 | ok = True |
| 782 | except: |
| 783 | self.fail("caught unexpected exception (TCP)") |
| 784 | if not ok: |
| 785 | self.fail("accept() returned success when we did not expect it") |
| 786 | |
| 787 | class UDPTimeoutTest(SocketTCPTest): |
| 788 | |
| 789 | def testUDPTimeout(self): |
| 790 | def raise_timeout(*args, **kwargs): |
| 791 | self.serv.settimeout(1.0) |
| 792 | self.serv.recv(1024) |
| 793 | self.failUnlessRaises(socket.timeout, raise_timeout, |
| 794 | "Error generating a timeout exception (UDP)") |
| 795 | |
| 796 | def testTimeoutZero(self): |
| 797 | ok = False |
| 798 | try: |
| 799 | self.serv.settimeout(0.0) |
| 800 | foo = self.serv.recv(1024) |
| 801 | except socket.timeout: |
| 802 | self.fail("caught timeout instead of error (UDP)") |
| 803 | except socket.error: |
| 804 | ok = True |
| 805 | except: |
| 806 | self.fail("caught unexpected exception (UDP)") |
| 807 | if not ok: |
| 808 | self.fail("recv() returned success when we did not expect it") |
| 809 | |
| 810 | class TestExceptions(unittest.TestCase): |
| 811 | |
| 812 | def testExceptionTree(self): |
| 813 | self.assert_(issubclass(socket.error, Exception)) |
| 814 | self.assert_(issubclass(socket.herror, socket.error)) |
| 815 | self.assert_(issubclass(socket.gaierror, socket.error)) |
| 816 | self.assert_(issubclass(socket.timeout, socket.error)) |
| 817 | |
| 818 | |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 819 | def test_main(): |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 820 | tests = [GeneralModuleTests, BasicTCPTest, TCPTimeoutTest, TestExceptions] |
Jack Jansen | 522e769 | 2002-09-06 21:57:50 +0000 | [diff] [blame] | 821 | if sys.platform != 'mac': |
Raymond Hettinger | 11a35f5 | 2003-06-29 04:40:22 +0000 | [diff] [blame] | 822 | tests.extend([ BasicUDPTest, UDPTimeoutTest ]) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 823 | |
| 824 | tests.extend([ |
| 825 | NonBlockingTCPTests, |
| 826 | FileObjectClassTestCase, |
| 827 | UnbufferedFileObjectClassTestCase, |
| 828 | LineBufferedFileObjectClassTestCase, |
| 829 | SmallBufferedFileObjectClassTestCase |
| 830 | ]) |
Dave Cole | 331708b | 2004-08-09 04:51:41 +0000 | [diff] [blame] | 831 | if hasattr(socket, "socketpair"): |
| 832 | tests.append(BasicSocketPairTest) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 833 | test_support.run_unittest(*tests) |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 834 | |
| 835 | if __name__ == "__main__": |
Guido van Rossum | b995eb7 | 2002-07-31 16:08:40 +0000 | [diff] [blame] | 836 | test_main() |