Skip to content

Commit

Permalink
Merge pull request #1040 from trailofbits/ww/return-bin-name-when-del…
Browse files Browse the repository at this point in the history
…egating

tuf/repository_tool: Return delegated bin_name during modifications
  • Loading branch information
mnm678 authored Jun 1, 2020
2 parents a4b52e7 + 1e532e8 commit a354fc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
26 changes: 15 additions & 11 deletions tests/test_repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,10 +1495,10 @@ def test_add_target_to_bin(self):

# 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, 16)
rolename = self.targets_object.add_target_to_bin(target1_filepath, 16)

for delegation in self.targets_object.delegations:
if delegation.rolename == '5':
if delegation.rolename == rolename:
self.assertTrue('file1.txt' in delegation.target_files)

else:
Expand All @@ -1513,7 +1513,7 @@ def test_add_target_to_bin(self):
target1_filepath, 16)

# Test for a required hashed bin that does not exist.
self.targets_object.revoke('5')
self.targets_object.revoke(rolename)
self.assertRaises(securesystemslib.exceptions.Error,
self.targets_object.add_target_to_bin,
target1_filepath, 16)
Expand All @@ -1523,10 +1523,11 @@ def test_add_target_to_bin(self):
target2_fileinfo = tuf.formats.make_fileinfo(37, target2_hashes)
target2_filepath = 'file2.txt'

self.targets_object.add_target_to_bin(target2_filepath, 16, fileinfo=target2_fileinfo)
rolename = self.targets_object.add_target_to_bin(target2_filepath, 16,
fileinfo=target2_fileinfo)

for delegation in self.targets_object.delegations:
if delegation.rolename == '0':
if delegation.rolename == rolename:
self.assertTrue(target2_filepath in delegation.target_files)

else:
Expand Down Expand Up @@ -1561,18 +1562,19 @@ def test_remove_target_from_bin(self):

# 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, 16)
added_rolename = self.targets_object.add_target_to_bin(target1_filepath, 16)

for delegation in self.targets_object.delegations:
if delegation.rolename == '5':
if delegation.rolename == added_rolename:
self.assertTrue('file1.txt' in delegation.target_files)
self.assertTrue(len(delegation.target_files) == 1)
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, 16)
removed_rolename = self.targets_object.remove_target_from_bin(target1_filepath, 16)
self.assertEqual(added_rolename, removed_rolename)

for delegation in self.targets_object.delegations:
self.assertTrue(target1_filepath not in delegation.target_files)
Expand Down Expand Up @@ -1608,17 +1610,19 @@ def test_default_bin_num(self):

# 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(os.path.basename(target1_filepath))
added_rolename = self.targets_object.add_target_to_bin(os.path.basename(target1_filepath))

for delegation in self.targets_object.delegations:
if delegation.rolename == '558-55b':
if delegation.rolename == added_rolename:
self.assertTrue('file1.txt' in delegation.target_files)

else:
self.assertFalse('file1.txt' in delegation.target_files)

# Remove target1_filepath and verify that all bins are now empty
self.targets_object.remove_target_from_bin(os.path.basename(target1_filepath))
removed_rolename = self.targets_object.remove_target_from_bin(
os.path.basename(target1_filepath))
self.assertEqual(added_rolename, removed_rolename)

for delegation in self.targets_object.delegations:
self.assertEqual(delegation.target_files, {})
Expand Down
7 changes: 4 additions & 3 deletions tuf/repository_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
object.
<Returns>
None.
The name of the hashed bin that the target was added to.
"""

# Do the arguments have the correct format?
Expand All @@ -2689,6 +2689,7 @@ def add_target_to_bin(self, target_filepath, number_of_bins=DEFAULT_NUM_BINS,
self._delegated_roles[bin_name].add_target(target_filepath,
fileinfo=fileinfo)

return bin_name



Expand Down Expand Up @@ -2727,7 +2728,7 @@ def remove_target_from_bin(self, target_filepath,
Targets object.
<Returns>
None.
The name of the hashed bin that the target was added to.
"""

# Do the arguments have the correct format?
Expand All @@ -2749,7 +2750,7 @@ def remove_target_from_bin(self, target_filepath,

self._delegated_roles[bin_name].remove_target(target_filepath)


return bin_name


@property
Expand Down

0 comments on commit a354fc0

Please sign in to comment.