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
|
|
|
|
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-03-31 21:28:51 -04:00
|
|
|
# XXX Tim disabled this test on all platforms, for now, since the
|
|
|
|
# XXX s.connect(("gmail.org", 995))
|
|
|
|
# XXX line starting timing out on all the builbot slaves.
|
|
|
|
if 0: not sys.platform.startswith('win'):
|
2006-03-31 15:09:56 -04:00
|
|
|
def test_timeout():
|
|
|
|
test_support.requires('network')
|
|
|
|
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.settimeout(30.0)
|
2006-03-31 21:28:51 -04:00
|
|
|
# connect to service which issues an welcome banner (without need to
|
|
|
|
# write anything)
|
2006-03-31 15:09:56 -04:00
|
|
|
s.connect(("gmail.org", 995))
|
|
|
|
ss = socket.ssl(s)
|
2006-03-31 21:28:51 -04:00
|
|
|
# read part of return welcome banner twice
|
2006-03-31 15:09:56 -04:00
|
|
|
ss.read(1)
|
|
|
|
ss.read(1)
|
|
|
|
s.close()
|
2006-03-31 17:12:32 -04:00
|
|
|
else:
|
|
|
|
def test_timeout():
|
|
|
|
pass
|
2006-03-31 21:28:51 -04:00
|
|
|
|
2003-06-30 00:25:20 -03:00
|
|
|
def test_rude_shutdown():
|
|
|
|
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()
|