1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
import errno
import os
import socket
import ssl
import threading
import time
import unittest
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
from test import test_support
def data_file(*name):
return os.path.join(os.path.dirname(__file__), *name)
CERTFILE = data_file("keycert.pem")
ONLYCERT = data_file("ssl_cert.pem")
ONLYKEY = data_file("ssl_key.pem")
def start_server():
server_address = ('127.0.0.1', 0)
class DaemonThreadingMixIn(ThreadingMixIn):
daemon_threads = True
class ThreadedHTTPServer(DaemonThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
# not actually going to do anything with this server, so a
# do-nothing handler is reasonable
httpd = ThreadedHTTPServer(server_address, BaseHTTPRequestHandler)
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
return httpd, server_thread
class SocketConnectTest(unittest.TestCase):
def setUp(self):
self.httpd, self.server_thread = start_server()
self.address = self.httpd.server_name, self.httpd.server_port
def tearDown(self):
self.httpd.shutdown()
self.server_thread.join()
def do_nonblocking_connection(self, results, index):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
connect_errno = 0
connect_attempt = 0
while connect_errno != errno.EISCONN and connect_attempt < 10000:
connect_attempt += 1
connect_errno = sock.connect_ex(self.address)
results[index].append(connect_errno)
time.sleep(0.01)
sock.close()
def do_workout(self, num_threads=10):
connect_results = []
connect_threads = []
for i in xrange(num_threads):
connect_results.append([])
connect_threads.append(threading.Thread(
target=self.do_nonblocking_connection,
name="socket-workout-%s" % i,
args=(connect_results, i)))
for thread in connect_threads:
thread.start()
for thread in connect_threads:
thread.join()
return connect_results
def test_connect_ex_workout(self):
"""Verify connect_ex states go through EINPROGRESS?, EALREADY*, EISCONN"""
# Tests fix for http://bugs.jython.org/issue2428; based in part on the
# code showing failure that was submitted with that bug
for result in self.do_workout():
if len(result) == 0: self.fail("A socket-workout thread failed to run")
self.assertIn(result[0], {errno.EINPROGRESS, errno.EISCONN})
self.assertEqual(result[-1], errno.EISCONN)
for code in result[1:-1]:
self.assertEqual(code, errno.EALREADY)
class SSLSocketConnectTest(unittest.TestCase):
def setUp(self):
self.httpd, self.server_thread = start_server()
self.httpd.socket = ssl.wrap_socket(
self.httpd.socket,
certfile=ONLYCERT,
server_side=True,
keyfile=ONLYKEY,
)
self.address = self.httpd.server_name, self.httpd.server_port
def tearDown(self):
self.httpd.shutdown()
self.server_thread.join()
def do_nonblocking_connection(self, results, index):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
connect_errno = 0
connect_attempt = 0
sock = ssl.wrap_socket(sock, certfile=CERTFILE, do_handshake_on_connect=True)
while connect_errno != errno.EISCONN and connect_attempt < 10000:
connect_attempt += 1
connect_errno = sock.connect_ex(self.address)
results[index].append(connect_errno)
time.sleep(0.01)
sock.close()
def do_workout(self, num_threads=10):
connect_results = []
connect_threads = []
for i in xrange(num_threads):
connect_results.append([])
connect_threads.append(threading.Thread(
target=self.do_nonblocking_connection,
name="socket-workout-%s" % i,
args=(connect_results, i)))
for thread in connect_threads:
thread.start()
for thread in connect_threads:
thread.join()
return connect_results
@unittest.skipIf(test_support.is_jython and test_support.get_java_version() >= (9,), # FIXME
"Fails on Java 9+. See b.j.o. issue #2710")
def test_connect_ex_workout(self):
"""Verify connect_ex states go through EINPROGRESS?, EALREADY*, EISCONN"""
# Tests fix for http://bugs.jython.org/issue2428; based in part on the
# code showing failure that was submitted with that bug
for result in self.do_workout():
if len(result) == 0: self.fail("A socket-workout thread failed to run")
self.assertIn(result[0], {errno.EINPROGRESS, errno.EISCONN})
self.assertEqual(result[-1], errno.EISCONN)
for code in result[1:-1]:
self.assertEqual(code, errno.EALREADY)
class SocketOptionsTest(unittest.TestCase):
def test_socket_options_defined(self):
# Basic existence test to verify trivial fix for
# http://bugs.jython.org/issue2436
self.assertEqual(socket.SOL_TCP, socket.IPPROTO_TCP)
def test_main():
test_support.run_unittest(SocketConnectTest, SSLSocketConnectTest, SocketOptionsTest)
if __name__ == "__main__":
test_main()
|