Skip to content

Commit ebcb17b

Browse files
committed
Test: clear requests sessions when updating expected ssl certs
to make sure that the test uses the intended certificate. (There's some indirect indication that the updated environment variable might not always have been used.) Signed-off-by: Sebastien Awwad <sebastien.awwad@gmail.com>
1 parent e8a1ab1 commit ebcb17b

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tests/test_download.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ def test_https_connection(self):
290290
# Trust the certfile that happens to use a different hostname than we
291291
# will expect.
292292
os.environ['REQUESTS_CA_BUNDLE'] = bad_cert_fname
293+
# Clear sessions to ensure that the certificate we just specified is used.
294+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
295+
tuf.download._sessions = {}
293296

294297
# Try connecting to the server process with the bad cert while trusting
295298
# the bad cert. Expect failure because even though we trust it, the
@@ -316,8 +319,12 @@ def test_https_connection(self):
316319
with self.assertRaises(requests.exceptions.SSLError):
317320
download.unsafe_download(good2_https_url, target_data_length)
318321

322+
319323
# Configure environment to now trust the certfile that is expired.
320324
os.environ['REQUESTS_CA_BUNDLE'] = expired_cert_fname
325+
# Clear sessions to ensure that the certificate we just specified is used.
326+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
327+
tuf.download._sessions = {}
321328

322329
# Try connecting to the server process with the expired cert while
323330
# trusting the expired cert. Expect failure because even though we trust
@@ -328,20 +335,23 @@ def test_https_connection(self):
328335
with self.assertRaises(requests.exceptions.SSLError):
329336
download.unsafe_download(expired_https_url, target_data_length)
330337

338+
331339
# Try connecting to the server processes with the good certs while
332340
# trusting the appropriate good certs. Expect success.
333-
# Note: running these OK downloads at the top of this try section causes
334-
# a failure in a previous assertion: retrieving the same good URL
335-
# again after no longer "trusting" the good certfile still succeeds
336-
# if we had previously succeeded in retrieving that same URL while
337-
# still trusting the good cert. Perhaps it's a caching issue....?
338-
# I'm not especially concerned yet, but take note for later....
341+
# TODO: expand testing to switch expected certificates back and forth a
342+
# bit more while clearing / not clearing sessions.
339343
os.environ['REQUESTS_CA_BUNDLE'] = good_cert_fname
344+
# Clear sessions to ensure that the certificate we just specified is used.
345+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
346+
tuf.download._sessions = {}
340347
logger.info('Trying HTTPS download of target file: ' + good_https_url)
341348
download.safe_download(good_https_url, target_data_length)
342349
download.unsafe_download(good_https_url, target_data_length)
343350

344351
os.environ['REQUESTS_CA_BUNDLE'] = good2_cert_fname
352+
# Clear sessions to ensure that the certificate we just specified is used.
353+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
354+
tuf.download._sessions = {}
345355
logger.info('Trying HTTPS download of target file: ' + good2_https_url)
346356
download.safe_download(good2_https_url, target_data_length)
347357
download.unsafe_download(good2_https_url, target_data_length)

tests/test_proxy_use.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ def test_https_dl_via_smart_http_proxy(self):
238238

239239
self.set_env_value('REQUESTS_CA_BUNDLE',
240240
os.path.join('ssl_certs', 'ssl_cert.crt'))
241+
# Clear sessions to ensure that the certificate we just specified is used.
242+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
243+
tuf.download._sessions = {}
241244

242245
logger.info('Trying HTTPS download via HTTP proxy: ' + self.url_https)
243246
download.safe_download(self.url_https, self.target_data_length)
@@ -259,6 +262,9 @@ def test_http_dl_via_https_proxy(self):
259262
# plain HTTP connection to the target server.
260263
self.set_env_value('REQUESTS_CA_BUNDLE',
261264
os.path.join('ssl_certs', 'proxy_ca.crt'))
265+
# Clear sessions to ensure that the certificate we just specified is used.
266+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
267+
tuf.download._sessions = {}
262268

263269
logger.info('Trying HTTP download via HTTPS proxy: ' + self.url_https)
264270
download.safe_download(self.url, self.target_data_length)
@@ -282,6 +288,9 @@ def test_https_dl_via_https_proxy(self):
282288
# started in setUpClass().
283289
self.set_env_value('REQUESTS_CA_BUNDLE',
284290
os.path.join('ssl_certs', 'proxy_ca.crt'))
291+
# Clear sessions to ensure that the certificate we just specified is used.
292+
# TODO: Confirm necessity of this session clearing and lay out mechanics.
293+
tuf.download._sessions = {}
285294

286295
logger.info('Trying HTTPS download via HTTPS proxy: ' + self.url_https)
287296
download.safe_download(self.url_https, self.target_data_length)
@@ -325,7 +334,7 @@ def restore_env_value(self, key):
325334
assert key in self.old_env_values, 'Test coding mistake: something is ' \
326335
'trying to restore environment variable ' + key + ', but that ' \
327336
'variable does not appear in the list of values to restore. ' \
328-
'Please make sure to use _set_env_value().'
337+
'Please make sure to use set_env_value().'
329338

330339
if self.old_env_values[key] is None:
331340
# If it was not previously set, try to unset it.

0 commit comments

Comments
 (0)