1 import unittest
2 import web
3 from web.wsgiserver import CherryPyWSGIServer
4 import threading
5 from clientjob.secure_connection import SecureConnection
6 from web.wsgiserver.ssl_pyopenssl import pyOpenSSLAdapter
7 import string
8 import random
9 import os
10 import sys
11 import time
12 import config
13 from ssl import SSLError
14
16 '''
17 This is a view for the test server which prints the argument given to it
18 '''
21
24 self.ssl_certificate = os.path.join('ssl', 'localhost.pem')
25 self.ssl_private_key = os.path.join('ssl', 'localhost.key')
26 self.client_CA = os.path.join('ssl', 'ca-cert.pem')
27
41
43 '''
44 Request a random url from the test serverand checks the response,
45 using the secure connection with a valid certificate signed by the CA.
46 '''
47 port = 8081
48 server = threading.Thread(target=self.launchServer, args=[port])
49 server.start()
50 time.sleep(1)
51 connection = SecureConnection("127.0.0.1", port)
52 random_string = ''.join(random.choice(string.letters) for i in xrange(40))
53 connection.request("GET", "/" + random_string)
54 response = connection.getresponse()
55 self.assertEqual(response.status, 200)
56 self.assertEqual(response.read(), random_string)
57 server = None
58
60 '''
61 Similar to testSecureConnection, but this time the client has an invalid
62 (self-signed) certificate and thus the server should reject the connection.
63 '''
64 port = 8082
65 server = threading.Thread(target=self.launchServer, args=[port])
66 server.start()
67 time.sleep(1)
68
69
70 connection = SecureConnection("127.0.0.1", port,
71 cert_file = os.path.join('ssl', 'self-signed-localhost.cert'))
72
73 random_string = ''.join(random.choice(string.letters) for i in xrange(40))
74
75
76
77
78 try:
79 connection.request("GET", "/" + random_string)
80 raise Exception
81 except SSLError:
82
83
84
85 server = None
86
88 '''
89 Similar to testInsecureConnection, but now it's the server the one using an
90 invalid (self-signed) certificate, and thus the client should reject the connection.
91 '''
92 port = 8083
93
94
95 self.ssl_certificate = os.path.join('ssl', 'self-signed-localhost.cert')
96
97 server = threading.Thread(target=self.launchServer, args=[port])
98 server.start()
99 time.sleep(1)
100 connection = SecureConnection("127.0.0.1", port)
101 random_string = ''.join(random.choice(string.letters) for i in xrange(40))
102 try:
103 connection.request("GET", "/" + random_string)
104 raise Exception
105 except SSLError:
106
107 server = None
108
113