summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socket_ssl.py
diff options
context:
space:
mode:
authorFacundo Batista <facundobatista@gmail.com>2007-04-03 17:29:48 (GMT)
committerFacundo Batista <facundobatista@gmail.com>2007-04-03 17:29:48 (GMT)
commitc4a106733cd464ec2d2746fd3e868f09734eeb10 (patch)
treea7b8a95000a6d56f5c4fb6b2d9ce53adebba113f /Lib/test/test_socket_ssl.py
parente1a9b42c38c8466775f02a2659cda8e6aa6ca9e5 (diff)
downloadcpython-c4a106733cd464ec2d2746fd3e868f09734eeb10.zip
cpython-c4a106733cd464ec2d2746fd3e868f09734eeb10.tar.gz
cpython-c4a106733cd464ec2d2746fd3e868f09734eeb10.tar.bz2
Added a SSL server to test_socket_ssl.py to be able to test
locally. Now, it checks if have openssl available and run those specific tests (it starts openssl at the beggining of all the tests and then kills it at the end).
Diffstat (limited to 'Lib/test/test_socket_ssl.py')
-rw-r--r--Lib/test/test_socket_ssl.py91
1 files changed, 80 insertions, 11 deletions
diff --git a/Lib/test/test_socket_ssl.py b/Lib/test/test_socket_ssl.py
index 4c502d1..86b1943 100644
--- a/Lib/test/test_socket_ssl.py
+++ b/Lib/test/test_socket_ssl.py
@@ -8,6 +8,9 @@ import errno
import threading
import subprocess
import time
+import ctypes
+import os
+import urllib
# Optionally test SSL support, if we have it in the tested platform
skip_expected = not hasattr(socket, "ssl")
@@ -15,11 +18,6 @@ skip_expected = not hasattr(socket, "ssl")
class ConnectedTests(unittest.TestCase):
def testBasic(self):
- import urllib
-
- if test_support.verbose:
- print "test_basic ..."
-
socket.RAND_status()
try:
socket.RAND_egd(1)
@@ -42,9 +40,6 @@ class ConnectedTests(unittest.TestCase):
hoped for. If this message is seen often, test_timeout should be
changed to use a more reliable address.""" % (ADDR, extra_msg)
- 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
@@ -75,9 +70,6 @@ class ConnectedTests(unittest.TestCase):
class BasicTests(unittest.TestCase):
def testRudeShutdown(self):
- if test_support.verbose:
- print "test_rude_shutdown ..."
-
# Some random port to connect to.
PORT = [9934]
@@ -115,7 +107,68 @@ class BasicTests(unittest.TestCase):
connector()
t.join()
+class OpenSSLTests(unittest.TestCase):
+
+ def testBasic(self):
+ time.sleep(.2)
+ s = socket.socket()
+ s.connect(("localhost", 4433))
+ ss = socket.ssl(s)
+ ss.write("Foo\n")
+ i = ss.read(4)
+ self.assertEqual(i, "Foo\n")
+
+
+def haveOpenSSL():
+ try:
+ s = subprocess.Popen("openssl rand 1".split(), stdout=subprocess.PIPE)
+ s.stdout.read(1)
+ except OSError, err:
+ if err.errno == 2:
+ return False
+ raise
+ return True
+
+class OpenSSLServer(threading.Thread):
+ def __init__(self):
+ self.s = None
+ self.keepServing = True
+ threading.Thread.__init__(self)
+ def run(self):
+ if os.access("ssl_cert.pem", os.F_OK):
+ cert_file = "ssl_cert.pem"
+ elif os.access("./Lib/test/ssl_cert.pem", os.F_OK):
+ cert_file = "./Lib/test/ssl_cert.pem"
+ else:
+ raise ValueError("No cert file found!")
+ if os.access("ssl_key.pem", os.F_OK):
+ key_file = "ssl_key.pem"
+ elif os.access("./Lib/test/ssl_key.pem", os.F_OK):
+ key_file = "./Lib/test/ssl_key.pem"
+ else:
+ raise ValueError("No cert file found!")
+
+ cmd = "openssl s_server -cert %s -key %s -quiet" % (cert_file, key_file)
+ self.s = subprocess.Popen(cmd.split(), stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ while self.keepServing:
+ time.sleep(.5)
+ l = self.s.stdout.readline()
+ self.s.stdin.write(l)
+
+ def shutdown(self):
+ self.keepServing = False
+ if not self.s:
+ return
+ if sys.platform == "win32":
+ handle = ctypes.windll.kernel32.OpenProcess(1, False, self.s.pid)
+ ctypes.windll.kernel32.TerminateProcess(handle, -1)
+ ctypes.windll.kernel32.CloseHandle(handle)
+ else:
+ os.kill(self.s.pid, 15)
+
def test_main():
if not hasattr(socket, "ssl"):
raise test_support.TestSkipped("socket module has no ssl support")
@@ -125,9 +178,25 @@ def test_main():
if test_support.is_resource_enabled('network'):
tests.append(ConnectedTests)
+ # in these platforms we can kill the openssl process
+ if sys.platform in ("sunos5", "darwin", "linux1",
+ "linux2", "win32", "hp-ux11"):
+ if haveOpenSSL():
+ haveServer = True
+ tests.append(OpenSSLTests)
+ else:
+ haveServer = False
+
+ if haveServer:
+ server = OpenSSLServer()
+ server.start()
+
thread_info = test_support.threading_setup()
test_support.run_unittest(*tests)
test_support.threading_cleanup(*thread_info)
+ if haveServer:
+ server.shutdown()
+
if __name__ == "__main__":
test_main()