Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_socket.py @ 51611:dfcf690a67c8 legacy-trunk
Add optional code signing after merging.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Fri, 13 Feb 2009 20:51:48 +0000 |
| parents | edcf996f07e9 |
| children | 6768b2350c57 |
| 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() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
126 if not callable(test_func): |
|
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 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
240 self.failUnlessRaises(socket.error, raise_error, |
|
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.") |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
242 self.failUnlessRaises(socket.error, raise_herror, |
|
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.") |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
244 self.failUnlessRaises(socket.error, raise_gaierror, |
|
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 |
|
23831
f7306ae8662d
Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents:
23819
diff
changeset
|
266 self.assert_(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 | 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) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
284 except SystemError: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
285 if sys.getrefcount(__name__) <> orig: |
|
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()) |
|
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
|
494 self.assert_(name[0] in ("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) |
|
23795
80d450e552f6
testSetSockOpt() should not require the reuse flag to be 1 -- any
Guido van Rossum <guido@python.org>
parents:
23794
diff
changeset
|
502 self.failIf(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) |
|
23795
80d450e552f6
testSetSockOpt() should not require the reuse flag to be 1 -- any
Guido van Rossum <guido@python.org>
parents:
23794
diff
changeset
|
509 self.failIf(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 |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
542 self.assert_(hasattr(socket.socket, 'ioctl')) |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
543 self.assert_(hasattr(socket, 'SIO_RCVALL')) |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
544 self.assert_(hasattr(socket, 'RCVALL_ON')) |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
545 self.assert_(hasattr(socket, 'RCVALL_OFF')) |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
546 |
|
73bc002252bf
socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents:
43703
diff
changeset
|
547 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
548 class BasicTCPTest(SocketConnectedTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
549 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
550 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
|
551 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
|
552 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
553 def testRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
554 # 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
|
555 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
|
556 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
|
557 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
558 def _testRecv(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
559 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
|
560 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
561 def testOverFlowRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
562 # 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
|
563 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
|
564 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
|
565 msg = seg1 + seg2 |
|
23794
1f1a6f7dd600
Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
566 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
|
567 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
568 def _testOverFlowRecv(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
569 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
|
570 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
571 def testRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
572 # 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
|
573 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
|
574 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
|
575 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
576 def _testRecvFrom(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
577 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
|
578 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
579 def testOverFlowRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
580 # 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
|
581 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
|
582 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
|
583 msg = seg1 + seg2 |
|
23794
1f1a6f7dd600
Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
584 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
|
585 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
586 def _testOverFlowRecvFrom(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
587 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
|
588 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
589 def testSendAll(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
590 # 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
|
591 msg = '' |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
592 while 1: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
593 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
|
594 if not read: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
595 break |
|
24675
fec0b0aef14b
testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents:
24674
diff
changeset
|
596 msg += read |
|
fec0b0aef14b
testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents:
24674
diff
changeset
|
597 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
|
598 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
599 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
|
600 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
|
601 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
|
602 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
603 def testFromFd(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
604 # Testing fromfd() |
| 23797 | 605 if not hasattr(socket, "fromfd"): |
|
23796
fcc36ee1924b
Allow absent fromfd(), for Windows.
Guido van Rossum <guido@python.org>
parents:
23795
diff
changeset
|
606 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
|
607 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
|
608 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
|
609 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
|
610 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
|
611 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
612 def _testFromFd(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
613 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
|
614 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
615 def testShutdown(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
616 # Testing shutdown() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
617 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
|
618 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
|
619 # 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
|
620 # closes the connection the client also becomes disconnected, |
| 50797 | 621 # 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
|
622 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
|
623 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
624 def _testShutdown(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
625 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
|
626 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
|
627 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
628 class BasicUDPTest(ThreadedUDPSocketTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
629 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
630 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
|
631 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
|
632 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
633 def testSendtoAndRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
634 # 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
|
635 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
|
636 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
|
637 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
638 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
|
639 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
|
640 |
|
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
|
641 def testRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
642 # 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
|
643 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
|
644 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
|
645 |
|
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
|
646 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
|
647 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
|
648 |
|
41313
d144f81574c0
Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents:
41300
diff
changeset
|
649 def testRecvFromNegative(self): |
|
d144f81574c0
Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents:
41300
diff
changeset
|
650 # 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
|
651 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
|
652 |
|
d144f81574c0
Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents:
41300
diff
changeset
|
653 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
|
654 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
|
655 |
|
38976
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
656 class TCPCloserTest(ThreadedTCPSocketTest): |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
657 |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
658 def testClose(self): |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
659 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
|
660 conn.close() |
|
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 sd = self.cli |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
663 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
|
664 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
|
665 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
|
666 |
|
c547f6e91c58
Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents:
38867
diff
changeset
|
667 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
|
668 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
|
669 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
|
670 |
|
33063
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
671 class BasicSocketPairTest(SocketPairTest): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
672 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
673 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
|
674 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
|
675 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
676 def testRecv(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
677 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
|
678 self.assertEqual(msg, MSG) |
|
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 self.cli.send(MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
682 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
683 def testSend(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
684 self.serv.send(MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
685 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
686 def _testSend(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
687 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
|
688 self.assertEqual(msg, MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
689 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
690 class NonBlockingTCPTests(ThreadedTCPSocketTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
691 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
692 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
|
693 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
|
694 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
695 def testSetBlocking(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
696 # 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
|
697 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
|
698 start = time.time() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
699 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
700 self.serv.accept() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
701 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
702 pass |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
703 end = time.time() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
704 self.assert_((end - start) < 1.0, "Error setting non-blocking mode.") |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
705 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
706 def _testSetBlocking(self): |
|
17208
c8817a9bbbdd
Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents:
16519
diff
changeset
|
707 pass |
|
4880
9a7e9f10aed6
Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
708 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
709 def testAccept(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
710 # 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
|
711 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
|
712 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
713 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
|
714 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
715 pass |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
716 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
717 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
|
718 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
|
719 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
|
720 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
|
721 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
722 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
|
723 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
724 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
|
725 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
|
726 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
|
727 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
728 def testConnect(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
729 # 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
|
730 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
|
731 |
|
23790
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): |
|
23818
8a0a14e3725e
Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents:
23815
diff
changeset
|
733 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
|
734 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
|
735 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
736 def testRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
737 # 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
|
738 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
|
739 conn.setblocking(0) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
740 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
741 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
|
742 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
743 pass |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
744 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
745 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
|
746 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
|
747 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
|
748 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
|
749 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
|
750 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
751 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
|
752 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
753 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
|
754 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
|
755 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
|
756 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
|
757 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
758 class FileObjectClassTestCase(SocketConnectedTest): |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21315
diff
changeset
|
759 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
760 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
|
761 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
762 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
|
763 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
|
764 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
765 def setUp(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
766 SocketConnectedTest.setUp(self) |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
767 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
|
768 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
769 def tearDown(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
770 self.serv_file.close() |
|
31833
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
771 self.assert_(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
|
772 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
|
773 SocketConnectedTest.tearDown(self) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
774 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
775 def clientSetUp(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
776 SocketConnectedTest.clientSetUp(self) |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
777 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
|
778 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
779 def clientTearDown(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
780 self.cli_file.close() |
|
31833
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
781 self.assert_(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
|
782 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
|
783 SocketConnectedTest.clientTearDown(self) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
784 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
785 def testSmallRead(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
786 # 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
|
787 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
|
788 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
|
789 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
|
790 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
|
791 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
792 def _testSmallRead(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
793 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
|
794 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
|
795 |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
796 def testFullRead(self): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
797 # read until EOF |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
798 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
|
799 self.assertEqual(msg, MSG) |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
800 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
801 def _testFullRead(self): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
802 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
|
803 self.cli_file.close() |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
804 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
805 def testUnbufferedRead(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
806 # 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
|
807 buf = '' |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
808 while 1: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
809 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
|
810 if not char: |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
811 break |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
812 buf += char |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
813 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
|
814 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
815 def _testUnbufferedRead(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
816 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
|
817 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
|
818 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
819 def testReadline(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
820 # 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
|
821 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
|
822 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
|
823 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
824 def _testReadline(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
825 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
|
826 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
|
827 |
|
46684
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46362
diff
changeset
|
828 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
|
829 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
|
830 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
|
831 _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
|
832 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
|
833 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
|
834 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
|
835 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
|
836 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
|
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(MSG, line) |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46362
diff
changeset
|
839 |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46362
diff
changeset
|
840 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
|
841 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
|
842 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
|
843 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
|
844 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
|
845 |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46362
diff
changeset
|
846 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
|
847 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
|
848 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
|
849 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
|
850 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
|
851 |
|
2729e977fdd9
Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents:
46362
diff
changeset
|
852 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
|
853 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
|
854 |
|
31833
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
855 def testClosedAttr(self): |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
856 self.assert_(not self.serv_file.closed) |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
857 |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
858 def _testClosedAttr(self): |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
859 self.assert_(not self.cli_file.closed) |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
860 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
861 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
862 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
863 """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
|
24674
3269bdd65988
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24655
diff
changeset
|
864 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
865 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
|
866 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
|
867 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
|
868 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
|
869 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
|
870 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
871 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
|
872 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
873 def testUnbufferedReadline(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
874 # 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
|
875 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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
881 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
|
882 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
|
883 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
|
884 self.cli_file.flush() |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
885 |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
886 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
887 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
888 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
|
889 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
890 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
891 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
892 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
893 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
|
894 |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
895 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
896 class NetworkConnectionTest(object): |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
897 """Prove network connection.""" |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
898 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
|
899 # 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
|
900 # 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
|
901 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
|
902 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
|
903 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
904 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
|
905 """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
|
906 """ |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
907 |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
908 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
|
909 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
|
910 port = test_support.find_unused_port() |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46298
diff
changeset
|
911 self.failUnlessRaises( |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46298
diff
changeset
|
912 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
|
913 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
|
914 ) |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
915 |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
916 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
917 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
918 def __init__(self, methodName='runTest'): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
919 SocketTCPTest.__init__(self, methodName=methodName) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
920 ThreadableTest.__init__(self) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
921 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
922 def clientSetUp(self): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
923 pass |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
924 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
925 def clientTearDown(self): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
926 self.cli.close() |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
927 self.cli = None |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
928 ThreadableTest.clientTearDown(self) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
929 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
930 def _justAccept(self): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
931 conn, addr = self.serv.accept() |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
932 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
933 testFamily = _justAccept |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
934 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
|
935 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
|
936 self.assertEqual(self.cli.family, 2) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
937 |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41313
diff
changeset
|
938 testTimeoutDefault = _justAccept |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
939 def _testTimeoutDefault(self): |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
940 # passing no explicit timeout uses socket's global default |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
941 self.assert_(socket.getdefaulttimeout() is None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
942 socket.setdefaulttimeout(42) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
943 try: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
944 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
|
945 finally: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
946 socket.setdefaulttimeout(None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
947 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
|
948 |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
949 testTimeoutNone = _justAccept |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
950 def _testTimeoutNone(self): |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
951 # None timeout means the same as sock.settimeout(None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
952 self.assert_(socket.getdefaulttimeout() is None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
953 socket.setdefaulttimeout(30) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
954 try: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
955 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
|
956 finally: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
957 socket.setdefaulttimeout(None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
47248
diff
changeset
|
958 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
|
959 |
|
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41313
diff
changeset
|
960 testTimeoutValueNamed = _justAccept |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
961 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
|
962 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
|
963 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
|
964 |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41313
diff
changeset
|
965 testTimeoutValueNonamed = _justAccept |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
966 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
|
967 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
|
968 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
|
969 |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
970 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
971 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
972 def __init__(self, methodName='runTest'): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
973 SocketTCPTest.__init__(self, methodName=methodName) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
974 ThreadableTest.__init__(self) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
975 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
976 def clientSetUp(self): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
977 pass |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
978 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
979 def clientTearDown(self): |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
980 self.cli.close() |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
981 self.cli = None |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
982 ThreadableTest.clientTearDown(self) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
983 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
984 def testInsideTimeout(self): |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
985 conn, addr = self.serv.accept() |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
986 time.sleep(3) |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
987 conn.send("done!") |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
988 testOutsideTimeout = testInsideTimeout |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
989 |
|
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
990 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
|
991 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
|
992 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
|
993 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
|
994 |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
995 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
|
996 self.cli = sock = socket.create_connection((HOST, self.port), timeout=1) |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
997 self.failUnlessRaises(socket.timeout, lambda: sock.recv(5)) |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
998 |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
999 |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1000 class Urllib2FileobjectTest(unittest.TestCase): |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1001 |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1002 # 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
|
1003 # 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
|
1004 |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1005 def testClose(self): |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1006 class MockSocket: |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1007 closed = False |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1008 def flush(self): pass |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1009 def close(self): self.closed = True |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1010 |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1011 # 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
|
1012 # 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
|
1013 # 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
|
1014 s = MockSocket() |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1015 f = socket._fileobject(s) |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1016 f.close() |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1017 self.assert_(not s.closed) |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1018 |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1019 s = MockSocket() |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1020 f = socket._fileobject(s, close=True) |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1021 f.close() |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1022 self.assert_(s.closed) |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1023 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1024 class TCPTimeoutTest(SocketTCPTest): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1025 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1026 def testTCPTimeout(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1027 def raise_timeout(*args, **kwargs): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1028 self.serv.settimeout(1.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1029 self.serv.accept() |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1030 self.failUnlessRaises(socket.timeout, raise_timeout, |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1031 "Error generating a timeout exception (TCP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1032 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1033 def testTimeoutZero(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1034 ok = False |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1035 try: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1036 self.serv.settimeout(0.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1037 foo = self.serv.accept() |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1038 except socket.timeout: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1039 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
|
1040 except socket.error: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1041 ok = True |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1042 except: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1043 self.fail("caught unexpected exception (TCP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1044 if not ok: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1045 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
|
1046 |
|
39354
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1047 def testInterruptedTimeout(self): |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1048 # 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
|
1049 # 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
|
1050 # 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
|
1051 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
|
1052 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
|
1053 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
|
1054 class Alarm(Exception): |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1055 pass |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1056 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
|
1057 raise Alarm |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1058 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
|
1059 try: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1060 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
|
1061 try: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1062 foo = self.serv.accept() |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1063 except socket.timeout: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1064 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
|
1065 except Alarm: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1066 pass |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1067 except: |
|
46148
59d082591f56
Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44770
diff
changeset
|
1068 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
|
1069 " %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
|
1070 (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
|
1071 else: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1072 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
|
1073 finally: |
|
59d082591f56
Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44770
diff
changeset
|
1074 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
|
1075 except Alarm: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1076 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
|
1077 finally: |
|
85fdcdfca85c
Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents:
38976
diff
changeset
|
1078 # 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
|
1079 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
|
1080 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1081 class UDPTimeoutTest(SocketTCPTest): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1082 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1083 def testUDPTimeout(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1084 def raise_timeout(*args, **kwargs): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1085 self.serv.settimeout(1.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1086 self.serv.recv(1024) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1087 self.failUnlessRaises(socket.timeout, raise_timeout, |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1088 "Error generating a timeout exception (UDP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1089 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1090 def testTimeoutZero(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1091 ok = False |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1092 try: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1093 self.serv.settimeout(0.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1094 foo = self.serv.recv(1024) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1095 except socket.timeout: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1096 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
|
1097 except socket.error: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1098 ok = True |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1099 except: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1100 self.fail("caught unexpected exception (UDP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1101 if not ok: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1102 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
|
1103 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1104 class TestExceptions(unittest.TestCase): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1105 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1106 def testExceptionTree(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1107 self.assert_(issubclass(socket.error, Exception)) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1108 self.assert_(issubclass(socket.herror, socket.error)) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1109 self.assert_(issubclass(socket.gaierror, socket.error)) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1110 self.assert_(issubclass(socket.timeout, socket.error)) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1111 |
|
37853
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1112 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
|
1113 |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1114 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
|
1115 |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1116 def testLinuxAbstractNamespace(self): |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1117 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
|
1118 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
|
1119 s1.bind(address) |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1120 s1.listen(1) |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1121 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
|
1122 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
|
1123 s1.accept() |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1124 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
|
1125 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
|
1126 |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1127 def testMaxName(self): |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1128 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
|
1129 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
|
1130 s.bind(address) |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1131 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
|
1132 |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1133 def testNameOverflow(self): |
|
29257ef6abeb
SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents:
37422
diff
changeset
|
1134 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
|
1135 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
|
1136 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
|
1137 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1138 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1139 class BufferIOTest(SocketConnectedTest): |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1140 """ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1141 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
|
1142 """ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1143 def __init__(self, methodName='runTest'): |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1144 SocketConnectedTest.__init__(self, methodName=methodName) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1145 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1146 def testRecvInto(self): |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1147 buf = array.array('c', ' '*1024) |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1148 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
|
1149 self.assertEqual(nbytes, len(MSG)) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1150 msg = buf.tostring()[:len(MSG)] |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1151 self.assertEqual(msg, MSG) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1152 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1153 def _testRecvInto(self): |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1154 buf = buffer(MSG) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1155 self.serv_conn.send(buf) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1156 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1157 def testRecvFromInto(self): |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1158 buf = array.array('c', ' '*1024) |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1159 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
|
1160 self.assertEqual(nbytes, len(MSG)) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1161 msg = buf.tostring()[:len(MSG)] |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1162 self.assertEqual(msg, MSG) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1163 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38353
diff
changeset
|
1164 def _testRecvFromInto(self): |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1165 buf = buffer(MSG) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1166 self.serv_conn.send(buf) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
37853
diff
changeset
|
1167 |
|
44561
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1168 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1169 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
|
1170 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
|
1171 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
|
1172 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1173 def isTipcAvailable(): |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1174 """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
|
1175 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1176 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
|
1177 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
|
1178 """ |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1179 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
|
1180 return False |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1181 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
|
1182 return False |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1183 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
|
1184 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
|
1185 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
|
1186 return True |
|
44592
cb22f8748da7
It's verbose, not debug
Christian Heimes <christian@cheimes.de>
parents:
44581
diff
changeset
|
1187 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
|
1188 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
|
1189 return False |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1190 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1191 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
|
1192 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
|
1193 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
|
1194 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
|
1195 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1196 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
|
1197 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
|
1198 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
|
1199 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
|
1200 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1201 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
|
1202 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
|
1203 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
|
1204 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1205 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
|
1206 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1207 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
|
1208 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
|
1209 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1210 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1211 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
|
1212 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
|
1213 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
|
1214 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
|
1215 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1216 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
|
1217 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
|
1218 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
|
1219 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
|
1220 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
|
1221 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
|
1222 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
|
1223 self.serverExplicitReady() |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1224 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
|
1225 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1226 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
|
1227 # 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
|
1228 # 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
|
1229 # 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
|
1230 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
|
1231 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
|
1232 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
|
1233 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
|
1234 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
|
1235 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
|
1236 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1237 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
|
1238 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
|
1239 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
|
1240 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
|
1241 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1242 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
|
1243 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
|
1244 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
|
1245 |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1246 |
|
24451
22570e76cca1
Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents:
24450
diff
changeset
|
1247 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
|
1248 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
|
1249 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
|
1250 if sys.platform != 'mac': |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1251 tests.extend([ BasicUDPTest, UDPTimeoutTest ]) |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1252 |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1253 tests.extend([ |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1254 NonBlockingTCPTests, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1255 FileObjectClassTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1256 UnbufferedFileObjectClassTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1257 LineBufferedFileObjectClassTestCase, |
|
40684
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1258 SmallBufferedFileObjectClassTestCase, |
|
2fa6e82b1f17
Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents:
40632
diff
changeset
|
1259 Urllib2FileobjectTest, |
|
41300
8258f25d43d7
Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
1260 NetworkConnectionNoServer, |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
1261 NetworkConnectionAttributesTest, |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40684
diff
changeset
|
1262 NetworkConnectionBehaviourTest, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1263 ]) |
|
33063
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
1264 if hasattr(socket, "socketpair"): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
1265 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
|
1266 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
|
1267 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
|
1268 if isTipcAvailable(): |
|
ac51cfe5c5c6
Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents:
44478
diff
changeset
|
1269 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
|
1270 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
|
1271 |
|
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1272 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
|
1273 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
|
1274 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
|
1275 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1276 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
|
1277 test_main() |
