From cf4902131efaf2f39109c5e2f883d91d18edc06f Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Thu, 19 Nov 2020 14:41:12 +0200 Subject: [PATCH 1/2] Fix empty list as a default value for function arg This quote from the Google Python style guide made me realize why empty list as a default value for an argument could be dangerous: "Default arguments are evaluated once at module load time. This may cause problems if the argument is a mutable object such as a list or a dictionary. If the function modifies the object (e.g., by appending an item to a list), the default value is modified." Read more here: https://google.github.io/styleguide/pyguide.html#2123-cons Signed-off-by: Martin Vrachev --- tests/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index 5d4e572421..ea85d32086 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -156,17 +156,19 @@ class TestServerProcess(): List of additional arguments for the command which will start the subprocess. More precisely "python -u ". - Default is empty list. + When no list is provided, an empty list ("[]") will be assigned to it. """ def __init__(self, log, server='simple_server.py', - timeout=10, popen_cwd=".", extra_cmd_args=[]): + timeout=10, popen_cwd=".", extra_cmd_args=None): self.server = server self.__logger = log # Stores popped messages from the queue. self.__logged_messages = [] + if extra_cmd_args is None: + extra_cmd_args = [] try: self._start_server(timeout, extra_cmd_args, popen_cwd) From 028d1bc9f72891805609d695666f492ee4a1c8f3 Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Thu, 19 Nov 2020 14:45:09 +0200 Subject: [PATCH 2/2] Make "utils" import more definite Currently, we are importing the "utils" module in tests/utils with "import utils". This could become a problem when there is another module with the same general name "utils" and could lead to import mistakes. Signed-off-by: Martin Vrachev --- tests/test_api.py | 2 +- tests/test_arbitrary_package_attack.py | 2 +- tests/test_developer_tool.py | 2 +- tests/test_download.py | 2 +- tests/test_endless_data_attack.py | 2 +- tests/test_extraneous_dependencies_attack.py | 2 +- tests/test_formats.py | 2 +- tests/test_indefinite_freeze_attack.py | 2 +- tests/test_key_revocation_integration.py | 2 +- tests/test_keydb.py | 2 +- tests/test_log.py | 2 +- tests/test_mirrors.py | 2 +- tests/test_mix_and_match_attack.py | 2 +- tests/test_multiple_repositories_integration.py | 2 +- tests/test_proxy_use.py | 2 +- tests/test_replay_attack.py | 2 +- tests/test_repository_lib.py | 2 +- tests/test_repository_tool.py | 2 +- tests/test_roledb.py | 2 +- tests/test_root_versioning_integration.py | 2 +- tests/test_sig.py | 2 +- tests/test_slow_retrieval_attack.py | 2 +- tests/test_tutorial.py | 2 +- tests/test_unittest_toolbox.py | 2 +- tests/test_updater.py | 2 +- tests/test_updater_root_rotation_integration.py | 2 +- tests/test_utils.py | 2 +- 27 files changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 7bdaf3ae3b..5d2dce0269 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -18,7 +18,7 @@ from datetime import datetime, timedelta from dateutil.relativedelta import relativedelta -import utils +from tests import utils # TODO: Remove case handling when fully dropping support for versions >= 3.6 IS_PY_VERSION_SUPPORTED = sys.version_info >= (3, 6) diff --git a/tests/test_arbitrary_package_attack.py b/tests/test_arbitrary_package_attack.py index 80ea48fa5c..2dd329ced5 100755 --- a/tests/test_arbitrary_package_attack.py +++ b/tests/test_arbitrary_package_attack.py @@ -52,7 +52,7 @@ import tuf.client.updater as updater import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_developer_tool.py b/tests/test_developer_tool.py index 2a34786b72..d066964437 100755 --- a/tests/test_developer_tool.py +++ b/tests/test_developer_tool.py @@ -41,7 +41,7 @@ from tuf.developer_tool import METADATA_DIRECTORY_NAME from tuf.developer_tool import TARGETS_DIRECTORY_NAME -import utils +from tests import utils logger = logging.getLogger(__name__) diff --git a/tests/test_download.py b/tests/test_download.py index af13128e33..c6933fcb1b 100755 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -47,7 +47,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.exceptions -import utils +from tests import utils import requests.exceptions diff --git a/tests/test_endless_data_attack.py b/tests/test_endless_data_attack.py index 18bb52155c..759119ae9d 100755 --- a/tests/test_endless_data_attack.py +++ b/tests/test_endless_data_attack.py @@ -54,7 +54,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.roledb -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_extraneous_dependencies_attack.py b/tests/test_extraneous_dependencies_attack.py index 55e2015831..8eece9971d 100755 --- a/tests/test_extraneous_dependencies_attack.py +++ b/tests/test_extraneous_dependencies_attack.py @@ -57,7 +57,7 @@ import tuf.keydb import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_formats.py b/tests/test_formats.py index 6f37a53250..d34c2363cf 100755 --- a/tests/test_formats.py +++ b/tests/test_formats.py @@ -36,7 +36,7 @@ import tuf import tuf.formats -import utils +from tests import utils import securesystemslib import securesystemslib.util diff --git a/tests/test_indefinite_freeze_attack.py b/tests/test_indefinite_freeze_attack.py index 84819b7492..e72299c456 100755 --- a/tests/test_indefinite_freeze_attack.py +++ b/tests/test_indefinite_freeze_attack.py @@ -68,7 +68,7 @@ import tuf.keydb import tuf.exceptions -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_key_revocation_integration.py b/tests/test_key_revocation_integration.py index 3858413395..ed0909b14f 100755 --- a/tests/test_key_revocation_integration.py +++ b/tests/test_key_revocation_integration.py @@ -52,7 +52,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.client.updater as updater -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_keydb.py b/tests/test_keydb.py index 4d481bd776..1db0b421db 100755 --- a/tests/test_keydb.py +++ b/tests/test_keydb.py @@ -39,7 +39,7 @@ import tuf.keydb import tuf.log -import utils +from tests import utils logger = logging.getLogger(__name__) diff --git a/tests/test_log.py b/tests/test_log.py index ea545ea12c..cba1762f10 100755 --- a/tests/test_log.py +++ b/tests/test_log.py @@ -33,7 +33,7 @@ import securesystemslib import securesystemslib.util -import utils +from tests import utils from six.moves import reload_module diff --git a/tests/test_mirrors.py b/tests/test_mirrors.py index 170274bfac..5f49294876 100755 --- a/tests/test_mirrors.py +++ b/tests/test_mirrors.py @@ -34,7 +34,7 @@ import tuf.mirrors as mirrors import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils import securesystemslib import securesystemslib.util diff --git a/tests/test_mix_and_match_attack.py b/tests/test_mix_and_match_attack.py index 61d5c7cbef..472d88b940 100755 --- a/tests/test_mix_and_match_attack.py +++ b/tests/test_mix_and_match_attack.py @@ -53,7 +53,7 @@ import tuf.roledb import tuf.keydb -import utils +from tests import utils import six diff --git a/tests/test_multiple_repositories_integration.py b/tests/test_multiple_repositories_integration.py index 211d1a5f44..24bd48e5bd 100755 --- a/tests/test_multiple_repositories_integration.py +++ b/tests/test_multiple_repositories_integration.py @@ -45,7 +45,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.repository_tool as repo_tool -import utils +from tests import utils import six import securesystemslib diff --git a/tests/test_proxy_use.py b/tests/test_proxy_use.py index 231ccce0bd..b3305d97e2 100755 --- a/tests/test_proxy_use.py +++ b/tests/test_proxy_use.py @@ -46,7 +46,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.exceptions -import utils +from tests import utils import six diff --git a/tests/test_replay_attack.py b/tests/test_replay_attack.py index 2a22809b7a..3c5fa389f8 100755 --- a/tests/test_replay_attack.py +++ b/tests/test_replay_attack.py @@ -52,7 +52,7 @@ import tuf.repository_tool as repo_tool import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_repository_lib.py b/tests/test_repository_lib.py index a30037477c..36d1826a2e 100755 --- a/tests/test_repository_lib.py +++ b/tests/test_repository_lib.py @@ -50,7 +50,7 @@ import tuf.repository_lib as repo_lib import tuf.repository_tool as repo_tool -import utils +from tests import utils import securesystemslib import securesystemslib.exceptions diff --git a/tests/test_repository_tool.py b/tests/test_repository_tool.py index 86d76fb060..0fac025a31 100755 --- a/tests/test_repository_tool.py +++ b/tests/test_repository_tool.py @@ -44,7 +44,7 @@ import tuf.keydb import tuf.repository_tool as repo_tool -import utils +from tests import utils import securesystemslib import securesystemslib.exceptions diff --git a/tests/test_roledb.py b/tests/test_roledb.py index 4bc4efb0c8..74017dd065 100755 --- a/tests/test_roledb.py +++ b/tests/test_roledb.py @@ -38,7 +38,7 @@ import tuf.exceptions import tuf.log -import utils +from tests import utils import securesystemslib import securesystemslib.keys diff --git a/tests/test_root_versioning_integration.py b/tests/test_root_versioning_integration.py index ee6f143c34..b0eda37bd5 100755 --- a/tests/test_root_versioning_integration.py +++ b/tests/test_root_versioning_integration.py @@ -40,7 +40,7 @@ import tuf.keydb import tuf.repository_tool as repo_tool -import utils +from tests import utils import securesystemslib import securesystemslib.storage diff --git a/tests/test_sig.py b/tests/test_sig.py index 100c1368fa..cb836bd7dd 100755 --- a/tests/test_sig.py +++ b/tests/test_sig.py @@ -42,7 +42,7 @@ import tuf.sig import tuf.exceptions -import utils +from tests import utils import securesystemslib import securesystemslib.keys diff --git a/tests/test_slow_retrieval_attack.py b/tests/test_slow_retrieval_attack.py index db2e1b3544..3c87e0d912 100755 --- a/tests/test_slow_retrieval_attack.py +++ b/tests/test_slow_retrieval_attack.py @@ -58,7 +58,7 @@ import tuf.roledb import tuf.keydb -import utils +from tests import utils import six diff --git a/tests/test_tutorial.py b/tests/test_tutorial.py index 1296dd1ddb..3fb3aaadfb 100755 --- a/tests/test_tutorial.py +++ b/tests/test_tutorial.py @@ -44,7 +44,7 @@ from tuf.repository_tool import * # part of TUTORIAL.md -import utils +from tests import utils import securesystemslib.exceptions diff --git a/tests/test_unittest_toolbox.py b/tests/test_unittest_toolbox.py index 308c0a4c80..acc6f44371 100755 --- a/tests/test_unittest_toolbox.py +++ b/tests/test_unittest_toolbox.py @@ -35,7 +35,7 @@ import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils logger = logging.getLogger(__name__) diff --git a/tests/test_updater.py b/tests/test_updater.py index f2665b780b..0d6f35bfa4 100755 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -71,7 +71,7 @@ import tuf.unittest_toolbox as unittest_toolbox import tuf.client.updater as updater -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_updater_root_rotation_integration.py b/tests/test_updater_root_rotation_integration.py index b1b83ba2d5..1b47f49eb8 100755 --- a/tests/test_updater_root_rotation_integration.py +++ b/tests/test_updater_root_rotation_integration.py @@ -59,7 +59,7 @@ import tuf.client.updater as updater import tuf.settings -import utils +from tests import utils import securesystemslib import six diff --git a/tests/test_utils.py b/tests/test_utils.py index d26a1662cc..28d974944c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -28,7 +28,7 @@ import tuf.unittest_toolbox as unittest_toolbox -import utils +from tests import utils logger = logging.getLogger(__name__)