Skip to content

Commit cddae3b

Browse files
committed
Updates to TargetFile.get_prefixed_paths()
* Use the same solution for producing the paths as we already do in ngclient * Fix linting issues * Modify the test results according to new code (I believe these are correct, although some cases are so edge cases that disagreement may exist. Most importantly I think the method should always return as many paths as there are hashes listed Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 0eef15a commit cddae3b

2 files changed

Lines changed: 22 additions & 46 deletions

File tree

tests/test_api.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -725,41 +725,33 @@ def test_targetfile_from_data(self) -> None:
725725
targetfile_from_data = TargetFile.from_data(target_file_path, data)
726726
targetfile_from_data.verify_length_and_hashes(data)
727727

728-
def test_targetfile_hash_prefix_paths(self) -> None:
729-
target = TargetFile(
730-
100, {"sha256": "abc", "md5": "def"}, "public/path/file.ext"
728+
def test_targetfile_get_prefixed_paths(self) -> None:
729+
target = TargetFile(100, {"sha256": "abc", "md5": "def"}, "a/b/f.ext")
730+
self.assertEqual(
731+
target.get_prefixed_paths(), ["a/b/abc.f.ext", "a/b/def.f.ext"]
731732
)
732-
self.assertEqual(sorted(target.get_prefixed_paths()), ["public/path/abc.file.ext", "public/path/def.file.ext"])
733733

734-
target = TargetFile(
735-
100, {"sha256": "abc", "md5": "def"}, ""
736-
)
737-
self.assertEqual(sorted(target.get_prefixed_paths()), [])
734+
target = TargetFile(100, {"sha256": "abc", "md5": "def"}, "")
735+
self.assertEqual(target.get_prefixed_paths(), ["abc.", "def."])
738736

739-
target = TargetFile(
740-
100, {"sha256": "abc", "md5": "def"}, "public/path/"
741-
)
742-
self.assertEqual(sorted(target.get_prefixed_paths()), [])
737+
target = TargetFile(100, {"sha256": "abc", "md5": "def"}, "a/b/")
738+
self.assertEqual(target.get_prefixed_paths(), ["a/b/abc.", "a/b/def."])
743739

744-
target = TargetFile(
745-
100, {"sha256": "abc", "md5": "def"}, "file.ext"
740+
target = TargetFile(100, {"sha256": "abc", "md5": "def"}, "f.ext")
741+
self.assertEqual(
742+
target.get_prefixed_paths(), ["abc.f.ext", "def.f.ext"]
746743
)
747-
self.assertEqual(sorted(target.get_prefixed_paths()), ["abc.file.ext", "def.file.ext"])
748744

749-
target = TargetFile(
750-
100, {"sha256": "abc", "md5": "def"}, "public/path/.ext"
745+
target = TargetFile(100, {"sha256": "abc", "md5": "def"}, "a/b/.ext")
746+
self.assertEqual(
747+
target.get_prefixed_paths(), ["a/b/abc..ext", "a/b/def..ext"]
751748
)
752-
self.assertEqual(sorted(target.get_prefixed_paths()), ["public/path/abc..ext", "public/path/def..ext"])
753749

754-
target = TargetFile(
755-
100, {"sha256": "abc"}, "/root/file.ext"
756-
)
757-
self.assertEqual(sorted(target.get_prefixed_paths()), ["/root/abc.file.ext"])
750+
target = TargetFile(100, {"sha256": "abc"}, "/root/file.ext")
751+
self.assertEqual(target.get_prefixed_paths(), ["/root/abc.file.ext"])
758752

759-
target = TargetFile(
760-
100, {"sha256": "abc"}, "/"
761-
)
762-
self.assertEqual(sorted(target.get_prefixed_paths()), [])
753+
target = TargetFile(100, {"sha256": "abc"}, "/")
754+
self.assertEqual(target.get_prefixed_paths(), ["/abc."])
763755

764756
def test_is_delegated_role(self) -> None:
765757
# test path matches

tuf/api/metadata.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import io
3434
import logging
3535
import tempfile
36-
import pathlib
3736
from datetime import datetime
3837
from typing import (
3938
IO,
@@ -1740,28 +1739,13 @@ def verify_length_and_hashes(self, data: Union[bytes, IO[bytes]]) -> None:
17401739

17411740
def get_prefixed_paths(self) -> List[str]:
17421741
"""
1743-
Returns hash-prefixed paths for the given target file path. Empty result in the case of an invalid path.
1744-
1745-
Expects self.path to be a URL path fragment, not a filesystem path.
1742+
Return hash-prefixed URL path fragments for the target file path.
17461743
"""
17471744
paths = []
1748-
try:
1749-
if not self.path:
1750-
raise ValueError
1751-
elif "/" not in self.path:
1752-
parent, name = None, self.path
1753-
else:
1754-
parent, name = self.path.rsplit("/", 1)
1755-
if name == "":
1756-
raise ValueError
1757-
except ValueError:
1758-
return paths
1745+
parent, sep, name = self.path.rpartition("/")
1746+
for hash_value in self.hashes.values():
1747+
paths.append(f"{parent}{sep}{hash_value}.{name}")
17591748

1760-
for hash in self.hashes.values():
1761-
path = f"{hash}.{name}"
1762-
if parent is not None:
1763-
path = f"{parent}/{path}"
1764-
paths.append(path)
17651749
return paths
17661750

17671751

0 commit comments

Comments
 (0)