@@ -57,7 +57,7 @@ class CertTests(unittest.TestCase):
57
57
58
58
def check_load_cert_chain_error (self , certfile , keyfile = None , errno = - 1 , strerror = None , err = ssl .SSLError ):
59
59
try :
60
- if ( keyfile is None ) :
60
+ if keyfile is None :
61
61
self .ctx .load_cert_chain (data_file (certfile ))
62
62
else :
63
63
self .ctx .load_cert_chain (data_file (certfile ), data_file (keyfile ))
@@ -70,17 +70,41 @@ def check_load_cert_chain_error(self, certfile, keyfile=None, errno=-1, strerror
70
70
else :
71
71
assert False
72
72
73
- def check_load_verify_locations_error (self , cafile , capath = None , errno = - 1 , strerror = None , err = ssl .SSLError ):
73
+ def check_load_verify_locations_error (self , cafile = None , capath = None , cadata = None , errno = - 1 , strerror = None , err = ssl .SSLError ):
74
74
try :
75
- if (capath is None ):
76
- self .ctx .load_verify_locations (data_file (cafile ))
77
- else :
78
- self .ctx .load_verify_locations (data_file (cafile ), data_file (capath ))
75
+ if capath is not None :
76
+ capath = data_file (capath )
77
+ if cafile is not None :
78
+ cafile = data_file (cafile )
79
+ if cadata is not None :
80
+ cadata = open (data_file (cadata )).read ()
81
+ self .ctx .load_verify_locations (cafile , capath , cadata )
79
82
except err as e :
80
83
if errno != - 1 :
81
84
self .assertEqual (e .errno , errno )
82
85
if strerror is not None :
83
- self .assertIn (strerror , e .strerror )
86
+ if isinstance (ssl .SSLError , err ):
87
+ self .assertIn (strerror , e .strerror )
88
+ else :
89
+ self .assertIn (strerror , str (e ))
90
+ self .assertIsInstance (type (e ), type (err ))
91
+ else :
92
+ assert False
93
+
94
+ def check_load_verify_locations_cadata_bytes_error (self , cadata , errno = - 1 , strerror = None , err = ssl .SSLError ):
95
+ try :
96
+
97
+ cadata = open (data_file (cadata )).read ()
98
+ cadata .replace ("" )
99
+ self .ctx .load_verify_locations (cafile , capath , cadata )
100
+ except err as e :
101
+ if errno != - 1 :
102
+ self .assertEqual (e .errno , errno )
103
+ if strerror is not None :
104
+ if isinstance (ssl .SSLError , err ):
105
+ self .assertIn (strerror , e .strerror )
106
+ else :
107
+ self .assertIn (strerror , str (e ))
84
108
self .assertIsInstance (type (e ), type (err ))
85
109
else :
86
110
assert False
@@ -127,6 +151,10 @@ def test_load_cert_chain(self):
127
151
def test_load_verify_locations (self ):
128
152
self .ctx .load_verify_locations (data_file ("cert_rsa.pem" ))
129
153
self .ctx .load_verify_locations (capath = data_file ("cert_rsa.pem" ))
154
+ cad = open (data_file ("cert_rsa.pem" )).read ()
155
+ self .ctx .load_verify_locations (cadata = cad )
156
+ cad = ssl .PEM_cert_to_DER_cert (cad )
157
+ self .ctx .load_verify_locations (cadata = cad )
130
158
self .ctx .load_verify_locations (data_file ("cert_rsa.pem" ), 'does_not_exit' )
131
159
self .ctx .load_verify_locations (StringWrapper (data_file ("cert_rsa.pem" )), )
132
160
self .ctx .load_verify_locations (capath = StringWrapper (data_file ("cert_rsa.pem" )))
@@ -156,9 +184,16 @@ def test_load_verify_locations(self):
156
184
self .check_load_verify_locations_error (cafile = "broken_cert_data.pem" , errno = 9 , strerror = "[X509] PEM lib" )
157
185
self .check_load_verify_locations_error (cafile = "broken_cert_data_at_begin.pem" , errno = 9 , strerror = "[X509] PEM lib" )
158
186
self .check_load_verify_locations_error (cafile = "broken_cert_data_at_end.pem" , errno = 9 , strerror = "[X509] PEM lib" )
159
-
160
- # TODO test cadata
161
- # TODO load_DH_params
187
+
188
+ self .check_load_verify_locations_error (cadata = "empty.pem" , strerror = "Empty certificate data" , err = ValueError )
189
+ self .check_load_verify_locations_error (cadata = "empty_cert.pem" )
190
+
191
+ self .check_load_verify_locations_error (cadata = "broken_cert_double_begin.pem" )
192
+ self .check_load_verify_locations_error (cadata = "broken_cert_only_begin.pem" )
193
+ self .check_load_verify_locations_error (cadata = "broken_cert_no_end.pem" )
194
+ self .check_load_verify_locations_error (cadata = "broken_cert_data.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
195
+ self .check_load_verify_locations_error (cadata = "broken_cert_data_at_begin.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
196
+ self .check_load_verify_locations_error (cadata = "broken_cert_data_at_end.pem" , errno = 100 , strerror = "[PEM: BAD_BASE64_DECODE]" )
162
197
163
198
def test_load_default_verify_paths (self ):
164
199
env = os .environ
@@ -198,7 +233,7 @@ def test_verify_error(self):
198
233
s_out = ssl .MemoryBIO ()
199
234
client = context .wrap_bio (c_in , c_out , server_hostname = hostname )
200
235
server = server_context .wrap_bio (s_in , s_out , server_side = True )
201
-
236
+
202
237
try :
203
238
for _ in range (5 ):
204
239
try :
@@ -214,11 +249,11 @@ def test_verify_error(self):
214
249
if s_out .pending :
215
250
c_in .write (s_out .read ())
216
251
except ssl .SSLCertVerificationError as e :
217
- self .assertIsNotNone (e .verify_code )
218
- self .assertIsNotNone (e .verify_message )
252
+ self .assertIsNotNone (e .verify_code )
253
+ self .assertIsNotNone (e .verify_message )
219
254
else :
220
- assert False
221
-
255
+ assert False
256
+
222
257
def get_cipher_list (cipher_string ):
223
258
context = ssl .SSLContext ()
224
259
context .set_ciphers (cipher_string )
0 commit comments