annotate Lib/test/test_socket.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 1dc2b50d344f
children 7025278c5a41
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1 #!/usr/bin/env python
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
2
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
3 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 24326
diff changeset
4 from test import test_support
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
5
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
6 import errno
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
7 import socket
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
8 import select
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
9 import time
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
10 import traceback
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
11 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
12 import sys
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
13 import os
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
14 import array
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
15 import contextlib
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
16 from weakref import proxy
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
17 import signal
64962
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
18 import math
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
19
64002
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
20 def try_address(host, port=0, family=socket.AF_INET):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
21 """Try to bind a socket on the given host:port and return True
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
22 if that has been possible."""
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
23 try:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
24 sock = socket.socket(family, socket.SOCK_STREAM)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
25 sock.bind((host, port))
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
26 except (socket.error, socket.gaierror):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
27 return False
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
28 else:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
29 sock.close()
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
30 return True
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
31
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
32 HOST = test_support.HOST
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
33 MSG = b'Michael Gilfix was here\n'
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
34 SUPPORTS_IPV6 = socket.has_ipv6 and try_address('::1', family=socket.AF_INET6)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
35
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
36 try:
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
37 import thread
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
38 import threading
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
39 except ImportError:
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
40 thread = None
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
41 threading = None
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
42
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
43 HOST = test_support.HOST
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
44 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
45
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
46 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
47
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
48 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
49 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
50 self.port = test_support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
51 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
52
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
53 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
54 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
55 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
56
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
57 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
58
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
59 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
60 self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
61 self.port = test_support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
62
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
63 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
64 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
65 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
66
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
67 class ThreadableTest:
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
68 """Threadable Test class
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
69
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
70 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
71 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
72 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
73 inheritance:
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 class NewClass (OldClass, ThreadableTest):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
76 pass
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
77
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
78 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
79 purposes for overriding:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
80
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
81 clientSetUp ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
82 clientTearDown ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
83
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
84 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
85 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
86 '_' 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
87
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
88 def testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
89 # Server portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
90
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
91 def _testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
92 # Client portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
93
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
94 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
95 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
96 the testing framework.
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
97
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
98 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
99 functions that rely on the client thread during setup,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
100 unless serverExplicitReady() is called just before
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
101 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
102 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
103 """
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
104
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
105 def __init__(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
106 # 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
107 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
108 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
109 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
110 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
111
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
112 def serverExplicitReady(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
113 """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
114 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
115 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
116 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
117 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
118
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
119 def _setUp(self):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
120 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
121 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
122 self.done = threading.Event()
47248
9acb314b9072 Queue renaming reversal part 3: move module into place and
Georg Brandl <georg@python.org>
parents: 46897
diff changeset
123 self.queue = Queue.Queue(1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
124
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
125 # 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
126 methodname = self.id()
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
127 i = methodname.rfind('.')
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
128 methodname = methodname[i+1:]
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
129 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
130 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
131 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
132
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
133 self.__setUp()
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47372
diff changeset
134 if not self.server_ready.is_set():
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
135 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
136 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
137
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
138 def _tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
139 self.__tearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
140 self.done.wait()
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 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
143 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
144 self.fail(msg)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
145
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
146 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
147 self.server_ready.wait()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
148 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
149 self.clientSetUp()
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
150 with test_support.check_py3k_warnings():
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
151 if not callable(test_func):
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
152 raise TypeError("test_func must be a callable function.")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
153 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
154 test_func()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
155 except Exception, strerror:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
156 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
157 self.clientTearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
158
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
159 def clientSetUp(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
160 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
161
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
162 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
163 self.done.set()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
164 thread.exit()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
165
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
166 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
167
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
168 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
169 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
170 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
171
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
172 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
173 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
174
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
175 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
176 self.cli.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
177 self.cli = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
178 ThreadableTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
179
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
180 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
181
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
182 def __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
183 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
184 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
185
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
186 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
187 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
188
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
189 class SocketConnectedTest(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
190
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
191 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
192 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
193
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
194 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
195 ThreadedTCPSocketTest.setUp(self)
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
196 # 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
197 # 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
198 self.serverExplicitReady()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
199 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
200 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
201
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
202 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
203 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
204 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
205 ThreadedTCPSocketTest.tearDown(self)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
206
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
207 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
208 ThreadedTCPSocketTest.clientSetUp(self)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
209 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
210 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
211
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
212 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
213 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
214 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
215 ThreadedTCPSocketTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
216
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
217 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
218
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
219 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
220 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
221 ThreadableTest.__init__(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
222
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
223 def setUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
224 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
225
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
226 def tearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
227 self.serv.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
228 self.serv = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
229
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
230 def clientSetUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
231 pass
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
232
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
233 def clientTearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
234 self.cli.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
235 self.cli = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
236 ThreadableTest.clientTearDown(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
237
33076
836e77266f97 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33063
diff changeset
238
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
239 #######################################################################
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
240 ## Begin Tests
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
241
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
242 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
243
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
244 def test_weakref(self):
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
245 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
246 p = proxy(s)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
247 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
248 s.close()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
249 s = None
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
250 try:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
251 p.fileno()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
252 except ReferenceError:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
253 pass
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
254 else:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
255 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
256
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
257 def testSocketError(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
258 # 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
259 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
260 raise socket.error
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
261 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
262 raise socket.herror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
263 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
264 raise socket.gaierror
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
265 self.assertRaises(socket.error, raise_error,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
266 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
267 self.assertRaises(socket.error, raise_herror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
268 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
269 self.assertRaises(socket.error, raise_gaierror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
270 "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
271
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
272 def testCrucialConstants(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
273 # 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
274 socket.AF_INET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
275 socket.SOCK_STREAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
276 socket.SOCK_DGRAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
277 socket.SOCK_RAW
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
278 socket.SOCK_RDM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
279 socket.SOCK_SEQPACKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
280 socket.SOL_SOCKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
281 socket.SO_REUSEADDR
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
282
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
283 def testHostnameRes(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
284 # Testing hostname resolution mechanisms
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
285 hostname = socket.gethostname()
26799
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
286 try:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
287 ip = socket.gethostbyname(hostname)
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
288 except socket.error:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
289 # 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
290 return
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
291 self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.")
26800
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
292 try:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
293 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
294 except socket.error:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
295 # 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
296 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
297 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
298 fqhn = socket.getfqdn(ip)
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
299 if not fqhn in all_host_names:
37227
8d57d25e6548 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 37225
diff changeset
300 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
301
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
302 def testRefCountGetNameInfo(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
303 # Testing reference count for getnameinfo
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
304 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
305 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
306 # 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
307 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
308 socket.getnameinfo(__name__,0)
56107
6a278e4d9f61 #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
Georg Brandl <georg@python.org>
parents: 55627
diff changeset
309 except TypeError:
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
310 self.assertEqual(sys.getrefcount(__name__), orig,
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
311 "socket.getnameinfo loses a reference")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
312
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
313 def testInterpreterCrash(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
314 # 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
315 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
316 # 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
317 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
318 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
319 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
320
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
321 def testNtoH(self):
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
322 # 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
323 # 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
324 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
325 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
326 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
327 mask = (1L<<size) - 1
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
328 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
329 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
330
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
331 swapped = func(mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
332 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
333 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
334
40632
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
335 def testNtoHErrors(self):
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
336 good_values = [ 1, 2, 3, 1L, 2L, 3L ]
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
337 bad_values = [ -1, -2, -3, -1L, -2L, -3L ]
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
338 for k in good_values:
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
339 socket.ntohl(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
340 socket.ntohs(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
341 socket.htonl(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
342 socket.htons(k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
343 for k in bad_values:
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
344 self.assertRaises(OverflowError, socket.ntohl, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
345 self.assertRaises(OverflowError, socket.ntohs, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
346 self.assertRaises(OverflowError, socket.htonl, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
347 self.assertRaises(OverflowError, socket.htons, k)
6efe3a4b10ac Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or
Guido van Rossum <guido@python.org>
parents: 39354
diff changeset
348
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
349 def testGetServBy(self):
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
350 eq = self.assertEqual
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
351 # 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
352 # 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
353 # 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
354 if sys.platform in ('linux2', 'freebsd4', 'freebsd5', 'freebsd6',
43703
365913d7af7d Enable platform-specific tweaks for FreeBSD 8 (exactly same to FreeBSD 7's yet)
Hye-Shik Chang <hyeshik@gmail.com>
parents: 41532
diff changeset
355 'freebsd7', 'freebsd8', 'darwin'):
32630
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
356 # 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
357 # 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
358 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
359 else:
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
360 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
361 for service in services:
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
362 try:
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
363 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
364 break
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
365 except socket.error:
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
366 pass
31503
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
367 else:
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
368 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
369 # 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
370 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
371 eq(port, port2)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
372 # 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
373 try:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
374 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
375 except socket.error:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
376 udpport = None
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
377 else:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
378 eq(udpport, port)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
379 # 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
380 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
381 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
382 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
383 eq(socket.getservbyport(udpport, 'udp'), service)
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
384 # Make sure getservbyport does not accept out of range ports.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
385 self.assertRaises(OverflowError, socket.getservbyport, -1)
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
386 self.assertRaises(OverflowError, socket.getservbyport, 65536)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
387
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
388 def testDefaultTimeout(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
389 # Testing default timeout
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
390 # 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
391 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
392 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
393 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
394 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
395
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
396 # 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
397 socket.setdefaulttimeout(10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
398 self.assertEqual(socket.getdefaulttimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
399 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
400 self.assertEqual(s.gettimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
401 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
402
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
403 # 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
404 socket.setdefaulttimeout(None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
405 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
406 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
407 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
408 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
409
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
410 # 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
411 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
412
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
413 # 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
414 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
415
51538
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
416 def testIPv4_inet_aton_fourbytes(self):
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
417 if not hasattr(socket, 'inet_aton'):
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
418 return # No inet_aton, nothing to check
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
419 # Test that issue1008086 and issue767150 are fixed.
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
420 # It must return 4 bytes.
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
421 self.assertEquals('\x00'*4, socket.inet_aton('0.0.0.0'))
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
422 self.assertEquals('\xff'*4, socket.inet_aton('255.255.255.255'))
edcf996f07e9 Issue #1008086: Fixes socket.inet_aton() to always return 4 bytes even
Gregory P. Smith <greg@mad-scientist.com>
parents: 51220
diff changeset
423
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
424 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
425 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
426 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
427 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
428 g = lambda a: inet_pton(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
429
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
430 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
431 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
432 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
433 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
434 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
435
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
436 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
437 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
438 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
439 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
440
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
441 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
442 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
443 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
444 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
445 from socket import inet_pton, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
446 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
447 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
448 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
449 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
450 f = lambda a: inet_pton(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
451
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
452 self.assertEquals('\x00' * 16, f('::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
453 self.assertEquals('\x00' * 16, f('0::0'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
454 self.assertEquals('\x00\x01' + '\x00' * 14, f('1::'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
455 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
456 '\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
457 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
458 )
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
459
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
460 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
461 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
462 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
463 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
464 g = lambda a: inet_ntop(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
465
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
466 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
467 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
468 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
469 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
470
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
471 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
472 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
473 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
474
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
475 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
476 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
477 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
478 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
479 from socket import inet_ntop, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
480 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
481 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
482 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
483 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
484 f = lambda a: inet_ntop(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
485
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
486 self.assertEquals('::', f('\x00' * 16))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
487 self.assertEquals('::1', f('\x00' * 15 + '\x01'))
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
488 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
489 'aef:b01:506:1001:ffff:9997:55:170',
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
490 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
491 )
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
492
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
493 # 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
494
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
495 def _get_unused_port(self, bind_address='0.0.0.0'):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
496 """Use a temporary socket to elicit an unused ephemeral port.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
497
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
498 Args:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
499 bind_address: Hostname or IP address to search for a port on.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
500
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
501 Returns: A most likely to be unused port.
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
502 """
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
503 tempsock = socket.socket()
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
504 tempsock.bind((bind_address, 0))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
505 host, port = tempsock.getsockname()
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
506 tempsock.close()
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
507 return port
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
508
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
509 def testSockName(self):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
510 # Testing getsockname()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
511 port = self._get_unused_port()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
512 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
513 sock.bind(("0.0.0.0", port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
514 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
515 # 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
516 # 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
517 # 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
518 my_ip_addr = socket.gethostbyname(socket.gethostname())
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
519 self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0])
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
520 self.assertEqual(name[1], port)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
521
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
522 def testGetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
523 # Testing getsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
524 # 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
525 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
526 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
527 self.assertFalse(reuse != 0, "initial mode is reuse")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
528
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
529 def testSetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
530 # Testing setsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
531 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
532 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
533 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
534 self.assertFalse(reuse == 0, "failed to set reuse mode")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
535
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
536 def testSendAfterClose(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
537 # 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
538 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
539 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
540 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
541 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
542
37123
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
543 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
544 # 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
545 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
546 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
547 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
548 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
549 sock.close()
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
550
51220
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
551 def test_getsockaddrarg(self):
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
552 host = '0.0.0.0'
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
553 port = self._get_unused_port(bind_address=host)
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
554 big_port = port + 65536
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
555 neg_port = port - 65536
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
556 sock = socket.socket()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
557 try:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
558 self.assertRaises(OverflowError, sock.bind, (host, big_port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
559 self.assertRaises(OverflowError, sock.bind, (host, neg_port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
560 sock.bind((host, port))
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
561 finally:
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
562 sock.close()
eb2607a6516e - Issue #5104: The socket module now raises OverflowError when 16-bit port and
Gregory P. Smith <greg@mad-scientist.com>
parents: 50797
diff changeset
563
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
564 def test_sock_ioctl(self):
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
565 if os.name != "nt":
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
566 return
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
567 self.assertTrue(hasattr(socket.socket, 'ioctl'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
568 self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
569 self.assertTrue(hasattr(socket, 'RCVALL_ON'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
570 self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
56212
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
571 self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS'))
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
572 s = socket.socket()
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
573 self.assertRaises(ValueError, s.ioctl, -1, None)
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
574 s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
575
64002
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
576 def testGetaddrinfo(self):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
577 try:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
578 socket.getaddrinfo('localhost', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
579 except socket.gaierror as err:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
580 if err.errno == socket.EAI_SERVICE:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
581 # see http://bugs.python.org/issue1282647
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
582 self.skipTest("buggy libc version")
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
583 raise
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
584 # len of every sequence is supposed to be == 5
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
585 for info in socket.getaddrinfo(HOST, None):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
586 self.assertEqual(len(info), 5)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
587 # host can be a domain name, a string representation of an
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
588 # IPv4/v6 address or None
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
589 socket.getaddrinfo('localhost', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
590 socket.getaddrinfo('127.0.0.1', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
591 socket.getaddrinfo(None, 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
592 if SUPPORTS_IPV6:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
593 socket.getaddrinfo('::1', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
594 # port can be a string service name such as "http", a numeric
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
595 # port number or None
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
596 socket.getaddrinfo(HOST, "http")
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
597 socket.getaddrinfo(HOST, 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
598 socket.getaddrinfo(HOST, None)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
599 # test family and socktype filters
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
600 infos = socket.getaddrinfo(HOST, None, socket.AF_INET)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
601 for family, _, _, _, _ in infos:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
602 self.assertEqual(family, socket.AF_INET)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
603 infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
604 for _, socktype, _, _, _ in infos:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
605 self.assertEqual(socktype, socket.SOCK_STREAM)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
606 # test proto and flags arguments
64061
b4d88f9ef3f9 Merged revisions 84089 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64002
diff changeset
607 socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
64002
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
608 socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
609 # a server willing to support both IPv4 and IPv6 will
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
610 # usually do this
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
611 socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
612 socket.AI_PASSIVE)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
613
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
614
64962
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
615 def check_sendall_interrupted(self, with_timeout):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
616 # socketpair() is not stricly required, but it makes things easier.
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
617 if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
618 self.skipTest("signal.alarm and socket.socketpair required for this test")
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
619 # Our signal handlers clobber the C errno by calling a math function
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
620 # with an invalid domain value.
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
621 def ok_handler(*args):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
622 self.assertRaises(ValueError, math.acosh, 0)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
623 def raising_handler(*args):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
624 self.assertRaises(ValueError, math.acosh, 0)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
625 1 // 0
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
626 c, s = socket.socketpair()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
627 old_alarm = signal.signal(signal.SIGALRM, raising_handler)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
628 try:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
629 if with_timeout:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
630 # Just above the one second minimum for signal.alarm
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
631 c.settimeout(1.5)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
632 with self.assertRaises(ZeroDivisionError):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
633 signal.alarm(1)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
634 c.sendall(b"x" * (1024**2))
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
635 if with_timeout:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
636 signal.signal(signal.SIGALRM, ok_handler)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
637 signal.alarm(1)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
638 self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2))
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
639 finally:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
640 signal.signal(signal.SIGALRM, old_alarm)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
641 c.close()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
642 s.close()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
643
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
644 def test_sendall_interrupted(self):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
645 self.check_sendall_interrupted(False)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
646
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
647 def test_sendall_interrupted_with_timeout(self):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
648 self.check_sendall_interrupted(True)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
649
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
650
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
651 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
652 class BasicTCPTest(SocketConnectedTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
653
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
654 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
655 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
656
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
657 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
658 # 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
659 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
660 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
661
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
662 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
663 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
664
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
665 def testOverFlowRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
666 # 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
667 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
668 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
669 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
670 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
671
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
672 def _testOverFlowRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
673 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
674
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
675 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
676 # 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
677 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
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
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
680 def _testRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
681 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
682
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
683 def testOverFlowRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
684 # 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
685 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
686 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
687 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
688 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
689
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
690 def _testOverFlowRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
691 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
692
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
693 def testSendAll(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
694 # 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
695 msg = ''
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
696 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
697 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
698 if not read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
699 break
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
700 msg += read
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
701 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
702
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
703 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
704 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
705 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
706
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
707 def testFromFd(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
708 # Testing fromfd()
23797
ba29485d0720 Argh. Typo. :-(
Guido van Rossum <guido@python.org>
parents: 23796
diff changeset
709 if not hasattr(socket, "fromfd"):
23796
fcc36ee1924b Allow absent fromfd(), for Windows.
Guido van Rossum <guido@python.org>
parents: 23795
diff changeset
710 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
711 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
712 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
713 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
714 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
715
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
716 def _testFromFd(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
717 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
718
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
719 def testShutdown(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
720 # Testing shutdown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
721 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
722 self.assertEqual(msg, MSG)
50796
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
723 # wait for _testShutdown to finish: on OS X, when the server
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
724 # closes the connection the client also becomes disconnected,
50797
ae5ed15774b4 Comment typo
Mark Dickinson <dickinsm@gmail.com>
parents: 50796
diff changeset
725 # and the client's shutdown call will fail. (Issue #4397.)
50796
3378eafd27df Issue #4397. Fix occasional test_socket failure on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
726 self.done.wait()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
727
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
728 def _testShutdown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
729 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
730 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
731
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
732 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
733 class BasicUDPTest(ThreadedUDPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
734
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
735 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
736 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
737
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
738 def testSendtoAndRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
739 # 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
740 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
741 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
742
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
743 def _testSendtoAndRecv(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
744 self.cli.sendto(MSG, 0, (HOST, self.port))
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
745
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
746 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
747 # 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
748 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
749 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
750
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
751 def _testRecvFrom(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
752 self.cli.sendto(MSG, 0, (HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
753
41313
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
754 def testRecvFromNegative(self):
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
755 # Negative lengths passed to recvfrom should give ValueError.
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
756 self.assertRaises(ValueError, self.serv.recvfrom, -1)
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
757
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
758 def _testRecvFromNegative(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
759 self.cli.sendto(MSG, 0, (HOST, self.port))
41313
d144f81574c0 Bug 1688393. Adds a control of negative values in
Facundo Batista <facundobatista@gmail.com>
parents: 41300
diff changeset
760
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
761 @unittest.skipUnless(thread, 'Threading required for this test.')
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
762 class TCPCloserTest(ThreadedTCPSocketTest):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
763
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
764 def testClose(self):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
765 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
766 conn.close()
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
767
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
768 sd = self.cli
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
769 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
770 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
771 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
772
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
773 def _testClose(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
774 self.cli.connect((HOST, self.port))
38976
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
775 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
776
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
777 @unittest.skipUnless(thread, 'Threading required for this test.')
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
778 class BasicSocketPairTest(SocketPairTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
779
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
780 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
781 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
782
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
783 def testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
784 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
785 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
786
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
787 def _testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
788 self.cli.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
789
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
790 def testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
791 self.serv.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
792
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
793 def _testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
794 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
795 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
796
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
797 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
798 class NonBlockingTCPTests(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
799
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
800 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
801 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
802
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
803 def testSetBlocking(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
804 # 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
805 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
806 start = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
807 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
808 self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
809 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
810 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
811 end = time.time()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
812 self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
813
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
814 def _testSetBlocking(self):
17208
c8817a9bbbdd Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents: 16519
diff changeset
815 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
816
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
817 def testAccept(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
818 # 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
819 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
820 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
821 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
822 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
823 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
824 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
825 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
826 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
827 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
828 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
829 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
830 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
831
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
832 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
833 time.sleep(0.1)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
834 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
835
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
836 def testConnect(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
837 # 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
838 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
839
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
840 def _testConnect(self):
23818
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
841 self.cli.settimeout(10)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
842 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
843
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
844 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
845 # 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
846 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
847 conn.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
848 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
849 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
850 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
851 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
852 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
853 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
854 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
855 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
856 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
857 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
858 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
859 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
860
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
861 def _testRecv(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
862 self.cli.connect((HOST, self.port))
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
863 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
864 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
865
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
866 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
867 class FileObjectClassTestCase(SocketConnectedTest):
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
868
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
869 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
870
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
871 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
872 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
873
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
874 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
875 SocketConnectedTest.setUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
876 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
877
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
878 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
879 self.serv_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
880 self.assertTrue(self.serv_file.closed)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
881 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
882 SocketConnectedTest.tearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
883
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
884 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
885 SocketConnectedTest.clientSetUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
886 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
887
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
888 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
889 self.cli_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
890 self.assertTrue(self.cli_file.closed)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
891 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
892 SocketConnectedTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
893
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
894 def testSmallRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
895 # 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
896 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
897 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
898 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
899 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
900
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
901 def _testSmallRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
902 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
903 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
904
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
905 def testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
906 # read until EOF
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
907 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
908 self.assertEqual(msg, MSG)
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
909
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
910 def _testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
911 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
912 self.cli_file.close()
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
913
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
914 def testUnbufferedRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
915 # 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
916 buf = ''
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
917 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
918 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
919 if not char:
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
920 break
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
921 buf += char
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
922 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
923
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
924 def _testUnbufferedRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
925 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
926 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
927
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
928 def testReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
929 # 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
930 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
931 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
932
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
933 def _testReadline(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
934 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
935 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
936
46684
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
937 def testReadlineAfterRead(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
938 a_baloo_is = self.serv_file.read(len("A baloo is"))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
939 self.assertEqual("A baloo is", a_baloo_is)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
940 _a_bear = self.serv_file.read(len(" a bear"))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
941 self.assertEqual(" a bear", _a_bear)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
942 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
943 self.assertEqual("\n", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
944 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
945 self.assertEqual("A BALOO IS A BEAR.\n", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
946 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
947 self.assertEqual(MSG, line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
948
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
949 def _testReadlineAfterRead(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
950 self.cli_file.write("A baloo is a bear\n")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
951 self.cli_file.write("A BALOO IS A BEAR.\n")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
952 self.cli_file.write(MSG)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
953 self.cli_file.flush()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
954
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
955 def testReadlineAfterReadNoNewline(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
956 end_of_ = self.serv_file.read(len("End Of "))
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
957 self.assertEqual("End Of ", end_of_)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
958 line = self.serv_file.readline()
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
959 self.assertEqual("Line", line)
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
960
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
961 def _testReadlineAfterReadNoNewline(self):
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
962 self.cli_file.write("End Of Line")
2729e977fdd9 Fix a bug introduced in r62627. see issue2760 and issue2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 46362
diff changeset
963
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
964 def testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
965 self.assertTrue(not self.serv_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
966
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
967 def _testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
968 self.assertTrue(not self.cli_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
969
55627
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
970
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
971 class FileObjectInterruptedTestCase(unittest.TestCase):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
972 """Test that the file object correctly handles EINTR internally."""
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
973
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
974 class MockSocket(object):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
975 def __init__(self, recv_funcs=()):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
976 # A generator that returns callables that we'll call for each
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
977 # call to recv().
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
978 self._recv_step = iter(recv_funcs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
979
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
980 def recv(self, size):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
981 return self._recv_step.next()()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
982
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
983 @staticmethod
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
984 def _raise_eintr():
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
985 raise socket.error(errno.EINTR)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
986
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
987 def _test_readline(self, size=-1, **kwargs):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
988 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
989 lambda : "This is the first line\nAnd the sec",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
990 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
991 lambda : "ond line is here\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
992 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
993 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
994 fo = socket._fileobject(mock_sock, **kwargs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
995 self.assertEquals(fo.readline(size), "This is the first line\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
996 self.assertEquals(fo.readline(size), "And the second line is here\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
997
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
998 def _test_read(self, size=-1, **kwargs):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
999 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1000 lambda : "This is the first line\nAnd the sec",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1001 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1002 lambda : "ond line is here\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1003 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1004 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1005 fo = socket._fileobject(mock_sock, **kwargs)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1006 self.assertEquals(fo.read(size), "This is the first line\n"
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1007 "And the second line is here\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1008
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1009 def test_default(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1010 self._test_readline()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1011 self._test_readline(size=100)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1012 self._test_read()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1013 self._test_read(size=100)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1014
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1015 def test_with_1k_buffer(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1016 self._test_readline(bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1017 self._test_readline(size=100, bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1018 self._test_read(bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1019 self._test_read(size=100, bufsize=1024)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1020
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1021 def _test_readline_no_buffer(self, size=-1):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1022 mock_sock = self.MockSocket(recv_funcs=[
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1023 lambda : "aa",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1024 lambda : "\n",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1025 lambda : "BB",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1026 self._raise_eintr,
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1027 lambda : "bb",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1028 lambda : "",
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1029 ])
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1030 fo = socket._fileobject(mock_sock, bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1031 self.assertEquals(fo.readline(size), "aa\n")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1032 self.assertEquals(fo.readline(size), "BBbb")
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1033
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1034 def test_no_buffer(self):
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1035 self._test_readline_no_buffer()
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1036 self._test_readline_no_buffer(size=4)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1037 self._test_read(bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1038 self._test_read(size=100, bufsize=0)
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1039
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1040
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1041 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1042
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1043 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24655
diff changeset
1044
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1045 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
1046 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
1047 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
1048 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
1049 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
1050
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1051 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
1052
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1053 def testUnbufferedReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1054 # 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
1055 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
1056 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
1057 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
1058 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
1059 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
1060
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1061 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
1062 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
1063 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
1064 self.cli_file.flush()
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1065
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1066 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1067
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1068 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
1069
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1070
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1071 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1072
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1073 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
1074
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1075
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1076 class NetworkConnectionTest(object):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1077 """Prove network connection."""
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1078 def clientSetUp(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1079 # We're inherited below by BasicTCPTest2, which also inherits
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1080 # BasicTCPTest, which defines self.port referenced below.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1081 self.cli = socket.create_connection((HOST, self.port))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1082 self.serv_conn = self.cli
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1083
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1084 class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1085 """Tests that NetworkConnection does not break existing TCP functionality.
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1086 """
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1087
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1088 class NetworkConnectionNoServer(unittest.TestCase):
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1089 class MockSocket(socket.socket):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1090 def connect(self, *args):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1091 raise socket.timeout('timed out')
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1092
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1093 @contextlib.contextmanager
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1094 def mocked_socket_module(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1095 """Return a socket which times out on connect"""
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1096 old_socket = socket.socket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1097 socket.socket = self.MockSocket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1098 try:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1099 yield
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1100 finally:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1101 socket.socket = old_socket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1102
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1103 def test_connect(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1104 port = test_support.find_unused_port()
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1105 cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1106 with self.assertRaises(socket.error) as cm:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1107 cli.connect((HOST, port))
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1108 self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1109
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1110 def test_create_connection(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1111 # Issue #9792: errors raised by create_connection() should have
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1112 # a proper errno attribute.
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1113 port = test_support.find_unused_port()
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1114 with self.assertRaises(socket.error) as cm:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1115 socket.create_connection((HOST, port))
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1116 self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1117
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1118 def test_create_connection_timeout(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1119 # Issue #9792: create_connection() should not recast timeout errors
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1120 # as generic socket errors.
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1121 with self.mocked_socket_module():
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1122 with self.assertRaises(socket.timeout):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1123 socket.create_connection((HOST, 1234))
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1124
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1125
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
1126 @unittest.skipUnless(thread, 'Threading required for this test.')
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1127 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1128
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1129 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1130 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1131 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1132
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1133 def clientSetUp(self):
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1134 self.source_port = test_support.find_unused_port()
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1135
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1136 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1137 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1138 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1139 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1140
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1141 def _justAccept(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1142 conn, addr = self.serv.accept()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1143
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1144 testFamily = _justAccept
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1145 def _testFamily(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1146 self.cli = socket.create_connection((HOST, self.port), timeout=30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1147 self.assertEqual(self.cli.family, 2)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1148
58135
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1149 testSourceAddress = _justAccept
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1150 def _testSourceAddress(self):
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1151 self.cli = socket.create_connection((HOST, self.port), timeout=30,
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1152 source_address=('', self.source_port))
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1153 self.assertEqual(self.cli.getsockname()[1], self.source_port)
58135
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1154 # The port number being used is sufficient to show that the bind()
c72652145b23 Fix testSourceAddress to not test the host, it wasn't passing on some platforms.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58112
diff changeset
1155 # call happened.
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1156
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1157 testTimeoutDefault = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1158 def _testTimeoutDefault(self):
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1159 # passing no explicit timeout uses socket's global default
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1160 self.assertTrue(socket.getdefaulttimeout() is None)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1161 socket.setdefaulttimeout(42)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1162 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1163 self.cli = socket.create_connection((HOST, self.port))
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1164 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1165 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1166 self.assertEquals(self.cli.gettimeout(), 42)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1167
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1168 testTimeoutNone = _justAccept
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1169 def _testTimeoutNone(self):
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1170 # None timeout means the same as sock.settimeout(None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1171 self.assertTrue(socket.getdefaulttimeout() is None)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1172 socket.setdefaulttimeout(30)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1173 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1174 self.cli = socket.create_connection((HOST, self.port), timeout=None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1175 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1176 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1177 self.assertEqual(self.cli.gettimeout(), None)
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1178
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1179 testTimeoutValueNamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1180 def _testTimeoutValueNamed(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1181 self.cli = socket.create_connection((HOST, self.port), timeout=30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1182 self.assertEqual(self.cli.gettimeout(), 30)
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1183
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1184 testTimeoutValueNonamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1185 def _testTimeoutValueNonamed(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1186 self.cli = socket.create_connection((HOST, self.port), 30)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1187 self.assertEqual(self.cli.gettimeout(), 30)
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1188
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
1189 @unittest.skipUnless(thread, 'Threading required for this test.')
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1190 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1191
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1192 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1193 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1194 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1195
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1196 def clientSetUp(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1197 pass
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1198
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1199 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1200 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1201 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1202 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1203
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1204 def testInsideTimeout(self):
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1205 conn, addr = self.serv.accept()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1206 time.sleep(3)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1207 conn.send("done!")
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1208 testOutsideTimeout = testInsideTimeout
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1209
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1210 def _testInsideTimeout(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1211 self.cli = sock = socket.create_connection((HOST, self.port))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1212 data = sock.recv(5)
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1213 self.assertEqual(data, "done!")
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1214
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1215 def _testOutsideTimeout(self):
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
1216 self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1217 self.assertRaises(socket.timeout, lambda: sock.recv(5))
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1218
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1219
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1220 class Urllib2FileobjectTest(unittest.TestCase):
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1221
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1222 # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1223 # it close the socket if the close c'tor argument is true
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1224
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1225 def testClose(self):
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1226 class MockSocket:
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1227 closed = False
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1228 def flush(self): pass
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1229 def close(self): self.closed = True
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1230
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1231 # must not close unless we request it: the original use of _fileobject
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1232 # by module socket requires that the underlying socket not be closed until
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1233 # the _socketobject that created the _fileobject is closed
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1234 s = MockSocket()
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1235 f = socket._fileobject(s)
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1236 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1237 self.assertTrue(not s.closed)
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1238
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1239 s = MockSocket()
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1240 f = socket._fileobject(s, close=True)
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1241 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1242 self.assertTrue(s.closed)
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1243
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1244 class TCPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1245
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1246 def testTCPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1247 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1248 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1249 self.serv.accept()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1250 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1251 "Error generating a timeout exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1252
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1253 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1254 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1255 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1256 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1257 foo = self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1258 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1259 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
1260 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1261 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1262 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1263 self.fail("caught unexpected exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1264 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1265 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
1266
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1267 def testInterruptedTimeout(self):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1268 # XXX I don't know how to do this test on MSWindows or any other
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1269 # plaform that doesn't support signal.alarm() or os.kill(), though
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1270 # the bug should have existed on all platforms.
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1271 if not hasattr(signal, "alarm"):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1272 return # can only test on *nix
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1273 self.serv.settimeout(5.0) # must be longer than alarm
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1274 class Alarm(Exception):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1275 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1276 def alarm_handler(signal, frame):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1277 raise Alarm
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1278 old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1279 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1280 signal.alarm(2) # POSIX allows alarm to be up to 1 second early
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1281 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1282 foo = self.serv.accept()
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1283 except socket.timeout:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1284 self.fail("caught timeout instead of Alarm")
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1285 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1286 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1287 except:
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1288 self.fail("caught other exception instead of Alarm:"
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1289 " %s(%s):\n%s" %
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1290 (sys.exc_info()[:2] + (traceback.format_exc(),)))
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1291 else:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1292 self.fail("nothing caught")
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1293 finally:
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1294 signal.alarm(0) # shut off alarm
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1295 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1296 self.fail("got Alarm in wrong place")
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1297 finally:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1298 # no alarm can be pending. Safe to restore old handler.
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1299 signal.signal(signal.SIGALRM, old_alarm)
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1300
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1301 class UDPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1302
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1303 def testUDPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1304 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1305 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1306 self.serv.recv(1024)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1307 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1308 "Error generating a timeout exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1309
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1310 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1311 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1312 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1313 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1314 foo = self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1315 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1316 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
1317 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1318 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1319 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1320 self.fail("caught unexpected exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1321 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1322 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
1323
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1324 class TestExceptions(unittest.TestCase):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1325
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1326 def testExceptionTree(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1327 self.assertTrue(issubclass(socket.error, Exception))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1328 self.assertTrue(issubclass(socket.herror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1329 self.assertTrue(issubclass(socket.gaierror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1330 self.assertTrue(issubclass(socket.timeout, socket.error))
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1331
37853
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1332 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
1333
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1334 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
1335
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1336 def testLinuxAbstractNamespace(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1337 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
1338 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
1339 s1.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1340 s1.listen(1)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1341 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
1342 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
1343 s1.accept()
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1344 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
1345 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
1346
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1347 def testMaxName(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1348 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
1349 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
1350 s.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1351 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
1352
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1353 def testNameOverflow(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1354 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
1355 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
1356 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
1357
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1358
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59783
diff changeset
1359 @unittest.skipUnless(thread, 'Threading required for this test.')
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1360 class BufferIOTest(SocketConnectedTest):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1361 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1362 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
1363 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1364 def __init__(self, methodName='runTest'):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1365 SocketConnectedTest.__init__(self, methodName=methodName)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1366
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1367 def testRecvIntoArray(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1368 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1369 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
1370 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1371 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1372 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1373
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1374 def _testRecvIntoArray(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
1375 with test_support.check_py3k_warnings():
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
1376 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1377 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1378
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1379 def testRecvIntoBytearray(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1380 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1381 nbytes = self.cli_conn.recv_into(buf)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1382 self.assertEqual(nbytes, len(MSG))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1383 msg = buf[:len(MSG)]
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1384 self.assertEqual(msg, MSG)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1385
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1386 _testRecvIntoBytearray = _testRecvIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1387
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1388 def testRecvIntoMemoryview(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1389 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1390 nbytes = self.cli_conn.recv_into(memoryview(buf))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1391 self.assertEqual(nbytes, len(MSG))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1392 msg = buf[:len(MSG)]
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1393 self.assertEqual(msg, MSG)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1394
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1395 _testRecvIntoMemoryview = _testRecvIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1396
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1397 def testRecvFromIntoArray(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1398 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1399 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
1400 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1401 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1402 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1403
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1404 def _testRecvFromIntoArray(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
1405 with test_support.check_py3k_warnings():
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59676
diff changeset
1406 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1407 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1408
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1409 def testRecvFromIntoBytearray(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1410 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1411 nbytes, addr = self.cli_conn.recvfrom_into(buf)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1412 self.assertEqual(nbytes, len(MSG))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1413 msg = buf[:len(MSG)]
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1414 self.assertEqual(msg, MSG)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1415
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1416 _testRecvFromIntoBytearray = _testRecvFromIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1417
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1418 def testRecvFromIntoMemoryview(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1419 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1420 nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1421 self.assertEqual(nbytes, len(MSG))
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1422 msg = buf[:len(MSG)]
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1423 self.assertEqual(msg, MSG)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1424
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1425 _testRecvFromIntoMemoryview = _testRecvFromIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1426
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1427
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1428 TIPC_STYPE = 2000
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1429 TIPC_LOWER = 200
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1430 TIPC_UPPER = 210
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1431
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1432 def isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1433 """Check if the TIPC module is loaded
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1434
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1435 The TIPC module is not loaded automatically on Ubuntu and probably
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1436 other Linux distros.
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1437 """
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1438 if not hasattr(socket, "AF_TIPC"):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1439 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1440 if not os.path.isfile("/proc/modules"):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1441 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1442 with open("/proc/modules") as f:
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1443 for line in f:
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1444 if line.startswith("tipc "):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1445 return True
44592
cb22f8748da7 It's verbose, not debug
Christian Heimes <christian@cheimes.de>
parents: 44581
diff changeset
1446 if test_support.verbose:
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1447 print "TIPC module is not loaded, please 'sudo modprobe tipc'"
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1448 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1449
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1450 class TIPCTest (unittest.TestCase):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1451 def testRDM(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1452 srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1453 cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1454
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1455 srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1456 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1457 TIPC_LOWER, TIPC_UPPER)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1458 srv.bind(srvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1459
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1460 sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1461 TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1462 cli.sendto(MSG, sendaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1463
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1464 msg, recvaddr = srv.recvfrom(1024)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1465
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1466 self.assertEqual(cli.getsockname(), recvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1467 self.assertEqual(msg, MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1468
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1469
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1470 class TIPCThreadableTest (unittest.TestCase, ThreadableTest):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1471 def __init__(self, methodName = 'runTest'):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1472 unittest.TestCase.__init__(self, methodName = methodName)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1473 ThreadableTest.__init__(self)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1474
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1475 def setUp(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1476 self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1477 self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1478 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1479 TIPC_LOWER, TIPC_UPPER)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1480 self.srv.bind(srvaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1481 self.srv.listen(5)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1482 self.serverExplicitReady()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1483 self.conn, self.connaddr = self.srv.accept()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1484
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1485 def clientSetUp(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1486 # The is a hittable race between serverExplicitReady() and the
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1487 # accept() call; sleep a little while to avoid it, otherwise
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1488 # we could get an exception
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1489 time.sleep(0.1)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1490 self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1491 addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1492 TIPC_LOWER + (TIPC_UPPER - TIPC_LOWER) / 2, 0)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1493 self.cli.connect(addr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1494 self.cliaddr = self.cli.getsockname()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1495
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1496 def testStream(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1497 msg = self.conn.recv(1024)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1498 self.assertEqual(msg, MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1499 self.assertEqual(self.cliaddr, self.connaddr)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1500
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1501 def _testStream(self):
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1502 self.cli.send(MSG)
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1503 self.cli.close()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1504
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1505
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1506 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
1507 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
61258
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61046
diff changeset
1508 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest,
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61046
diff changeset
1509 UDPTimeoutTest ]
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1510
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1511 tests.extend([
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1512 NonBlockingTCPTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1513 FileObjectClassTestCase,
55627
fbf8b791ce81 Fix issue1628205: Socket file objects returned by socket.socket.makefile() now
Gregory P. Smith <greg@mad-scientist.com>
parents: 55002
diff changeset
1514 FileObjectInterruptedTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1515 UnbufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1516 LineBufferedFileObjectClassTestCase,
40684
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1517 SmallBufferedFileObjectClassTestCase,
2fa6e82b1f17 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 40632
diff changeset
1518 Urllib2FileobjectTest,
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1519 NetworkConnectionNoServer,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1520 NetworkConnectionAttributesTest,
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1521 NetworkConnectionBehaviourTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1522 ])
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1523 if hasattr(socket, "socketpair"):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1524 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
1525 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
1526 tests.append(TestLinuxAbstractNamespace)
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1527 if isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1528 tests.append(TIPCTest)
44581
6d521603e07d Fixed indention problem that caused the second TIPC test to run on systems without TIPC
Christian Heimes <christian@cheimes.de>
parents: 44561
diff changeset
1529 tests.append(TIPCThreadableTest)
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1530
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1531 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
1532 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
1533 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
1534
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1535 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
1536 test_main()