diff --git a/tests/test_arbitrary_package_attack.py b/tests/test_arbitrary_package_attack.py index a4127c6462..118d13f9c7 100755 --- a/tests/test_arbitrary_package_attack.py +++ b/tests/test_arbitrary_package_attack.py @@ -37,7 +37,6 @@ from __future__ import unicode_literals import os -import tempfile import shutil import json import logging @@ -64,11 +63,6 @@ class TestArbitraryPackageAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -85,10 +79,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -98,12 +88,15 @@ def setUp(self): self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) + # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_endless_data_attack.py b/tests/test_endless_data_attack.py index dd5b901635..73a2f58d87 100755 --- a/tests/test_endless_data_attack.py +++ b/tests/test_endless_data_attack.py @@ -40,7 +40,6 @@ from __future__ import unicode_literals import os -import tempfile import shutil import json import logging @@ -66,11 +65,6 @@ class TestEndlessDataAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -87,10 +81,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -99,13 +89,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_extraneous_dependencies_attack.py b/tests/test_extraneous_dependencies_attack.py index 195b0a2e5b..1b28fd5de4 100755 --- a/tests/test_extraneous_dependencies_attack.py +++ b/tests/test_extraneous_dependencies_attack.py @@ -43,7 +43,6 @@ from __future__ import unicode_literals import os -import tempfile import shutil import json import logging @@ -70,11 +69,6 @@ class TestExtraneousDependenciesAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -91,10 +85,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -103,13 +93,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_indefinite_freeze_attack.py b/tests/test_indefinite_freeze_attack.py index 55cc664b3e..74a74a01b5 100755 --- a/tests/test_indefinite_freeze_attack.py +++ b/tests/test_indefinite_freeze_attack.py @@ -46,7 +46,6 @@ import os import time -import tempfile import shutil import json import logging @@ -78,11 +77,6 @@ class TestIndefiniteFreezeAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -99,10 +93,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -110,13 +100,15 @@ def setUp(self): # We are inheriting from custom class. unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_key_revocation_integration.py b/tests/test_key_revocation_integration.py index bf13cfe530..4aaf0d30a3 100755 --- a/tests/test_key_revocation_integration.py +++ b/tests/test_key_revocation_integration.py @@ -39,7 +39,6 @@ import os import shutil -import tempfile import logging import unittest import sys @@ -65,11 +64,6 @@ class TestKeyRevocation(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). Test # cases will request metadata and target files that have been pre-generated # in 'tuf/tests/repository_data', which will be served by the @@ -86,10 +80,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated for the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -98,13 +88,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf.tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_mix_and_match_attack.py b/tests/test_mix_and_match_attack.py index 45325dca5a..551691da37 100755 --- a/tests/test_mix_and_match_attack.py +++ b/tests/test_mix_and_match_attack.py @@ -39,7 +39,6 @@ from __future__ import unicode_literals import os -import tempfile import shutil import logging import unittest @@ -69,11 +68,6 @@ class TestMixAndMatchAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and - # target files. 'temporary_directory' must be deleted in TearDownModule() - # so that temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -90,10 +84,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -102,13 +92,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_multiple_repositories_integration.py b/tests/test_multiple_repositories_integration.py index 2c85b9be69..1f556bc22a 100755 --- a/tests/test_multiple_repositories_integration.py +++ b/tests/test_multiple_repositories_integration.py @@ -30,7 +30,6 @@ from __future__ import unicode_literals import os -import tempfile import logging import shutil import unittest @@ -61,15 +60,13 @@ def setUp(self): # We are inheriting from custom class. unittest_toolbox.Modified_TestCase.setUp(self) - self.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - self.temporary_repository_root = self.make_temp_directory(directory= - self.temporary_directory) + self.temporary_repository_root = self.make_temp_directory( + directory=os.getcwd()) # The original repository, keystore, and client directories will be copied # for each test case. @@ -171,8 +168,6 @@ def tearDown(self): tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) - shutil.rmtree(self.temporary_directory) - def test_update(self): self.assertEqual('test_repository1', str(self.repository_updater)) @@ -271,8 +266,8 @@ def test_repository_tool(self): valid_targetinfo = multi_repo_updater.get_valid_targetinfo('file3.txt') for my_updater, my_targetinfo in six.iteritems(valid_targetinfo): - my_updater.download_target(my_targetinfo, self.temporary_directory) - self.assertTrue(os.path.exists(os.path.join(self.temporary_directory, 'file3.txt'))) + my_updater.download_target(my_targetinfo, self.temporary_repository_root) + self.assertTrue(os.path.exists(os.path.join(self.temporary_repository_root, 'file3.txt'))) diff --git a/tests/test_replay_attack.py b/tests/test_replay_attack.py index 58fdc13ffd..a1a28a5c20 100755 --- a/tests/test_replay_attack.py +++ b/tests/test_replay_attack.py @@ -39,7 +39,6 @@ from __future__ import unicode_literals import os -import tempfile import datetime import shutil import logging @@ -69,11 +68,6 @@ class TestReplayAttack(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). # Test cases will request metadata and target files that have been # pre-generated in 'tuf/tests/repository_data', which will be served by the @@ -90,10 +84,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -102,13 +92,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. diff --git a/tests/test_repository_lib.py b/tests/test_repository_lib.py index 40cf6f964b..0b4ed1d9db 100755 --- a/tests/test_repository_lib.py +++ b/tests/test_repository_lib.py @@ -32,7 +32,6 @@ import time import datetime import logging -import tempfile import json import shutil import unittest @@ -42,11 +41,11 @@ import tuf import tuf.formats import tuf.log -import tuf.formats import tuf.roledb import tuf.keydb import tuf.settings +import tuf.unittest_toolbox as unittest_toolbox import tuf.repository_lib as repo_lib import tuf.repository_tool as repo_tool @@ -67,15 +66,11 @@ 'snapshot.json'] -class TestRepositoryToolFunctions(unittest.TestCase): +class TestRepositoryToolFunctions(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownClass() so that - # temporary files are always removed, even when exceptions occur. tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) @@ -86,24 +81,26 @@ def tearDownClass(cls): tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) - shutil.rmtree(cls.temporary_directory) - def setUp(self): + # We are inheriting from custom class. + unittest_toolbox.Modified_TestCase.setUp(self) tuf.roledb.create_roledb('test_repository') tuf.keydb.create_keydb('test_repository') + self.temporary_directory = self.make_temp_directory(directory=os.getcwd()) def tearDown(self): tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) + # Modified_TestCase.tearDown() automatically deletes temporary files and + # directories that may have been created during each test case. + unittest_toolbox.Modified_TestCase.tearDown(self) def test_import_rsa_privatekey_from_file(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - # Load one of the pre-generated key files from 'tuf/tests/repository_data'. # 'password' unlocks the pre-generated key files. key_filepath = os.path.join('repository_data', 'keystore', @@ -122,14 +119,14 @@ def test_import_rsa_privatekey_from_file(self): # Test invalid argument. # Non-existent key file. - nonexistent_keypath = os.path.join(temporary_directory, + nonexistent_keypath = os.path.join(self.temporary_directory, 'nonexistent_keypath') self.assertRaises(securesystemslib.exceptions.StorageError, repo_lib.import_rsa_privatekey_from_file, nonexistent_keypath, 'pw') # Invalid key file argument. - invalid_keyfile = os.path.join(temporary_directory, 'invalid_keyfile') + invalid_keyfile = os.path.join(self.temporary_directory, 'invalid_keyfile') with open(invalid_keyfile, 'wb') as file_object: file_object.write(b'bad keyfile') self.assertRaises(securesystemslib.exceptions.CryptoError, repo_lib.import_rsa_privatekey_from_file, @@ -140,8 +137,7 @@ def test_import_rsa_privatekey_from_file(self): def test_import_ed25519_privatekey_from_file(self): # Test normal case. # Generate ed25519 keys that can be imported. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - ed25519_keypath = os.path.join(temporary_directory, 'ed25519_key') + ed25519_keypath = os.path.join(self.temporary_directory, 'ed25519_key') securesystemslib.interface.generate_and_write_ed25519_keypair( ed25519_keypath, password='pw') @@ -157,14 +153,14 @@ def test_import_ed25519_privatekey_from_file(self): # Test invalid argument. # Non-existent key file. - nonexistent_keypath = os.path.join(temporary_directory, + nonexistent_keypath = os.path.join(self.temporary_directory, 'nonexistent_keypath') self.assertRaises(securesystemslib.exceptions.StorageError, repo_lib.import_ed25519_privatekey_from_file, nonexistent_keypath, 'pw') # Invalid key file argument. - invalid_keyfile = os.path.join(temporary_directory, 'invalid_keyfile') + invalid_keyfile = os.path.join(self.temporary_directory, 'invalid_keyfile') with open(invalid_keyfile, 'wb') as file_object: file_object.write(b'bad keyfile') @@ -198,7 +194,6 @@ def test_import_ed25519_privatekey_from_file(self): def test_get_top_level_metadata_filenames(self): - # Test normal case. metadata_directory = os.path.join('metadata/') filenames = {'root.json': metadata_directory + 'root.json', @@ -228,8 +223,7 @@ def test_get_top_level_metadata_filenames(self): def test_get_targets_metadata_fileinfo(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - test_filepath = os.path.join(temporary_directory, 'file.txt') + test_filepath = os.path.join(self.temporary_directory, 'file.txt') with open(test_filepath, 'wt') as file_object: file_object.write('test file') @@ -257,7 +251,7 @@ def test_get_targets_metadata_fileinfo(self): # Test non-existent file. - nonexistent_filepath = os.path.join(temporary_directory, 'oops.txt') + nonexistent_filepath = os.path.join(self.temporary_directory, 'oops.txt') self.assertRaises(securesystemslib.exceptions.Error, repo_lib.get_targets_metadata_fileinfo, nonexistent_filepath, storage_backend) @@ -325,8 +319,7 @@ def test_generate_root_metadata(self): def test_generate_targets_metadata(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - targets_directory = os.path.join(temporary_directory, 'targets') + targets_directory = os.path.join(self.temporary_directory, 'targets') file1_path = os.path.join(targets_directory, 'file.txt') securesystemslib.util.ensure_parent_dir(file1_path) @@ -469,10 +462,9 @@ def test_generate_targets_metadata(self): def _setup_generate_snapshot_metadata_test(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) original_repository_path = os.path.join('repository_data', 'repository') - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') shutil.copytree(original_repository_path, repository_directory) metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) @@ -629,10 +621,9 @@ def test_generate_snapshot_metadata_with_hashes_and_length(self): def _setup_generate_timestamp_metadata_test(self): # Test normal case. repository_name = 'test_repository' - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) original_repository_path = os.path.join('repository_data', 'repository') - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') shutil.copytree(original_repository_path, repository_directory) metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) @@ -733,7 +724,6 @@ def test_generate_timestamp_metadata_without_length_and_hashes(self): def test_sign_metadata(self): # Test normal case. repository_name = 'test_repository' - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) metadata_path = os.path.join('repository_data', 'repository', 'metadata') keystore_path = os.path.join('repository_data', 'keystore') root_filename = os.path.join(metadata_path, 'root.json') @@ -792,12 +782,11 @@ def test_sign_metadata(self): def test_write_metadata_file(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) metadata_directory = os.path.join('repository_data', 'repository', 'metadata') root_filename = os.path.join(metadata_directory, 'root.json') root_signable = securesystemslib.util.load_json_file(root_filename) - output_filename = os.path.join(temporary_directory, 'root.json') + output_filename = os.path.join(self.temporary_directory, 'root.json') version_number = root_signable['signed']['version'] + 1 self.assertFalse(os.path.exists(output_filename)) @@ -825,9 +814,8 @@ def test_write_metadata_file(self): def test_create_tuf_client_directory(self): # Test normal case. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) repository_directory = os.path.join('repository_data', 'repository') - client_directory = os.path.join(temporary_directory, 'client') + client_directory = os.path.join(self.temporary_directory, 'client') repo_lib.create_tuf_client_directory(repository_directory, client_directory) @@ -887,10 +875,9 @@ def test__generate_and_write_metadata(self): repository_name) tuf.keydb.create_keydb_from_root_metadata(root_signable['signed'], repository_name) - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - targets_directory = os.path.join(temporary_directory, 'targets') + targets_directory = os.path.join(self.temporary_directory, 'targets') os.mkdir(targets_directory) - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) targets_metadata = os.path.join('repository_data', 'repository', 'metadata', @@ -942,8 +929,7 @@ def test__generate_and_write_metadata(self): def test__delete_obsolete_metadata(self): repository_name = 'test_repository' - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) os.makedirs(metadata_directory) @@ -978,8 +964,7 @@ def test__delete_obsolete_metadata(self): def test__load_top_level_metadata(self): repository_name = 'test_repository' - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) targets_directory = os.path.join(repository_directory, diff --git a/tests/test_repository_tool.py b/tests/test_repository_tool.py index 9f955f72a6..e50af3e524 100755 --- a/tests/test_repository_tool.py +++ b/tests/test_repository_tool.py @@ -42,6 +42,7 @@ import tuf.formats import tuf.roledb import tuf.keydb +import tuf.unittest_toolbox as unittest_toolbox import tuf.repository_tool as repo_tool import utils @@ -55,30 +56,21 @@ repo_tool.disable_console_log_messages() -class TestRepository(unittest.TestCase): - @classmethod - def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownClass() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - - - @classmethod - def tearDownClass(cls): - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated for the test cases. - shutil.rmtree(cls.temporary_directory) - +class TestRepository(unittest_toolbox.Modified_TestCase): def setUp(self): + # We are inheriting from custom class. + unittest_toolbox.Modified_TestCase.setUp(self) tuf.roledb.create_roledb('test_repository') tuf.keydb.create_keydb('test_repository') - + self.temporary_directory = self.make_temp_directory(directory=os.getcwd()) def tearDown(self): + # Modified_TestCase.tearDown() automatically deletes temporary files and + # directories that may have been created during each test case. + unittest_toolbox.Modified_TestCase.tearDown(self) tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) diff --git a/tests/test_root_versioning_integration.py b/tests/test_root_versioning_integration.py index ee6f143c34..c08e36be5b 100755 --- a/tests/test_root_versioning_integration.py +++ b/tests/test_root_versioning_integration.py @@ -38,6 +38,7 @@ import tuf.exceptions import tuf.roledb import tuf.keydb +import tuf.unittest_toolbox as unittest_toolbox import tuf.repository_tool as repo_tool import utils @@ -50,20 +51,22 @@ repo_tool.disable_console_log_messages() -class TestRepository(unittest.TestCase): +class TestRepository(unittest_toolbox.Modified_TestCase): - @classmethod - def setUpClass(cls): - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) + def setUp(self): + # We are inheriting from custom class. + unittest_toolbox.Modified_TestCase.setUp(self) + self.temporary_directory = self.make_temp_directory(directory=os.getcwd()) - @classmethod - def tearDownClass(cls): - shutil.rmtree(cls.temporary_directory) def tearDown(self): + # Modified_TestCase.tearDown() automatically deletes temporary files and + # directories that may have been created during each test case. + unittest_toolbox.Modified_TestCase.tearDown(self) tuf.roledb.clear_roledb() tuf.keydb.clear_keydb() + def test_init(self): # Test normal case. storage_backend = securesystemslib.storage.FilesystemBackend() @@ -98,8 +101,7 @@ def test_root_role_versioning(self): # # Copy the target files from 'tuf/tests/repository_data' so that writeall() # has target fileinfo to include in metadata. - temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory) - targets_directory = os.path.join(temporary_directory, 'repository', + targets_directory = os.path.join(self.temporary_directory, 'repository', repo_tool.TARGETS_DIRECTORY_NAME) original_targets_directory = os.path.join('repository_data', 'repository', 'targets') @@ -107,7 +109,7 @@ def test_root_role_versioning(self): # In this case, create_new_repository() creates the 'repository/' # sub-directory in 'temporary_directory' if it does not exist. - repository_directory = os.path.join(temporary_directory, 'repository') + repository_directory = os.path.join(self.temporary_directory, 'repository') metadata_directory = os.path.join(repository_directory, repo_tool.METADATA_STAGED_DIRECTORY_NAME) repository = repo_tool.create_new_repository(repository_directory) diff --git a/tests/test_slow_retrieval_attack.py b/tests/test_slow_retrieval_attack.py index 529212b32e..deadb1388b 100755 --- a/tests/test_slow_retrieval_attack.py +++ b/tests/test_slow_retrieval_attack.py @@ -45,7 +45,6 @@ from __future__ import unicode_literals import os -import tempfile import time import shutil import logging @@ -76,17 +75,15 @@ def setUp(self): self.repository_name = 'test_repository1' - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - self.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. @@ -163,10 +160,6 @@ def setUp(self): os.path.join(self.client_directory, self.repository_name, 'metadata', 'current')) - # Set the url prefix required by the 'tuf/client/updater.py' updater. - # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'. - repository_basepath = self.repository_directory[len(os.getcwd()):] - self.server_process_handler = utils.TestServerProcess(log=logger, server='slow_retrieval_server.py', timeout=0) @@ -180,6 +173,9 @@ def setUp(self): # increasing this to 3s, sadly. time.sleep(3) + # Set the url prefix required by the 'tuf/client/updater.py' updater. + # 'path/to/tmp/repository' -> 'localhost:8001/tmp/repository'. + repository_basepath = self.repository_directory[len(os.getcwd()):] url_prefix = 'http://localhost:' \ + str(self.server_process_handler.port) + repository_basepath @@ -209,11 +205,6 @@ def tearDown(self): # kills it and closes the temp file used for logging. self.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases. - shutil.rmtree(self.temporary_directory) - - def test_delay_before_send(self): # Simulate a slow retrieval attack. diff --git a/tests/test_updater.py b/tests/test_updater.py index bbecc411fc..917a548ae3 100755 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -84,11 +84,6 @@ class TestUpdater(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Needed because in some tests simple_server.py cannot be found. # The reason is that the current working directory # has been changed when executing a subprocess. @@ -111,10 +106,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated for the test cases - shutil.rmtree(cls.temporary_directory) - def setUp(self): @@ -124,13 +115,15 @@ def setUp(self): tuf.keydb.clear_keydb(clear_all=True) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf.tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case. @@ -1804,16 +1797,16 @@ def setUp(self): # We are inheriting from custom class. unittest_toolbox.Modified_TestCase.setUp(self) - self.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf/tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - self.temporary_repository_root = self.make_temp_directory(directory= - self.temporary_directory) - # Needed because in some tests simple_server.py cannot be found. # The reason is that the current working directory # has been changed when executing a subprocess. @@ -1828,24 +1821,24 @@ def setUp(self): # Save references to the often-needed client repository directories. # Test cases need these references to access metadata and target files. - self.repository_directory = os.path.join(self.temporary_repository_root, + self.repository_directory = os.path.join(temporary_repository_root, 'repository_server1') - self.repository_directory2 = os.path.join(self.temporary_repository_root, + self.repository_directory2 = os.path.join(temporary_repository_root, 'repository_server2') # Setting 'tuf.settings.repositories_directory' with the temporary client # directory copied from the original repository files. - tuf.settings.repositories_directory = self.temporary_repository_root + tuf.settings.repositories_directory = temporary_repository_root repository_name = 'test_repository1' repository_name2 = 'test_repository2' - self.client_directory = os.path.join(self.temporary_repository_root, + self.client_directory = os.path.join(temporary_repository_root, repository_name) - self.client_directory2 = os.path.join(self.temporary_repository_root, + self.client_directory2 = os.path.join(temporary_repository_root, repository_name2) - self.keystore_directory = os.path.join(self.temporary_repository_root, + self.keystore_directory = os.path.join(temporary_repository_root, 'keystore') self.map_file = os.path.join(self.client_directory, 'map.json') self.map_file2 = os.path.join(self.client_directory2, 'map.json') @@ -1938,10 +1931,6 @@ def tearDown(self): tuf.roledb.clear_roledb(clear_all=True) tuf.keydb.clear_keydb(clear_all=True) - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated of all the test cases - shutil.rmtree(self.temporary_directory) - # UNIT TESTS. diff --git a/tests/test_updater_root_rotation_integration.py b/tests/test_updater_root_rotation_integration.py index a8e74616aa..77cb491181 100755 --- a/tests/test_updater_root_rotation_integration.py +++ b/tests/test_updater_root_rotation_integration.py @@ -72,11 +72,6 @@ class TestUpdater(unittest_toolbox.Modified_TestCase): @classmethod def setUpClass(cls): - # Create a temporary directory to store the repository, metadata, and target - # files. 'temporary_directory' must be deleted in TearDownModule() so that - # temporary files are always removed, even when exceptions occur. - cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd()) - # Launch a SimpleHTTPServer (serves files in the current directory). Test # cases will request metadata and target files that have been pre-generated # in 'tuf/tests/repository_data', which will be served by the @@ -94,10 +89,6 @@ def tearDownClass(cls): # Kills the server subprocess and closes the temp file used for logging. cls.server_process_handler.clean() - # Remove the temporary repository directory, which should contain all the - # metadata, targets, and key files generated for the test cases. - shutil.rmtree(cls.temporary_directory) - @@ -106,13 +97,15 @@ def setUp(self): unittest_toolbox.Modified_TestCase.setUp(self) self.repository_name = 'test_repository1' + # Create a temporary directory to store the repository, metadata, + # and target files. + temporary_repository_root = \ + self.make_temp_directory(directory=os.getcwd()) # Copy the original repository files provided in the test folder so that # any modifications made to repository files are restricted to the copies. # The 'repository_data' directory is expected to exist in 'tuf.tests/'. original_repository_files = os.path.join(os.getcwd(), 'repository_data') - temporary_repository_root = \ - self.make_temp_directory(directory=self.temporary_directory) # The original repository, keystore, and client directories will be copied # for each test case.