blob: 429ec6f978d0b78f078d1274467bb1a85f547b65 [file] [log] [blame]
Guido van Rossum24e4af82002-06-12 19:18:08 +00001#!/usr/bin/env python
Barry Warsawcf3d4b51997-01-03 20:03:32 +00002
Guido van Rossum24e4af82002-06-12 19:18:08 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Barry Warsawcf3d4b51997-01-03 20:03:32 +00005
Christian Heimes5e696852008-04-09 08:37:03 +00006import errno
Barry Warsawcf3d4b51997-01-03 20:03:32 +00007import socket
Guido van Rossum24e4af82002-06-12 19:18:08 +00008import select
Georg Brandl2067bfd2008-05-25 13:05:15 +00009import _thread as thread
10import threading
Christian Heimesbbe741d2008-03-28 10:53:29 +000011import time
12import traceback
Alexandre Vassalottif260e442008-05-11 19:59:59 +000013import queue
Jack Jansen522e7692002-09-06 21:57:50 +000014import sys
Christian Heimesfaf2f632008-01-06 16:59:19 +000015import os
16import array
Raymond Hettinger027bb632004-05-31 03:09:25 +000017from weakref import proxy
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018import signal
Barry Warsawcf3d4b51997-01-03 20:03:32 +000019
Benjamin Petersonee8712c2008-05-20 21:35:26 +000020HOST = support.HOST
Guido van Rossum7d0a8262007-05-21 23:13:11 +000021MSG = b'Michael Gilfix was here\n'
Barry Warsawcf3d4b51997-01-03 20:03:32 +000022
Guido van Rossum24e4af82002-06-12 19:18:08 +000023class SocketTCPTest(unittest.TestCase):
Barry Warsawcf3d4b51997-01-03 20:03:32 +000024
Guido van Rossum24e4af82002-06-12 19:18:08 +000025 def setUp(self):
26 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000027 self.port = support.bind_port(self.serv)
Guido van Rossum24e4af82002-06-12 19:18:08 +000028 self.serv.listen(1)
Barry Warsawcf3d4b51997-01-03 20:03:32 +000029
Guido van Rossum24e4af82002-06-12 19:18:08 +000030 def tearDown(self):
31 self.serv.close()
32 self.serv = None
Barry Warsawcf3d4b51997-01-03 20:03:32 +000033
Guido van Rossum24e4af82002-06-12 19:18:08 +000034class SocketUDPTest(unittest.TestCase):
35
36 def setUp(self):
37 self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000038 self.port = support.bind_port(self.serv)
Guido van Rossum24e4af82002-06-12 19:18:08 +000039
40 def tearDown(self):
41 self.serv.close()
42 self.serv = None
43
44class ThreadableTest:
Guido van Rossum83ccb4e2002-06-18 18:35:13 +000045 """Threadable Test class
46
47 The ThreadableTest class makes it easy to create a threaded
48 client/server pair from an existing unit test. To create a
49 new threaded class from an existing unit test, use multiple
50 inheritance:
51
52 class NewClass (OldClass, ThreadableTest):
53 pass
54
55 This class defines two new fixture functions with obvious
56 purposes for overriding:
57
58 clientSetUp ()
59 clientTearDown ()
60
61 Any new test functions within the class must then define
62 tests in pairs, where the test name is preceeded with a
63 '_' to indicate the client portion of the test. Ex:
64
65 def testFoo(self):
66 # Server portion
67
68 def _testFoo(self):
69 # Client portion
70
71 Any exceptions raised by the clients during their tests
72 are caught and transferred to the main thread to alert
73 the testing framework.
74
75 Note, the server setup function cannot call any blocking
76 functions that rely on the client thread during setup,
Guido van Rossumd8faa362007-04-27 19:54:29 +000077 unless serverExplicitReady() is called just before
Guido van Rossum83ccb4e2002-06-18 18:35:13 +000078 the blocking call (such as in setting up a client/server
79 connection and performing the accept() in setUp().
80 """
Guido van Rossum24e4af82002-06-12 19:18:08 +000081
82 def __init__(self):
83 # Swap the true setup function
84 self.__setUp = self.setUp
85 self.__tearDown = self.tearDown
86 self.setUp = self._setUp
87 self.tearDown = self._tearDown
88
Guido van Rossum83ccb4e2002-06-18 18:35:13 +000089 def serverExplicitReady(self):
90 """This method allows the server to explicitly indicate that
91 it wants the client thread to proceed. This is useful if the
92 server is about to execute a blocking routine that is
93 dependent upon the client thread during its setup routine."""
94 self.server_ready.set()
95
Guido van Rossum24e4af82002-06-12 19:18:08 +000096 def _setUp(self):
Guido van Rossum83ccb4e2002-06-18 18:35:13 +000097 self.server_ready = threading.Event()
98 self.client_ready = threading.Event()
Guido van Rossum24e4af82002-06-12 19:18:08 +000099 self.done = threading.Event()
Alexandre Vassalottif260e442008-05-11 19:59:59 +0000100 self.queue = queue.Queue(1)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000101
102 # Do some munging to start the client test.
Guido van Rossum11ba0942002-06-13 15:07:44 +0000103 methodname = self.id()
104 i = methodname.rfind('.')
105 methodname = methodname[i+1:]
106 test_method = getattr(self, '_' + methodname)
Guido van Rossumab659962002-06-12 21:29:43 +0000107 self.client_thread = thread.start_new_thread(
108 self.clientRun, (test_method,))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000109
110 self.__setUp()
Benjamin Peterson672b8032008-06-11 19:14:14 +0000111 if not self.server_ready.is_set():
Guido van Rossum83ccb4e2002-06-18 18:35:13 +0000112 self.server_ready.set()
113 self.client_ready.wait()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000114
115 def _tearDown(self):
116 self.__tearDown()
117 self.done.wait()
118
Raymond Hettingerda3caed2008-01-14 21:39:24 +0000119 if self.queue.qsize():
Guido van Rossum24e4af82002-06-12 19:18:08 +0000120 msg = self.queue.get()
121 self.fail(msg)
122
123 def clientRun(self, test_func):
Guido van Rossum83ccb4e2002-06-18 18:35:13 +0000124 self.server_ready.wait()
125 self.client_ready.set()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000126 self.clientSetUp()
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000127 if not hasattr(test_func, '__call__'):
Collin Winter3add4d72007-08-29 23:37:32 +0000128 raise TypeError("test_func must be a callable function")
Guido van Rossum24e4af82002-06-12 19:18:08 +0000129 try:
130 test_func()
Guido van Rossumb940e112007-01-10 16:19:56 +0000131 except Exception as strerror:
Guido van Rossum24e4af82002-06-12 19:18:08 +0000132 self.queue.put(strerror)
133 self.clientTearDown()
134
135 def clientSetUp(self):
Collin Winter3add4d72007-08-29 23:37:32 +0000136 raise NotImplementedError("clientSetUp must be implemented.")
Guido van Rossum24e4af82002-06-12 19:18:08 +0000137
138 def clientTearDown(self):
139 self.done.set()
140 thread.exit()
141
142class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest):
143
144 def __init__(self, methodName='runTest'):
145 SocketTCPTest.__init__(self, methodName=methodName)
146 ThreadableTest.__init__(self)
147
148 def clientSetUp(self):
149 self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
150
151 def clientTearDown(self):
152 self.cli.close()
153 self.cli = None
154 ThreadableTest.clientTearDown(self)
155
156class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest):
157
158 def __init__(self, methodName='runTest'):
159 SocketUDPTest.__init__(self, methodName=methodName)
160 ThreadableTest.__init__(self)
161
162 def clientSetUp(self):
163 self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
164
165class SocketConnectedTest(ThreadedTCPSocketTest):
Jeremy Hylton5accbdb2007-08-03 20:40:09 +0000166 """Socket tests for client-server connection.
167
168 self.cli_conn is a client socket connected to the server. The
169 setUp() method guarantees that it is connected to the server.
170 """
Guido van Rossum24e4af82002-06-12 19:18:08 +0000171
172 def __init__(self, methodName='runTest'):
173 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
174
175 def setUp(self):
176 ThreadedTCPSocketTest.setUp(self)
Guido van Rossum83ccb4e2002-06-18 18:35:13 +0000177 # Indicate explicitly we're ready for the client thread to
178 # proceed and then perform the blocking call to accept
179 self.serverExplicitReady()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000180 conn, addr = self.serv.accept()
181 self.cli_conn = conn
182
183 def tearDown(self):
184 self.cli_conn.close()
185 self.cli_conn = None
186 ThreadedTCPSocketTest.tearDown(self)
187
188 def clientSetUp(self):
189 ThreadedTCPSocketTest.clientSetUp(self)
Christian Heimes5e696852008-04-09 08:37:03 +0000190 self.cli.connect((HOST, self.port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000191 self.serv_conn = self.cli
192
193 def clientTearDown(self):
194 self.serv_conn.close()
195 self.serv_conn = None
196 ThreadedTCPSocketTest.clientTearDown(self)
197
Dave Cole331708b2004-08-09 04:51:41 +0000198class SocketPairTest(unittest.TestCase, ThreadableTest):
199
200 def __init__(self, methodName='runTest'):
201 unittest.TestCase.__init__(self, methodName=methodName)
202 ThreadableTest.__init__(self)
203
204 def setUp(self):
205 self.serv, self.cli = socket.socketpair()
206
207 def tearDown(self):
208 self.serv.close()
209 self.serv = None
210
211 def clientSetUp(self):
212 pass
213
214 def clientTearDown(self):
215 self.cli.close()
216 self.cli = None
217 ThreadableTest.clientTearDown(self)
218
Tim Peters494aaee2004-08-09 18:54:11 +0000219
Guido van Rossum24e4af82002-06-12 19:18:08 +0000220#######################################################################
221## Begin Tests
222
223class GeneralModuleTests(unittest.TestCase):
224
Walter Dörwalda7eb93e2007-06-05 13:41:53 +0000225 def test_repr(self):
226 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
227 self.assert_(repr(s).startswith("<socket.socket object"))
228
Raymond Hettinger027bb632004-05-31 03:09:25 +0000229 def test_weakref(self):
230 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
231 p = proxy(s)
232 self.assertEqual(p.fileno(), s.fileno())
233 s.close()
234 s = None
235 try:
236 p.fileno()
237 except ReferenceError:
238 pass
239 else:
240 self.fail('Socket proxy still exists')
241
Guido van Rossum24e4af82002-06-12 19:18:08 +0000242 def testSocketError(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000243 # Testing socket module exceptions
Guido van Rossum24e4af82002-06-12 19:18:08 +0000244 def raise_error(*args, **kwargs):
245 raise socket.error
246 def raise_herror(*args, **kwargs):
247 raise socket.herror
248 def raise_gaierror(*args, **kwargs):
249 raise socket.gaierror
250 self.failUnlessRaises(socket.error, raise_error,
251 "Error raising socket exception.")
252 self.failUnlessRaises(socket.error, raise_herror,
253 "Error raising socket exception.")
254 self.failUnlessRaises(socket.error, raise_gaierror,
255 "Error raising socket exception.")
256
257 def testCrucialConstants(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000258 # Testing for mission critical constants
Guido van Rossum24e4af82002-06-12 19:18:08 +0000259 socket.AF_INET
260 socket.SOCK_STREAM
261 socket.SOCK_DGRAM
262 socket.SOCK_RAW
263 socket.SOCK_RDM
264 socket.SOCK_SEQPACKET
265 socket.SOL_SOCKET
266 socket.SO_REUSEADDR
267
Guido van Rossum654c11e2002-06-13 20:24:17 +0000268 def testHostnameRes(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000269 # Testing hostname resolution mechanisms
Guido van Rossum654c11e2002-06-13 20:24:17 +0000270 hostname = socket.gethostname()
Guido van Rossum71e02942002-12-26 16:55:15 +0000271 try:
272 ip = socket.gethostbyname(hostname)
273 except socket.error:
274 # Probably name lookup wasn't set up right; skip this test
275 return
Guido van Rossum654c11e2002-06-13 20:24:17 +0000276 self.assert_(ip.find('.') >= 0, "Error resolving host to ip.")
Guido van Rossum9647b522002-12-26 17:04:45 +0000277 try:
278 hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
279 except socket.error:
280 # Probably a similar problem as above; skip this test
281 return
Brett Cannon01668a12005-03-11 00:04:17 +0000282 all_host_names = [hostname, hname] + aliases
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283 fqhn = socket.getfqdn(ip)
Guido van Rossum654c11e2002-06-13 20:24:17 +0000284 if not fqhn in all_host_names:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285 self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000286
Guido van Rossum284a2cf2002-06-12 21:19:40 +0000287 def testRefCountGetNameInfo(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000288 # Testing reference count for getnameinfo
Guido van Rossum284a2cf2002-06-12 21:19:40 +0000289 if hasattr(sys, "getrefcount"):
Guido van Rossum24e4af82002-06-12 19:18:08 +0000290 try:
291 # On some versions, this loses a reference
292 orig = sys.getrefcount(__name__)
293 socket.getnameinfo(__name__,0)
294 except SystemError:
Guido van Rossumb053cd82006-08-24 03:53:23 +0000295 if sys.getrefcount(__name__) != orig:
Guido van Rossum24e4af82002-06-12 19:18:08 +0000296 self.fail("socket.getnameinfo loses a reference")
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000297
Guido van Rossum24e4af82002-06-12 19:18:08 +0000298 def testInterpreterCrash(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000299 # Making sure getnameinfo doesn't crash the interpreter
Guido van Rossum24e4af82002-06-12 19:18:08 +0000300 try:
301 # On some versions, this crashes the interpreter.
302 socket.getnameinfo(('x', 0, 0, 0), 0)
303 except socket.error:
304 pass
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000305
Guido van Rossumc0a0e082002-09-16 01:30:03 +0000306 def testNtoH(self):
Guido van Rossuma2627af2002-09-14 00:58:46 +0000307 # This just checks that htons etc. are their own inverse,
308 # when looking at the lower 16 or 32 bits.
309 sizes = {socket.htonl: 32, socket.ntohl: 32,
310 socket.htons: 16, socket.ntohs: 16}
311 for func, size in sizes.items():
Guido van Rossume2a383d2007-01-15 16:59:06 +0000312 mask = (1<<size) - 1
Guido van Rossuma2627af2002-09-14 00:58:46 +0000313 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
314 self.assertEqual(i & mask, func(func(i&mask)) & mask)
Jeremy Hyltoncbd5b892002-07-31 15:57:39 +0000315
Guido van Rossuma2627af2002-09-14 00:58:46 +0000316 swapped = func(mask)
317 self.assertEqual(swapped & mask, mask)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000318 self.assertRaises(OverflowError, func, 1<<34)
Jeremy Hyltonc075e192002-07-25 16:01:12 +0000319
Guido van Rossum018919a2007-01-15 00:07:32 +0000320 def testNtoHErrors(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000321 good_values = [ 1, 2, 3, 1, 2, 3 ]
322 bad_values = [ -1, -2, -3, -1, -2, -3 ]
Guido van Rossum018919a2007-01-15 00:07:32 +0000323 for k in good_values:
324 socket.ntohl(k)
325 socket.ntohs(k)
326 socket.htonl(k)
327 socket.htons(k)
328 for k in bad_values:
329 self.assertRaises(OverflowError, socket.ntohl, k)
330 self.assertRaises(OverflowError, socket.ntohs, k)
331 self.assertRaises(OverflowError, socket.htonl, k)
332 self.assertRaises(OverflowError, socket.htons, k)
333
Barry Warsaw11b91a02004-06-28 00:50:43 +0000334 def testGetServBy(self):
335 eq = self.assertEqual
336 # Find one service that exists, then check all the related interfaces.
337 # I've ordered this by protocols that have both a tcp and udp
338 # protocol, at least for modern Linuxes.
Brett Cannon08febeb2004-11-20 21:10:07 +0000339 if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000340 'freebsd7', 'freebsd8', 'darwin'):
Andrew MacIntyre18bf43c2004-07-12 12:10:30 +0000341 # avoid the 'echo' service on this platform, as there is an
342 # assumption breaking non-standard port/protocol entry
343 services = ('daytime', 'qotd', 'domain')
344 else:
345 services = ('echo', 'daytime', 'domain')
346 for service in services:
Skip Montanarof4433302002-08-02 15:52:30 +0000347 try:
Barry Warsaw11b91a02004-06-28 00:50:43 +0000348 port = socket.getservbyname(service, 'tcp')
Skip Montanarof4433302002-08-02 15:52:30 +0000349 break
350 except socket.error:
351 pass
Skip Montanaro05eb4012004-02-10 15:51:15 +0000352 else:
353 raise socket.error
Barry Warsaw11b91a02004-06-28 00:50:43 +0000354 # Try same call with optional protocol omitted
355 port2 = socket.getservbyname(service)
356 eq(port, port2)
357 # Try udp, but don't barf it it doesn't exist
358 try:
359 udpport = socket.getservbyname(service, 'udp')
360 except socket.error:
361 udpport = None
362 else:
363 eq(udpport, port)
364 # Now make sure the lookup by port returns the same service name
365 eq(socket.getservbyport(port2), service)
366 eq(socket.getservbyport(port, 'tcp'), service)
367 if udpport is not None:
368 eq(socket.getservbyport(udpport, 'udp'), service)
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000369 # Make sure getservbyport does not accept out of range ports.
370 self.assertRaises(OverflowError, socket.getservbyport, -1)
371 self.assertRaises(OverflowError, socket.getservbyport, 65536)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000372
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000373 def testDefaultTimeout(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000374 # Testing default timeout
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000375 # The default timeout should initially be None
376 self.assertEqual(socket.getdefaulttimeout(), None)
377 s = socket.socket()
378 self.assertEqual(s.gettimeout(), None)
379 s.close()
380
381 # Set the default timeout to 10, and see if it propagates
382 socket.setdefaulttimeout(10)
383 self.assertEqual(socket.getdefaulttimeout(), 10)
384 s = socket.socket()
385 self.assertEqual(s.gettimeout(), 10)
386 s.close()
387
388 # Reset the default timeout to None, and see if it propagates
389 socket.setdefaulttimeout(None)
390 self.assertEqual(socket.getdefaulttimeout(), None)
391 s = socket.socket()
392 self.assertEqual(s.gettimeout(), None)
393 s.close()
394
395 # Check that setting it to an invalid value raises ValueError
396 self.assertRaises(ValueError, socket.setdefaulttimeout, -1)
397
398 # Check that setting it to an invalid type raises TypeError
399 self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
400
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000401 def testIPv4toString(self):
Guido van Rossumf4001ee2003-04-25 15:11:23 +0000402 if not hasattr(socket, 'inet_pton'):
403 return # No inet_pton() on this platform
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000404 from socket import inet_aton as f, inet_pton, AF_INET
405 g = lambda a: inet_pton(AF_INET, a)
406
Guido van Rossumb5b22702007-05-18 18:55:53 +0000407 self.assertEquals(b'\x00\x00\x00\x00', f('0.0.0.0'))
408 self.assertEquals(b'\xff\x00\xff\x00', f('255.0.255.0'))
409 self.assertEquals(b'\xaa\xaa\xaa\xaa', f('170.170.170.170'))
410 self.assertEquals(b'\x01\x02\x03\x04', f('1.2.3.4'))
411 self.assertEquals(b'\xff\xff\xff\xff', f('255.255.255.255'))
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000412
Guido van Rossumb5b22702007-05-18 18:55:53 +0000413 self.assertEquals(b'\x00\x00\x00\x00', g('0.0.0.0'))
414 self.assertEquals(b'\xff\x00\xff\x00', g('255.0.255.0'))
415 self.assertEquals(b'\xaa\xaa\xaa\xaa', g('170.170.170.170'))
416 self.assertEquals(b'\xff\xff\xff\xff', g('255.255.255.255'))
Tim Petersc2659cf2003-05-12 20:19:37 +0000417
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000418 def testIPv6toString(self):
Guido van Rossumf4001ee2003-04-25 15:11:23 +0000419 if not hasattr(socket, 'inet_pton'):
420 return # No inet_pton() on this platform
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000421 try:
422 from socket import inet_pton, AF_INET6, has_ipv6
423 if not has_ipv6:
424 return
425 except ImportError:
426 return
427 f = lambda a: inet_pton(AF_INET6, a)
428
Guido van Rossum540d9872007-08-17 03:51:09 +0000429 self.assertEquals(b'\x00' * 16, f('::'))
430 self.assertEquals(b'\x00' * 16, f('0::0'))
431 self.assertEquals(b'\x00\x01' + b'\x00' * 14, f('1::'))
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000432 self.assertEquals(
Guido van Rossum67180622007-07-10 07:29:12 +0000433 b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000434 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
435 )
Tim Petersc2659cf2003-05-12 20:19:37 +0000436
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000437 def testStringToIPv4(self):
Guido van Rossumf4001ee2003-04-25 15:11:23 +0000438 if not hasattr(socket, 'inet_ntop'):
439 return # No inet_ntop() on this platform
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000440 from socket import inet_ntoa as f, inet_ntop, AF_INET
441 g = lambda a: inet_ntop(AF_INET, a)
442
Guido van Rossumb5b22702007-05-18 18:55:53 +0000443 self.assertEquals('1.0.1.0', f(b'\x01\x00\x01\x00'))
444 self.assertEquals('170.85.170.85', f(b'\xaa\x55\xaa\x55'))
445 self.assertEquals('255.255.255.255', f(b'\xff\xff\xff\xff'))
446 self.assertEquals('1.2.3.4', f(b'\x01\x02\x03\x04'))
Tim Petersc2659cf2003-05-12 20:19:37 +0000447
Guido van Rossumb5b22702007-05-18 18:55:53 +0000448 self.assertEquals('1.0.1.0', g(b'\x01\x00\x01\x00'))
449 self.assertEquals('170.85.170.85', g(b'\xaa\x55\xaa\x55'))
450 self.assertEquals('255.255.255.255', g(b'\xff\xff\xff\xff'))
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000451
452 def testStringToIPv6(self):
Guido van Rossumf4001ee2003-04-25 15:11:23 +0000453 if not hasattr(socket, 'inet_ntop'):
454 return # No inet_ntop() on this platform
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000455 try:
456 from socket import inet_ntop, AF_INET6, has_ipv6
457 if not has_ipv6:
458 return
459 except ImportError:
460 return
461 f = lambda a: inet_ntop(AF_INET6, a)
462
Guido van Rossum540d9872007-08-17 03:51:09 +0000463 self.assertEquals('::', f(b'\x00' * 16))
464 self.assertEquals('::1', f(b'\x00' * 15 + b'\x01'))
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000465 self.assertEquals(
466 'aef:b01:506:1001:ffff:9997:55:170',
Guido van Rossum67180622007-07-10 07:29:12 +0000467 f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
Guido van Rossum47dfa4a2003-04-25 05:48:32 +0000468 )
469
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000470 # XXX The following don't test module-level functionality...
Guido van Rossum9d0c8ce2002-07-18 17:08:35 +0000471
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000472 def _get_unused_port(self, bind_address='0.0.0.0'):
473 """Use a temporary socket to elicit an unused ephemeral port.
Christian Heimes5e696852008-04-09 08:37:03 +0000474
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000475 Args:
476 bind_address: Hostname or IP address to search for a port on.
477
478 Returns: A most likely to be unused port.
479 """
480 tempsock = socket.socket()
481 tempsock.bind((bind_address, 0))
482 host, port = tempsock.getsockname()
483 tempsock.close()
484 return port
485
486 def testSockName(self):
487 # Testing getsockname()
488 port = self._get_unused_port()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000489 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Christian Heimes5e696852008-04-09 08:37:03 +0000490 sock.bind(("0.0.0.0", port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000491 name = sock.getsockname()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
493 # it reasonable to get the host's addr in addition to 0.0.0.0.
494 # At least for eCos. This is required for the S/390 to pass.
495 my_ip_addr = socket.gethostbyname(socket.gethostname())
496 self.assert_(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[0])
Christian Heimes5e696852008-04-09 08:37:03 +0000497 self.assertEqual(name[1], port)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000498
499 def testGetSockOpt(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000500 # Testing getsockopt()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000501 # We know a socket should start without reuse==0
502 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
503 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
Guido van Rossum733632a2002-06-12 20:46:49 +0000504 self.failIf(reuse != 0, "initial mode is reuse")
Guido van Rossum24e4af82002-06-12 19:18:08 +0000505
506 def testSetSockOpt(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000507 # Testing setsockopt()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000508 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
509 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
510 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
Guido van Rossum733632a2002-06-12 20:46:49 +0000511 self.failIf(reuse == 0, "failed to set reuse mode")
Guido van Rossum24e4af82002-06-12 19:18:08 +0000512
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000513 def testSendAfterClose(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000514 # testing send() after close() with timeout
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000515 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
516 sock.settimeout(1)
517 sock.close()
Guido van Rossum8a392d72007-11-21 22:09:45 +0000518 self.assertRaises(socket.error, sock.send, b"spam")
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000519
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 def testNewAttributes(self):
521 # testing .family, .type and .protocol
522 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
523 self.assertEqual(sock.family, socket.AF_INET)
524 self.assertEqual(sock.type, socket.SOCK_STREAM)
525 self.assertEqual(sock.proto, 0)
526 sock.close()
527
Benjamin Peterson5c6d7872009-02-06 02:40:07 +0000528 def test_getsockaddrarg(self):
529 host = '0.0.0.0'
530 port = self._get_unused_port(bind_address=host)
531 big_port = port + 65536
532 neg_port = port - 65536
533 sock = socket.socket()
534 try:
535 self.assertRaises(OverflowError, sock.bind, (host, big_port))
536 self.assertRaises(OverflowError, sock.bind, (host, neg_port))
537 sock.bind((host, port))
538 finally:
539 sock.close()
540
Christian Heimesfaf2f632008-01-06 16:59:19 +0000541 def test_sock_ioctl(self):
542 if os.name != "nt":
543 return
544 self.assert_(hasattr(socket.socket, 'ioctl'))
545 self.assert_(hasattr(socket, 'SIO_RCVALL'))
546 self.assert_(hasattr(socket, 'RCVALL_ON'))
547 self.assert_(hasattr(socket, 'RCVALL_OFF'))
548
549
Guido van Rossum24e4af82002-06-12 19:18:08 +0000550class BasicTCPTest(SocketConnectedTest):
551
552 def __init__(self, methodName='runTest'):
553 SocketConnectedTest.__init__(self, methodName=methodName)
554
555 def testRecv(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000556 # Testing large receive over TCP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000557 msg = self.cli_conn.recv(1024)
Guido van Rossum76489682002-06-12 20:38:30 +0000558 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000559
560 def _testRecv(self):
561 self.serv_conn.send(MSG)
562
563 def testOverFlowRecv(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000564 # Testing receive in chunks over TCP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000565 seg1 = self.cli_conn.recv(len(MSG) - 3)
566 seg2 = self.cli_conn.recv(1024)
Guido van Rossumab659962002-06-12 21:29:43 +0000567 msg = seg1 + seg2
Guido van Rossum76489682002-06-12 20:38:30 +0000568 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000569
570 def _testOverFlowRecv(self):
571 self.serv_conn.send(MSG)
572
573 def testRecvFrom(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000574 # Testing large recvfrom() over TCP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000575 msg, addr = self.cli_conn.recvfrom(1024)
Guido van Rossum76489682002-06-12 20:38:30 +0000576 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000577
578 def _testRecvFrom(self):
579 self.serv_conn.send(MSG)
580
581 def testOverFlowRecvFrom(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000582 # Testing recvfrom() in chunks over TCP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000583 seg1, addr = self.cli_conn.recvfrom(len(MSG)-3)
584 seg2, addr = self.cli_conn.recvfrom(1024)
Guido van Rossumab659962002-06-12 21:29:43 +0000585 msg = seg1 + seg2
Guido van Rossum76489682002-06-12 20:38:30 +0000586 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000587
588 def _testOverFlowRecvFrom(self):
589 self.serv_conn.send(MSG)
590
591 def testSendAll(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000592 # Testing sendall() with a 2048 byte string over TCP
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000593 msg = b''
Guido van Rossum24e4af82002-06-12 19:18:08 +0000594 while 1:
595 read = self.cli_conn.recv(1024)
596 if not read:
597 break
Guido van Rossume531e292002-08-08 20:28:34 +0000598 msg += read
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000599 self.assertEqual(msg, b'f' * 2048)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000600
601 def _testSendAll(self):
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000602 big_chunk = b'f' * 2048
Guido van Rossum24e4af82002-06-12 19:18:08 +0000603 self.serv_conn.sendall(big_chunk)
604
605 def testFromFd(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000606 # Testing fromfd()
Guido van Rossum8e95ca82002-06-12 20:55:17 +0000607 if not hasattr(socket, "fromfd"):
Guido van Rossum6fb3d5e2002-06-12 20:48:59 +0000608 return # On Windows, this doesn't exist
Guido van Rossum24e4af82002-06-12 19:18:08 +0000609 fd = self.cli_conn.fileno()
610 sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
611 msg = sock.recv(1024)
Guido van Rossum76489682002-06-12 20:38:30 +0000612 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000613
614 def _testFromFd(self):
615 self.serv_conn.send(MSG)
616
Guido van Rossum39eb8fa2007-11-16 01:24:05 +0000617 def testDup(self):
618 # Testing dup()
619 sock = self.cli_conn.dup()
620 msg = sock.recv(1024)
621 self.assertEqual(msg, MSG)
622
623 def _testDup(self):
624 self.serv_conn.send(MSG)
625
Guido van Rossum24e4af82002-06-12 19:18:08 +0000626 def testShutdown(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000627 # Testing shutdown()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000628 msg = self.cli_conn.recv(1024)
Guido van Rossum76489682002-06-12 20:38:30 +0000629 self.assertEqual(msg, MSG)
Mark Dickinson29500f62009-01-15 15:36:10 +0000630 # wait for _testShutdown to finish: on OS X, when the server
631 # closes the connection the client also becomes disconnected,
632 # and the client's shutdown call will fail. (Issue #4397.)
633 self.done.wait()
Guido van Rossum24e4af82002-06-12 19:18:08 +0000634
635 def _testShutdown(self):
636 self.serv_conn.send(MSG)
637 self.serv_conn.shutdown(2)
638
639class BasicUDPTest(ThreadedUDPSocketTest):
640
641 def __init__(self, methodName='runTest'):
642 ThreadedUDPSocketTest.__init__(self, methodName=methodName)
643
644 def testSendtoAndRecv(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000645 # Testing sendto() and Recv() over UDP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000646 msg = self.serv.recv(len(MSG))
Guido van Rossum76489682002-06-12 20:38:30 +0000647 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000648
649 def _testSendtoAndRecv(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000650 self.cli.sendto(MSG, 0, (HOST, self.port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000651
Guido van Rossum1c938012002-06-12 21:17:20 +0000652 def testRecvFrom(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000653 # Testing recvfrom() over UDP
Guido van Rossum24e4af82002-06-12 19:18:08 +0000654 msg, addr = self.serv.recvfrom(len(MSG))
Guido van Rossum76489682002-06-12 20:38:30 +0000655 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000656
Guido van Rossum1c938012002-06-12 21:17:20 +0000657 def _testRecvFrom(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000658 self.cli.sendto(MSG, 0, (HOST, self.port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000659
Guido van Rossumd8faa362007-04-27 19:54:29 +0000660 def testRecvFromNegative(self):
661 # Negative lengths passed to recvfrom should give ValueError.
662 self.assertRaises(ValueError, self.serv.recvfrom, -1)
663
664 def _testRecvFromNegative(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000665 self.cli.sendto(MSG, 0, (HOST, self.port))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000666
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667class TCPCloserTest(ThreadedTCPSocketTest):
668
669 def testClose(self):
670 conn, addr = self.serv.accept()
671 conn.close()
672
673 sd = self.cli
674 read, write, err = select.select([sd], [], [], 1.0)
675 self.assertEqual(read, [sd])
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000676 self.assertEqual(sd.recv(1), b'')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677
Jeremy Hylton5accbdb2007-08-03 20:40:09 +0000678 # Calling close() many times should be safe.
679 conn.close()
680 conn.close()
681
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000682 def _testClose(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000683 self.cli.connect((HOST, self.port))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684 time.sleep(1.0)
685
Dave Cole331708b2004-08-09 04:51:41 +0000686class BasicSocketPairTest(SocketPairTest):
687
688 def __init__(self, methodName='runTest'):
689 SocketPairTest.__init__(self, methodName=methodName)
690
691 def testRecv(self):
692 msg = self.serv.recv(1024)
693 self.assertEqual(msg, MSG)
694
695 def _testRecv(self):
696 self.cli.send(MSG)
697
698 def testSend(self):
699 self.serv.send(MSG)
700
701 def _testSend(self):
702 msg = self.cli.recv(1024)
703 self.assertEqual(msg, MSG)
704
Guido van Rossum24e4af82002-06-12 19:18:08 +0000705class NonBlockingTCPTests(ThreadedTCPSocketTest):
706
707 def __init__(self, methodName='runTest'):
708 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
709
710 def testSetBlocking(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000711 # Testing whether set blocking works
Guido van Rossum24e4af82002-06-12 19:18:08 +0000712 self.serv.setblocking(0)
713 start = time.time()
714 try:
715 self.serv.accept()
716 except socket.error:
717 pass
718 end = time.time()
719 self.assert_((end - start) < 1.0, "Error setting non-blocking mode.")
720
721 def _testSetBlocking(self):
Barry Warsaw6870bba2001-03-23 17:40:16 +0000722 pass
Barry Warsawcf3d4b51997-01-03 20:03:32 +0000723
Guido van Rossum24e4af82002-06-12 19:18:08 +0000724 def testAccept(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000725 # Testing non-blocking accept
Guido van Rossum24e4af82002-06-12 19:18:08 +0000726 self.serv.setblocking(0)
Guido van Rossum41360a41998-03-26 19:42:58 +0000727 try:
Guido van Rossum24e4af82002-06-12 19:18:08 +0000728 conn, addr = self.serv.accept()
729 except socket.error:
730 pass
731 else:
732 self.fail("Error trying to do non-blocking accept.")
733 read, write, err = select.select([self.serv], [], [])
734 if self.serv in read:
735 conn, addr = self.serv.accept()
736 else:
737 self.fail("Error trying to do accept after select.")
Guido van Rossum67f7a382002-06-06 21:08:16 +0000738
Guido van Rossum24e4af82002-06-12 19:18:08 +0000739 def _testAccept(self):
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000740 time.sleep(0.1)
Christian Heimes5e696852008-04-09 08:37:03 +0000741 self.cli.connect((HOST, self.port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000742
743 def testConnect(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000744 # Testing non-blocking connect
Guido van Rossum24e4af82002-06-12 19:18:08 +0000745 conn, addr = self.serv.accept()
746
747 def _testConnect(self):
Guido van Rossum7b8bac12002-06-13 16:07:04 +0000748 self.cli.settimeout(10)
Christian Heimes5e696852008-04-09 08:37:03 +0000749 self.cli.connect((HOST, self.port))
Guido van Rossum24e4af82002-06-12 19:18:08 +0000750
751 def testRecv(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000752 # Testing non-blocking recv
Guido van Rossum24e4af82002-06-12 19:18:08 +0000753 conn, addr = self.serv.accept()
754 conn.setblocking(0)
755 try:
756 msg = conn.recv(len(MSG))
757 except socket.error:
758 pass
759 else:
760 self.fail("Error trying to do non-blocking recv.")
761 read, write, err = select.select([conn], [], [])
762 if conn in read:
763 msg = conn.recv(len(MSG))
Guido van Rossum76489682002-06-12 20:38:30 +0000764 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000765 else:
766 self.fail("Error during select call to non-blocking socket.")
767
768 def _testRecv(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000769 self.cli.connect((HOST, self.port))
Guido van Rossumb6cc7d22002-07-19 12:46:46 +0000770 time.sleep(0.1)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000771 self.cli.send(MSG)
772
773class FileObjectClassTestCase(SocketConnectedTest):
Jeremy Hylton5accbdb2007-08-03 20:40:09 +0000774 """Unit tests for the object returned by socket.makefile()
775
776 self.serv_file is the io object returned by makefile() on
777 the client connection. You can read from this file to
778 get output from the server.
779
780 self.cli_file is the io object returned by makefile() on the
781 server connection. You can write to this file to send output
782 to the client.
783 """
Guido van Rossum24e4af82002-06-12 19:18:08 +0000784
Guido van Rossume9f66142002-08-07 15:46:19 +0000785 bufsize = -1 # Use default buffer size
786
Guido van Rossum24e4af82002-06-12 19:18:08 +0000787 def __init__(self, methodName='runTest'):
788 SocketConnectedTest.__init__(self, methodName=methodName)
789
790 def setUp(self):
791 SocketConnectedTest.setUp(self)
Guido van Rossume9f66142002-08-07 15:46:19 +0000792 self.serv_file = self.cli_conn.makefile('rb', self.bufsize)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000793
794 def tearDown(self):
795 self.serv_file.close()
Tim Peters116d83c2004-03-28 02:20:45 +0000796 self.assert_(self.serv_file.closed)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000797 self.serv_file = None
798 SocketConnectedTest.tearDown(self)
799
800 def clientSetUp(self):
801 SocketConnectedTest.clientSetUp(self)
Guido van Rossume9f66142002-08-07 15:46:19 +0000802 self.cli_file = self.serv_conn.makefile('wb')
Guido van Rossum24e4af82002-06-12 19:18:08 +0000803
804 def clientTearDown(self):
805 self.cli_file.close()
Tim Peters116d83c2004-03-28 02:20:45 +0000806 self.assert_(self.cli_file.closed)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000807 self.cli_file = None
808 SocketConnectedTest.clientTearDown(self)
809
810 def testSmallRead(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000811 # Performing small file read test
Guido van Rossum24e4af82002-06-12 19:18:08 +0000812 first_seg = self.serv_file.read(len(MSG)-3)
813 second_seg = self.serv_file.read(3)
Guido van Rossumab659962002-06-12 21:29:43 +0000814 msg = first_seg + second_seg
Guido van Rossum76489682002-06-12 20:38:30 +0000815 self.assertEqual(msg, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000816
817 def _testSmallRead(self):
818 self.cli_file.write(MSG)
819 self.cli_file.flush()
820
Guido van Rossum8c943832002-08-08 01:00:28 +0000821 def testFullRead(self):
822 # read until EOF
823 msg = self.serv_file.read()
824 self.assertEqual(msg, MSG)
825
826 def _testFullRead(self):
827 self.cli_file.write(MSG)
828 self.cli_file.close()
829
Guido van Rossum24e4af82002-06-12 19:18:08 +0000830 def testUnbufferedRead(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000831 # Performing unbuffered file read test
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000832 buf = b''
Guido van Rossum24e4af82002-06-12 19:18:08 +0000833 while 1:
834 char = self.serv_file.read(1)
Guido van Rossum8c943832002-08-08 01:00:28 +0000835 if not char:
Guido van Rossum24e4af82002-06-12 19:18:08 +0000836 break
Guido van Rossum8c943832002-08-08 01:00:28 +0000837 buf += char
838 self.assertEqual(buf, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000839
840 def _testUnbufferedRead(self):
841 self.cli_file.write(MSG)
842 self.cli_file.flush()
843
844 def testReadline(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000845 # Performing file readline test
Guido van Rossum24e4af82002-06-12 19:18:08 +0000846 line = self.serv_file.readline()
Guido van Rossum76489682002-06-12 20:38:30 +0000847 self.assertEqual(line, MSG)
Guido van Rossum24e4af82002-06-12 19:18:08 +0000848
849 def _testReadline(self):
850 self.cli_file.write(MSG)
851 self.cli_file.flush()
852
Jeremy Hylton5accbdb2007-08-03 20:40:09 +0000853 def testCloseAfterMakefile(self):
854 # The file returned by makefile should keep the socket open.
855 self.cli_conn.close()
856 # read until EOF
857 msg = self.serv_file.read()
858 self.assertEqual(msg, MSG)
859
860 def _testCloseAfterMakefile(self):
861 self.cli_file.write(MSG)
862 self.cli_file.flush()
863
864 def testMakefileAfterMakefileClose(self):
865 self.serv_file.close()
866 msg = self.cli_conn.recv(len(MSG))
867 self.assertEqual(msg, MSG)
868
869 def _testMakefileAfterMakefileClose(self):
870 self.cli_file.write(MSG)
871 self.cli_file.flush()
872
Tim Peters116d83c2004-03-28 02:20:45 +0000873 def testClosedAttr(self):
874 self.assert_(not self.serv_file.closed)
875
876 def _testClosedAttr(self):
877 self.assert_(not self.cli_file.closed)
878
Amaury Forgeot d'Arc9d24ff02008-11-20 23:15:52 +0000879 def testAttributes(self):
Benjamin Peterson44309e62008-11-22 00:41:45 +0000880 self.assertEqual(self.serv_file.mode, 'rb')
Amaury Forgeot d'Arc9d24ff02008-11-20 23:15:52 +0000881 self.assertEqual(self.serv_file.name, self.cli_conn.fileno())
882
883 def _testAttributes(self):
Benjamin Peterson44309e62008-11-22 00:41:45 +0000884 self.assertEqual(self.cli_file.mode, 'wb')
Amaury Forgeot d'Arc9d24ff02008-11-20 23:15:52 +0000885 self.assertEqual(self.cli_file.name, self.serv_conn.fileno())
886
Gregory P. Smithde3369f2009-01-12 04:50:11 +0000887 def testRealClose(self):
888 self.serv_file.close()
889 self.assertRaises(ValueError, self.serv_file.fileno)
890 self.cli_conn.close()
891 self.assertRaises(socket.error, self.cli_conn.getsockname)
892
893 def _testRealClose(self):
894 pass
895
896
Guido van Rossume9f66142002-08-07 15:46:19 +0000897class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
898
899 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
Tim Peters469cdad2002-08-08 20:19:19 +0000900
Guido van Rossume9f66142002-08-07 15:46:19 +0000901 In this case (and in this case only), it should be possible to
902 create a file object, read a line from it, create another file
903 object, read another line from it, without loss of data in the
Georg Brandl24420152008-05-26 16:32:26 +0000904 first file object's buffer. Note that http.client relies on this
Guido van Rossume9f66142002-08-07 15:46:19 +0000905 when reading multiple requests from the same socket."""
906
907 bufsize = 0 # Use unbuffered mode
908
909 def testUnbufferedReadline(self):
Guido van Rossum8c943832002-08-08 01:00:28 +0000910 # Read a line, create a new file object, read another line with it
Guido van Rossume9f66142002-08-07 15:46:19 +0000911 line = self.serv_file.readline() # first line
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000912 self.assertEqual(line, b"A. " + MSG) # first line
Guido van Rossume9f66142002-08-07 15:46:19 +0000913 self.serv_file = self.cli_conn.makefile('rb', 0)
914 line = self.serv_file.readline() # second line
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000915 self.assertEqual(line, b"B. " + MSG) # second line
Guido van Rossume9f66142002-08-07 15:46:19 +0000916
917 def _testUnbufferedReadline(self):
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000918 self.cli_file.write(b"A. " + MSG)
919 self.cli_file.write(b"B. " + MSG)
Guido van Rossume9f66142002-08-07 15:46:19 +0000920 self.cli_file.flush()
921
Gregory P. Smithde3369f2009-01-12 04:50:11 +0000922 def testMakefileClose(self):
923 # The file returned by makefile should keep the socket open...
924 self.cli_conn.close()
925 msg = self.cli_conn.recv(1024)
926 self.assertEqual(msg, MSG)
927 # ...until the file is itself closed
928 self.serv_file.close()
929 self.assertRaises(socket.error, self.cli_conn.recv, 1024)
930
931 def _testMakefileClose(self):
932 self.cli_file.write(MSG)
933 self.cli_file.flush()
934
935 def testMakefileCloseSocketDestroy(self):
936 refcount_before = sys.getrefcount(self.cli_conn)
937 self.serv_file.close()
938 refcount_after = sys.getrefcount(self.cli_conn)
939 self.assertEqual(refcount_before - 1, refcount_after)
940
941 def _testMakefileCloseSocketDestroy(self):
942 pass
943
944
Guido van Rossum8c943832002-08-08 01:00:28 +0000945class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
946
947 bufsize = 1 # Default-buffered for reading; line-buffered for writing
948
949
950class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
951
952 bufsize = 2 # Exercise the buffering code
Guido van Rossume9f66142002-08-07 15:46:19 +0000953
Thomas Woutersb2137042007-02-01 18:02:27 +0000954
Guido van Rossumd8faa362007-04-27 19:54:29 +0000955class NetworkConnectionTest(object):
956 """Prove network connection."""
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000957
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958 def clientSetUp(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000959 # We're inherited below by BasicTCPTest2, which also inherits
960 # BasicTCPTest, which defines self.port referenced below.
961 self.cli = socket.create_connection((HOST, self.port))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000962 self.serv_conn = self.cli
963
964class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
965 """Tests that NetworkConnection does not break existing TCP functionality.
966 """
967
968class NetworkConnectionNoServer(unittest.TestCase):
Guido van Rossum7d0a8262007-05-21 23:13:11 +0000969
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970 def testWithoutServer(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000971 port = support.find_unused_port()
Christian Heimes5e696852008-04-09 08:37:03 +0000972 self.failUnlessRaises(
973 socket.error,
974 lambda: socket.create_connection((HOST, port))
975 )
Guido van Rossumd8faa362007-04-27 19:54:29 +0000976
977class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
978
979 def __init__(self, methodName='runTest'):
980 SocketTCPTest.__init__(self, methodName=methodName)
981 ThreadableTest.__init__(self)
982
983 def clientSetUp(self):
984 pass
985
986 def clientTearDown(self):
987 self.cli.close()
988 self.cli = None
989 ThreadableTest.clientTearDown(self)
990
991 def _justAccept(self):
992 conn, addr = self.serv.accept()
993
994 testFamily = _justAccept
995 def _testFamily(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000996 self.cli = socket.create_connection((HOST, self.port), timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000997 self.assertEqual(self.cli.family, 2)
998
999 testTimeoutDefault = _justAccept
1000 def _testTimeoutDefault(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001001 # passing no explicit timeout uses socket's global default
1002 self.assert_(socket.getdefaulttimeout() is None)
1003 socket.setdefaulttimeout(42)
1004 try:
1005 self.cli = socket.create_connection((HOST, self.port))
1006 finally:
1007 socket.setdefaulttimeout(None)
1008 self.assertEquals(self.cli.gettimeout(), 42)
1009
1010 testTimeoutNone = _justAccept
1011 def _testTimeoutNone(self):
1012 # None timeout means the same as sock.settimeout(None)
1013 self.assert_(socket.getdefaulttimeout() is None)
1014 socket.setdefaulttimeout(30)
1015 try:
1016 self.cli = socket.create_connection((HOST, self.port), timeout=None)
1017 finally:
1018 socket.setdefaulttimeout(None)
1019 self.assertEqual(self.cli.gettimeout(), None)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001020
1021 testTimeoutValueNamed = _justAccept
1022 def _testTimeoutValueNamed(self):
Christian Heimes5e696852008-04-09 08:37:03 +00001023 self.cli = socket.create_connection((HOST, self.port), timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001024 self.assertEqual(self.cli.gettimeout(), 30)
1025
1026 testTimeoutValueNonamed = _justAccept
1027 def _testTimeoutValueNonamed(self):
Christian Heimes5e696852008-04-09 08:37:03 +00001028 self.cli = socket.create_connection((HOST, self.port), 30)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001029 self.assertEqual(self.cli.gettimeout(), 30)
1030
Guido van Rossumd8faa362007-04-27 19:54:29 +00001031class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
1032
1033 def __init__(self, methodName='runTest'):
1034 SocketTCPTest.__init__(self, methodName=methodName)
1035 ThreadableTest.__init__(self)
1036
1037 def clientSetUp(self):
1038 pass
1039
1040 def clientTearDown(self):
1041 self.cli.close()
1042 self.cli = None
1043 ThreadableTest.clientTearDown(self)
1044
1045 def testInsideTimeout(self):
1046 conn, addr = self.serv.accept()
1047 time.sleep(3)
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001048 conn.send(b"done!")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049 testOutsideTimeout = testInsideTimeout
1050
1051 def _testInsideTimeout(self):
Christian Heimes5e696852008-04-09 08:37:03 +00001052 self.cli = sock = socket.create_connection((HOST, self.port))
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053 data = sock.recv(5)
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001054 self.assertEqual(data, b"done!")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001055
1056 def _testOutsideTimeout(self):
Christian Heimes5e696852008-04-09 08:37:03 +00001057 self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001058 self.failUnlessRaises(socket.timeout, lambda: sock.recv(5))
1059
1060
Raymond Hettinger11a35f52003-06-29 04:40:22 +00001061class TCPTimeoutTest(SocketTCPTest):
1062
1063 def testTCPTimeout(self):
1064 def raise_timeout(*args, **kwargs):
1065 self.serv.settimeout(1.0)
1066 self.serv.accept()
1067 self.failUnlessRaises(socket.timeout, raise_timeout,
1068 "Error generating a timeout exception (TCP)")
1069
1070 def testTimeoutZero(self):
1071 ok = False
1072 try:
1073 self.serv.settimeout(0.0)
1074 foo = self.serv.accept()
1075 except socket.timeout:
1076 self.fail("caught timeout instead of error (TCP)")
1077 except socket.error:
1078 ok = True
1079 except:
1080 self.fail("caught unexpected exception (TCP)")
1081 if not ok:
1082 self.fail("accept() returned success when we did not expect it")
1083
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001084 def testInterruptedTimeout(self):
1085 # XXX I don't know how to do this test on MSWindows or any other
1086 # plaform that doesn't support signal.alarm() or os.kill(), though
1087 # the bug should have existed on all platforms.
1088 if not hasattr(signal, "alarm"):
1089 return # can only test on *nix
1090 self.serv.settimeout(5.0) # must be longer than alarm
1091 class Alarm(Exception):
1092 pass
1093 def alarm_handler(signal, frame):
1094 raise Alarm
1095 old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
1096 try:
1097 signal.alarm(2) # POSIX allows alarm to be up to 1 second early
1098 try:
1099 foo = self.serv.accept()
1100 except socket.timeout:
1101 self.fail("caught timeout instead of Alarm")
1102 except Alarm:
1103 pass
1104 except:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001105 self.fail("caught other exception instead of Alarm:"
1106 " %s(%s):\n%s" %
1107 (sys.exc_info()[:2] + (traceback.format_exc(),)))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001108 else:
1109 self.fail("nothing caught")
Christian Heimesbbe741d2008-03-28 10:53:29 +00001110 finally:
1111 signal.alarm(0) # shut off alarm
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001112 except Alarm:
1113 self.fail("got Alarm in wrong place")
1114 finally:
1115 # no alarm can be pending. Safe to restore old handler.
1116 signal.signal(signal.SIGALRM, old_alarm)
1117
Raymond Hettinger11a35f52003-06-29 04:40:22 +00001118class UDPTimeoutTest(SocketTCPTest):
1119
1120 def testUDPTimeout(self):
1121 def raise_timeout(*args, **kwargs):
1122 self.serv.settimeout(1.0)
1123 self.serv.recv(1024)
1124 self.failUnlessRaises(socket.timeout, raise_timeout,
1125 "Error generating a timeout exception (UDP)")
1126
1127 def testTimeoutZero(self):
1128 ok = False
1129 try:
1130 self.serv.settimeout(0.0)
1131 foo = self.serv.recv(1024)
1132 except socket.timeout:
1133 self.fail("caught timeout instead of error (UDP)")
1134 except socket.error:
1135 ok = True
1136 except:
1137 self.fail("caught unexpected exception (UDP)")
1138 if not ok:
1139 self.fail("recv() returned success when we did not expect it")
1140
1141class TestExceptions(unittest.TestCase):
1142
1143 def testExceptionTree(self):
1144 self.assert_(issubclass(socket.error, Exception))
1145 self.assert_(issubclass(socket.herror, socket.error))
1146 self.assert_(issubclass(socket.gaierror, socket.error))
1147 self.assert_(issubclass(socket.timeout, socket.error))
1148
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149class TestLinuxAbstractNamespace(unittest.TestCase):
1150
1151 UNIX_PATH_MAX = 108
1152
1153 def testLinuxAbstractNamespace(self):
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001154 address = b"\x00python-test-hello\x00\xff"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1156 s1.bind(address)
1157 s1.listen(1)
1158 s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1159 s2.connect(s1.getsockname())
1160 s1.accept()
1161 self.assertEqual(s1.getsockname(), address)
1162 self.assertEqual(s2.getpeername(), address)
1163
1164 def testMaxName(self):
Guido van Rossum32c4ac02007-08-15 03:56:40 +00001165 address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1167 s.bind(address)
1168 self.assertEqual(s.getsockname(), address)
1169
1170 def testNameOverflow(self):
1171 address = "\x00" + "h" * self.UNIX_PATH_MAX
1172 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1173 self.assertRaises(socket.error, s.bind, address)
1174
Raymond Hettinger11a35f52003-06-29 04:40:22 +00001175
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176class BufferIOTest(SocketConnectedTest):
1177 """
1178 Test the buffer versions of socket.recv() and socket.send().
1179 """
1180 def __init__(self, methodName='runTest'):
1181 SocketConnectedTest.__init__(self, methodName=methodName)
1182
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001183 def testRecvInto(self):
Antoine Pitrou2f89aa62008-08-02 21:02:48 +00001184 buf = bytearray(1024)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001185 nbytes = self.cli_conn.recv_into(buf)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186 self.assertEqual(nbytes, len(MSG))
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001187 msg = buf[:len(MSG)]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188 self.assertEqual(msg, MSG)
1189
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001190 def _testRecvInto(self):
Guido van Rossumb5b22702007-05-18 18:55:53 +00001191 buf = bytes(MSG)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001192 self.serv_conn.send(buf)
1193
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001194 def testRecvFromInto(self):
Antoine Pitrou2f89aa62008-08-02 21:02:48 +00001195 buf = bytearray(1024)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001196 nbytes, addr = self.cli_conn.recvfrom_into(buf)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001197 self.assertEqual(nbytes, len(MSG))
Guido van Rossum7d0a8262007-05-21 23:13:11 +00001198 msg = buf[:len(MSG)]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001199 self.assertEqual(msg, MSG)
1200
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001201 def _testRecvFromInto(self):
Guido van Rossumb5b22702007-05-18 18:55:53 +00001202 buf = bytes(MSG)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203 self.serv_conn.send(buf)
1204
Christian Heimes043d6f62008-01-07 17:19:16 +00001205
1206TIPC_STYPE = 2000
1207TIPC_LOWER = 200
1208TIPC_UPPER = 210
1209
1210def isTipcAvailable():
1211 """Check if the TIPC module is loaded
1212
1213 The TIPC module is not loaded automatically on Ubuntu and probably
1214 other Linux distros.
1215 """
1216 if not hasattr(socket, "AF_TIPC"):
1217 return False
1218 if not os.path.isfile("/proc/modules"):
1219 return False
1220 with open("/proc/modules") as f:
1221 for line in f:
1222 if line.startswith("tipc "):
1223 return True
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001224 if support.verbose:
Christian Heimes043d6f62008-01-07 17:19:16 +00001225 print("TIPC module is not loaded, please 'sudo modprobe tipc'")
1226 return False
1227
1228class TIPCTest (unittest.TestCase):
1229 def testRDM(self):
1230 srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
1231 cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
1232
1233 srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1234 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
1235 TIPC_LOWER, TIPC_UPPER)
1236 srv.bind(srvaddr)
1237
1238 sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
1239 TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
1240 cli.sendto(MSG, sendaddr)
1241
1242 msg, recvaddr = srv.recvfrom(1024)
1243
1244 self.assertEqual(cli.getsockname(), recvaddr)
1245 self.assertEqual(msg, MSG)
1246
1247
1248class TIPCThreadableTest (unittest.TestCase, ThreadableTest):
1249 def __init__(self, methodName = 'runTest'):
1250 unittest.TestCase.__init__(self, methodName = methodName)
1251 ThreadableTest.__init__(self)
1252
1253 def setUp(self):
1254 self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
1255 self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1256 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
1257 TIPC_LOWER, TIPC_UPPER)
1258 self.srv.bind(srvaddr)
1259 self.srv.listen(5)
1260 self.serverExplicitReady()
1261 self.conn, self.connaddr = self.srv.accept()
1262
1263 def clientSetUp(self):
1264 # The is a hittable race between serverExplicitReady() and the
1265 # accept() call; sleep a little while to avoid it, otherwise
1266 # we could get an exception
1267 time.sleep(0.1)
1268 self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
1269 addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
1270 TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
1271 self.cli.connect(addr)
1272 self.cliaddr = self.cli.getsockname()
1273
1274 def testStream(self):
1275 msg = self.conn.recv(1024)
1276 self.assertEqual(msg, MSG)
1277 self.assertEqual(self.cliaddr, self.connaddr)
1278
1279 def _testStream(self):
1280 self.cli.send(MSG)
1281 self.cli.close()
1282
1283
Guido van Rossumb995eb72002-07-31 16:08:40 +00001284def test_main():
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001285 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001286 TestExceptions, BufferIOTest, BasicTCPTest2]
Jack Jansen522e7692002-09-06 21:57:50 +00001287 if sys.platform != 'mac':
Raymond Hettinger11a35f52003-06-29 04:40:22 +00001288 tests.extend([ BasicUDPTest, UDPTimeoutTest ])
Walter Dörwald21d3a322003-05-01 17:45:56 +00001289
1290 tests.extend([
1291 NonBlockingTCPTests,
1292 FileObjectClassTestCase,
1293 UnbufferedFileObjectClassTestCase,
1294 LineBufferedFileObjectClassTestCase,
Thomas Woutersb2137042007-02-01 18:02:27 +00001295 SmallBufferedFileObjectClassTestCase,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001296 NetworkConnectionNoServer,
1297 NetworkConnectionAttributesTest,
1298 NetworkConnectionBehaviourTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001299 ])
Dave Cole331708b2004-08-09 04:51:41 +00001300 if hasattr(socket, "socketpair"):
1301 tests.append(BasicSocketPairTest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 if sys.platform == 'linux2':
1303 tests.append(TestLinuxAbstractNamespace)
Christian Heimes043d6f62008-01-07 17:19:16 +00001304 if isTipcAvailable():
1305 tests.append(TIPCTest)
Christian Heimes790c8232008-01-07 21:14:23 +00001306 tests.append(TIPCThreadableTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001307
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001308 thread_info = support.threading_setup()
1309 support.run_unittest(*tests)
1310 support.threading_cleanup(*thread_info)
Guido van Rossum24e4af82002-06-12 19:18:08 +00001311
1312if __name__ == "__main__":
Guido van Rossumb995eb72002-07-31 16:08:40 +00001313 test_main()