Skip to content

Commit e82fd14

Browse files
committed
Update test_download
Replace tuf.download with a tuf.download.FileDownload() object. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent cdf7f73 commit e82fd14

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

tests/test_download.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import warnings
4343

4444
import tuf
45-
import tuf.download as download
45+
import tuf.download
4646
import tuf.log
4747
import tuf.unittest_toolbox as unittest_toolbox
4848
import tuf.exceptions
@@ -85,6 +85,9 @@ def setUp(self):
8585
digest = m.hexdigest()
8686
self.target_hash = {'md5':digest}
8787

88+
# Create a 'FileDownload' object with the default 'RequestsFetcher'
89+
self.download = tuf.download.FileDownload()
90+
8891

8992
# Stop server process and perform clean up.
9093
def tearDown(self):
@@ -99,7 +102,7 @@ def tearDown(self):
99102
# Test: Normal case.
100103
def test_download_url_to_tempfileobj(self):
101104

102-
download_file = download.safe_download
105+
download_file = self.download.safe_download
103106
with download_file(self.url, self.target_data_length) as temp_fileobj:
104107
temp_fileobj.seek(0)
105108
temp_file_data = temp_fileobj.read().decode('utf-8')
@@ -118,18 +121,18 @@ def test_download_url_to_tempfileobj_and_lengths(self):
118121
# the server-reported length of the file does not match the
119122
# required_length. 'updater.py' *does* verify the hashes of downloaded
120123
# content.
121-
download.safe_download(self.url, self.target_data_length - 4).close()
122-
download.unsafe_download(self.url, self.target_data_length - 4).close()
124+
self.download.safe_download(self.url, self.target_data_length - 4).close()
125+
self.download.unsafe_download(self.url, self.target_data_length - 4).close()
123126

124127
# We catch 'tuf.exceptions.DownloadLengthMismatchError' for safe_download()
125128
# because it will not download more bytes than requested (in this case, a
126129
# length greater than the size of the target file).
127130
self.assertRaises(tuf.exceptions.DownloadLengthMismatchError,
128-
download.safe_download, self.url, self.target_data_length + 1)
131+
self.download.safe_download, self.url, self.target_data_length + 1)
129132

130133
# Calling unsafe_download() with a mismatched length should not raise an
131134
# exception.
132-
download.unsafe_download(self.url, self.target_data_length + 1).close()
135+
self.download.unsafe_download(self.url, self.target_data_length + 1).close()
133136

134137

135138

@@ -161,16 +164,12 @@ def test_download_url_to_tempfileobj_and_performance(self):
161164
# Test: Incorrect/Unreachable URLs.
162165
def test_download_url_to_tempfileobj_and_urls(self):
163166

164-
download_file = download.safe_download
165-
unsafe_download_file = download.unsafe_download
167+
download_file = self.download.safe_download
168+
unsafe_download_file = self.download.unsafe_download
166169

167170
self.assertRaises(securesystemslib.exceptions.FormatError,
168171
download_file, None, self.target_data_length)
169172

170-
self.assertRaises(tuf.exceptions.URLParsingError,
171-
download_file,
172-
self.random_string(), self.target_data_length)
173-
174173
url = 'http://localhost:' \
175174
+ str(self.server_process_handler.port) + '/' + self.random_string()
176175
self.assertRaises(requests.exceptions.HTTPError,
@@ -213,10 +212,10 @@ def test_https_validation(self):
213212
'https://wrong.host.badssl.com/', ]: # hostname verification fail
214213
215214
with self.assertRaises(requests.exceptions.SSLError):
216-
download.safe_download(bad_url, irrelevant_length)
215+
self.download.safe_download(bad_url, irrelevant_length)
217216
218217
with self.assertRaises(requests.exceptions.SSLError):
219-
download.unsafe_download(bad_url, irrelevant_length)
218+
self.download.unsafe_download(bad_url, irrelevant_length)
220219
'''
221220

222221

@@ -302,25 +301,25 @@ def test_https_connection(self):
302301
category=urllib3.exceptions.SubjectAltNameWarning)
303302

304303
with self.assertRaises(requests.exceptions.SSLError):
305-
download.safe_download(bad_https_url, target_data_length)
304+
self.download.safe_download(bad_https_url, target_data_length)
306305
with self.assertRaises(requests.exceptions.SSLError):
307-
download.unsafe_download(bad_https_url, target_data_length)
306+
self.download.unsafe_download(bad_https_url, target_data_length)
308307

309308
# Try connecting to the server processes with the good certs while not
310309
# trusting the good certs (trusting the bad cert instead). Expect failure
311310
# because even though the server's cert file is otherwise OK, we don't
312311
# trust it.
313312
logger.info('Trying HTTPS download of target file: ' + good_https_url)
314313
with self.assertRaises(requests.exceptions.SSLError):
315-
download.safe_download(good_https_url, target_data_length)
314+
self.download.safe_download(good_https_url, target_data_length)
316315
with self.assertRaises(requests.exceptions.SSLError):
317-
download.unsafe_download(good_https_url, target_data_length)
316+
self.download.unsafe_download(good_https_url, target_data_length)
318317

319318
logger.info('Trying HTTPS download of target file: ' + good2_https_url)
320319
with self.assertRaises(requests.exceptions.SSLError):
321-
download.safe_download(good2_https_url, target_data_length)
320+
self.download.safe_download(good2_https_url, target_data_length)
322321
with self.assertRaises(requests.exceptions.SSLError):
323-
download.unsafe_download(good2_https_url, target_data_length)
322+
self.download.unsafe_download(good2_https_url, target_data_length)
324323

325324

326325
# Configure environment to now trust the certfile that is expired.
@@ -334,9 +333,9 @@ def test_https_connection(self):
334333
# it, it is expired.
335334
logger.info('Trying HTTPS download of target file: ' + expired_https_url)
336335
with self.assertRaises(requests.exceptions.SSLError):
337-
download.safe_download(expired_https_url, target_data_length)
336+
self.download.safe_download(expired_https_url, target_data_length)
338337
with self.assertRaises(requests.exceptions.SSLError):
339-
download.unsafe_download(expired_https_url, target_data_length)
338+
self.download.unsafe_download(expired_https_url, target_data_length)
340339

341340

342341
# Try connecting to the server processes with the good certs while
@@ -348,16 +347,16 @@ def test_https_connection(self):
348347
# TODO: Confirm necessity of this session clearing and lay out mechanics.
349348
tuf.download._sessions = {}
350349
logger.info('Trying HTTPS download of target file: ' + good_https_url)
351-
download.safe_download(good_https_url, target_data_length).close()
352-
download.unsafe_download(good_https_url, target_data_length).close()
350+
self.download.safe_download(good_https_url, target_data_length).close()
351+
self.download.unsafe_download(good_https_url, target_data_length).close()
353352

354353
os.environ['REQUESTS_CA_BUNDLE'] = good2_cert_fname
355354
# Clear sessions to ensure that the certificate we just specified is used.
356355
# TODO: Confirm necessity of this session clearing and lay out mechanics.
357356
tuf.download._sessions = {}
358357
logger.info('Trying HTTPS download of target file: ' + good2_https_url)
359-
download.safe_download(good2_https_url, target_data_length).close()
360-
download.unsafe_download(good2_https_url, target_data_length).close()
358+
self.download.safe_download(good2_https_url, target_data_length).close()
359+
self.download.unsafe_download(good2_https_url, target_data_length).close()
361360

362361
finally:
363362
for proc_handler in [

0 commit comments

Comments
 (0)