def test_get_metadata_filenames(self): # Test normal case. metadata_directory = os.path.join('metadata/') filenames = { 'root.json': metadata_directory + 'root.json', 'targets.json': metadata_directory + 'targets.json', 'snapshot.json': metadata_directory + 'snapshot.json', 'timestamp.json': metadata_directory + 'timestamp.json' } self.assertEqual(filenames, repo_lib.get_metadata_filenames('metadata/')) # If a directory argument is not specified, the current working directory # is used. metadata_directory = os.getcwd() filenames = { 'root.json': os.path.join(metadata_directory, 'root.json'), 'targets.json': os.path.join(metadata_directory, 'targets.json'), 'snapshot.json': os.path.join(metadata_directory, 'snapshot.json'), 'timestamp.json': os.path.join(metadata_directory, 'timestamp.json') } self.assertEqual(filenames, repo_lib.get_metadata_filenames()) # Test improperly formatted argument. self.assertRaises(securesystemslib.exceptions.FormatError, repo_lib.get_metadata_filenames, 3)
def test_get_metadata_filenames(self): # Test normal case. metadata_directory = os.path.join("metadata/") filenames = { "root.json": metadata_directory + "root.json", "targets.json": metadata_directory + "targets.json", "snapshot.json": metadata_directory + "snapshot.json", "timestamp.json": metadata_directory + "timestamp.json", } self.assertEqual(filenames, repo_lib.get_metadata_filenames("metadata/")) # If a directory argument is not specified, the current working directory # is used. metadata_directory = os.getcwd() filenames = { "root.json": os.path.join(metadata_directory, "root.json"), "targets.json": os.path.join(metadata_directory, "targets.json"), "snapshot.json": os.path.join(metadata_directory, "snapshot.json"), "timestamp.json": os.path.join(metadata_directory, "timestamp.json"), } self.assertEqual(filenames, repo_lib.get_metadata_filenames()) # Test improperly formatted argument. self.assertRaises(tuf.FormatError, repo_lib.get_metadata_filenames, 3)
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') metadata_directory = os.path.join(repository_directory, repo_lib.METADATA_STAGED_DIRECTORY_NAME) targets_directory = os.path.join(repository_directory, repo_lib.TARGETS_DIRECTORY_NAME) shutil.copytree(os.path.join('repository_data', 'repository', 'metadata'), metadata_directory) shutil.copytree(os.path.join('repository_data', 'repository', 'targets'), targets_directory) # Add a duplicate signature to the Root file for testing purposes). root_file = os.path.join(metadata_directory, 'root.json') signable = securesystemslib.util.load_json_file(os.path.join(metadata_directory, 'root.json')) signable['signatures'].append(signable['signatures'][0]) repo_lib.write_metadata_file(signable, root_file, 8, False) # Attempt to load a repository that contains a compressed Root file. repository = repo_tool.create_new_repository(repository_directory, repository_name) filenames = repo_lib.get_metadata_filenames(metadata_directory) repo_lib._load_top_level_metadata(repository, filenames, repository_name) filenames = repo_lib.get_metadata_filenames(metadata_directory) repository = repo_tool.create_new_repository(repository_directory, repository_name) repo_lib._load_top_level_metadata(repository, filenames, repository_name) # Partially write all top-level roles (we increase the threshold of each # top-level role so that they are flagged as partially written. repository.root.threshold = repository.root.threshold + 1 repository.snapshot.threshold = repository.snapshot.threshold + 1 repository.targets.threshold = repository.targets.threshold + 1 repository.timestamp.threshold = repository.timestamp.threshold + 1 repository.write('root', ) repository.write('snapshot') repository.write('targets') repository.write('timestamp') repo_lib._load_top_level_metadata(repository, filenames, repository_name) # Attempt to load a repository with missing top-level metadata. for role_file in os.listdir(metadata_directory): if role_file.endswith('.json') and not role_file.startswith('root'): role_filename = os.path.join(metadata_directory, role_file) os.remove(role_filename) repo_lib._load_top_level_metadata(repository, filenames, repository_name) # Remove the required Root file and verify that an exception is raised. os.remove(os.path.join(metadata_directory, 'root.json')) self.assertRaises(tuf.exceptions.RepositoryError, repo_lib._load_top_level_metadata, repository, filenames, repository_name)