Skip to content

Commit ec27630

Browse files
committed
minor: PR tweaks based on review: doc, casing, typos, updates
- two reversions to unnecessary changes - some typo fixes - capitalization of HTTP/S where reasonable - commenting out code section with ''' rather than # Signed-off-by: Sebastien Awwad <sebastien.awwad@gmail.com>
1 parent b163caa commit ec27630

File tree

5 files changed

+49
-48
lines changed

5 files changed

+49
-48
lines changed

tests/proxy_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# This code is taken from: https://github.com/inaz2/proxy2
3+
# This code is taken from: github.com/inaz2/proxy2
44
# Credit goes to the author. It has been very slightly modified here to use
55
# IPv4 instead of IPv6, and to only attempt interception of HTTPS traffic
66
# (instead of relaying via HTTP CONNECT) if new global variable INTERCEPT is

tests/test_download.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,29 @@ def test_download_url_to_tempfileobj_and_urls(self):
197197

198198

199199

200+
'''
200201
# This test uses sites on the internet, requiring a net connection to succeed.
201202
# Since this is the only such test in TUF, I'm not going to enable it... but
202203
# it's here in case it's useful for diagnosis.
203-
# def test_https_validation(self):
204-
# """
205-
# Use some known URLs on the net to ensure that TUF download checks SSL
206-
# certificates appropriately.
207-
# """
208-
# # We should never get as far as the target file download itself, so the
209-
# # length we pass to safe_download and unsafe_download shouldn't matter.
210-
# irrelevant_length = 10
211-
#
212-
# for bad_url in [
213-
# 'https://expired.badssl.com/', # expired certificate
214-
# 'https://wrong.host.badssl.com/', ]: # hostname verification fail
215-
#
216-
# with self.assertRaises(requests.exceptions.SSLError):
217-
# download.safe_download(bad_url, irrelevant_length)
218-
#
219-
# with self.assertRaises(requests.exceptions.SSLError):
220-
# download.unsafe_download(bad_url, irrelevant_length)
204+
def test_https_validation(self):
205+
"""
206+
Use some known URLs on the net to ensure that TUF download checks SSL
207+
certificates appropriately.
208+
"""
209+
# We should never get as far as the target file download itself, so the
210+
# length we pass to safe_download and unsafe_download shouldn't matter.
211+
irrelevant_length = 10
212+
213+
for bad_url in [
214+
'https://expired.badssl.com/', # expired certificate
215+
'https://wrong.host.badssl.com/', ]: # hostname verification fail
221216
217+
with self.assertRaises(requests.exceptions.SSLError):
218+
download.safe_download(bad_url, irrelevant_length)
219+
220+
with self.assertRaises(requests.exceptions.SSLError):
221+
download.unsafe_download(bad_url, irrelevant_length)
222+
'''
222223

223224

224225

@@ -250,10 +251,12 @@ def test_https_connection(self):
250251
bad_cert_fname = os.path.join('ssl_certs', 'ssl_cert_wronghost.crt')
251252
expired_cert_fname = os.path.join('ssl_certs', 'ssl_cert_expired.crt')
252253

253-
# Launch three https servers (serve files in the current dir).
254-
# The first we expect to operate correctly.
255-
# The second we run with an HTTPS certificate with an unexpected hostname.
256-
# The third we run with an HTTPS certificate that is expired.
254+
# Launch four HTTPS servers (serve files in the current dir).
255+
# 1: we expect to operate correctly
256+
# 2: also good; uses a slightly different cert (controls for the cert
257+
# generation method used for the next two, in case it comes to matter)
258+
# 3: run with an HTTPS certificate with an unexpected hostname
259+
# 4: run with an HTTPS certificate that is expired
257260
port1 = str(random.randint(30000, 45000))
258261
port2 = str(int(port1) + 1)
259262
port3 = str(int(port1) + 2)
@@ -267,7 +270,7 @@ def test_https_connection(self):
267270
bad_https_server_proc = subprocess.Popen(command3, stderr=subprocess.PIPE)
268271
expd_https_server_proc = subprocess.Popen(command4, stderr=subprocess.PIPE)
269272

270-
# Provide a delay long enough to allow the https servers to start.
273+
# Provide a delay long enough to allow the HTTPS servers to start.
271274
# Encountered an error on one test system at delay value of 0.2s, so
272275
# increasing to 0.5s.
273276
# Expect to see "Connection refused" if this delay is not long enough
@@ -280,7 +283,7 @@ def test_https_connection(self):
280283
bad_https_url = good_https_url.replace(':' + port1, ':' + port3)
281284
expired_https_url = good_https_url.replace(':' + port1, ':' + port4)
282285

283-
# Download the target file using an https connection.
286+
# Download the target file using an HTTPS connection.
284287

285288
# Use try-finally solely to ensure that the server processes are killed.
286289
try:
@@ -291,7 +294,7 @@ def test_https_connection(self):
291294
# Try connecting to the server process with the bad cert while trusting
292295
# the bad cert. Expect failure because even though we trust it, the
293296
# hostname we're connecting to does not match the hostname in the cert.
294-
logger.info('Trying https download of target file: ' + bad_https_url)
297+
logger.info('Trying HTTPS download of target file: ' + bad_https_url)
295298
with self.assertRaises(requests.exceptions.SSLError):
296299
download.safe_download(bad_https_url, target_data_length)
297300
with self.assertRaises(requests.exceptions.SSLError):
@@ -301,13 +304,13 @@ def test_https_connection(self):
301304
# trusting the good certs (trusting the bad cert instead). Expect failure
302305
# because even though the server's cert file is otherwise OK, we don't
303306
# trust it.
304-
print('Trying https download of target file: ' + good_https_url)
307+
print('Trying HTTPS download of target file: ' + good_https_url)
305308
with self.assertRaises(requests.exceptions.SSLError):
306309
download.safe_download(good_https_url, target_data_length)
307310
with self.assertRaises(requests.exceptions.SSLError):
308311
download.unsafe_download(good_https_url, target_data_length)
309312

310-
print('Trying https download of target file: ' + good2_https_url)
313+
print('Trying HTTPS download of target file: ' + good2_https_url)
311314
with self.assertRaises(requests.exceptions.SSLError):
312315
download.safe_download(good2_https_url, target_data_length)
313316
with self.assertRaises(requests.exceptions.SSLError):
@@ -319,7 +322,7 @@ def test_https_connection(self):
319322
# Try connecting to the server process with the expired cert while
320323
# trusting the expired cert. Expect failure because even though we trust
321324
# it, it is expired.
322-
logger.info('Trying https download of target file: ' + expired_https_url)
325+
logger.info('Trying HTTPS download of target file: ' + expired_https_url)
323326
with self.assertRaises(requests.exceptions.SSLError):
324327
download.safe_download(expired_https_url, target_data_length)
325328
with self.assertRaises(requests.exceptions.SSLError):
@@ -334,12 +337,12 @@ def test_https_connection(self):
334337
# still trusting the good cert. Perhaps it's a caching issue....?
335338
# I'm not especially concerned yet, but take note for later....
336339
os.environ['REQUESTS_CA_BUNDLE'] = good_cert_fname
337-
logger.info('Trying https download of target file: ' + good_https_url)
340+
logger.info('Trying HTTPS download of target file: ' + good_https_url)
338341
download.safe_download(good_https_url, target_data_length)
339342
download.unsafe_download(good_https_url, target_data_length)
340343

341344
os.environ['REQUESTS_CA_BUNDLE'] = good2_cert_fname
342-
logger.info('Trying https download of target file: ' + good2_https_url)
345+
logger.info('Trying HTTPS download of target file: ' + good2_https_url)
343346
download.safe_download(good2_https_url, target_data_length)
344347
download.unsafe_download(good2_https_url, target_data_length)
345348

tests/test_proxy_use.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def setUpClass(cls):
6464
"""
6565
Setup performed before the first test function (TestWithProxies class
6666
method) runs.
67-
Launch http, https, and proxy servers in the current working directory.
67+
Launch HTTP, HTTPS, and proxy servers in the current working directory.
6868
We'll set up four servers:
6969
- HTTP server (simple_server.py)
7070
- HTTPS server (simple_https_server.py)
@@ -95,8 +95,7 @@ def setUpClass(cls):
9595
cls.http_proxy_port = cls.http_port + 2
9696
cls.http_proxy_proc = subprocess.Popen(
9797
['python', 'proxy_server.py', str(cls.http_proxy_port)],
98-
stderr=subprocess.PIPE)
99-
# Note that the http proxy server's address uses http://, regardless of the
98+
# Note that the HTTP proxy server's address uses http://, regardless of the
10099
# type of connection used with the target server.
101100
cls.http_proxy_addr = 'http://127.0.0.1:' + str(cls.http_proxy_port)
102101

@@ -117,8 +116,7 @@ def setUpClass(cls):
117116
cls.https_proxy_proc = subprocess.Popen(
118117
['python', 'proxy_server.py', str(cls.https_proxy_port), 'intercept',
119118
os.path.join('ssl_certs', 'ssl_cert.crt')],
120-
stderr=subprocess.PIPE)
121-
# Note that the https proxy server's address uses https://, regardless of
119+
# Note that the HTTPS proxy server's address uses https://, regardless of
122120
# the type of connection used with the target server.
123121
cls.https_proxy_addr = 'https://127.0.0.1:' + str(cls.https_proxy_port)
124122

@@ -201,7 +199,7 @@ def test_baseline_no_proxy(self):
201199
HTTP proxy, and perform an HTTP connection with the final server.
202200
"""
203201

204-
logger.info('Trying http download with no proxy: ' + self.url)
202+
logger.info('Trying HTTP download with no proxy: ' + self.url)
205203
download.safe_download(self.url, self.target_data_length)
206204
download.unsafe_download(self.url, self.target_data_length)
207205

@@ -217,15 +215,15 @@ def test_http_dl_via_smart_http_proxy(self):
217215

218216
self.set_env_value('HTTP_PROXY', self.http_proxy_addr)
219217

220-
logger.info('Trying http download via http proxy: ' + self.url)
218+
logger.info('Trying HTTP download via HTTP proxy: ' + self.url)
221219
download.safe_download(self.url, self.target_data_length)
222220
download.unsafe_download(self.url, self.target_data_length)
223221

224222

225223

226224

227225

228-
def test_httpS_dl_via_smart_http_proxy(self):
226+
def test_https_dl_via_smart_http_proxy(self):
229227
"""
230228
Test a length-validating TUF download of a file through a proxy. Use an
231229
HTTP proxy that supports HTTP CONNECT (which essentially causes it to act
@@ -242,15 +240,15 @@ def test_httpS_dl_via_smart_http_proxy(self):
242240
self.set_env_value('REQUESTS_CA_BUNDLE',
243241
os.path.join('ssl_certs', 'ssl_cert.crt'))
244242

245-
logger.info('Trying httpS download via http proxy: ' + self.url_https)
243+
logger.info('Trying HTTPS download via HTTP proxy: ' + self.url_https)
246244
download.safe_download(self.url_https, self.target_data_length)
247245
download.unsafe_download(self.url_https, self.target_data_length)
248246

249247

250248

251249

252250

253-
def test_http_dl_via_httpS_proxy(self):
251+
def test_http_dl_via_https_proxy(self):
254252
"""
255253
Test a length-validating TUF download of a file through a proxy. Use an
256254
HTTPS proxy, and perform an HTTP connection with the final server.
@@ -263,15 +261,15 @@ def test_http_dl_via_httpS_proxy(self):
263261
self.set_env_value('REQUESTS_CA_BUNDLE',
264262
os.path.join('ssl_certs', 'proxy_ca.crt'))
265263

266-
logger.info('Trying http download via httpS proxy: ' + self.url_https)
264+
logger.info('Trying HTTP download via HTTPS proxy: ' + self.url_https)
267265
download.safe_download(self.url, self.target_data_length)
268266
download.unsafe_download(self.url, self.target_data_length)
269267

270268

271269

272270

273271

274-
def test_httpS_dl_via_httpS_proxy(self):
272+
def test_https_dl_via_https_proxy(self):
275273
"""
276274
Test a length-validating TUF download of a file through a proxy. Use an
277275
HTTPS proxy, and perform an HTTPS connection with the final server.
@@ -286,7 +284,7 @@ def test_httpS_dl_via_httpS_proxy(self):
286284
self.set_env_value('REQUESTS_CA_BUNDLE',
287285
os.path.join('ssl_certs', 'proxy_ca.crt'))
288286

289-
logger.info('Trying httpS download via httpS proxy: ' + self.url_https)
287+
logger.info('Trying HTTPS download via HTTPS proxy: ' + self.url_https)
290288
download.safe_download(self.url_https, self.target_data_length)
291289
download.unsafe_download(self.url_https, self.target_data_length)
292290

@@ -340,7 +338,7 @@ def restore_env_value(self, key):
340338
# del os.environ[key] should unset the variable. Otherwise, we'll just
341339
# have to settle for setting it to an empty string.
342340
# See os.environ in:
343-
# https://docs.python.org/2/library/os.html#process-parameters)
341+
# https://docs.python.org/2/library/os.html#process-parameters
344342
os.environ[key] = ''
345343
del os.environ[key]
346344

tuf/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ def __init__(self, expected_length, observed_length):
172172
self.observed_length = observed_length #bytes
173173

174174
def __str__(self):
175-
return 'Observed length (' + repr(self.observed_length)+\
176-
') != expected length (' + repr(self.expected_length) + ').'
175+
return 'Observed length (' + repr(self.observed_length) + \
176+
') < expected length (' + repr(self.expected_length) + ').'
177177

178178

179179
class SlowRetrievalError(DownloadError):

tuf/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
DEFAULT_TARGETS_REQUIRED_LENGTH = 5000000 #bytes
7676

7777
# Set a timeout value in seconds (float) for non-blocking socket operations.
78-
SOCKET_TIMEOUT = 5 #seconds
78+
SOCKET_TIMEOUT = 4 #seconds
7979

8080
# The maximum chunk of data, in bytes, we would download in every round.
8181
CHUNK_SIZE = 400000 #bytes

0 commit comments

Comments
 (0)