mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Merge branch 'dachshund-remove_target_from_bin' into develop
This commit is contained in:
commit
5102dda144
2 changed files with 145 additions and 16 deletions
|
|
@ -901,8 +901,7 @@ def test_add_target(self):
|
|||
|
||||
|
||||
# Test improperly formatted arguments.
|
||||
self.assertRaises(tuf.FormatError, self.targets_object.add_target,
|
||||
3)
|
||||
self.assertRaises(tuf.FormatError, self.targets_object.add_target, 3)
|
||||
|
||||
|
||||
# Test invalid filepath argument (i.e., non-existent or invalid file.)
|
||||
|
|
@ -916,8 +915,7 @@ def test_add_target(self):
|
|||
# Not a file (i.e., a valid path, but a directory.)
|
||||
test_directory = os.path.join(self.targets_directory, 'test_directory')
|
||||
os.mkdir(test_directory)
|
||||
self.assertRaises(tuf.Error, self.targets_object.add_target,
|
||||
test_directory)
|
||||
self.assertRaises(tuf.Error, self.targets_object.add_target, test_directory)
|
||||
|
||||
|
||||
|
||||
|
|
@ -968,8 +966,7 @@ def test_remove_target(self):
|
|||
|
||||
|
||||
# Test improperly formatted arguments.
|
||||
self.assertRaises(tuf.FormatError, self.targets_object.remove_target,
|
||||
3)
|
||||
self.assertRaises(tuf.FormatError, self.targets_object.remove_target, 3)
|
||||
|
||||
|
||||
# Test invalid filepath argument (i.e., non-existent or invalid file.)
|
||||
|
|
@ -1167,6 +1164,61 @@ def test_add_target_to_bin(self):
|
|||
# Invalid target file path argument.
|
||||
self.assertRaises(tuf.Error,
|
||||
self.targets_object.add_target_to_bin, '/non-existent')
|
||||
|
||||
|
||||
|
||||
def test_remove_target_from_bin(self):
|
||||
# Test normal case.
|
||||
# Delegate the hashed bins so that add_target_to_bin() can be tested.
|
||||
keystore_directory = os.path.join('repository_data', 'keystore')
|
||||
public_keypath = os.path.join(keystore_directory, 'targets_key.pub')
|
||||
public_key = repo_tool.import_rsa_publickey_from_file(public_keypath)
|
||||
target1_filepath = os.path.join(self.targets_directory, 'file1.txt')
|
||||
|
||||
# Set needed arguments by delegate_hashed_bins().
|
||||
public_keys = [public_key]
|
||||
list_of_targets = [target1_filepath]
|
||||
|
||||
# Delegate to hashed bins. The target filepath to be tested is expected
|
||||
# to contain a hash prefix of 'e', so it should be added to the
|
||||
# 'targets/e' role.
|
||||
self.targets_object.delegate_hashed_bins(list_of_targets, public_keys,
|
||||
number_of_bins=16)
|
||||
|
||||
# Ensure each hashed bin initially contains zero targets.
|
||||
for delegation in self.targets_object.delegations:
|
||||
self.assertTrue(target1_filepath not in delegation.target_files)
|
||||
|
||||
# Add 'target1_filepath' and verify that the relative path of
|
||||
# 'target1_filepath' is added to the correct bin.
|
||||
self.targets_object.add_target_to_bin(target1_filepath)
|
||||
|
||||
for delegation in self.targets_object.delegations:
|
||||
if delegation.rolename == 'targets/e':
|
||||
self.assertTrue('/file1.txt' in delegation.target_files)
|
||||
|
||||
else:
|
||||
self.assertTrue('/file1.txt' not in delegation.target_files)
|
||||
|
||||
# Test the remove_target_from_bin() method. Verify that 'target1_filepath'
|
||||
# has been removed.
|
||||
self.targets_object.remove_target_from_bin(target1_filepath)
|
||||
|
||||
for delegation in self.targets_object.delegations:
|
||||
if delegation.rolename == 'targets/e':
|
||||
self.assertTrue('/file1.txt' not in delegation.target_files)
|
||||
|
||||
else:
|
||||
self.assertTrue('/file1.txt' not in delegation.target_files)
|
||||
|
||||
|
||||
# Test improperly formatted argument.
|
||||
self.assertRaises(tuf.FormatError,
|
||||
self.targets_object.remove_target_from_bin, 3)
|
||||
|
||||
# Invalid target file path argument.
|
||||
self.assertRaises(tuf.Error, self.targets_object.remove_target_from_bin,
|
||||
'/non-existent')
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2335,7 +2335,7 @@ def delegate_hashed_bins(self, list_of_targets, keys_of_hashed_bins,
|
|||
def add_target_to_bin(self, target_filepath):
|
||||
"""
|
||||
<Purpose>
|
||||
Add the fileinfo of 'target_filepath' to the expected hashed bin if
|
||||
Add the fileinfo of 'target_filepath' to the expected hashed bin, if
|
||||
the bin is available. The hashed bin should have been created by
|
||||
{targets_role}.delegate_hashed_bins(). Assuming the target filepath
|
||||
falls under the repository's targets directory, determine the filepath's
|
||||
|
|
@ -2344,10 +2344,6 @@ def add_target_to_bin(self, target_filepath):
|
|||
the 'targets/unclaimed/58-5f.json' role's list of targets by calling this
|
||||
method.
|
||||
|
||||
>>>
|
||||
>>>
|
||||
>>>
|
||||
|
||||
<Arguments>
|
||||
target_filepath:
|
||||
The filepath of the target to be added to a hashed bin. The filepath
|
||||
|
|
@ -2373,6 +2369,84 @@ def add_target_to_bin(self, target_filepath):
|
|||
# types, and that all dict keys are properly named.
|
||||
# Raise 'tuf.FormatError' if there is a mismatch.
|
||||
tuf.formats.PATH_SCHEMA.check_match(target_filepath)
|
||||
|
||||
return self._locate_and_update_target_in_bin(target_filepath, 'add_target')
|
||||
|
||||
|
||||
|
||||
def remove_target_from_bin(self, target_filepath):
|
||||
"""
|
||||
<Purpose>
|
||||
Remove the fileinfo of 'target_filepath' from the expected hashed bin, if
|
||||
the bin is available. The hashed bin should have been created by
|
||||
{targets_role}.delegate_hashed_bins(). Assuming the target filepath
|
||||
falls under the repository's targets directory, determine the filepath's
|
||||
hash prefix, locate the expected bin (if any), and then remove the
|
||||
fileinfo from the expected bin. Example: 'targets/foo.tar.gz' may be
|
||||
removed from the 'targets/unclaimed/58-5f.json' role's list of targets by
|
||||
calling this method.
|
||||
|
||||
<Arguments>
|
||||
target_filepath:
|
||||
The filepath of the target to be added to a hashed bin. The filepath
|
||||
must fall under repository's targets directory.
|
||||
|
||||
<Exceptions>
|
||||
tuf.FormatError, if 'target_filepath' is improperly formatted.
|
||||
|
||||
tuf.Error, if 'target_filepath' cannot be removed from a hashed bin
|
||||
(e.g., an invalid target filepath, or the expected hashed bin does not
|
||||
exist.)
|
||||
|
||||
<Side Effects>
|
||||
The fileinfo of 'target_filepath' is removed from a hashed bin of this
|
||||
Targets object.
|
||||
|
||||
<Returns>
|
||||
None.
|
||||
"""
|
||||
|
||||
# Do the arguments have the correct format?
|
||||
# Ensure the arguments have the appropriate number of objects and object
|
||||
# types, and that all dict keys are properly named.
|
||||
# Raise 'tuf.FormatError' if there is a mismatch.
|
||||
tuf.formats.PATH_SCHEMA.check_match(target_filepath)
|
||||
|
||||
return self._locate_and_update_target_in_bin(target_filepath, 'remove_target')
|
||||
|
||||
|
||||
|
||||
def _locate_and_update_target_in_bin(self, target_filepath, method_name):
|
||||
"""
|
||||
<Purpose>
|
||||
Assuming the target filepath falls under the repository's targets
|
||||
directory, determine the filepath's hash prefix, locate the expected bin
|
||||
(if any), and then call the 'method_name' method of the expected hashed
|
||||
bin role.
|
||||
|
||||
<Arguments>
|
||||
target_filepath:
|
||||
The filepath of the target that may be specified in one of the hashed
|
||||
bins. 'target_filepath' must fall under repository's targets directory.
|
||||
|
||||
method_name:
|
||||
A supported method, in string format, of the Targets() class. For
|
||||
example, add_target() and remove_target():
|
||||
|
||||
repository.targets('unclaimed')('58-f7).add_target(target_filepath)
|
||||
repository.targets('unclaimed')('000-007).remove_target(target_filepath)
|
||||
|
||||
<Exceptions>
|
||||
tuf.Error, if 'target_filepath' cannot be updated (e.g., an invalid target
|
||||
filepath, or the expected hashed bin does not exist.)
|
||||
|
||||
<Side Effects>
|
||||
The fileinfo of 'target_filepath' is added to a hashed bin of this Targets
|
||||
object.
|
||||
|
||||
<Returns>
|
||||
None.
|
||||
"""
|
||||
|
||||
# Determine the prefix length of any one of the hashed bins. The prefix
|
||||
# length is not stored in the roledb, so it must be determined here by
|
||||
|
|
@ -2399,8 +2473,8 @@ def add_target_to_bin(self, target_filepath):
|
|||
# Ensure the filepath falls under the repository's targets directory.
|
||||
filepath = os.path.abspath(target_filepath)
|
||||
if not filepath.startswith(self._targets_directory + os.sep):
|
||||
message = repr(filepath)+' is not under the Repository\'s targets '+\
|
||||
'directory: '+repr(self._targets_directory)
|
||||
message = repr(filepath) + ' is not under the Repository\'s targets ' +\
|
||||
'directory: ' + repr(self._targets_directory)
|
||||
raise tuf.Error(message)
|
||||
|
||||
# Determine the hash prefix of 'target_path' by computing the digest of
|
||||
|
|
@ -2427,11 +2501,14 @@ def add_target_to_bin(self, target_filepath):
|
|||
# 'self._delegated_roles' is keyed by relative rolenames, so update
|
||||
# 'hashed_bin_name'.
|
||||
if hashed_bin_name is not None:
|
||||
hashed_bin_name = hashed_bin_name[len(self.rolename)+1:]
|
||||
self._delegated_roles[hashed_bin_name].add_target(target_filepath)
|
||||
hashed_bin_name = hashed_bin_name[len(self.rolename) + 1:]
|
||||
|
||||
# 'method_name' should be one of the supported methods of the Targets()
|
||||
# class.
|
||||
getattr(self._delegated_roles[hashed_bin_name], method_name)(target_filepath)
|
||||
|
||||
else:
|
||||
raise tuf.Error(target_filepath + ' cannot be added to any bins.')
|
||||
raise tuf.Error(target_filepath + ' not found in any of the bins.')
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue