|
14 | 14 |
|
15 | 15 | from tests import utils
|
16 | 16 | from tests.repository_simulator import RepositorySimulator
|
| 17 | +from tuf.exceptions import RepositoryError |
17 | 18 | from tuf.ngclient import Updater
|
18 | 19 |
|
19 | 20 |
|
@@ -105,6 +106,63 @@ def test_fetch_target(self, test_case_data: Tuple[str, bytes, str]) -> None:
|
105 | 106 | self.assertEqual(path, updater.find_cached_target(info))
|
106 | 107 | self.assertEqual(path, updater.find_cached_target(info, path))
|
107 | 108 |
|
| 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 | + |
108 | 166 |
|
109 | 167 | if __name__ == "__main__":
|
110 | 168 | if "--dump" in sys.argv:
|
|
0 commit comments