-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathtest_updater_fetch_target.py
More file actions
231 lines (184 loc) · 8.16 KB
/
test_updater_fetch_target.py
File metadata and controls
231 lines (184 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Copyright 2021, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""Test 'Fetch target' from 'Detailed client workflow' as well as
target files storing/loading from cache.
"""
from __future__ import annotations
import os
import sys
import tempfile
import unittest
from dataclasses import dataclass
from tests import utils
from tests.repository_simulator import RepositorySimulator
from tuf.api.exceptions import RepositoryError
from tuf.api.metadata import DelegatedRole, Delegations
from tuf.ngclient import Updater
@dataclass
class TestTarget:
path: str
content: bytes
encoded_path: str
class TestFetchTarget(unittest.TestCase):
"""Test ngclient downloading and caching target files."""
# set dump_dir to trigger repository state dumps
dump_dir: str | None = None
def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
self.metadata_dir = os.path.join(self.temp_dir.name, "metadata")
self.targets_dir = os.path.join(self.temp_dir.name, "targets")
os.mkdir(self.metadata_dir)
os.mkdir(self.targets_dir)
# Setup the repository
self.sim = RepositorySimulator()
if self.dump_dir is not None:
# create test specific dump directory
name = self.id().split(".")[-1]
self.sim.dump_dir = os.path.join(self.dump_dir, name)
os.mkdir(self.sim.dump_dir)
def tearDown(self) -> None:
self.temp_dir.cleanup()
def _init_updater(self) -> Updater:
"""Creates a new updater instance."""
if self.sim.dump_dir is not None:
self.sim.write()
return Updater(
self.metadata_dir,
"https://example.com/metadata/",
self.targets_dir,
"https://example.com/targets/",
self.sim,
bootstrap=self.sim.signed_roots[0],
)
targets = {
"standard case": TestTarget(
path="targetpath",
content=b"target content",
encoded_path="targetpath",
),
"non-asci case": TestTarget(
path="åäö",
content=b"more content",
encoded_path="%C3%A5%C3%A4%C3%B6",
),
"subdirectory case": TestTarget(
path="a/b/c/targetpath",
content=b"dir target content",
encoded_path="a%2Fb%2Fc%2Ftargetpath",
),
}
@utils.run_sub_tests_with_dataset(targets)
def test_fetch_target(self, target: TestTarget) -> None:
path = os.path.join(self.targets_dir, target.encoded_path)
updater = self._init_updater()
# target does not exist yet
self.assertIsNone(updater.get_targetinfo(target.path))
# Add targets to repository
self.sim.targets.version += 1
self.sim.add_target("targets", target.content, target.path)
self.sim.update_snapshot()
updater = self._init_updater()
# target now exists, is not in cache yet
info = updater.get_targetinfo(target.path)
assert info is not None
# Test without and with explicit local filepath
self.assertIsNone(updater.find_cached_target(info))
self.assertIsNone(updater.find_cached_target(info, path))
# download target, assert it is in cache and content is correct
self.assertEqual(path, updater.download_target(info))
self.assertEqual(path, updater.find_cached_target(info))
self.assertEqual(path, updater.find_cached_target(info, path))
with open(path, "rb") as f:
self.assertEqual(f.read(), target.content)
# download using explicit filepath as well
os.remove(path)
self.assertEqual(path, updater.download_target(info, path))
self.assertEqual(path, updater.find_cached_target(info))
self.assertEqual(path, updater.find_cached_target(info, path))
def test_download_targets_with_succinct_roles(self) -> None:
self.sim.add_succinct_roles("targets", 8, "bin")
self.sim.update_snapshot()
assert self.sim.targets.delegations is not None
assert self.sim.targets.delegations.succinct_roles is not None
succinct_roles = self.sim.targets.delegations.succinct_roles
# Add lots of targets with unique data to imitate a real repository.
for i in range(20):
target_name = f"target-{i}"
target_bin = succinct_roles.get_role_for_target(target_name)
self.sim.add_target(
target_bin, bytes(target_name, "utf-8"), target_name
)
# download each target
updater = self._init_updater()
for i in range(20):
target_name = f"target-{i}"
# Verify that the target info was successfully found.
target_info = updater.get_targetinfo(target_name)
assert target_info is not None
target_full_path = updater.download_target(target_info)
# Verify that the target content is the same as the target name.
with open(target_full_path, encoding="utf-8") as target:
self.assertEqual(target.read(), target_name)
def test_invalid_target_download(self) -> None:
target = TestTarget("targetpath", b"content", "targetpath")
# Add target to repository
self.sim.targets.version += 1
self.sim.add_target("targets", target.content, target.path)
self.sim.update_snapshot()
updater = self._init_updater()
info = updater.get_targetinfo(target.path)
assert info is not None
# Corrupt the file content to not match the hash
self.sim.target_files[target.path].data = b"conten@"
with self.assertRaises(RepositoryError):
updater.download_target(info)
# Corrupt the file content to not match the length
self.sim.target_files[target.path].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:
target = TestTarget("targetpath", b"content", "targetpath")
# Add target to repository
self.sim.targets.version += 1
self.sim.add_target("targets", target.content, target.path)
self.sim.update_snapshot()
# Download the target
updater = self._init_updater()
info = updater.get_targetinfo(target.path)
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
target.content = b"contentv2"
self.sim.targets.version += 1
self.sim.add_target("targets", target.content, target.path)
self.sim.update_snapshot()
# Newer content is detected, old cached version is not used
updater = self._init_updater()
info = updater.get_targetinfo(target.path)
assert info is not None
self.assertIsNone(updater.find_cached_target(info))
# Download target, assert it is in cache and content is the newer
path = updater.download_target(info)
self.assertEqual(path, updater.find_cached_target(info))
with open(path, "rb") as f:
self.assertEqual(f.read(), target.content)
def test_meta_missing_delegated_role(self) -> None:
"""Test a delegation where the role is not part of the snapshot"""
# Add new delegation, update snapshot. Do not add the actual role
role = DelegatedRole("role1", [], 1, True, ["*"])
self.sim.targets.delegations = Delegations({}, roles={role.name: role})
self.sim.update_snapshot()
# assert that RepositoryError is raised when role1 is needed
updater = self._init_updater()
with self.assertRaises(RepositoryError):
updater.get_targetinfo("")
if __name__ == "__main__":
if "--dump" in sys.argv:
TestFetchTarget.dump_dir = tempfile.mkdtemp()
print(f"Repository Simulator dumps in {TestFetchTarget.dump_dir}")
sys.argv.remove("--dump")
utils.configure_test_logging(sys.argv)
unittest.main()