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
|
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
|
|
|
|
2002-12-03 23:26:57 -04:00
|
|
|
f = urllib.urlopen('https://sf.net')
|
|
|
|
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')
|
|
|
|
|
|
|
|
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:
|
|
|
|
print >> sys.stderr, """\
|
|
|
|
WARNING: an attempt to connect to %r timed out, 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,)
|
|
|
|
return
|
|
|
|
|
|
|
|
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.
|
2003-06-30 00:25:20 -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()
|
2003-06-30 00:25:20 -03:00
|
|
|
s.bind(('', PORT))
|
|
|
|
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()
|
2003-07-01 11:49:32 -03:00
|
|
|
s.connect(('localhost', PORT))
|
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()
|