2001-08-20 19:39:42 -03:00
|
|
|
# Test just the SSL support in the socket module, in a moderately bogus way.
|
|
|
|
|
2006-03-31 15:09:56 -04:00
|
|
|
import sys
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-12-03 23:26:57 -04:00
|
|
|
import socket
|
2006-06-20 14:30:26 -03:00
|
|
|
import errno
|
2001-08-20 19:39:42 -03:00
|
|
|
|
2002-12-03 23:26:57 -04:00
|
|
|
# Optionally test SSL support. This requires the 'network' resource as given
|
|
|
|
# on the regrtest command line.
|
|
|
|
skip_expected = not (test_support.is_resource_enabled('network') and
|
|
|
|
hasattr(socket, "ssl"))
|
2001-08-20 19:39:42 -03:00
|
|
|
|
2003-06-30 00:25:20 -03:00
|
|
|
def test_basic():
|
2002-12-03 23:26:57 -04:00
|
|
|
test_support.requires('network')
|
|
|
|
|
|
|
|
import urllib
|
2001-09-06 06:54:47 -03:00
|
|
|
|
2006-04-08 09:05:15 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
print "test_basic ..."
|
|
|
|
|
2002-12-03 23:26:57 -04:00
|
|
|
socket.RAND_status()
|
|
|
|
try:
|
|
|
|
socket.RAND_egd(1)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print "didn't raise TypeError"
|
|
|
|
socket.RAND_add("this is a random string", 75.0)
|
2001-08-20 19:39:42 -03:00
|
|
|
|
2007-03-09 00:57:00 -04:00
|
|
|
try:
|
|
|
|
f = urllib.urlopen('https://sf.net')
|
|
|
|
except IOError, exc:
|
|
|
|
if exc.errno == errno.ETIMEDOUT:
|
|
|
|
raise test_support.ResourceDenied('HTTPS connection is timing out')
|
|
|
|
else:
|
|
|
|
raise
|
2002-12-03 23:26:57 -04:00
|
|
|
buf = f.read()
|
|
|
|
f.close()
|
2001-10-17 21:30:14 -03:00
|
|
|
|
2006-04-08 09:05:15 -03:00
|
|
|
def test_timeout():
|
|
|
|
test_support.requires('network')
|
|
|
|
|
2006-06-29 02:48:14 -03:00
|
|
|
def error_msg(extra_msg):
|
|
|
|
print >> sys.stderr, """\
|
|
|
|
WARNING: an attempt to connect to %r %s, in
|
|
|
|
test_timeout. That may be legitimate, but is not the outcome we hoped
|
|
|
|
for. If this message is seen often, test_timeout should be changed to
|
|
|
|
use a more reliable address.""" % (ADDR, extra_msg)
|
|
|
|
|
2006-04-08 09:05:15 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
print "test_timeout ..."
|
|
|
|
|
|
|
|
# A service which issues a welcome banner (without need to write
|
|
|
|
# anything).
|
|
|
|
# XXX ("gmail.org", 995) has been unreliable so far, from time to time
|
|
|
|
# XXX non-responsive for hours on end (& across all buildbot slaves,
|
|
|
|
# XXX so that's not just a local thing).
|
|
|
|
ADDR = "gmail.org", 995
|
|
|
|
|
|
|
|
s = socket.socket()
|
|
|
|
s.settimeout(30.0)
|
|
|
|
try:
|
|
|
|
s.connect(ADDR)
|
|
|
|
except socket.timeout:
|
2006-06-29 02:48:14 -03:00
|
|
|
error_msg('timed out')
|
2006-04-08 09:05:15 -03:00
|
|
|
return
|
2006-06-20 16:20:17 -03:00
|
|
|
except socket.error, exc: # In case connection is refused.
|
2006-06-21 13:57:57 -03:00
|
|
|
if exc.args[0] == errno.ECONNREFUSED:
|
2006-06-29 02:48:14 -03:00
|
|
|
error_msg('was refused')
|
2006-06-21 13:57:57 -03:00
|
|
|
return
|
2006-06-20 14:30:26 -03:00
|
|
|
else:
|
2006-06-21 13:57:57 -03:00
|
|
|
raise
|
2006-04-08 09:05:15 -03:00
|
|
|
|
|
|
|
ss = socket.ssl(s)
|
|
|
|
# Read part of return welcome banner twice.
|
|
|
|
ss.read(1)
|
|
|
|
ss.read(1)
|
|
|
|
s.close()
|
2006-03-31 21:28:51 -04:00
|
|
|
|
2003-06-30 00:25:20 -03:00
|
|
|
def test_rude_shutdown():
|
2006-04-08 09:05:15 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
print "test_rude_shutdown ..."
|
|
|
|
|
2003-06-30 00:25:20 -03:00
|
|
|
try:
|
2006-01-24 18:44:54 -04:00
|
|
|
import threading
|
2003-06-30 00:25:20 -03:00
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
2006-01-24 18:44:54 -04:00
|
|
|
# Some random port to connect to.
|
2006-06-11 23:13:21 -03:00
|
|
|
PORT = [9934]
|
2006-01-24 18:44:54 -04:00
|
|
|
|
2006-01-25 04:39:35 -04:00
|
|
|
listener_ready = threading.Event()
|
2006-01-24 18:44:54 -04:00
|
|
|
listener_gone = threading.Event()
|
|
|
|
|
|
|
|
# `listener` runs in a thread. It opens a socket listening on PORT, and
|
|
|
|
# sits in an accept() until the main thread connects. Then it rudely
|
|
|
|
# closes the socket, and sets Event `listener_gone` to let the main thread
|
|
|
|
# know the socket is gone.
|
2003-06-30 00:25:20 -03:00
|
|
|
def listener():
|
2006-01-24 18:44:54 -04:00
|
|
|
s = socket.socket()
|
2006-06-11 23:13:21 -03:00
|
|
|
PORT[0] = test_support.bind_port(s, '', PORT[0])
|
2003-06-30 00:25:20 -03:00
|
|
|
s.listen(5)
|
2006-01-25 04:39:35 -04:00
|
|
|
listener_ready.set()
|
2003-06-30 00:25:20 -03:00
|
|
|
s.accept()
|
2006-01-24 18:44:54 -04:00
|
|
|
s = None # reclaim the socket object, which also closes it
|
|
|
|
listener_gone.set()
|
2003-06-30 00:25:20 -03:00
|
|
|
|
|
|
|
def connector():
|
2006-01-25 04:39:35 -04:00
|
|
|
listener_ready.wait()
|
2006-01-24 18:44:54 -04:00
|
|
|
s = socket.socket()
|
2006-06-11 23:13:21 -03:00
|
|
|
s.connect(('localhost', PORT[0]))
|
2006-01-24 18:44:54 -04:00
|
|
|
listener_gone.wait()
|
2003-06-30 00:25:20 -03:00
|
|
|
try:
|
|
|
|
ssl_sock = socket.ssl(s)
|
|
|
|
except socket.sslerror:
|
|
|
|
pass
|
|
|
|
else:
|
2006-01-24 18:44:54 -04:00
|
|
|
raise test_support.TestFailed(
|
|
|
|
'connecting to closed SSL socket should have failed')
|
2003-06-30 00:25:20 -03:00
|
|
|
|
2006-01-24 18:44:54 -04:00
|
|
|
t = threading.Thread(target=listener)
|
|
|
|
t.start()
|
2003-06-30 00:25:20 -03:00
|
|
|
connector()
|
2006-01-24 18:44:54 -04:00
|
|
|
t.join()
|
2003-06-30 00:25:20 -03:00
|
|
|
|
|
|
|
def test_main():
|
2003-07-01 10:44:28 -03:00
|
|
|
if not hasattr(socket, "ssl"):
|
|
|
|
raise test_support.TestSkipped("socket module has no ssl support")
|
2003-06-30 00:25:20 -03:00
|
|
|
test_rude_shutdown()
|
|
|
|
test_basic()
|
2006-03-31 14:01:16 -04:00
|
|
|
test_timeout()
|
2003-06-30 00:25:20 -03:00
|
|
|
|
2002-12-03 23:26:57 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|