Skip to content

Commit adcaf58

Browse files
committed
Add test_invalid_target*
Add test cases covering downloading and loading from cache targets with non-matching hash and length. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent e513460 commit adcaf58

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/test_updater_fetch_target.py

+58
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from tests import utils
1616
from tests.repository_simulator import RepositorySimulator
17+
from tuf.exceptions import RepositoryError
1718
from tuf.ngclient import Updater
1819

1920

@@ -105,6 +106,63 @@ def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
105106
self.assertEqual(path, updater.find_cached_target(info))
106107
self.assertEqual(path, updater.find_cached_target(info, path))
107108

109+
def test_invalid_target_download(self) -> None:
110+
targetpath = "targetpath"
111+
# Add target to repository
112+
self.sim.targets.version += 1
113+
self.sim.add_target("targets", b"content", targetpath)
114+
self.sim.update_snapshot()
115+
116+
updater = self._init_updater()
117+
info = updater.get_targetinfo(targetpath)
118+
assert info is not None
119+
120+
# Corrupt the file content to not match the hash
121+
self.sim.target_files[targetpath].data = b"conten@"
122+
with self.assertRaises(RepositoryError):
123+
updater.download_target(info)
124+
125+
# Corrupt the file content to not match the length
126+
self.sim.target_files[targetpath].data = b"cont"
127+
with self.assertRaises(RepositoryError):
128+
updater.download_target(info)
129+
130+
# Verify the file is not persisted in cache
131+
self.assertIsNone(updater.find_cached_target(info))
132+
133+
def test_invalid_target_cache(self) -> None:
134+
targetpath = "targetpath"
135+
content = b"content"
136+
# Add target to repository
137+
self.sim.targets.version += 1
138+
self.sim.add_target("targets", content, targetpath)
139+
self.sim.update_snapshot()
140+
141+
# Download the target
142+
updater = self._init_updater()
143+
info = updater.get_targetinfo(targetpath)
144+
assert info is not None
145+
path = updater.download_target(info)
146+
self.assertEqual(path, updater.find_cached_target(info))
147+
148+
# Add newer content to the same targetpath
149+
content = b"contentv2"
150+
self.sim.targets.version += 1
151+
self.sim.add_target("targets", content, targetpath)
152+
self.sim.update_snapshot()
153+
154+
# Newer content is detected, old cached version is not used
155+
updater = self._init_updater()
156+
info = updater.get_targetinfo(targetpath)
157+
assert info is not None
158+
self.assertIsNone(updater.find_cached_target(info))
159+
160+
# Download target, assert it is in cache and content is the newer
161+
path = updater.download_target(info)
162+
self.assertEqual(path, updater.find_cached_target(info))
163+
with open(path, "rb") as f:
164+
self.assertEqual(f.read(), content)
165+
108166

109167
if __name__ == "__main__":
110168
if "--dump" in sys.argv:

0 commit comments

Comments
 (0)