Skip to content

Commit cc640ea

Browse files
committed
more ssl load_verify_locations tests
1 parent 58847bf commit cc640ea

File tree

1 file changed

+50
-15
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests

1 file changed

+50
-15
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_ssl.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CertTests(unittest.TestCase):
5757

5858
def check_load_cert_chain_error(self, certfile, keyfile=None, errno=-1, strerror=None, err=ssl.SSLError):
5959
try:
60-
if(keyfile is None):
60+
if keyfile is None:
6161
self.ctx.load_cert_chain(data_file(certfile))
6262
else:
6363
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
7070
else:
7171
assert False
7272

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):
7474
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)
7982
except err as e:
8083
if errno != -1:
8184
self.assertEqual(e.errno, errno)
8285
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))
84108
self.assertIsInstance(type(e), type(err))
85109
else:
86110
assert False
@@ -127,6 +151,10 @@ def test_load_cert_chain(self):
127151
def test_load_verify_locations(self):
128152
self.ctx.load_verify_locations(data_file("cert_rsa.pem"))
129153
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)
130158
self.ctx.load_verify_locations(data_file("cert_rsa.pem"), 'does_not_exit')
131159
self.ctx.load_verify_locations(StringWrapper(data_file("cert_rsa.pem")), )
132160
self.ctx.load_verify_locations(capath=StringWrapper(data_file("cert_rsa.pem")))
@@ -156,9 +184,16 @@ def test_load_verify_locations(self):
156184
self.check_load_verify_locations_error(cafile="broken_cert_data.pem", errno=9, strerror="[X509] PEM lib")
157185
self.check_load_verify_locations_error(cafile="broken_cert_data_at_begin.pem", errno=9, strerror="[X509] PEM lib")
158186
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]")
162197

163198
def test_load_default_verify_paths(self):
164199
env = os.environ
@@ -198,7 +233,7 @@ def test_verify_error(self):
198233
s_out = ssl.MemoryBIO()
199234
client = context.wrap_bio(c_in, c_out, server_hostname=hostname)
200235
server = server_context.wrap_bio(s_in, s_out, server_side=True)
201-
236+
202237
try:
203238
for _ in range(5):
204239
try:
@@ -214,11 +249,11 @@ def test_verify_error(self):
214249
if s_out.pending:
215250
c_in.write(s_out.read())
216251
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)
219254
else:
220-
assert False
221-
255+
assert False
256+
222257
def get_cipher_list(cipher_string):
223258
context = ssl.SSLContext()
224259
context.set_ciphers(cipher_string)

0 commit comments

Comments
 (0)