Skip to content

Commit

Permalink
Add test_invalid_target*
Browse files Browse the repository at this point in the history
Add test cases covering downloading and loading from cache
targets with non-matching hash and length.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
  • Loading branch information
sechkova committed Dec 14, 2021
1 parent 8c7e24b commit 7a902dc
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/test_updater_fetch_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from tests import utils
from tests.repository_simulator import RepositorySimulator
from tuf.exceptions import RepositoryError
from tuf.ngclient import Updater


Expand Down Expand Up @@ -105,6 +106,55 @@ def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
self.assertEqual(path, updater.find_cached_target(info))
self.assertEqual(path, updater.find_cached_target(info, path))

def test_invalid_target_download(self) -> None:
targetpath = "targetpath"
# Add target to repository
self.sim.targets.version += 1
self.sim.add_target("targets", b"content", targetpath)
self.sim.update_snapshot()

updater = self._init_updater()
info = updater.get_targetinfo(targetpath)
assert info is not None

# Corrupt the file content to not match the hash
self.sim.target_files[targetpath].data = b"conten@"
with self.assertRaises(RepositoryError):
updater.download_target(info)

# Corrupt the file content to not match the length
self.sim.target_files[targetpath].data = b"cont"
with self.assertRaises(RepositoryError):
updater.download_target(info)

# Verify the file is not persisted in cache
self.assertIsNone(updater.find_cached_target(info))

def test_invalid_target_cache(self) -> None:
targetpath = "targetpath"
# Add target to repository
self.sim.targets.version += 1
self.sim.add_target("targets", b"content", targetpath)
self.sim.update_snapshot()

# Download the target
updater = self._init_updater()
info = updater.get_targetinfo(targetpath)
assert info is not None
path = updater.download_target(info)
self.assertEqual(path, updater.find_cached_target(info))

# Add newer content to the same targetpath
self.sim.targets.version += 1
self.sim.add_target("targets", b"contentv2", targetpath)
self.sim.update_snapshot()

# Newer content is detected, old cached version is not used
updater = self._init_updater()
info = updater.get_targetinfo(targetpath)
assert info is not None
self.assertIsNone(updater.find_cached_target(info))


if __name__ == "__main__":
if "--dump" in sys.argv:
Expand Down

0 comments on commit 7a902dc

Please sign in to comment.