annotate Lib/test/test_socket.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 8599036d9140
children 39803c20c9bf
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
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
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
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
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()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
123 self.queue = Queue.Queue(1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
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()
72447
93a5388a3721 Fix a race condition in test_socket.ThreadableTest: the client is reported
Charles-François Natali <neologix@free.fr>
parents: 70011
diff changeset
148 self.clientSetUp()
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
149 self.client_ready.set()
73405
8a0e7eb18f16 Fix another callable warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72447
diff changeset
150 if not callable(test_func):
8a0e7eb18f16 Fix another callable warning.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72447
diff changeset
151 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
152 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
153 test_func()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
154 except Exception, strerror:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
155 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
156 self.clientTearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
157
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
158 def clientSetUp(self):
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61875
diff changeset
159 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
160
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
161 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
162 self.done.set()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
163 thread.exit()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
164
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
165 class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
166
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
167 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
168 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
169 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
170
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
171 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
172 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
173
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
174 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
175 self.cli.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
176 self.cli = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
177 ThreadableTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
178
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
179 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
180
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
181 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
182 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
183 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
184
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
185 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
186 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
187
65928
299735e5fd7f Merged revisions 86151 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65891
diff changeset
188 def clientTearDown(self):
299735e5fd7f Merged revisions 86151 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65891
diff changeset
189 self.cli.close()
299735e5fd7f Merged revisions 86151 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65891
diff changeset
190 self.cli = None
299735e5fd7f Merged revisions 86151 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65891
diff changeset
191 ThreadableTest.clientTearDown(self)
299735e5fd7f Merged revisions 86151 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65891
diff changeset
192
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
193 class SocketConnectedTest(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
194
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
195 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
196 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
197
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
198 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
199 ThreadedTCPSocketTest.setUp(self)
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
200 # 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
201 # 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
202 self.serverExplicitReady()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
203 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
204 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
205
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
206 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
207 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
208 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
209 ThreadedTCPSocketTest.tearDown(self)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
210
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
211 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
212 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
213 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
214 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
215
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
216 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
217 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
218 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
219 ThreadedTCPSocketTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
220
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
221 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
222
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
223 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
224 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
225 ThreadableTest.__init__(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
226
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
227 def setUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
228 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
229
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
230 def tearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
231 self.serv.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
232 self.serv = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
233
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
234 def clientSetUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
235 pass
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
236
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
237 def clientTearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
238 self.cli.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
239 self.cli = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
240 ThreadableTest.clientTearDown(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
241
33076
836e77266f97 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33063
diff changeset
242
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
243 #######################################################################
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
244 ## Begin Tests
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
245
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
246 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
247
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
248 def test_weakref(self):
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
249 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
250 p = proxy(s)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
251 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
252 s.close()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
253 s = None
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
254 try:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
255 p.fileno()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
256 except ReferenceError:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
257 pass
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
258 else:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
259 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
260
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
261 def testSocketError(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
262 # 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
263 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
264 raise socket.error
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
265 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
266 raise socket.herror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
267 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
268 raise socket.gaierror
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
269 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
270 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
271 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
272 "Error raising socket exception.")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
273 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
274 "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
275
69915
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
276 def testSendtoErrors(self):
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
277 # Testing that sendto doens't masks failures. See #10169.
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
278 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
279 self.addCleanup(s.close)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
280 s.bind(('', 0))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
281 sockname = s.getsockname()
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
282 # 2 args
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
283 with self.assertRaises(UnicodeEncodeError):
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
284 s.sendto(u'\u2620', sockname)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
285 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
286 s.sendto(5j, sockname)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
287 self.assertIn('not complex', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
288 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
289 s.sendto('foo', None)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
290 self.assertIn('not NoneType', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
291 # 3 args
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
292 with self.assertRaises(UnicodeEncodeError):
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
293 s.sendto(u'\u2620', 0, sockname)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
294 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
295 s.sendto(5j, 0, sockname)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
296 self.assertIn('not complex', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
297 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
298 s.sendto('foo', 0, None)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
299 self.assertIn('not NoneType', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
300 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
301 s.sendto('foo', 'bar', sockname)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
302 self.assertIn('an integer is required', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
303 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
304 s.sendto('foo', None, None)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
305 self.assertIn('an integer is required', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
306 # wrong number of args
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
307 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
308 s.sendto('foo')
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
309 self.assertIn('(1 given)', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
310 with self.assertRaises(TypeError) as cm:
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
311 s.sendto('foo', 0, sockname, 4)
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
312 self.assertIn('(4 given)', str(cm.exception))
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
313
7c3a20b5943a #10169: Fix argument parsing in socket.sendto() to avoid error masking.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
314
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
315 def testCrucialConstants(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
316 # 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
317 socket.AF_INET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
318 socket.SOCK_STREAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
319 socket.SOCK_DGRAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
320 socket.SOCK_RAW
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
321 socket.SOCK_RDM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
322 socket.SOCK_SEQPACKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
323 socket.SOL_SOCKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
324 socket.SO_REUSEADDR
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
325
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
326 def testHostnameRes(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
327 # Testing hostname resolution mechanisms
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
328 hostname = socket.gethostname()
26799
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
329 try:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
330 ip = socket.gethostbyname(hostname)
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
331 except socket.error:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
332 # 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
333 return
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
334 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
335 try:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
336 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
337 except socket.error:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
338 # 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
339 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
340 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
341 fqhn = socket.getfqdn(ip)
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
342 if not fqhn in all_host_names:
37227
8d57d25e6548 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 37225
diff changeset
343 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
344
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
345 def testRefCountGetNameInfo(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
346 # 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
347 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
348 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
349 # 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
350 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
351 socket.getnameinfo(__name__,0)
56109
58ba85c0d4b0 Merged revisions 74943 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50799
diff changeset
352 except TypeError:
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61875
diff changeset
353 self.assertEqual(sys.getrefcount(__name__), orig,
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61875
diff changeset
354 "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
355
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
356 def testInterpreterCrash(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
357 # 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
358 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
359 # 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
360 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
361 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
362 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
363
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
364 def testNtoH(self):
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
365 # 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
366 # 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
367 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
368 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
369 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
370 mask = (1L<<size) - 1
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
371 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
372 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
373
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
374 swapped = func(mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
375 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
376 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
377
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
378 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
379 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
380 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
381 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
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 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
390 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
391
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
392 def testGetServBy(self):
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
393 eq = self.assertEqual
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
394 # 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
395 # 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
396 # protocol, at least for modern Linuxes.
65430
7025278c5a41 Merged revisions 85586-85587,85596-85598 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 64962
diff changeset
397 if (sys.platform.startswith('linux') or
7025278c5a41 Merged revisions 85586-85587,85596-85598 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 64962
diff changeset
398 sys.platform.startswith('freebsd') or
7025278c5a41 Merged revisions 85586-85587,85596-85598 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 64962
diff changeset
399 sys.platform.startswith('netbsd') or
7025278c5a41 Merged revisions 85586-85587,85596-85598 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 64962
diff changeset
400 sys.platform == '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
401 # 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
402 # 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
403 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
404 else:
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
405 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
406 for service in services:
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
407 try:
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
408 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
409 break
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
410 except socket.error:
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
411 pass
31503
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
412 else:
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
413 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
414 # 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
415 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
416 eq(port, port2)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
417 # 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
418 try:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
419 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
420 except socket.error:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
421 udpport = None
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
422 else:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
423 eq(udpport, port)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
424 # 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
425 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
426 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
427 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
428 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
429 # 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
430 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
431 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
432
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
433 def testDefaultTimeout(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
434 # Testing default timeout
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
435 # 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
436 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
437 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
438 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
439 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
440
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
441 # 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
442 socket.setdefaulttimeout(10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
443 self.assertEqual(socket.getdefaulttimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
444 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
445 self.assertEqual(s.gettimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
446 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
447
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
448 # 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
449 socket.setdefaulttimeout(None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
450 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
451 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
452 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
453 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
454
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
455 # 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
456 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
457
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
458 # 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
459 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
460
57037
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
461 def testIPv4_inet_aton_fourbytes(self):
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
462 if not hasattr(socket, 'inet_aton'):
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
463 return # No inet_aton, nothing to check
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
464 # Test that issue1008086 and issue767150 are fixed.
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
465 # It must return 4 bytes.
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
466 self.assertEqual('\x00'*4, socket.inet_aton('0.0.0.0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
467 self.assertEqual('\xff'*4, socket.inet_aton('255.255.255.255'))
57037
c78a00a1d6db Merged revisions 69519 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56109
diff changeset
468
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
469 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
470 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
471 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
472 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
473 g = lambda a: inet_pton(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
474
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
475 self.assertEqual('\x00\x00\x00\x00', f('0.0.0.0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
476 self.assertEqual('\xff\x00\xff\x00', f('255.0.255.0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
477 self.assertEqual('\xaa\xaa\xaa\xaa', f('170.170.170.170'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
478 self.assertEqual('\x01\x02\x03\x04', f('1.2.3.4'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
479 self.assertEqual('\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
480
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
481 self.assertEqual('\x00\x00\x00\x00', g('0.0.0.0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
482 self.assertEqual('\xff\x00\xff\x00', g('255.0.255.0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
483 self.assertEqual('\xaa\xaa\xaa\xaa', g('170.170.170.170'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
484 self.assertEqual('\xff\xff\xff\xff', g('255.255.255.255'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
485
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
486 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
487 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
488 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
489 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
490 from socket import inet_pton, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
491 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
492 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
493 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
494 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
495 f = lambda a: inet_pton(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
496
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
497 self.assertEqual('\x00' * 16, f('::'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
498 self.assertEqual('\x00' * 16, f('0::0'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
499 self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
500 self.assertEqual(
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
501 '\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
502 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
503 )
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
504
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
505 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
506 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
507 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
508 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
509 g = lambda a: inet_ntop(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
510
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
511 self.assertEqual('1.0.1.0', f('\x01\x00\x01\x00'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
512 self.assertEqual('170.85.170.85', f('\xaa\x55\xaa\x55'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
513 self.assertEqual('255.255.255.255', f('\xff\xff\xff\xff'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
514 self.assertEqual('1.2.3.4', f('\x01\x02\x03\x04'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
515
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
516 self.assertEqual('1.0.1.0', g('\x01\x00\x01\x00'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
517 self.assertEqual('170.85.170.85', g('\xaa\x55\xaa\x55'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
518 self.assertEqual('255.255.255.255', g('\xff\xff\xff\xff'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
519
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
520 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
521 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
522 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
523 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
524 from socket import inet_ntop, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
525 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
526 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
527 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
528 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
529 f = lambda a: inet_ntop(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
530
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
531 self.assertEqual('::', f('\x00' * 16))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
532 self.assertEqual('::1', f('\x00' * 15 + '\x01'))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
533 self.assertEqual(
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
534 'aef:b01:506:1001:ffff:9997:55:170',
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
535 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
536 )
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
537
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 # 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
539
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
540 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
541 """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
542
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
543 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
544 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
545
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
546 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
547 """
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
548 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
549 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
550 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
551 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
552 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
553
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
554 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
555 # 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
556 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
557 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
558 self.addCleanup(sock.close)
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46298
diff changeset
559 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
560 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
561 # 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
562 # 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
563 # At least for eCos. This is required for the S/390 to pass.
66467
7c0eb8d56146 Merged revisions 86561-86562,86564-86565,86705,86708,86713 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
564 try:
7c0eb8d56146 Merged revisions 86561-86562,86564-86565,86705,86708,86713 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
565 my_ip_addr = socket.gethostbyname(socket.gethostname())
7c0eb8d56146 Merged revisions 86561-86562,86564-86565,86705,86708,86713 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
566 except socket.error:
7c0eb8d56146 Merged revisions 86561-86562,86564-86565,86705,86708,86713 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
567 # Probably name lookup wasn't set up right; skip this test
7c0eb8d56146 Merged revisions 86561-86562,86564-86565,86705,86708,86713 via svnmerge from
Georg Brandl <georg@python.org>
parents: 66348
diff changeset
568 return
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
569 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
570 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
571
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
572 def testGetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
573 # Testing getsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
574 # 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
575 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
576 self.addCleanup(sock.close)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
577 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
578 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
579
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
580 def testSetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
581 # Testing setsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
582 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
583 self.addCleanup(sock.close)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
584 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
585 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
586 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
587
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
588 def testSendAfterClose(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
589 # 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
590 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
591 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
592 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
593 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
594
37123
251ecec04f1b Correct API design mistake from rev. 43126: make socket attributes readonly properties.
Georg Brandl <georg@python.org>
parents: 37069
diff changeset
595 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
596 # 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
597 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
598 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
599 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
600 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
601 sock.close()
dd49847a60d3 RFE #567972: Socket objects' family, type and proto properties are
Georg Brandl <georg@python.org>
parents: 35453
diff changeset
602
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
603 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
604 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
605 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
606 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
607 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
608 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
609 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
610 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
611 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
612 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
613 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
614 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
615
65976
edf8c4b00041 Merged revisions 86205 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65928
diff changeset
616 @unittest.skipUnless(os.name == "nt", "Windows specific")
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
617 def test_sock_ioctl(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
618 self.assertTrue(hasattr(socket.socket, 'ioctl'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
619 self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
620 self.assertTrue(hasattr(socket, 'RCVALL_ON'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
621 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
622 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
623 s = socket.socket()
65976
edf8c4b00041 Merged revisions 86205 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65928
diff changeset
624 self.addCleanup(s.close)
56212
3def952b515c http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 56107
diff changeset
625 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
626 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
627
64002
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
628 def testGetaddrinfo(self):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
629 try:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
630 socket.getaddrinfo('localhost', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
631 except socket.gaierror as err:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
632 if err.errno == socket.EAI_SERVICE:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
633 # see http://bugs.python.org/issue1282647
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
634 self.skipTest("buggy libc version")
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
635 raise
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
636 # 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
637 for info in socket.getaddrinfo(HOST, None):
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
638 self.assertEqual(len(info), 5)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
639 # 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
640 # IPv4/v6 address or None
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
641 socket.getaddrinfo('localhost', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
642 socket.getaddrinfo('127.0.0.1', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
643 socket.getaddrinfo(None, 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
644 if SUPPORTS_IPV6:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
645 socket.getaddrinfo('::1', 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
646 # 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
647 # port number or None
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
648 socket.getaddrinfo(HOST, "http")
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
649 socket.getaddrinfo(HOST, 80)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
650 socket.getaddrinfo(HOST, None)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
651 # test family and socktype filters
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
652 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
653 for family, _, _, _, _ in infos:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
654 self.assertEqual(family, socket.AF_INET)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
655 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
656 for _, socktype, _, _, _ in infos:
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
657 self.assertEqual(socktype, socket.SOCK_STREAM)
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
658 # test proto and flags arguments
64061
b4d88f9ef3f9 Merged revisions 84089 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64002
diff changeset
659 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
660 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
661 # 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
662 # usually do this
ab670e414c26 Merged revisions 84024 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 61258
diff changeset
663 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
664 socket.AI_PASSIVE)
44478
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
665
73bc002252bf socket.ioctl is only available on Windows
Christian Heimes <christian@cheimes.de>
parents: 43703
diff changeset
666
64962
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
667 def check_sendall_interrupted(self, with_timeout):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
668 # 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
669 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
670 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
671 # 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
672 # with an invalid domain value.
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
673 def ok_handler(*args):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
674 self.assertRaises(ValueError, math.acosh, 0)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
675 def raising_handler(*args):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
676 self.assertRaises(ValueError, math.acosh, 0)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
677 1 // 0
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
678 c, s = socket.socketpair()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
679 old_alarm = signal.signal(signal.SIGALRM, raising_handler)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
680 try:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
681 if with_timeout:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
682 # 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
683 c.settimeout(1.5)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
684 with self.assertRaises(ZeroDivisionError):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
685 signal.alarm(1)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
686 c.sendall(b"x" * (1024**2))
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
687 if with_timeout:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
688 signal.signal(signal.SIGALRM, ok_handler)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
689 signal.alarm(1)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
690 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
691 finally:
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
692 signal.signal(signal.SIGALRM, old_alarm)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
693 c.close()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
694 s.close()
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
695
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
696 def test_sendall_interrupted(self):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
697 self.check_sendall_interrupted(False)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
698
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
699 def test_sendall_interrupted_with_timeout(self):
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
700 self.check_sendall_interrupted(True)
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
701
70011
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
702 def testListenBacklog0(self):
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
703 srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
704 srv.bind((HOST, 0))
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
705 # backlog = 0
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
706 srv.listen(0)
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
707 srv.close()
48743ad2d2ef Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
Antoine Pitrou <solipsis@pitrou.net>
parents: 69915
diff changeset
708
74232
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
709 @unittest.skipUnless(SUPPORTS_IPV6, 'IPv6 required for this test.')
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
710 def test_flowinfo(self):
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
711 self.assertRaises(OverflowError, socket.getnameinfo,
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
712 ('::1',0, 0xffffffff), 0)
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
713 s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
714 try:
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
715 self.assertRaises(OverflowError, s.bind, ('::1', 0, -10))
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
716 finally:
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
717 s.close()
0c10061df711 Issue #9975: socket: Fix incorrect use of flowinfo and scope_id. Patch by
Charles-François Natali <neologix@free.fr>
parents: 73611
diff changeset
718
64962
1dc2b50d344f Merged revisions 85032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
719
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
720 @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
721 class BasicTCPTest(SocketConnectedTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
722
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
723 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
724 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
725
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
726 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
727 # 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
728 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
729 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
730
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
731 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
732 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
733
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
734 def testOverFlowRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
735 # 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
736 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
737 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
738 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
739 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
740
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
741 def _testOverFlowRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
742 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
743
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
744 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
745 # 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
746 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
747 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
748
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
749 def _testRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
750 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
751
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
752 def testOverFlowRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
753 # 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
754 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
755 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
756 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
757 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
758
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
759 def _testOverFlowRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
760 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
761
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
762 def testSendAll(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
763 # 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
764 msg = ''
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
765 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
766 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
767 if not read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
768 break
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
769 msg += read
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
770 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
771
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
772 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
773 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
774 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
775
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
776 def testFromFd(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
777 # Testing fromfd()
23797
ba29485d0720 Argh. Typo. :-(
Guido van Rossum <guido@python.org>
parents: 23796
diff changeset
778 if not hasattr(socket, "fromfd"):
23796
fcc36ee1924b Allow absent fromfd(), for Windows.
Guido van Rossum <guido@python.org>
parents: 23795
diff changeset
779 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
780 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
781 sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
782 self.addCleanup(sock.close)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
783 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
784 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
785
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
786 def _testFromFd(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
787 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
788
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
789 def testDup(self):
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
790 # Testing dup()
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
791 sock = self.cli_conn.dup()
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
792 self.addCleanup(sock.close)
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
793 msg = sock.recv(1024)
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
794 self.assertEqual(msg, MSG)
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
795
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
796 def _testDup(self):
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
797 self.serv_conn.send(MSG)
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
798
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
799 def testShutdown(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
800 # Testing shutdown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
801 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
802 self.assertEqual(msg, MSG)
50799
d0086c176e82 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
803 # wait for _testShutdown to finish: on OS X, when the server
d0086c176e82 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
804 # closes the connection the client also becomes disconnected,
d0086c176e82 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
805 # and the client's shutdown call will fail. (Issue #4397.)
d0086c176e82 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 47598
diff changeset
806 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
807
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
808 def _testShutdown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
809 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
810 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
811
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
812 @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
813 class BasicUDPTest(ThreadedUDPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
814
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
815 def __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
816 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
817
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
818 def testSendtoAndRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
819 # 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
820 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
821 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
822
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
823 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
824 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
825
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
826 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
827 # 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
828 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
829 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
830
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
831 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
832 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
833
41343
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
834 def testRecvFromNegative(self):
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
835 # Negative lengths passed to recvfrom should give ValueError.
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
836 self.assertRaises(ValueError, self.serv.recvfrom, -1)
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
837
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
838 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
839 self.cli.sendto(MSG, 0, (HOST, self.port))
41343
c2bf3d646eff Backport 54594:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40685
diff changeset
840
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
841 @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
842 class TCPCloserTest(ThreadedTCPSocketTest):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
843
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
844 def testClose(self):
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
845 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
846 conn.close()
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
847
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
848 sd = self.cli
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
849 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
850 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
851 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
852
c547f6e91c58 Release all forwarded functions in .close. Fixes #1513223.
Martin v. Löwis <martin@v.loewis.de>
parents: 38867
diff changeset
853 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
854 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
855 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
856
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
857 @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
858 class BasicSocketPairTest(SocketPairTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
859
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
860 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
861 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
862
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
863 def testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
864 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
865 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
866
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
867 def _testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
868 self.cli.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
869
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
870 def testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
871 self.serv.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
872
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
873 def _testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
874 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
875 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
876
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
877 @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
878 class NonBlockingTCPTests(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
879
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
880 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
881 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
882
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
883 def testSetBlocking(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
884 # 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
885 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
886 start = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
887 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
888 self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
889 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
890 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
891 end = time.time()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
892 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
893
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
894 def _testSetBlocking(self):
17208
c8817a9bbbdd Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents: 16519
diff changeset
895 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
896
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
897 def testAccept(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
898 # 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
899 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
900 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
901 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
902 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
903 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
904 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
905 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
906 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
907 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
908 conn, addr = self.serv.accept()
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
909 conn.close()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
910 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
911 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
912
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
913 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
914 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
915 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
916
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
917 def testConnect(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
918 # 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
919 conn, addr = self.serv.accept()
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
920 conn.close()
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
921
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
922 def _testConnect(self):
23818
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
923 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
924 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
925
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
926 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
927 # 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
928 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
929 conn.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
930 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
931 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
932 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
933 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
934 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
935 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
936 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
937 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
938 msg = conn.recv(len(MSG))
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
939 conn.close()
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
940 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
941 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
942 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
943
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
944 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
945 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
946 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
947 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
948
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
949 @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
950 class FileObjectClassTestCase(SocketConnectedTest):
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
951
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
952 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
953
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
954 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
955 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
956
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
957 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
958 SocketConnectedTest.setUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
959 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
960
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
961 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
962 self.serv_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
963 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
964 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
965 SocketConnectedTest.tearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
966
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
967 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
968 SocketConnectedTest.clientSetUp(self)
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
969 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
970
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
971 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
972 self.cli_file.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
973 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
974 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
975 SocketConnectedTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
976
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
977 def testSmallRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
978 # 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
979 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
980 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
981 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
982 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
983
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
984 def _testSmallRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
985 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
986 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
987
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
988 def testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
989 # read until EOF
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
990 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
991 self.assertEqual(msg, MSG)
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
992
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
993 def _testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
994 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
995 self.cli_file.close()
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
996
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
997 def testUnbufferedRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
998 # 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
999 buf = ''
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1000 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1001 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
1002 if not char:
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1003 break
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1004 buf += char
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1005 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
1006
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1007 def _testUnbufferedRead(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1008 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
1009 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
1010
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1011 def testReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1012 # 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
1013 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
1014 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
1015
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1016 def _testReadline(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1017 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
1018 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
1019
47898
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1020 def testReadlineAfterRead(self):
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1021 a_baloo_is = self.serv_file.read(len("A baloo is"))
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1022 self.assertEqual("A baloo is", a_baloo_is)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1023 _a_bear = self.serv_file.read(len(" a bear"))
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1024 self.assertEqual(" a bear", _a_bear)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1025 line = self.serv_file.readline()
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1026 self.assertEqual("\n", line)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1027 line = self.serv_file.readline()
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1028 self.assertEqual("A BALOO IS A BEAR.\n", line)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1029 line = self.serv_file.readline()
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1030 self.assertEqual(MSG, line)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1031
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1032 def _testReadlineAfterRead(self):
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1033 self.cli_file.write("A baloo is a bear\n")
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1034 self.cli_file.write("A BALOO IS A BEAR.\n")
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1035 self.cli_file.write(MSG)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1036 self.cli_file.flush()
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1037
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1038 def testReadlineAfterReadNoNewline(self):
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1039 end_of_ = self.serv_file.read(len("End Of "))
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1040 self.assertEqual("End Of ", end_of_)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1041 line = self.serv_file.readline()
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1042 self.assertEqual("Line", line)
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1043
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1044 def _testReadlineAfterReadNoNewline(self):
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1045 self.cli_file.write("End Of Line")
a312e03a0633 Backport r62627 + r62744 from trunk to fix issue 2632.
Gregory P. Smith <greg@mad-scientist.com>
parents: 41343
diff changeset
1046
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1047 def testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1048 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
1049
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1050 def _testClosedAttr(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1051 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
1052
61875
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1053
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1054 class FileObjectInterruptedTestCase(unittest.TestCase):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1055 """Test that the file object correctly handles EINTR internally."""
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1056
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1057 class MockSocket(object):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1058 def __init__(self, recv_funcs=()):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1059 # A generator that returns callables that we'll call for each
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1060 # call to recv().
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1061 self._recv_step = iter(recv_funcs)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1062
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1063 def recv(self, size):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1064 return self._recv_step.next()()
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1065
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1066 @staticmethod
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1067 def _raise_eintr():
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1068 raise socket.error(errno.EINTR)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1069
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1070 def _test_readline(self, size=-1, **kwargs):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1071 mock_sock = self.MockSocket(recv_funcs=[
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1072 lambda : "This is the first line\nAnd the sec",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1073 self._raise_eintr,
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1074 lambda : "ond line is here\n",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1075 lambda : "",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1076 ])
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1077 fo = socket._fileobject(mock_sock, **kwargs)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1078 self.assertEqual(fo.readline(size), "This is the first line\n")
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1079 self.assertEqual(fo.readline(size), "And the second line is here\n")
61875
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1080
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1081 def _test_read(self, size=-1, **kwargs):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1082 mock_sock = self.MockSocket(recv_funcs=[
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1083 lambda : "This is the first line\nAnd the sec",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1084 self._raise_eintr,
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1085 lambda : "ond line is here\n",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1086 lambda : "",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1087 ])
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1088 fo = socket._fileobject(mock_sock, **kwargs)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1089 self.assertEqual(fo.read(size), "This is the first line\n"
61875
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1090 "And the second line is here\n")
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1091
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1092 def test_default(self):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1093 self._test_readline()
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1094 self._test_readline(size=100)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1095 self._test_read()
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1096 self._test_read(size=100)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1097
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1098 def test_with_1k_buffer(self):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1099 self._test_readline(bufsize=1024)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1100 self._test_readline(size=100, bufsize=1024)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1101 self._test_read(bufsize=1024)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1102 self._test_read(size=100, bufsize=1024)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1103
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1104 def _test_readline_no_buffer(self, size=-1):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1105 mock_sock = self.MockSocket(recv_funcs=[
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1106 lambda : "aa",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1107 lambda : "\n",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1108 lambda : "BB",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1109 self._raise_eintr,
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1110 lambda : "bb",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1111 lambda : "",
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1112 ])
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1113 fo = socket._fileobject(mock_sock, bufsize=0)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1114 self.assertEqual(fo.readline(size), "aa\n")
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1115 self.assertEqual(fo.readline(size), "BBbb")
61875
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1116
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1117 def test_no_buffer(self):
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1118 self._test_readline_no_buffer()
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1119 self._test_readline_no_buffer(size=4)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1120 self._test_read(bufsize=0)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1121 self._test_read(size=100, bufsize=0)
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1122
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1123
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1124 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1125
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1126 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24655
diff changeset
1127
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1128 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
1129 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
1130 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
1131 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
1132 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
1133
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1134 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
1135
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1136 def testUnbufferedReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1137 # 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
1138 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
1139 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
1140 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
1141 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
1142 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
1143
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1144 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
1145 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
1146 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
1147 self.cli_file.flush()
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1148
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1149 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1150
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1151 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
1152
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1153
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1154 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1155
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1156 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
1157
40685
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1158
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1159 class NetworkConnectionTest(object):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1160 """Prove network connection."""
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1161 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
1162 # 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
1163 # 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
1164 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
1165 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
1166
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1167 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
1168 """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
1169 """
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1170
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1171 class NetworkConnectionNoServer(unittest.TestCase):
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1172 class MockSocket(socket.socket):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1173 def connect(self, *args):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1174 raise socket.timeout('timed out')
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1175
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1176 @contextlib.contextmanager
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1177 def mocked_socket_module(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1178 """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
1179 old_socket = socket.socket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1180 socket.socket = self.MockSocket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1181 try:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1182 yield
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1183 finally:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1184 socket.socket = old_socket
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1185
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1186 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
1187 port = test_support.find_unused_port()
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1188 cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1189 self.addCleanup(cli.close)
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1190 with self.assertRaises(socket.error) as cm:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1191 cli.connect((HOST, port))
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1192 self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1193
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1194 def test_create_connection(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1195 # 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
1196 # a proper errno attribute.
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1197 port = test_support.find_unused_port()
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1198 with self.assertRaises(socket.error) as cm:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1199 socket.create_connection((HOST, port))
79812
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1200
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1201 # Issue #16257: create_connection() calls getaddrinfo() against
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1202 # 'localhost'. This may result in an IPV6 addr being returned
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1203 # as well as an IPV4 one:
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1204 # >>> socket.getaddrinfo('localhost', port, 0, SOCK_STREAM)
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1205 # >>> [(2, 2, 0, '', ('127.0.0.1', 41230)),
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1206 # (26, 2, 0, '', ('::1', 41230, 0, 0))]
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1207 #
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1208 # create_connection() enumerates through all the addresses returned
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1209 # and if it doesn't successfully bind to any of them, it propagates
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1210 # the last exception it encountered.
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1211 #
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1212 # On Solaris, ENETUNREACH is returned in this circumstance instead
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1213 # of ECONNREFUSED. So, if that errno exists, add it to our list of
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1214 # expected errnos.
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1215 expected_errnos = [ errno.ECONNREFUSED, ]
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1216 if hasattr(errno, 'ENETUNREACH'):
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1217 expected_errnos.append(errno.ENETUNREACH)
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1218
8599036d9140 Issue #16257: make test_create_connection() handle ENETUNREACH.
Trent Nelson <trent@trent.me>
parents: 74232
diff changeset
1219 self.assertIn(cm.exception.errno, expected_errnos)
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1220
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1221 def test_create_connection_timeout(self):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1222 # 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
1223 # as generic socket errors.
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1224 with self.mocked_socket_module():
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1225 with self.assertRaises(socket.timeout):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1226 socket.create_connection((HOST, 1234))
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64061
diff changeset
1227
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1228
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
1229 @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
1230 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1231
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1232 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1233 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1234 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1235
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1236 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
1237 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
1238
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1239 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1240 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1241 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1242 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1243
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1244 def _justAccept(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1245 conn, addr = self.serv.accept()
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1246 conn.close()
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1247
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1248 testFamily = _justAccept
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1249 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
1250 self.cli = socket.create_connection((HOST, self.port), timeout=30)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1251 self.addCleanup(self.cli.close)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1252 self.assertEqual(self.cli.family, 2)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1253
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
1254 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
1255 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
1256 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
1257 source_address=('', self.source_port))
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1258 self.addCleanup(self.cli.close)
58112
72e3e85bac8b Adds an optional source_address parameter to socket.create_connection().
Gregory P. Smith <greg@mad-scientist.com>
parents: 56212
diff changeset
1259 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
1260 # 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
1261 # 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
1262
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1263 testTimeoutDefault = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1264 def _testTimeoutDefault(self):
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1265 # 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
1266 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
1267 socket.setdefaulttimeout(42)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1268 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1269 self.cli = socket.create_connection((HOST, self.port))
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1270 self.addCleanup(self.cli.close)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1271 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1272 socket.setdefaulttimeout(None)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65976
diff changeset
1273 self.assertEqual(self.cli.gettimeout(), 42)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1274
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1275 testTimeoutNone = _justAccept
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1276 def _testTimeoutNone(self):
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1277 # 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
1278 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
1279 socket.setdefaulttimeout(30)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1280 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1281 self.cli = socket.create_connection((HOST, self.port), timeout=None)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1282 self.addCleanup(self.cli.close)
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1283 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1284 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 47248
diff changeset
1285 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
1286
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1287 testTimeoutValueNamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1288 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
1289 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
1290 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
1291
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41313
diff changeset
1292 testTimeoutValueNonamed = _justAccept
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1293 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
1294 self.cli = socket.create_connection((HOST, self.port), 30)
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1295 self.addCleanup(self.cli.close)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1296 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
1297
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
1298 @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
1299 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1301 def __init__(self, methodName='runTest'):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1302 SocketTCPTest.__init__(self, methodName=methodName)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1303 ThreadableTest.__init__(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1304
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1305 def clientSetUp(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1306 pass
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1307
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1308 def clientTearDown(self):
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1309 self.cli.close()
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1310 self.cli = None
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1311 ThreadableTest.clientTearDown(self)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1312
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1313 def testInsideTimeout(self):
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1314 conn, addr = self.serv.accept()
65883
a37d6d81f4b1 Merged revisions 86105 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65430
diff changeset
1315 self.addCleanup(conn.close)
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1316 time.sleep(3)
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1317 conn.send("done!")
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1318 testOutsideTimeout = testInsideTimeout
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1319
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1320 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
1321 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
1322 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
1323 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
1324
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1325 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
1326 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
1327 self.assertRaises(socket.timeout, lambda: sock.recv(5))
63937
5343f5714fee #9543: Fix regression introduced in r83624.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63572
diff changeset
1328
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1329
40685
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1330 class Urllib2FileobjectTest(unittest.TestCase):
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1331
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1332 # urllib2.HTTPHandler has "borrowed" socket._fileobject, and requires that
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1333 # it close the socket if the close c'tor argument is true
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1334
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1335 def testClose(self):
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1336 class MockSocket:
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1337 closed = False
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1338 def flush(self): pass
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1339 def close(self): self.closed = True
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1340
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1341 # must not close unless we request it: the original use of _fileobject
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1342 # by module socket requires that the underlying socket not be closed until
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1343 # the _socketobject that created the _fileobject is closed
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1344 s = MockSocket()
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1345 f = socket._fileobject(s)
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1346 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1347 self.assertTrue(not s.closed)
40685
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1348
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1349 s = MockSocket()
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1350 f = socket._fileobject(s, close=True)
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1351 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1352 self.assertTrue(s.closed)
40685
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1353
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1354 class TCPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1355
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1356 def testTCPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1357 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1358 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1359 self.serv.accept()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1360 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1361 "Error generating a timeout exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1362
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1363 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1364 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1365 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1366 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1367 foo = self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1368 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1369 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
1370 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1371 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1372 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1373 self.fail("caught unexpected exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1374 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1375 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
1376
39354
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1377 def testInterruptedTimeout(self):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1378 # 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
1379 # 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
1380 # 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
1381 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
1382 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
1383 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
1384 class Alarm(Exception):
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1385 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1386 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
1387 raise Alarm
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1388 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
1389 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1390 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
1391 try:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1392 foo = self.serv.accept()
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1393 except socket.timeout:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1394 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
1395 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1396 pass
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1397 except:
46148
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1398 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
1399 " %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
1400 (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
1401 else:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1402 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
1403 finally:
59d082591f56 Print more information the next time test_socket throws the wrong exception.
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44770
diff changeset
1404 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
1405 except Alarm:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1406 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
1407 finally:
85fdcdfca85c Patch #1519025 and bug #926423: If a KeyboardInterrupt occurs during
Neal Norwitz <nnorwitz@gmail.com>
parents: 38976
diff changeset
1408 # 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
1409 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
1410
73611
0fdf7f7c353d #11112: Fix typo in a base class in test_socket.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73405
diff changeset
1411 class UDPTimeoutTest(SocketUDPTest):
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1412
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1413 def testUDPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1414 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1415 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1416 self.serv.recv(1024)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1417 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1418 "Error generating a timeout exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1419
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1420 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1421 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1422 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1423 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1424 foo = self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1425 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1426 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
1427 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1428 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1429 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1430 self.fail("caught unexpected exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1431 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1432 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
1433
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1434 class TestExceptions(unittest.TestCase):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1435
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1436 def testExceptionTree(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1437 self.assertTrue(issubclass(socket.error, Exception))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1438 self.assertTrue(issubclass(socket.herror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1439 self.assertTrue(issubclass(socket.gaierror, socket.error))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 51538
diff changeset
1440 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
1441
37853
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1442 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
1443
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1444 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
1445
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1446 def testLinuxAbstractNamespace(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1447 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
1448 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
1449 s1.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1450 s1.listen(1)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1451 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
1452 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
1453 s1.accept()
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1454 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
1455 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
1456
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1457 def testMaxName(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1458 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
1459 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
1460 s.bind(address)
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1461 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
1462
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1463 def testNameOverflow(self):
29257ef6abeb SF Patch #1062014: AF_UNIX sockets under Linux have a special
Armin Rigo <arigo@tunes.org>
parents: 37422
diff changeset
1464 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
1465 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
1466 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
1467
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1468
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
1469 @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
1470 class BufferIOTest(SocketConnectedTest):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1471 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1472 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
1473 """
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1474 def __init__(self, methodName='runTest'):
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1475 SocketConnectedTest.__init__(self, methodName=methodName)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1476
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1477 def testRecvIntoArray(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1478 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1479 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
1480 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1481 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1482 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1483
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1484 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
1485 with test_support.check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61875
diff changeset
1486 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1487 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1488
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1489 def testRecvIntoBytearray(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1490 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1491 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
1492 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
1493 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
1494 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
1495
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1496 _testRecvIntoBytearray = _testRecvIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1497
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1498 def testRecvIntoMemoryview(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1499 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1500 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
1501 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
1502 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
1503 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
1504
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1505 _testRecvIntoMemoryview = _testRecvIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1506
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1507 def testRecvFromIntoArray(self):
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1508 buf = array.array('c', ' '*1024)
38580
a114003aa3d3 Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents: 38353
diff changeset
1509 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
1510 self.assertEqual(nbytes, len(MSG))
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1511 msg = buf.tostring()[:len(MSG)]
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1512 self.assertEqual(msg, MSG)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1513
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1514 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
1515 with test_support.check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61875
diff changeset
1516 buf = buffer(MSG)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1517 self.serv_conn.send(buf)
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 37853
diff changeset
1518
59676
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1519 def testRecvFromIntoBytearray(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1520 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1521 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
1522 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
1523 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
1524 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
1525
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1526 _testRecvFromIntoBytearray = _testRecvFromIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1527
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1528 def testRecvFromIntoMemoryview(self):
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1529 buf = bytearray(1024)
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1530 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
1531 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
1532 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
1533 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
1534
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1535 _testRecvFromIntoMemoryview = _testRecvFromIntoArray
435fa215d33f Issue #8104: socket.recv_into() and socket.recvfrom_into() now support
Antoine Pitrou <solipsis@pitrou.net>
parents: 58460
diff changeset
1536
44561
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1537
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1538 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
1539 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
1540 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
1541
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1542 def isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1543 """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
1544
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1545 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
1546 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
1547 """
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1548 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
1549 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1550 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
1551 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1552 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
1553 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
1554 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
1555 return True
44592
cb22f8748da7 It's verbose, not debug
Christian Heimes <christian@cheimes.de>
parents: 44581
diff changeset
1556 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
1557 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
1558 return False
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1559
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1560 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
1561 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
1562 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
1563 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
1564
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1565 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
1566 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
1567 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
1568 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
1569
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1570 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
1571 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
1572 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
1573
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1574 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
1575
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1576 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
1577 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
1578
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1579
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1580 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
1581 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
1582 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
1583 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
1584
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1585 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
1586 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
1587 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
1588 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
1589 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
1590 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
1591 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
1592 self.serverExplicitReady()
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1593 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
1594
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1595 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
1596 # 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
1597 # 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
1598 # 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
1599 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
1600 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
1601 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
1602 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
1603 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
1604 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
1605
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1606 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
1607 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
1608 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
1609 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
1610
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1611 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
1612 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
1613 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
1614
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1615
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1616 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
1617 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
61258
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61046
diff changeset
1618 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest,
f6785bce54b5 In a number of places code still revers
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61046
diff changeset
1619 UDPTimeoutTest ]
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1620
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1621 tests.extend([
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1622 NonBlockingTCPTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1623 FileObjectClassTestCase,
61875
dca3cb19dbdd Merged revisions 74426 via svnmerge from
Jean-Paul Calderone <exarkun@divmod.com>
parents: 57037
diff changeset
1624 FileObjectInterruptedTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1625 UnbufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1626 LineBufferedFileObjectClassTestCase,
40685
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1627 SmallBufferedFileObjectClassTestCase,
573a46176175 Patch #1627441: close sockets properly in urllib2.
Georg Brandl <georg@python.org>
parents: 39354
diff changeset
1628 Urllib2FileobjectTest,
41300
8258f25d43d7 Redone the tests, using the infrastructure already present
Facundo Batista <facundobatista@gmail.com>
parents: 41292
diff changeset
1629 NetworkConnectionNoServer,
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1630 NetworkConnectionAttributesTest,
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40684
diff changeset
1631 NetworkConnectionBehaviourTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1632 ])
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1633 if hasattr(socket, "socketpair"):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1634 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
1635 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
1636 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
1637 if isTipcAvailable():
ac51cfe5c5c6 Issue #1646: Make socket support TIPC. The socket module now has support
Christian Heimes <christian@cheimes.de>
parents: 44478
diff changeset
1638 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
1639 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
1640
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1641 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
1642 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
1643 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
1644
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1645 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
1646 test_main()