annotate Lib/test/test_socket.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents c547f6e91c58
children 85fdcdfca85c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1 #!/usr/bin/env python
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
2
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
3 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 24326
diff changeset
4 from test import test_support
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
5
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
6 import socket
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
7 import select
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
8 import time
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
9 import thread, threading
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
10 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
11 import sys
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
12 import array
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
13 from weakref import proxy
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
14
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
15 PORT = 50007
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
16 HOST = 'localhost'
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
17 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
18
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
19 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
20
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
21 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
22 self.serv = 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
23 self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
38767
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38757
diff changeset
24 global PORT
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38757
diff changeset
25 PORT = test_support.bind_port(self.serv, HOST, PORT)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
26 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
27
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
28 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
29 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
30 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
31
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
32 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
33
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
34 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
35 self.serv = 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
36 self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
38767
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38757
diff changeset
37 global PORT
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38757
diff changeset
38 PORT = test_support.bind_port(self.serv, HOST, PORT)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
39
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
40 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
41 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
42 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
43
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
44 class ThreadableTest:
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
45 """Threadable Test class
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
46
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
47 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
48 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
49 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
50 inheritance:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
51
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
52 class NewClass (OldClass, ThreadableTest):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
53 pass
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
54
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
55 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
56 purposes for overriding:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
57
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
58 clientSetUp ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
59 clientTearDown ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
60
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
61 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
62 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
63 '_' 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
64
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
65 def testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
66 # Server portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
67
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
68 def _testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
69 # Client portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
70
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
71 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
72 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
73 the testing framework.
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
74
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
75 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
76 functions that rely on the client thread during setup,
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
77 unless serverExplicityReady() is called just before
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
78 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
79 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
80 """
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
81
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
82 def __init__(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
83 # 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
84 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
85 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
86 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
87 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
88
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
89 def serverExplicitReady(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
90 """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
91 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
92 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
93 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
94 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
95
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
96 def _setUp(self):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
97 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
98 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
99 self.done = threading.Event()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
100 self.queue = Queue.Queue(1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
101
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
102 # 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
103 methodname = self.id()
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
104 i = methodname.rfind('.')
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
105 methodname = methodname[i+1:]
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
106 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
107 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
108 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
109
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
110 self.__setUp()
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
111 if not self.server_ready.isSet():
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
112 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
113 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
114
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
115 def _tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
116 self.__tearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
117 self.done.wait()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
118
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
119 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
120 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
121 self.fail(msg)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
122
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
123 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
124 self.server_ready.wait()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
125 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
126 self.clientSetUp()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
127 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
128 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
129 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
130 test_func()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
131 except Exception, strerror:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
132 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
133 self.clientTearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
134
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
135 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
136 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
137
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
138 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
139 self.done.set()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
140 thread.exit()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
141
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
142 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
143
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
144 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
145 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
146 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
147
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
148 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
149 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
150
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
151 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
152 self.cli.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
153 self.cli = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
154 ThreadableTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
155
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
156 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
157
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
158 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
159 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
160 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
161
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
162 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
163 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
164
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
165 class SocketConnectedTest(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
166
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
167 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
168 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
169
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
170 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
171 ThreadedTCPSocketTest.setUp(self)
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
172 # 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
173 # 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
174 self.serverExplicitReady()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
175 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
176 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
177
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
178 def tearDown(self):
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.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
180 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
181 ThreadedTCPSocketTest.tearDown(self)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
182
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
183 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
184 ThreadedTCPSocketTest.clientSetUp(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
185 self.cli.connect((HOST, PORT))
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
186 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
187
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
188 def clientTearDown(self):
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.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
190 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
191 ThreadedTCPSocketTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
192
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
193 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
194
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
195 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
196 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
197 ThreadableTest.__init__(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
198
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
199 def setUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
200 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
201
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
202 def tearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
203 self.serv.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
204 self.serv = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
205
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
206 def clientSetUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
207 pass
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
208
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
209 def clientTearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
210 self.cli.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
211 self.cli = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
212 ThreadableTest.clientTearDown(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
213
33076
836e77266f97 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33063
diff changeset
214
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
215 #######################################################################
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
216 ## Begin Tests
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
217
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
218 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
219
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
220 def test_weakref(self):
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
221 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
222 p = proxy(s)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
223 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
224 s.close()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
225 s = None
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
226 try:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
227 p.fileno()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
228 except ReferenceError:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
229 pass
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
230 else:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
231 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
232
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
233 def testSocketError(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
234 # 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
235 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
236 raise socket.error
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
237 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
238 raise socket.herror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
239 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
240 raise socket.gaierror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
241 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
242 "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
243 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
244 "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
245 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
246 "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
247
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
248 def testCrucialConstants(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
249 # 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
250 socket.AF_INET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
251 socket.SOCK_STREAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
252 socket.SOCK_DGRAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
253 socket.SOCK_RAW
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
254 socket.SOCK_RDM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
255 socket.SOCK_SEQPACKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
256 socket.SOL_SOCKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
257 socket.SO_REUSEADDR
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
258
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
259 def testHostnameRes(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
260 # Testing hostname resolution mechanisms
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
261 hostname = socket.gethostname()
26799
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
262 try:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
263 ip = socket.gethostbyname(hostname)
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
264 except socket.error:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
265 # 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
266 return
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
267 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
268 try:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
269 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
270 except socket.error:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
271 # 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
272 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
273 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
274 fqhn = socket.getfqdn(ip)
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
275 if not fqhn in all_host_names:
37227
8d57d25e6548 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 37225
diff changeset
276 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
277
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
278 def testRefCountGetNameInfo(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
279 # Testing reference count for getnameinfo
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
280 import sys
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
281 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
282 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
283 # 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
284 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
285 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
286 except SystemError:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
287 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
288 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
289
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
290 def testInterpreterCrash(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
291 # 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
292 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
293 # 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
294 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
295 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
296 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
297
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
298 def testNtoH(self):
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
299 # 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
300 # 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
301 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
302 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
303 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
304 mask = (1L<<size) - 1
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
305 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
306 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
307
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
308 swapped = func(mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
309 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
310 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
311
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
312 def testGetServBy(self):
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
313 eq = self.assertEqual
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
314 # 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
315 # 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
316 # 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
317 if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
35238
526faaab00eb Add support for FreeBSD 7.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34739
diff changeset
318 'freebsd7', '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
319 # 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
320 # 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
321 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
322 else:
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
323 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
324 for service in services:
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
325 try:
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
326 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
327 break
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
328 except socket.error:
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
329 pass
31503
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
330 else:
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
331 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
332 # 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
333 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
334 eq(port, port2)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
335 # 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
336 try:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
337 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
338 except socket.error:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
339 udpport = None
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
340 else:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
341 eq(udpport, port)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
342 # 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
343 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
344 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
345 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
346 eq(socket.getservbyport(udpport, 'udp'), service)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
347
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
348 def testDefaultTimeout(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
349 # Testing default timeout
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
350 # 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
351 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
352 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
353 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
354 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
355
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
356 # 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
357 socket.setdefaulttimeout(10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
358 self.assertEqual(socket.getdefaulttimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
359 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
360 self.assertEqual(s.gettimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
361 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
362
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
363 # 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
364 socket.setdefaulttimeout(None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
365 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
366 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
367 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
368 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
369
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
370 # 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
371 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
372
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
373 # 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
374 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
375
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
376 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
377 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
378 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
379 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
380 g = lambda a: inet_pton(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
381
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
382 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
383 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
384 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
385 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
386 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
387
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
388 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
389 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
390 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
391 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
392
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
393 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
394 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
395 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
396 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
397 from socket import inet_pton, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
398 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
399 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
400 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
401 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
402 f = lambda a: inet_pton(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
403
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
404 self.assertEquals('\x00' * 16, f('::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
405 self.assertEquals('\x00' * 16, f('0::0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
406 self.assertEquals('\x00\x01' + '\x00' * 14, f('1::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
407 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
408 '\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
409 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
410 )
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
411
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
412 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
413 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
414 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
415 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
416 g = lambda a: inet_ntop(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
417
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
418 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
419 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
420 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
421 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
422
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
423 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
424 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
425 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
426
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
427 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
428 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
429 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
430 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
431 from socket import inet_ntop, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
432 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
433 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
434 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
435 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
436 f = lambda a: inet_ntop(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
437
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
438 self.assertEquals('::', f('\x00' * 16))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
439 self.assertEquals('::1', f('\x00' * 15 + '\x01'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
440 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
441 'aef:b01:506:1001:ffff:9997:55:170',
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
442 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
443 )
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
444
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
445 # 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
446
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
447 def testSockName(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
448 # Testing getsockname()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
449 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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
450 sock.bind(("0.0.0.0", PORT+1))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
451 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
452 # 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
453 # 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
454 # 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
455 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
456 self.assert_(name[0] in ("0.0.0.0", my_ip_addr), '%s invalid' % name[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
457 self.assertEqual(name[1], PORT+1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
458
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
459 def testGetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
460 # Testing getsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
461 # 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
462 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
463 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
464 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
465
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
466 def testSetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
467 # Testing setsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
468 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
469 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
470 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
471 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
472
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
473 def testSendAfterClose(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
474 # 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
475 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
476 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
477 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
478 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
479
37123
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
480 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
481 # 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
482 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
483 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
484 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
485 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
486 sock.close()
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
487
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
488 class BasicTCPTest(SocketConnectedTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
489
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
490 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
491 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
492
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
493 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
494 # 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
495 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
496 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
497
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
498 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
499 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
500
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
501 def testOverFlowRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
502 # 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
503 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
504 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
505 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
506 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
507
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
508 def _testOverFlowRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
509 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
510
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
511 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
512 # 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
513 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
514 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
515
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
516 def _testRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
517 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
518
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
519 def testOverFlowRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
520 # 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
521 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
522 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
523 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
524 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
525
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
526 def _testOverFlowRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
527 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
528
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
529 def testSendAll(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
530 # 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
531 msg = ''
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
532 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
533 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
534 if not read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
535 break
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
536 msg += read
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
537 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
538
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
539 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
540 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
541 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
542
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
543 def testFromFd(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
544 # Testing fromfd()
23797
ba29485d0720 Argh. Typo. :-(
Guido van Rossum <guido@python.org>
parents: 23796
diff changeset
545 if not hasattr(socket, "fromfd"):
23796
fcc36ee1924b Allow absent fromfd(), for Windows.
Guido van Rossum <guido@python.org>
parents: 23795
diff changeset
546 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
547 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
548 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
549 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
550 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
551
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
552 def _testFromFd(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
553 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
554
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
555 def testShutdown(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
556 # Testing shutdown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
557 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
558 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
559
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
560 def _testShutdown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
561 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
562 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
563
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
564 class BasicUDPTest(ThreadedUDPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
565
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
566 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
567 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
568
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
569 def testSendtoAndRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
570 # 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
571 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
572 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
573
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
574 def _testSendtoAndRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
575 self.cli.sendto(MSG, 0, (HOST, PORT))
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
576
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
577 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
578 # 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
579 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
580 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
581
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
582 def _testRecvFrom(self):
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
583 self.cli.sendto(MSG, 0, (HOST, PORT))
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
584
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
585 class TCPCloserTest(ThreadedTCPSocketTest):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
586
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
587 def testClose(self):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
588 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
589 conn.close()
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
590
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
591 sd = self.cli
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
592 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
593 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
594 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
595
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
596 def _testClose(self):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
597 self.cli.connect((HOST, PORT))
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
598 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
599
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
600 class BasicSocketPairTest(SocketPairTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
601
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
602 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
603 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
604
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
605 def testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
606 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
607 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
608
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
609 def _testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
610 self.cli.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
611
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
612 def testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
613 self.serv.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
614
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
615 def _testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
616 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
617 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
618
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
619 class NonBlockingTCPTests(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
620
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
621 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
622 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
623
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
624 def testSetBlocking(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
625 # 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
626 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
627 start = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
628 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
629 self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
630 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
631 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
632 end = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
633 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
634
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
635 def _testSetBlocking(self):
17208
c8817a9bbbdd Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents: 16519
diff changeset
636 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
637
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
638 def testAccept(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
639 # 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
640 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
641 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
642 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
643 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
644 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
645 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
646 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
647 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
648 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
649 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
650 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
651 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
652
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
653 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
654 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
655 self.cli.connect((HOST, PORT))
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
656
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
657 def testConnect(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
658 # 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
659 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
660
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
661 def _testConnect(self):
23818
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
662 self.cli.settimeout(10)
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
663 self.cli.connect((HOST, PORT))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
664
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
665 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
666 # 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
667 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
668 conn.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
669 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
670 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
671 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
672 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
673 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
674 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
675 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
676 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
677 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
678 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
679 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
680 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
681
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
682 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
683 self.cli.connect((HOST, 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
684 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
685 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
686
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
687 class FileObjectClassTestCase(SocketConnectedTest):
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
688
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
689 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
690
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
691 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
692 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
693
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
694 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
695 SocketConnectedTest.setUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
696 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
697
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
698 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
699 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
700 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
701 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
702 SocketConnectedTest.tearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
703
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
704 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
705 SocketConnectedTest.clientSetUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
706 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
707
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
708 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
709 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
710 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
711 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
712 SocketConnectedTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
713
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
714 def testSmallRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
715 # 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
716 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
717 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
718 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
719 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
720
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
721 def _testSmallRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
722 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
723 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
724
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
725 def testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
726 # read until EOF
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
727 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
728 self.assertEqual(msg, MSG)
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
729
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
730 def _testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
731 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
732 self.cli_file.close()
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
733
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
734 def testUnbufferedRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
735 # 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
736 buf = ''
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
737 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
738 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
739 if not char:
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
740 break
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
741 buf += char
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
742 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
743
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
744 def _testUnbufferedRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
745 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
746 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
747
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
748 def testReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
749 # 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
750 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
751 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
752
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
753 def _testReadline(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
754 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
755 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
756
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
757 def testClosedAttr(self):
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
758 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
759
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
760 def _testClosedAttr(self):
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
761 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
762
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
763 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
764
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
765 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24655
diff changeset
766
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
767 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
768 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
769 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
770 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
771 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
772
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
773 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
774
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
775 def testUnbufferedReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
776 # 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
777 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
778 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
779 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
780 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
781 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
782
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
783 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
784 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
785 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
786 self.cli_file.flush()
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
787
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
788 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
789
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
790 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
791
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
792
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
793 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
794
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
795 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
796
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
797 class TCPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
798
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
799 def testTCPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
800 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
801 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
802 self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
803 self.failUnlessRaises(socket.timeout, raise_timeout,
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
804 "Error generating a timeout exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
805
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
806 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
807 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
808 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
809 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
810 foo = self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
811 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
812 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
813 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
814 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
815 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
816 self.fail("caught unexpected exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
817 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
818 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
819
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
820 class UDPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
821
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
822 def testUDPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
823 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
824 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
825 self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
826 self.failUnlessRaises(socket.timeout, raise_timeout,
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
827 "Error generating a timeout exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
828
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
829 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
830 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
831 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
832 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
833 foo = self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
834 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
835 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
836 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
837 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
838 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
839 self.fail("caught unexpected exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
840 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
841 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
842
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
843 class TestExceptions(unittest.TestCase):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
844
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
845 def testExceptionTree(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
846 self.assert_(issubclass(socket.error, Exception))
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
847 self.assert_(issubclass(socket.herror, socket.error))
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
848 self.assert_(issubclass(socket.gaierror, socket.error))
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
849 self.assert_(issubclass(socket.timeout, socket.error))
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
850
37853
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
851 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
852
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
853 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
854
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
855 def testLinuxAbstractNamespace(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
856 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
857 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
858 s1.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
859 s1.listen(1)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
860 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
861 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
862 s1.accept()
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
863 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
864 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
865
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
866 def testMaxName(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
867 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
868 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
869 s.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
870 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
871
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
872 def testNameOverflow(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
873 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
874 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
875 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
876
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
877
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
878 class BufferIOTest(SocketConnectedTest):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
879 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
880 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
881 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
882 def __init__(self, methodName='runTest'):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
883 SocketConnectedTest.__init__(self, methodName=methodName)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
884
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
885 def testRecvInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
886 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
887 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
888 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
889 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
890 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
891
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
892 def _testRecvInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
893 buf = buffer(MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
894 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
895
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
896 def testRecvFromInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
897 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
898 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
899 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
900 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
901 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
902
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
903 def _testRecvFromInto(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
904 buf = buffer(MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
905 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
906
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
907 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
908 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
909 TestExceptions, BufferIOTest]
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
910 if sys.platform != 'mac':
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
911 tests.extend([ BasicUDPTest, UDPTimeoutTest ])
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
912
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
913 tests.extend([
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
914 NonBlockingTCPTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
915 FileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
916 UnbufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
917 LineBufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
918 SmallBufferedFileObjectClassTestCase
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
919 ])
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
920 if hasattr(socket, "socketpair"):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
921 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
922 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
923 tests.append(TestLinuxAbstractNamespace)
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
924
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
925 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
926 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
927 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
928
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
929 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
930 test_main()