annotate Lib/test/test_socket.py @ 82251:bb5a8564e186 2.7

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