From 9f1e4f60e955c831e63765ed46afdf0eaedf2c09 Mon Sep 17 00:00:00 2001 From: dachshund Date: Sun, 8 Sep 2013 02:14:22 -0400 Subject: [PATCH] Address comments from 01db53dac620db87801642c688c90b43f3d8832e. --- tuf/__init__.py | 2 +- tuf/conf.py | 8 ++++---- tuf/download.py | 17 ++++++++++------- tuf/tests/system_tests/slow_retrieval_server.py | 1 - 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tuf/__init__.py b/tuf/__init__.py index 9e66d15b..f1ec70ec 100755 --- a/tuf/__init__.py +++ b/tuf/__init__.py @@ -188,7 +188,7 @@ class SlowRetrievalError(DownloadError): def __init__(self, cumulative_moving_average_of_speed): self.__cumulative_moving_average_of_speed = \ - cumulative_moving_average_of_speed#bytes/second + cumulative_moving_average_of_speed #bytes/second def __str__(self): return "Cumulative moving average of download speed: "+\ diff --git a/tuf/conf.py b/tuf/conf.py index 042892be..8769dbed 100755 --- a/tuf/conf.py +++ b/tuf/conf.py @@ -39,16 +39,16 @@ # Since the timestamp role does not have signed metadata about itself, we set a # default but sane upper bound for the number of bytes required to download it. -DEFAULT_TIMESTAMP_REQUIRED_LENGTH = 2048#bytes +DEFAULT_TIMESTAMP_REQUIRED_LENGTH = 2048 #bytes # Set a timeout value in seconds (float) for non-blocking socket operations. -SOCKET_TIMEOUT = 1#seconds +SOCKET_TIMEOUT = 1 #seconds # The maximum chunk of data, in bytes, we would download in every round. -CHUNK_SIZE = 8192#bytes +CHUNK_SIZE = 8192 #bytes # The minimum cumulative moving average of download speed (bytes/second) that # must be met to avoid being considered as a slow retrieval attack. -MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED = CHUNK_SIZE#bytes/second +MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED = CHUNK_SIZE #bytes/second diff --git a/tuf/download.py b/tuf/download.py index a89a4973..370d36f4 100755 --- a/tuf/download.py +++ b/tuf/download.py @@ -107,13 +107,14 @@ def __start_clock(self): # We must have reset the clock before this. assert self.__start_time is None + # We are using wall time, so it will be imprecise sometimes. self.__start_time = time.time() - def __stop_clock(self, data_length): + def __stop_clock_and_check_speed(self, data_length): """ Stop the clock and try to detect slow retrieval. @@ -134,9 +135,10 @@ def __stop_clock(self, data_length): """ + # We are using wall time, so it will be imprecise sometimes. stop_time = time.time() # We must have already started the clock. - assert self.__start_time > 0, self.__start_time + assert self.__start_time > 0 time_delta = stop_time-self.__start_time # Reset the clock. self.__start_time = None @@ -153,8 +155,9 @@ def __stop_clock(self, data_length): # If the cumulative moving average of the download speed is below a certain # threshold, we flag this as a possible slow-retrieval attack. This - # threshold will determine our bias: if it is too slow, we will have more - # false negatives; if it is too high, we will have more false positives. + # threshold will determine our bias: if it is too low, we will have more + # false positives; if it is too high, we will have more false negatives. + # Presently, we know that this will punish a server with a slow start. if self.__cumulative_moving_average_of_speed < tuf.conf.MIN_CUMULATIVE_MOVING_AVERAGE_OF_DOWNLOAD_SPEED: raise tuf.SlowRetrievalError(self.__cumulative_moving_average_of_speed) logger.debug('Cumulative moving average of download speed: '+\ @@ -229,15 +232,15 @@ def read(self, size): self.__start_clock() data = self._sock.recv(left) except socket.timeout: - self.__stop_clock(0) + self.__stop_clock_and_check_speed(0) continue except socket.error, e: if e.args[0] == EINTR: - self.__stop_clock(0) + self.__stop_clock_and_check_speed(0) continue raise else: - self.__stop_clock(len(data)) + self.__stop_clock_and_check_speed(len(data)) if not data: break n = len(data) diff --git a/tuf/tests/system_tests/slow_retrieval_server.py b/tuf/tests/system_tests/slow_retrieval_server.py index 6ccb3cd2..dfddab79 100755 --- a/tuf/tests/system_tests/slow_retrieval_server.py +++ b/tuf/tests/system_tests/slow_retrieval_server.py @@ -89,7 +89,6 @@ def get_random_port(): def run(port, test_mode): server_address = ('localhost', port) httpd = HTTPServer_Test(server_address, Handler, test_mode) - print('Slow server is active on port: '+str(port)+' ...') httpd.handle_request()