annotate Lib/test/test_socket.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 17891566a478
children 435fa215d33f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1 #!/usr/bin/env python
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
2
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
3 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 24326
diff changeset
4 from test import test_support
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
5
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
6 import errno
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
7 import socket
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
8 import select
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
9 import thread, threading
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
10 import time
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
11 import traceback
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
12 import Queue
25152
0c9dfd5a06d4 Skip UDP testing for MacPython (for now), it hangs. This may be due to
Jack Jansen <jack.jansen@cwi.nl>
parents: 24772
diff changeset
13 import sys
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
14 import os
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
15 import array
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
16 from weakref import proxy
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
17 import signal
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
18
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
19 HOST = test_support.HOST
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
20 MSG = 'Michael Gilfix was here\n'
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
21
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
22 class SocketTCPTest(unittest.TestCase):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
23
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
24 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
25 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
26 self.port = test_support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
27 self.serv.listen(1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
28
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
29 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
30 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
31 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
32
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
33 class SocketUDPTest(unittest.TestCase):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
34
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
35 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
36 self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
37 self.port = test_support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
38
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
39 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
40 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
41 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
42
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
43 class ThreadableTest:
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
44 """Threadable Test class
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
45
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
46 The ThreadableTest class makes it easy to create a threaded
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
47 client/server pair from an existing unit test. To create a
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
48 new threaded class from an existing unit test, use multiple
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
49 inheritance:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
50
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
51 class NewClass (OldClass, ThreadableTest):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
52 pass
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
53
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
54 This class defines two new fixture functions with obvious
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
55 purposes for overriding:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
56
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
57 clientSetUp ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
58 clientTearDown ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
59
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
60 Any new test functions within the class must then define
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
61 tests in pairs, where the test name is preceeded with a
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
62 '_' to indicate the client portion of the test. Ex:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
63
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
64 def testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
65 # Server portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
66
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
67 def _testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
68 # Client portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
69
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
70 Any exceptions raised by the clients during their tests
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
71 are caught and transferred to the main thread to alert
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
72 the testing framework.
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
73
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
74 Note, the server setup function cannot call any blocking
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
75 functions that rely on the client thread during setup,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
76 unless serverExplicitReady() is called just before
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
77 the blocking call (such as in setting up a client/server
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
78 connection and performing the accept() in setUp().
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
79 """
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
80
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
81 def __init__(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
82 # Swap the true setup function
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
83 self.__setUp = self.setUp
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
84 self.__tearDown = self.tearDown
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
85 self.setUp = self._setUp
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
86 self.tearDown = self._tearDown
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
87
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
88 def serverExplicitReady(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
89 """This method allows the server to explicitly indicate that
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
90 it wants the client thread to proceed. This is useful if the
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
91 server is about to execute a blocking routine that is
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
92 dependent upon the client thread during its setup routine."""
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
93 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
94
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
95 def _setUp(self):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
96 self.server_ready = threading.Event()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
97 self.client_ready = threading.Event()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
98 self.done = threading.Event()
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
99 self.queue = Queue.Queue(1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
100
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
101 # Do some munging to start the client test.
23815
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
102 methodname = self.id()
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
103 i = methodname.rfind('.')
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
104 methodname = methodname[i+1:]
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
105 test_method = getattr(self, '_' + methodname)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
106 self.client_thread = thread.start_new_thread(
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
107 self.clientRun, (test_method,))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
108
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
109 self.__setUp()
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47372
diff changeset
110 if not self.server_ready.is_set():
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
111 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
112 self.client_ready.wait()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
113
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
114 def _tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
115 self.__tearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
116 self.done.wait()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
117
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
118 if not self.queue.empty():
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
119 msg = self.queue.get()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
120 self.fail(msg)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
121
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
122 def clientRun(self, test_func):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
123 self.server_ready.wait()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
124 self.client_ready.set()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
125 self.clientSetUp()
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
126 if not callable(test_func):
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
127 raise TypeError, "test_func must be a callable function"
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
128 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
129 test_func()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
130 except Exception, strerror:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
131 self.queue.put(strerror)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
132 self.clientTearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
133
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
134 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
135 raise NotImplementedError, "clientSetUp must be implemented."
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
136
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
137 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
138 self.done.set()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
139 thread.exit()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
140
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
141 class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
142
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
143 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
144 SocketTCPTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
145 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
146
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
147 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
148 self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
149
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
150 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
151 self.cli.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
152 self.cli = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
153 ThreadableTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
154
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
155 class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest):
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
156
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
157 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
158 SocketUDPTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
159 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
160
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
161 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
162 self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
163
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
164 class SocketConnectedTest(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
165
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
166 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
167 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
168
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
169 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
170 ThreadedTCPSocketTest.setUp(self)
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
171 # Indicate explicitly we're ready for the client thread to
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
172 # proceed and then perform the blocking call to accept
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
173 self.serverExplicitReady()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
174 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
175 self.cli_conn = conn
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
176
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
177 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
178 self.cli_conn.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
179 self.cli_conn = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
180 ThreadedTCPSocketTest.tearDown(self)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
181
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
182 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
183 ThreadedTCPSocketTest.clientSetUp(self)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
184 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
185 self.serv_conn = self.cli
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
186
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
187 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
188 self.serv_conn.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
189 self.serv_conn = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
190 ThreadedTCPSocketTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
191
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
192 class SocketPairTest(unittest.TestCase, ThreadableTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
193
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
194 def __init__(self, methodName='runTest'):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
195 unittest.TestCase.__init__(self, methodName=methodName)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
196 ThreadableTest.__init__(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
197
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
198 def setUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
199 self.serv, self.cli = socket.socketpair()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
200
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
201 def tearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
202 self.serv.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
203 self.serv = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
204
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
205 def clientSetUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
206 pass
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
207
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
208 def clientTearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
209 self.cli.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
210 self.cli = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
211 ThreadableTest.clientTearDown(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
212
33076
836e77266f97 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33063
diff changeset
213
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
214 #######################################################################
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
215 ## Begin Tests
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
216
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
217 class GeneralModuleTests(unittest.TestCase):
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
218
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
219 def test_weakref(self):
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
220 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
221 p = proxy(s)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
222 self.assertEqual(p.fileno(), s.fileno())
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
223 s.close()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
224 s = None
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
225 try:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
226 p.fileno()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
227 except ReferenceError:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
228 pass
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
229 else:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
230 self.fail('Socket proxy still exists')
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
231
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
232 def testSocketError(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
233 # Testing socket module exceptions
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
234 def raise_error(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
235 raise socket.error
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
236 def raise_herror(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
237 raise socket.herror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
238 def raise_gaierror(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
239 raise socket.gaierror
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
240 self.assertRaises(socket.error, raise_error,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
241 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
242 self.assertRaises(socket.error, raise_herror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
243 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
244 self.assertRaises(socket.error, raise_gaierror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
245 "Error raising socket exception.")
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
246
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
247 def testCrucialConstants(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
248 # Testing for mission critical constants
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
249 socket.AF_INET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
250 socket.SOCK_STREAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
251 socket.SOCK_DGRAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
252 socket.SOCK_RAW
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
253 socket.SOCK_RDM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
254 socket.SOCK_SEQPACKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
255 socket.SOL_SOCKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
256 socket.SO_REUSEADDR
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
257
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
258 def testHostnameRes(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
259 # Testing hostname resolution mechanisms
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
260 hostname = socket.gethostname()
26799
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
261 try:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
262 ip = socket.gethostbyname(hostname)
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
263 except socket.error:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
264 # Probably name lookup wasn't set up right; skip this test
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
265 return
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
266 self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.")
26800
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
267 try:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
268 hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
269 except socket.error:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
270 # Probably a similar problem as above; skip this test
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
271 return
34739
0c82a7ef9554 Fix test_socket's test for socket.getfqdn() to also accept the result from
Brett Cannon <bcannon@gmail.com>
parents: 34014
diff changeset
272 all_host_names = [hostname, hname] + aliases
37422
e956e16e7444 Deal with openbsd's different style of default /etc/hosts by forcing the fqdn
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37227
diff changeset
273 fqhn = socket.getfqdn(ip)
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
274 if not fqhn in all_host_names:
37227
8d57d25e6548 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 37225
diff changeset
275 self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
276
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
277 def testRefCountGetNameInfo(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
278 # Testing reference count for getnameinfo
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
279 if hasattr(sys, "getrefcount"):
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
280 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
281 # On some versions, this loses a reference
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
282 orig = sys.getrefcount(__name__)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
283 socket.getnameinfo(__name__,0)
56107
6a278e4d9f61 #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
Georg Brandl <georg@python.org>
parents: 55627
diff changeset
284 except TypeError:
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
285 if sys.getrefcount(__name__) <> orig:
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
286 self.fail("socket.getnameinfo loses a reference")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
287
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
288 def testInterpreterCrash(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
289 # Making sure getnameinfo doesn't crash the interpreter
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
290 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
291 # On some versions, this crashes the interpreter.
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
292 socket.getnameinfo(('x', 0, 0, 0), 0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
293 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
294 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
295
25285
4d6f8204234a Since it tests both ntohl and ntohs, the test should not be called
Guido van Rossum <guido@python.org>
parents: 25256
diff changeset
296 def testNtoH(self):
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
297 # This just checks that htons etc. are their own inverse,
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
298 # when looking at the lower 16 or 32 bits.
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
299 sizes = {socket.htonl: 32, socket.ntohl: 32,
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
300 socket.htons: 16, socket.ntohs: 16}
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
301 for func, size in sizes.items():
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
302 mask = (1L<<size) - 1
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
303 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
304 self.assertEqual(i & mask, func(func(i&mask)) & mask)
24450
747fa65a9b47 Repair testNtoH for large long arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24375
diff changeset
305
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
306 swapped = func(mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
307 self.assertEqual(swapped & mask, mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
308 self.assertRaises(OverflowError, func, 1L<<34)
24375
89ce62126664 Extended socket.htonl and ntohl to accept longs.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24374
diff changeset
309
40632
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
310 def testNtoHErrors(self):
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
311 good_values = [ 1, 2, 3, 1L, 2L, 3L ]
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
312 bad_values = [ -1, -2, -3, -1L, -2L, -3L ]
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
313 for k in good_values:
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
314 socket.ntohl(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
315 socket.ntohs(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
316 socket.htonl(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
317 socket.htons(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
318 for k in bad_values:
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
319 self.assertRaises(OverflowError, socket.ntohl, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
320 self.assertRaises(OverflowError, socket.ntohs, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
321 self.assertRaises(OverflowError, socket.htonl, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
322 self.assertRaises(OverflowError, socket.htons, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
323
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
324 def testGetServBy(self):
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
325 eq = self.assertEqual
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
326 # Find one service that exists, then check all the related interfaces.
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
327 # I've ordered this by protocols that have both a tcp and udp
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
328 # protocol, at least for modern Linuxes.
34014
8daa5a448403 Add 'linux2' as one of the platforms that does not use the echo service as one
Brett Cannon <bcannon@gmail.com>
parents: 33180
diff changeset
329 if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
43703
365913d7af7d Enable platform-specific tweaks for FreeBSD 8 (exactly same to FreeBSD 7's yet)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 41532
diff changeset
330 'freebsd7', 'freebsd8', 'darwin'):
32630
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
331 # avoid the 'echo' service on this platform, as there is an
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
332 # assumption breaking non-standard port/protocol entry
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
333 services = ('daytime', 'qotd', 'domain')
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
334 else:
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
335 services = ('echo', 'daytime', 'domain')
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
336 for service in services:
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
337 try:
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
338 port = socket.getservbyname(service, 'tcp')
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
339 break
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
340 except socket.error:
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
341 pass
31503
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
342 else:
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
343 raise socket.error
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
344 # Try same call with optional protocol omitted
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
345 port2 = socket.getservbyname(service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
346 eq(port, port2)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
347 # Try udp, but don't barf it it doesn't exist
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
348 try:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
349 udpport = socket.getservbyname(service, 'udp')
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
350 except socket.error:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
351 udpport = None
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
352 else:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
353 eq(udpport, port)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
354 # Now make sure the lookup by port returns the same service name
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
355 eq(socket.getservbyport(port2), service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
356 eq(socket.getservbyport(port, 'tcp'), service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
357 if udpport is not None:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
358 eq(socket.getservbyport(udpport, 'udp'), service)
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
359 # Make sure getservbyport does not accept out of range ports.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
360 self.assertRaises(OverflowError, socket.getservbyport, -1)
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
361 self.assertRaises(OverflowError, socket.getservbyport, 65536)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
362
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
363 def testDefaultTimeout(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
364 # Testing default timeout
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
365 # The default timeout should initially be None
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
366 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
367 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
368 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
369 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
370
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
371 # Set the default timeout to 10, and see if it propagates
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
372 socket.setdefaulttimeout(10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
373 self.assertEqual(socket.getdefaulttimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
374 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
375 self.assertEqual(s.gettimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
376 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
377
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
378 # Reset the default timeout to None, and see if it propagates
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
379 socket.setdefaulttimeout(None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
380 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
381 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
382 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
383 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
384
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
385 # Check that setting it to an invalid value raises ValueError
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
386 self.assertRaises(ValueError, socket.setdefaulttimeout, -1)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
387
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
388 # Check that setting it to an invalid type raises TypeError
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
389 self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
390
51538
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
391 def testIPv4_inet_aton_fourbytes(self):
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
392 if not hasattr(socket, 'inet_aton'):
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
393 return # No inet_aton, nothing to check
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
394 # Test that issue1008086 and issue767150 are fixed.
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
395 # It must return 4 bytes.
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
396 self.assertEquals('\x00'*4, socket.inet_aton('0.0.0.0'))
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
397 self.assertEquals('\xff'*4, socket.inet_aton('255.255.255.255'))
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
398
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
399 def testIPv4toString(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
400 if not hasattr(socket, 'inet_pton'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
401 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
402 from socket import inet_aton as f, inet_pton, AF_INET
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
403 g = lambda a: inet_pton(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
404
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
405 self.assertEquals('\x00\x00\x00\x00', f('0.0.0.0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
406 self.assertEquals('\xff\x00\xff\x00', f('255.0.255.0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
407 self.assertEquals('\xaa\xaa\xaa\xaa', f('170.170.170.170'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
408 self.assertEquals('\x01\x02\x03\x04', f('1.2.3.4'))
35453
3855a960a6aa patch [ 756021 ] Allow socket.inet_aton("255.255.255.255") on Windows
Georg Brandl <georg@python.org>
parents: 35238
diff changeset
409 self.assertEquals('\xff\xff\xff\xff', f('255.255.255.255'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
410
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
411 self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
412 self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
413 self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170'))
35453
3855a960a6aa patch [ 756021 ] Allow socket.inet_aton("255.255.255.255") on Windows
Georg Brandl <georg@python.org>
parents: 35238
diff changeset
414 self.assertEquals('\xff\xff\xff\xff', g('255.255.255.255'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
415
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
416 def testIPv6toString(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
417 if not hasattr(socket, 'inet_pton'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
418 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
419 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
420 from socket import inet_pton, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
421 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
422 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
423 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
424 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
425 f = lambda a: inet_pton(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
426
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
427 self.assertEquals('\x00' * 16, f('::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
428 self.assertEquals('\x00' * 16, f('0::0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
429 self.assertEquals('\x00\x01' + '\x00' * 14, f('1::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
430 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
431 '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
432 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
433 )
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
434
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
435 def testStringToIPv4(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
436 if not hasattr(socket, 'inet_ntop'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
437 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
438 from socket import inet_ntoa as f, inet_ntop, AF_INET
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
439 g = lambda a: inet_ntop(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
440
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
441 self.assertEquals('1.0.1.0', f('\x01\x00\x01\x00'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
442 self.assertEquals('170.85.170.85', f('\xaa\x55\xaa\x55'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
443 self.assertEquals('255.255.255.255', f('\xff\xff\xff\xff'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
444 self.assertEquals('1.2.3.4', f('\x01\x02\x03\x04'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
445
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
446 self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
447 self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
448 self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
449
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
450 def testStringToIPv6(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
451 if not hasattr(socket, 'inet_ntop'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
452 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
453 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
454 from socket import inet_ntop, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
455 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
456 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
457 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
458 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
459 f = lambda a: inet_ntop(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
460
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
461 self.assertEquals('::', f('\x00' * 16))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
462 self.assertEquals('::1', f('\x00' * 15 + '\x01'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
463 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
464 'aef:b01:506:1001:ffff:9997:55:170',
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
465 f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
466 )
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
467
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
468 # XXX The following don't test module-level functionality...
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
469
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
470 def _get_unused_port(self, bind_address='0.0.0.0'):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
471 """Use a temporary socket to elicit an unused ephemeral port.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
472
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
473 Args:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
474 bind_address: Hostname or IP address to search for a port on.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
475
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
476 Returns: A most likely to be unused port.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
477 """
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
478 tempsock = socket.socket()
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
479 tempsock.bind((bind_address, 0))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
480 host, port = tempsock.getsockname()
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
481 tempsock.close()
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
482 return port
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
483
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
484 def testSockName(self):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
485 # Testing getsockname()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
486 port = self._get_unused_port()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
487 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
488 sock.bind(("0.0.0.0", port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
489 name = sock.getsockname()
38757
1fa78fd1e933 Get test to pass on S/390. Shout if you think this change is incorrect.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38580
diff changeset
490 # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
1fa78fd1e933 Get test to pass on S/390. Shout if you think this change is incorrect.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38580
diff changeset
491 # it reasonable to get the host's addr in addition to 0.0.0.0.
1fa78fd1e933 Get test to pass on S/390. Shout if you think this change is incorrect.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38580
diff changeset
492 # At least for eCos. This is required for the S/390 to pass.
1fa78fd1e933 Get test to pass on S/390. Shout if you think this change is incorrect.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38580
diff changeset
493 my_ip_addr = socket.gethostbyname(socket.gethostname())
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
494 self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0])
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
495 self.assertEqual(name[1], port)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
496
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
497 def testGetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
498 # Testing getsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
499 # We know a socket should start without reuse==0
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
500 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
501 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
502 self.assertFalse(reuse != 0, "initial mode is reuse")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
503
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
504 def testSetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
505 # Testing setsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
506 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
507 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
508 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
509 self.assertFalse(reuse == 0, "failed to set reuse mode")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
510
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
511 def testSendAfterClose(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
512 # testing send() after close() with timeout
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
513 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
514 sock.settimeout(1)
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
515 sock.close()
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
516 self.assertRaises(socket.error, sock.send, "spam")
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
517
37123
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
518 def testNewAttributes(self):
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
519 # testing .family, .type and .protocol
37069
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
520 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37123
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
521 self.assertEqual(sock.family, socket.AF_INET)
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
522 self.assertEqual(sock.type, socket.SOCK_STREAM)
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
523 self.assertEqual(sock.proto, 0)
37069
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
524 sock.close()
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
525
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
526 def test_getsockaddrarg(self):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
527 host = '0.0.0.0'
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
528 port = self._get_unused_port(bind_address=host)
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
529 big_port = port + 65536
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
530 neg_port = port - 65536
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
531 sock = socket.socket()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
532 try:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
533 self.assertRaises(OverflowError, sock.bind, (host, big_port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
534 self.assertRaises(OverflowError, sock.bind, (host, neg_port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
535 sock.bind((host, port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
536 finally:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
537 sock.close()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
538
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
539 def test_sock_ioctl(self):
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
540 if os.name != "nt":
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
541 return
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
542 self.assertTrue(hasattr(socket.socket, 'ioctl'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
543 self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
544 self.assertTrue(hasattr(socket, 'RCVALL_ON'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
545 self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
56212
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
546 self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS'))
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
547 s = socket.socket()
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
548 self.assertRaises(ValueError, s.ioctl, -1, None)
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
549 s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
550
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
551
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
552 class BasicTCPTest(SocketConnectedTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
553
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
554 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
555 SocketConnectedTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
556
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
557 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
558 # Testing large receive over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
559 msg = self.cli_conn.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
560 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
561
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
562 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
563 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
564
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
565 def testOverFlowRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
566 # Testing receive in chunks over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
567 seg1 = self.cli_conn.recv(len(MSG) - 3)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
568 seg2 = self.cli_conn.recv(1024)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
569 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
570 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
571
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
572 def _testOverFlowRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
573 self.serv_conn.send(MSG)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
574
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
575 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
576 # Testing large recvfrom() over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
577 msg, addr = self.cli_conn.recvfrom(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
578 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
579
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
580 def _testRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
581 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
582
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
583 def testOverFlowRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
584 # Testing recvfrom() in chunks over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
585 seg1, addr = self.cli_conn.recvfrom(len(MSG)-3)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
586 seg2, addr = self.cli_conn.recvfrom(1024)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
587 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
588 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
589
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
590 def _testOverFlowRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
591 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
592
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
593 def testSendAll(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
594 # Testing sendall() with a 2048 byte string over TCP
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
595 msg = ''
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
596 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
597 read = self.cli_conn.recv(1024)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
598 if not read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
599 break
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
600 msg += read
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
601 self.assertEqual(msg, 'f' * 2048)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
602
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
603 def _testSendAll(self):
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
604 big_chunk = 'f' * 2048
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
605 self.serv_conn.sendall(big_chunk)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
606
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
607 def testFromFd(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
608 # Testing fromfd()
23797
ba29485d0720 Argh. Typo. :-(
Guido van Rossum <guido@python.org>
parents: 23796
diff changeset
609 if not hasattr(socket, "fromfd"):
23796
fcc36ee1924b Allow absent fromfd(), for Windows.
Guido van Rossum <guido@python.org>
parents: 23795
diff changeset
610 return # On Windows, this doesn't exist
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
611 fd = self.cli_conn.fileno()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
612 sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
613 msg = sock.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
614 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
615
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
616 def _testFromFd(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
617 self.serv_conn.send(MSG)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
618
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
619 def testShutdown(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
620 # Testing shutdown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
621 msg = self.cli_conn.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
622 self.assertEqual(msg, MSG)
50796
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
623 # wait for _testShutdown to finish: on OS X, when the server
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
624 # closes the connection the client also becomes disconnected,
50797
ae5ed15774b4 Comment typo
Mark Dickinson <dickinsm@gmail.com>
parents: 50796
diff changeset
625 # and the client's shutdown call will fail. (Issue #4397.)
50796
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
626 self.done.wait()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
627
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
628 def _testShutdown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
629 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
630 self.serv_conn.shutdown(2)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
631
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
632 class BasicUDPTest(ThreadedUDPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
633
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
634 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
635 ThreadedUDPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
636
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
637 def testSendtoAndRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
638 # Testing sendto() and Recv() over UDP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
639 msg = self.serv.recv(len(MSG))
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
640 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
641
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
642 def _testSendtoAndRecv(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
643 self.cli.sendto(MSG, 0, (HOST, self.port))
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
644
23798
1da6d67f4a05 Some provisional changes to get more tests to run on Windows (I hope).
Guido van Rossum <guido@python.org>
parents: 23797
diff changeset
645 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
646 # Testing recvfrom() over UDP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
647 msg, addr = self.serv.recvfrom(len(MSG))
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
648 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
649
23798
1da6d67f4a05 Some provisional changes to get more tests to run on Windows (I hope).
Guido van Rossum <guido@python.org>
parents: 23797
diff changeset
650 def _testRecvFrom(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
651 self.cli.sendto(MSG, 0, (HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
652
41313
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
653 def testRecvFromNegative(self):
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
654 # Negative lengths passed to recvfrom should give ValueError.
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
655 self.assertRaises(ValueError, self.serv.recvfrom, -1)
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
656
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
657 def _testRecvFromNegative(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
658 self.cli.sendto(MSG, 0, (HOST, self.port))
41313
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
659
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
660 class TCPCloserTest(ThreadedTCPSocketTest):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
661
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
662 def testClose(self):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
663 conn, addr = self.serv.accept()
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
664 conn.close()
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
665
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
666 sd = self.cli
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
667 read, write, err = select.select([sd], [], [], 1.0)
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
668 self.assertEqual(read, [sd])
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
669 self.assertEqual(sd.recv(1), '')
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
670
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
671 def _testClose(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
672 self.cli.connect((HOST, self.port))
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
673 time.sleep(1.0)
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
674
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
675 class BasicSocketPairTest(SocketPairTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
676
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
677 def __init__(self, methodName='runTest'):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
678 SocketPairTest.__init__(self, methodName=methodName)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
679
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
680 def testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
681 msg = self.serv.recv(1024)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
682 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
683
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
684 def _testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
685 self.cli.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
686
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
687 def testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
688 self.serv.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
689
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
690 def _testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
691 msg = self.cli.recv(1024)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
692 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
693
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
694 class NonBlockingTCPTests(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
695
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
696 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
697 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
698
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
699 def testSetBlocking(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
700 # Testing whether set blocking works
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
701 self.serv.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
702 start = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
703 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
704 self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
705 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
706 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
707 end = time.time()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
708 self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
709
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
710 def _testSetBlocking(self):
17208
c8817a9bbbdd Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents: 16519
diff changeset
711 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
712
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
713 def testAccept(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
714 # Testing non-blocking accept
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
715 self.serv.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
716 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
717 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
718 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
719 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
720 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
721 self.fail("Error trying to do non-blocking accept.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
722 read, write, err = select.select([self.serv], [], [])
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
723 if self.serv in read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
724 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
725 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
726 self.fail("Error trying to do accept after select.")
20853
77bb2ae067c2 Correct getnameinfo refcounting and tuple parsing. Fixes #476648.
Martin v. Löwis <martin@v.loewis.de>
parents: 20733
diff changeset
727
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
728 def _testAccept(self):
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
729 time.sleep(0.1)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
730 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
731
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
732 def testConnect(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
733 # Testing non-blocking connect
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
734 conn, addr = self.serv.accept()
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
735
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
736 def _testConnect(self):
23818
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
737 self.cli.settimeout(10)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
738 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
739
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
740 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
741 # Testing non-blocking recv
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
742 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
743 conn.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
744 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
745 msg = conn.recv(len(MSG))
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
746 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
747 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
748 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
749 self.fail("Error trying to do non-blocking recv.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
750 read, write, err = select.select([conn], [], [])
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
751 if conn in read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
752 msg = conn.recv(len(MSG))
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
753 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
754 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
755 self.fail("Error during select call to non-blocking socket.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
756
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
757 def _testRecv(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
758 self.cli.connect((HOST, self.port))
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
759 time.sleep(0.1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
760 self.cli.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
761
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
762 class FileObjectClassTestCase(SocketConnectedTest):
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
763
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
764 bufsize = -1 # Use default buffer size
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
765
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
766 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
767 SocketConnectedTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
768
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
769 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
770 SocketConnectedTest.setUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
771 self.serv_file = self.cli_conn.makefile('rb', self.bufsize)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
772
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
773 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
774 self.serv_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
775 self.assertTrue(self.serv_file.closed)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
776 self.serv_file = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
777 SocketConnectedTest.tearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
778
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
779 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
780 SocketConnectedTest.clientSetUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
781 self.cli_file = self.serv_conn.makefile('wb')
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
782
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
783 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
784 self.cli_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
785 self.assertTrue(self.cli_file.closed)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
786 self.cli_file = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
787 SocketConnectedTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
788
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
789 def testSmallRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
790 # Performing small file read test
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
791 first_seg = self.serv_file.read(len(MSG)-3)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
792 second_seg = self.serv_file.read(3)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
793 msg = first_seg + second_seg
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
794 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
795
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
796 def _testSmallRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
797 self.cli_file.write(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
798 self.cli_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
799
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
800 def testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
801 # read until EOF
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
802 msg = self.serv_file.read()
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
803 self.assertEqual(msg, MSG)
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
804
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
805 def _testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
806 self.cli_file.write(MSG)
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
807 self.cli_file.close()
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
808
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
809 def testUnbufferedRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
810 # Performing unbuffered file read test
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
811 buf = ''
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
812 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
813 char = self.serv_file.read(1)
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
814 if not char:
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
815 break
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
816 buf += char
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
817 self.assertEqual(buf, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
818
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
819 def _testUnbufferedRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
820 self.cli_file.write(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
821 self.cli_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
822
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
823 def testReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
824 # Performing file readline test
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
825 line = self.serv_file.readline()
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
826 self.assertEqual(line, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
827
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
828 def _testReadline(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
829 self.cli_file.write(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
830 self.cli_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
831
46684
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
832 def testReadlineAfterRead(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
833 a_baloo_is = self.serv_file.read(len("A baloo is"))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
834 self.assertEqual("A baloo is", a_baloo_is)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
835 _a_bear = self.serv_file.read(len(" a bear"))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
836 self.assertEqual(" a bear", _a_bear)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
837 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
838 self.assertEqual("\n", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
839 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
840 self.assertEqual("A BALOO IS A BEAR.\n", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
841 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
842 self.assertEqual(MSG, line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
843
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
844 def _testReadlineAfterRead(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
845 self.cli_file.write("A baloo is a bear\n")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
846 self.cli_file.write("A BALOO IS A BEAR.\n")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
847 self.cli_file.write(MSG)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
848 self.cli_file.flush()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
849
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
850 def testReadlineAfterReadNoNewline(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
851 end_of_ = self.serv_file.read(len("End Of "))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
852 self.assertEqual("End Of ", end_of_)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
853 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
854 self.assertEqual("Line", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
855
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
856 def _testReadlineAfterReadNoNewline(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
857 self.cli_file.write("End Of Line")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
858
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
859 def testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
860 self.assertTrue(not self.serv_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
861
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
862 def _testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
863 self.assertTrue(not self.cli_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
864
55627
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
865
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
866 class FileObjectInterruptedTestCase(unittest.TestCase):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
867 """Test that the file object correctly handles EINTR internally."""
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
868
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
869 class MockSocket(object):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
870 def __init__(self, recv_funcs=()):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
871 # A generator that returns callables that we'll call for each
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
872 # call to recv().
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
873 self._recv_step = iter(recv_funcs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
874
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
875 def recv(self, size):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
876 return self._recv_step.next()()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
877
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
878 @staticmethod
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
879 def _raise_eintr():
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
880 raise socket.error(errno.EINTR)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
881
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
882 def _test_readline(self, size=-1, **kwargs):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
883 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
884 lambda : "This is the first line\nAnd the sec",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
885 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
886 lambda : "ond line is here\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
887 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
888 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
889 fo = socket._fileobject(mock_sock, **kwargs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
890 self.assertEquals(fo.readline(size), "This is the first line\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
891 self.assertEquals(fo.readline(size), "And the second line is here\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
892
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
893 def _test_read(self, size=-1, **kwargs):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
894 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
895 lambda : "This is the first line\nAnd the sec",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
896 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
897 lambda : "ond line is here\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
898 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
899 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
900 fo = socket._fileobject(mock_sock, **kwargs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
901 self.assertEquals(fo.read(size), "This is the first line\n"
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
902 "And the second line is here\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
903
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
904 def test_default(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
905 self._test_readline()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
906 self._test_readline(size=100)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
907 self._test_read()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
908 self._test_read(size=100)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
909
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
910 def test_with_1k_buffer(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
911 self._test_readline(bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
912 self._test_readline(size=100, bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
913 self._test_read(bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
914 self._test_read(size=100, bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
915
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
916 def _test_readline_no_buffer(self, size=-1):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
917 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
918 lambda : "aa",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
919 lambda : "\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
920 lambda : "BB",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
921 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
922 lambda : "bb",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
923 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
924 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
925 fo = socket._fileobject(mock_sock, bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
926 self.assertEquals(fo.readline(size), "aa\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
927 self.assertEquals(fo.readline(size), "BBbb")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
928
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
929 def test_no_buffer(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
930 self._test_readline_no_buffer()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
931 self._test_readline_no_buffer(size=4)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
932 self._test_read(bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
933 self._test_read(size=100, bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
934
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
935
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
936 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
937
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
938 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24655
diff changeset
939
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
940 In this case (and in this case only), it should be possible to
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
941 create a file object, read a line from it, create another file
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
942 object, read another line from it, without loss of data in the
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
943 first file object's buffer. Note that httplib relies on this
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
944 when reading multiple requests from the same socket."""
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
945
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
946 bufsize = 0 # Use unbuffered mode
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
947
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
948 def testUnbufferedReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
949 # Read a line, create a new file object, read another line with it
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
950 line = self.serv_file.readline() # first line
24648
edf757213a92 Tighten the unbuffered readline test to distinguish between the two lines.
Guido van Rossum <guido@python.org>
parents: 24639
diff changeset
951 self.assertEqual(line, "A. " + MSG) # first line
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
952 self.serv_file = self.cli_conn.makefile('rb', 0)
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
953 line = self.serv_file.readline() # second line
24648
edf757213a92 Tighten the unbuffered readline test to distinguish between the two lines.
Guido van Rossum <guido@python.org>
parents: 24639
diff changeset
954 self.assertEqual(line, "B. " + MSG) # second line
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
955
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
956 def _testUnbufferedReadline(self):
24648
edf757213a92 Tighten the unbuffered readline test to distinguish between the two lines.
Guido van Rossum <guido@python.org>
parents: 24639
diff changeset
957 self.cli_file.write("A. " + MSG)
edf757213a92 Tighten the unbuffered readline test to distinguish between the two lines.
Guido van Rossum <guido@python.org>
parents: 24639
diff changeset
958 self.cli_file.write("B. " + MSG)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
959 self.cli_file.flush()
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
960
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
961 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
962
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
963 bufsize = 1 # Default-buffered for reading; line-buffered for writing
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
964
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
965
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
966 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
967
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
968 bufsize = 2 # Exercise the buffering code
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
969
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
970
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
971 class NetworkConnectionTest(object):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
972 """Prove network connection."""
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
973 def clientSetUp(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
974 # We're inherited below by BasicTCPTest2, which also inherits
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
975 # BasicTCPTest, which defines self.port referenced below.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
976 self.cli = socket.create_connection((HOST, self.port))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
977 self.serv_conn = self.cli
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
978
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
979 class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
980 """Tests that NetworkConnection does not break existing TCP functionality.
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
981 """
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
982
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
983 class NetworkConnectionNoServer(unittest.TestCase):
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
984 def testWithoutServer(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
985 port = test_support.find_unused_port()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
986 self.assertRaises(
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
987 socket.error,
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
988 lambda: socket.create_connection((HOST, port))
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
989 )
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
990
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
991 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
992
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
993 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
994 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
995 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
996
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
997 def clientSetUp(self):
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
998 self.source_port = test_support.find_unused_port()
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
999
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1000 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1001 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1002 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1003 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1004
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1005 def _justAccept(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1006 conn, addr = self.serv.accept()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1007
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1008 testFamily = _justAccept
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1009 def _testFamily(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1010 self.cli = socket.create_connection((HOST, self.port), timeout=30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1011 self.assertEqual(self.cli.family, 2)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1012
58135
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1013 testSourceAddress = _justAccept
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1014 def _testSourceAddress(self):
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1015 self.cli = socket.create_connection((HOST, self.port), timeout=30,
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1016 source_address=('', self.source_port))
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1017 self.assertEqual(self.cli.getsockname()[1], self.source_port)
58135
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1018 # The port number being used is sufficient to show that the bind()
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1019 # call happened.
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1020
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1021 testTimeoutDefault = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1022 def _testTimeoutDefault(self):
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1023 # passing no explicit timeout uses socket's global default
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1024 self.assertTrue(socket.getdefaulttimeout() is None)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1025 socket.setdefaulttimeout(42)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1026 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1027 self.cli = socket.create_connection((HOST, self.port))
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1028 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1029 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1030 self.assertEquals(self.cli.gettimeout(), 42)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1031
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1032 testTimeoutNone = _justAccept
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1033 def _testTimeoutNone(self):
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1034 # None timeout means the same as sock.settimeout(None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1035 self.assertTrue(socket.getdefaulttimeout() is None)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1036 socket.setdefaulttimeout(30)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1037 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1038 self.cli = socket.create_connection((HOST, self.port), timeout=None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1039 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1040 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1041 self.assertEqual(self.cli.gettimeout(), None)
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1042
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1043 testTimeoutValueNamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1044 def _testTimeoutValueNamed(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1045 self.cli = socket.create_connection((HOST, self.port), timeout=30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1046 self.assertEqual(self.cli.gettimeout(), 30)
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1047
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1048 testTimeoutValueNonamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1049 def _testTimeoutValueNonamed(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1050 self.cli = socket.create_connection((HOST, self.port), 30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1051 self.assertEqual(self.cli.gettimeout(), 30)
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1052
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1053 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1054
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1055 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1056 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1057 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1058
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1059 def clientSetUp(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1060 pass
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1061
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1062 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1063 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1064 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1065 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1066
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1067 def testInsideTimeout(self):
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1068 conn, addr = self.serv.accept()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1069 time.sleep(3)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1070 conn.send("done!")
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1071 testOutsideTimeout = testInsideTimeout
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1072
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1073 def _testInsideTimeout(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1074 self.cli = sock = socket.create_connection((HOST, self.port))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1075 data = sock.recv(5)
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1076 self.assertEqual(data, "done!")
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1077
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1078 def _testOutsideTimeout(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1079 self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1080 self.assertRaises(socket.timeout, lambda: sock.recv(5))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1081
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1082
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1083 class Urllib2FileobjectTest(unittest.TestCase):
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1084
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1085 # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1086 # it close the socket if the close c'tor argument is true
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1087
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1088 def testClose(self):
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1089 class MockSocket:
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1090 closed = False
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1091 def flush(self): pass
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1092 def close(self): self.closed = True
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1093
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1094 # must not close unless we request it: the original use of _fileobject
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1095 # by module socket requires that the underlying socket not be closed until
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1096 # the _socketobject that created the _fileobject is closed
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1097 s = MockSocket()
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1098 f = socket._fileobject(s)
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1099 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1100 self.assertTrue(not s.closed)
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1101
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1102 s = MockSocket()
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1103 f = socket._fileobject(s, close=True)
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1104 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1105 self.assertTrue(s.closed)
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1106
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1107 class TCPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1108
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1109 def testTCPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1110 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1111 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1112 self.serv.accept()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1113 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1114 "Error generating a timeout exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1115
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1116 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1117 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1118 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1119 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1120 foo = self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1121 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1122 self.fail("caught timeout instead of error (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1123 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1124 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1125 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1126 self.fail("caught unexpected exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1127 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1128 self.fail("accept() returned success when we did not expect it")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1129
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1130 def testInterruptedTimeout(self):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1131 # XXX I don't know how to do this test on MSWindows or any other
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1132 # plaform that doesn't support signal.alarm() or os.kill(), though
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1133 # the bug should have existed on all platforms.
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1134 if not hasattr(signal, "alarm"):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1135 return # can only test on *nix
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1136 self.serv.settimeout(5.0) # must be longer than alarm
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1137 class Alarm(Exception):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1138 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1139 def alarm_handler(signal, frame):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1140 raise Alarm
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1141 old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1142 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1143 signal.alarm(2) # POSIX allows alarm to be up to 1 second early
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1144 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1145 foo = self.serv.accept()
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1146 except socket.timeout:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1147 self.fail("caught timeout instead of Alarm")
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1148 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1149 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1150 except:
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1151 self.fail("caught other exception instead of Alarm:"
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1152 " %s(%s):\n%s" %
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1153 (sys.exc_info()[:2] + (traceback.format_exc(),)))
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1154 else:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1155 self.fail("nothing caught")
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1156 finally:
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1157 signal.alarm(0) # shut off alarm
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1158 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1159 self.fail("got Alarm in wrong place")
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1160 finally:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1161 # no alarm can be pending. Safe to restore old handler.
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1162 signal.signal(signal.SIGALRM, old_alarm)
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1163
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1164 class UDPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1165
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1166 def testUDPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1167 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1168 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1169 self.serv.recv(1024)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1170 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1171 "Error generating a timeout exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1172
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1173 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1174 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1175 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1176 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1177 foo = self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1178 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1179 self.fail("caught timeout instead of error (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1180 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1181 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1182 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1183 self.fail("caught unexpected exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1184 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1185 self.fail("recv() returned success when we did not expect it")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1186
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1187 class TestExceptions(unittest.TestCase):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1188
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1189 def testExceptionTree(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1190 self.assertTrue(issubclass(socket.error, Exception))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1191 self.assertTrue(issubclass(socket.herror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1192 self.assertTrue(issubclass(socket.gaierror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1193 self.assertTrue(issubclass(socket.timeout, socket.error))
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1194
37853
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1195 class TestLinuxAbstractNamespace(unittest.TestCase):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1196
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1197 UNIX_PATH_MAX = 108
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1198
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1199 def testLinuxAbstractNamespace(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1200 address = "\x00python-test-hello\x00\xff"
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1201 s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1202 s1.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1203 s1.listen(1)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1204 s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1205 s2.connect(s1.getsockname())
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1206 s1.accept()
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1207 self.assertEqual(s1.getsockname(), address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1208 self.assertEqual(s2.getpeername(), address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1209
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1210 def testMaxName(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1211 address = "\x00" + "h" * (self.UNIX_PATH_MAX - 1)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1212 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1213 s.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1214 self.assertEqual(s.getsockname(), address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1215
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1216 def testNameOverflow(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1217 address = "\x00" + "h" * self.UNIX_PATH_MAX
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1218 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1219 self.assertRaises(socket.error, s.bind, address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1220
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1221
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1222 class BufferIOTest(SocketConnectedTest):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1223 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1224 Test the buffer versions of socket.recv() and socket.send().
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1225 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1226 def __init__(self, methodName='runTest'):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1227 SocketConnectedTest.__init__(self, methodName=methodName)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1228
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1229 def testRecvInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1230 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1231 nbytes = self.cli_conn.recv_into(buf)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1232 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1233 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1234 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1235
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1236 def _testRecvInto(self):
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1237 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1238 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1239
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1240 def testRecvFromInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1241 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1242 nbytes, addr = self.cli_conn.recvfrom_into(buf)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1243 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1244 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1245 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1246
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1247 def _testRecvFromInto(self):
58198
0dd1aa33265f Reverting the Revision: 77368. I committed Flox's big patch for tests by
Senthil Kumaran <orsenthil@gmail.com>
parents: 58197
diff changeset
1248 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1249 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1250
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1251
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1252 TIPC_STYPE = 2000
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1253 TIPC_LOWER = 200
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1254 TIPC_UPPER = 210
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1255
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1256 def isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1257 """Check if the TIPC module is loaded
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1258
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1259 The TIPC module is not loaded automatically on Ubuntu and probably
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1260 other Linux distros.
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1261 """
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1262 if not hasattr(socket, "AF_TIPC"):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1263 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1264 if not os.path.isfile("/proc/modules"):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1265 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1266 with open("/proc/modules") as f:
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1267 for line in f:
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1268 if line.startswith("tipc "):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1269 return True
44592
cb22f8748da7 It's verbose, not debug
Christian Heimes <christian@cheimes.de>
parents: 44581
diff changeset
1270 if test_support.verbose:
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1271 print "TIPC module is not loaded, please 'sudo modprobe tipc'"
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1272 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1273
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1274 class TIPCTest (unittest.TestCase):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1275 def testRDM(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1276 srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1277 cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1278
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1279 srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1280 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1281 TIPC_LOWER, TIPC_UPPER)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1282 srv.bind(srvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1283
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1284 sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1285 TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1286 cli.sendto(MSG, sendaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1287
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1288 msg, recvaddr = srv.recvfrom(1024)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1289
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1290 self.assertEqual(cli.getsockname(), recvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1291 self.assertEqual(msg, MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1292
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1293
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1294 class TIPCThreadableTest (unittest.TestCase, ThreadableTest):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1295 def __init__(self, methodName = 'runTest'):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1296 unittest.TestCase.__init__(self, methodName = methodName)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1297 ThreadableTest.__init__(self)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1298
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1299 def setUp(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1300 self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1301 self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1302 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1303 TIPC_LOWER, TIPC_UPPER)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1304 self.srv.bind(srvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1305 self.srv.listen(5)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1306 self.serverExplicitReady()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1307 self.conn, self.connaddr = self.srv.accept()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1308
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1309 def clientSetUp(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1310 # The is a hittable race between serverExplicitReady() and the
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1311 # accept() call; sleep a little while to avoid it, otherwise
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1312 # we could get an exception
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1313 time.sleep(0.1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1314 self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1315 addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1316 TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1317 self.cli.connect(addr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1318 self.cliaddr = self.cli.getsockname()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1319
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1320 def testStream(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1321 msg = self.conn.recv(1024)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1322 self.assertEqual(msg, MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1323 self.assertEqual(self.cliaddr, self.connaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1324
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1325 def _testStream(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1326 self.cli.send(MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1327 self.cli.close()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1328
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1329
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1330 def test_main():
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
1331 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1332 TestExceptions, BufferIOTest, BasicTCPTest2]
25152
0c9dfd5a06d4 Skip UDP testing for MacPython (for now), it hangs. This may be due to
Jack Jansen <jack.jansen@cwi.nl>
parents: 24772
diff changeset
1333 if sys.platform != 'mac':
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1334 tests.extend([ BasicUDPTest, UDPTimeoutTest ])
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1335
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1336 tests.extend([
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1337 NonBlockingTCPTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1338 FileObjectClassTestCase,
55627
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1339 FileObjectInterruptedTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1340 UnbufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1341 LineBufferedFileObjectClassTestCase,
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1342 SmallBufferedFileObjectClassTestCase,
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1343 Urllib2FileobjectTest,
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1344 NetworkConnectionNoServer,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1345 NetworkConnectionAttributesTest,
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1346 NetworkConnectionBehaviourTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1347 ])
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1348 if hasattr(socket, "socketpair"):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1349 tests.append(BasicSocketPairTest)
37853
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1350 if sys.platform == 'linux2':
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1351 tests.append(TestLinuxAbstractNamespace)
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1352 if isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1353 tests.append(TIPCTest)
44581
6d521603e07d Fixed indention problem that caused the second TIPC test to run on systems without TIPC
Christian Heimes <christian@cheimes.de>
parents: 44561
diff changeset
1354 tests.append(TIPCThreadableTest)
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1355
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1356 thread_info = test_support.threading_setup()
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1357 test_support.run_unittest(*tests)
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1358 test_support.threading_cleanup(*thread_info)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1359
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1360 if __name__ == "__main__":
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1361 test_main()