2001-08-20 19:39:42 -03:00
|
|
|
# Test just the SSL support in the socket module, in a moderately bogus way.
|
|
|
|
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2002-12-03 23:26:57 -04:00
|
|
|
import socket
|
2003-06-30 00:25:20 -03:00
|
|
|
import time
|
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')
|
|
|
|
if not hasattr(socket, "ssl"):
|
|
|
|
raise test_support.TestSkipped("socket module has no ssl support")
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-06-30 00:25:20 -03:00
|
|
|
def test_rude_shutdown():
|
|
|
|
try:
|
|
|
|
import thread
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# some random port to connect to
|
|
|
|
PORT = 9934
|
|
|
|
def listener():
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.bind(('', PORT))
|
|
|
|
s.listen(5)
|
|
|
|
s.accept()
|
|
|
|
del s
|
|
|
|
thread.exit()
|
|
|
|
|
|
|
|
def connector():
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.connect(('', PORT))
|
|
|
|
try:
|
|
|
|
ssl_sock = socket.ssl(s)
|
|
|
|
except socket.sslerror:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise test_support.TestFailed, \
|
|
|
|
'connecting to closed SSL socket failed'
|
|
|
|
|
|
|
|
thread.start_new_thread(listener, ())
|
|
|
|
time.sleep(1)
|
|
|
|
connector()
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_rude_shutdown()
|
|
|
|
test_basic()
|
|
|
|
|
2002-12-03 23:26:57 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|