| 1 |
# Copyright 2021, New York University and the TUF contributors |
| 2 |
# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 |
|
| 4 |
"""Test updating delegated targets roles and searching for |
| 5 |
target files with various delegation graphs""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import os |
| 10 |
import sys |
| 11 |
import tempfile |
| 12 |
import unittest |
| 13 |
from dataclasses import astuple, dataclass, field |
| 14 |
from typing import TYPE_CHECKING |
| 15 |
|
| 16 |
from tests import utils |
| 17 |
from tests.repository_simulator import RepositorySimulator |
| 18 |
from tuf.api.exceptions import UnsignedMetadataError |
| 19 |
from tuf.api.metadata import ( |
| 20 |
SPECIFICATION_VERSION, |
| 21 |
TOP_LEVEL_ROLE_NAMES, |
| 22 |
DelegatedRole, |
| 23 |
Targets, |
| 24 |
) |
| 25 |
from tuf.ngclient import Updater |
| 26 |
|
| 27 |
if TYPE_CHECKING: |
| 28 |
from collections.abc import Iterable |
| 29 |
|
| 30 |
|
| 31 |
@dataclass |
| 32 |
class TestDelegation: |
| 33 |
delegator: str |
| 34 |
rolename: str |
| 35 |
keyids: list[str] = field(default_factory=list) |
| 36 |
threshold: int = 1 |
| 37 |
terminating: bool = False |
| 38 |
paths: list[str] | None = field(default_factory=lambda: ["*"]) |
| 39 |
path_hash_prefixes: list[str] | None = None |
| 40 |
|
| 41 |
|
| 42 |
@dataclass |
| 43 |
class TestTarget: |
| 44 |
rolename: str |
| 45 |
content: bytes |
| 46 |
targetpath: str |
| 47 |
|
| 48 |
|
| 49 |
@dataclass |
| 50 |
class DelegationsTestCase: |
| 51 |
"""A delegations graph as lists of delegations and target files |
| 52 |
and the expected order of traversal as a list of role names.""" |
| 53 |
|
| 54 |
delegations: list[TestDelegation] |
| 55 |
target_files: list[TestTarget] = field(default_factory=list) |
| 56 |
visited_order: list[str] = field(default_factory=list) |
| 57 |
|
| 58 |
|
| 59 |
@dataclass |
| 60 |
class TargetTestCase: |
| 61 |
targetpath: str |
| 62 |
found: bool |
| 63 |
visited_order: list[str] = field(default_factory=list) |
| 64 |
|
| 65 |
|
| 66 |
class TestDelegations(unittest.TestCase): |
| 67 |
"""Base class for delegation tests""" |
| 68 |
|
| 69 |
# set dump_dir to trigger repository state dumps |
| 70 |
dump_dir: str | None = None |
| 71 |
|
| 72 |
def setUp(self) -> None: |
| 73 |
self.subtest_count = 0 |
| 74 |
self.temp_dir = tempfile.TemporaryDirectory() |
| 75 |
self.metadata_dir = os.path.join(self.temp_dir.name, "metadata") |
| 76 |
self.targets_dir = os.path.join(self.temp_dir.name, "targets") |
| 77 |
os.mkdir(self.metadata_dir) |
| 78 |
os.mkdir(self.targets_dir) |
| 79 |
self.sim: RepositorySimulator |
| 80 |
|
| 81 |
def tearDown(self) -> None: |
| 82 |
self.temp_dir.cleanup() |
| 83 |
|
| 84 |
def setup_subtest(self) -> None: |
| 85 |
self.subtest_count += 1 |
| 86 |
if self.dump_dir is not None: |
| 87 |
# create subtest dumpdir |
| 88 |
name = f"{self.id().split('.')[-1]}-{self.subtest_count}" |
| 89 |
self.sim.dump_dir = os.path.join(self.dump_dir, name) |
| 90 |
os.mkdir(self.sim.dump_dir) |
| 91 |
# dump the repo simulator metadata |
| 92 |
self.sim.write() |
| 93 |
|
| 94 |
def teardown_subtest(self) -> None: |
| 95 |
utils.cleanup_metadata_dir(self.metadata_dir) |
| 96 |
|
| 97 |
def _init_repo(self, test_case: DelegationsTestCase) -> None: |
| 98 |
"""Create a new RepositorySimulator instance and |
| 99 |
populate it with delegations and target files""" |
| 100 |
|
| 101 |
self.sim = RepositorySimulator() |
| 102 |
spec_version = ".".join(SPECIFICATION_VERSION) |
| 103 |
for d in test_case.delegations: |
| 104 |
if d.rolename in self.sim.md_delegates: |
| 105 |
targets = self.sim.md_delegates[d.rolename].signed |
| 106 |
else: |
| 107 |
targets = Targets( |
| 108 |
1, spec_version, self.sim.safe_expiry, {}, None |
| 109 |
) |
| 110 |
# unpack 'd' but skip "delegator" |
| 111 |
role = DelegatedRole(*astuple(d)[1:]) |
| 112 |
self.sim.add_delegation(d.delegator, role, targets) |
| 113 |
|
| 114 |
for target in test_case.target_files: |
| 115 |
self.sim.add_target(*astuple(target)) |
| 116 |
|
| 117 |
if test_case.target_files: |
| 118 |
self.sim.targets.version += 1 |
| 119 |
self.sim.update_snapshot() |
| 120 |
|
| 121 |
def _init_updater(self) -> Updater: |
| 122 |
"""Create a new Updater instance""" |
| 123 |
return Updater( |
| 124 |
self.metadata_dir, |
| 125 |
"https://example.com/metadata/", |
| 126 |
self.targets_dir, |
| 127 |
"https://example.com/targets/", |
| 128 |
self.sim, |
| 129 |
bootstrap=self.sim.signed_roots[0], |
| 130 |
) |
| 131 |
|
| 132 |
def _assert_files_exist(self, roles: Iterable[str]) -> None: |
| 133 |
"""Assert that local metadata files match 'roles'""" |
| 134 |
expected_files = [f"{role}.json" for role in roles] |
| 135 |
found_files = [ |
| 136 |
e.name for e in os.scandir(self.metadata_dir) if e.is_file() |
| 137 |
] |
| 138 |
|
| 139 |
self.assertListEqual(sorted(found_files), sorted(expected_files)) |
| 140 |
|
| 141 |
|
| 142 |
class TestDelegationsGraphs(TestDelegations): |
| 143 |
"""Test creating delegations graphs with different complexity |
| 144 |
and successfully updating the delegated roles metadata""" |
| 145 |
|
| 146 |
graphs = { |
| 147 |
"basic delegation": DelegationsTestCase( |
| 148 |
delegations=[TestDelegation("targets", "A")], |
| 149 |
visited_order=["A"], |
| 150 |
), |
| 151 |
"single level delegations": DelegationsTestCase( |
| 152 |
delegations=[ |
| 153 |
TestDelegation("targets", "A"), |
| 154 |
TestDelegation("targets", "B"), |
| 155 |
], |
| 156 |
visited_order=["A", "B"], |
| 157 |
), |
| 158 |
"two-level delegations": DelegationsTestCase( |
| 159 |
delegations=[ |
| 160 |
TestDelegation("targets", "A"), |
| 161 |
TestDelegation("targets", "B"), |
| 162 |
TestDelegation("B", "C"), |
| 163 |
], |
| 164 |
visited_order=["A", "B", "C"], |
| 165 |
), |
| 166 |
"two-level test DFS order of traversal": DelegationsTestCase( |
| 167 |
delegations=[ |
| 168 |
TestDelegation("targets", "A"), |
| 169 |
TestDelegation("targets", "B"), |
| 170 |
TestDelegation("A", "C"), |
| 171 |
TestDelegation("A", "D"), |
| 172 |
], |
| 173 |
visited_order=["A", "C", "D", "B"], |
| 174 |
), |
| 175 |
"three-level delegation test DFS order of traversal": DelegationsTestCase( |
| 176 |
delegations=[ |
| 177 |
TestDelegation("targets", "A"), |
| 178 |
TestDelegation("targets", "B"), |
| 179 |
TestDelegation("A", "C"), |
| 180 |
TestDelegation("C", "D"), |
| 181 |
], |
| 182 |
visited_order=["A", "C", "D", "B"], |
| 183 |
), |
| 184 |
"two-level terminating ignores all but role's descendants": DelegationsTestCase( |
| 185 |
delegations=[ |
| 186 |
TestDelegation("targets", "A"), |
| 187 |
TestDelegation("targets", "B"), |
| 188 |
TestDelegation("A", "C", terminating=True), |
| 189 |
TestDelegation("A", "D"), |
| 190 |
], |
| 191 |
visited_order=["A", "C"], |
| 192 |
), |
| 193 |
"three-level terminating ignores all but role's descendants": DelegationsTestCase( |
| 194 |
delegations=[ |
| 195 |
TestDelegation("targets", "A"), |
| 196 |
TestDelegation("targets", "B"), |
| 197 |
TestDelegation("A", "C", terminating=True), |
| 198 |
TestDelegation("C", "D"), |
| 199 |
], |
| 200 |
visited_order=["A", "C", "D"], |
| 201 |
), |
| 202 |
"two-level ignores all branches not matching 'paths'": DelegationsTestCase( |
| 203 |
delegations=[ |
| 204 |
TestDelegation("targets", "A", paths=["*.py"]), |
| 205 |
TestDelegation("targets", "B"), |
| 206 |
TestDelegation("A", "C"), |
| 207 |
], |
| 208 |
visited_order=["B"], |
| 209 |
), |
| 210 |
"three-level ignores all branches not matching 'paths'": DelegationsTestCase( |
| 211 |
delegations=[ |
| 212 |
TestDelegation("targets", "A"), |
| 213 |
TestDelegation("targets", "B"), |
| 214 |
TestDelegation("A", "C", paths=["*.py"]), |
| 215 |
TestDelegation("C", "D"), |
| 216 |
], |
| 217 |
visited_order=["A", "B"], |
| 218 |
), |
| 219 |
"cyclic graph": DelegationsTestCase( |
| 220 |
delegations=[ |
| 221 |
TestDelegation("targets", "A"), |
| 222 |
TestDelegation("targets", "B"), |
| 223 |
TestDelegation("B", "C"), |
| 224 |
TestDelegation("C", "D"), |
| 225 |
TestDelegation("D", "B"), |
| 226 |
], |
| 227 |
visited_order=["A", "B", "C", "D"], |
| 228 |
), |
| 229 |
"two roles delegating to a third": DelegationsTestCase( |
| 230 |
delegations=[ |
| 231 |
TestDelegation("targets", "A"), |
| 232 |
TestDelegation("targets", "B"), |
| 233 |
TestDelegation("B", "C"), |
| 234 |
TestDelegation("A", "C"), |
| 235 |
], |
| 236 |
# Under all same conditions, 'C' is reached through 'A' first" |
| 237 |
visited_order=["A", "C", "B"], |
| 238 |
), |
| 239 |
"two roles delegating to a third different 'paths'": DelegationsTestCase( |
| 240 |
delegations=[ |
| 241 |
TestDelegation("targets", "A"), |
| 242 |
TestDelegation("targets", "B"), |
| 243 |
TestDelegation("B", "C"), |
| 244 |
TestDelegation("A", "C", paths=["*.py"]), |
| 245 |
], |
| 246 |
# 'C' is reached through 'B' since 'A' does not delegate a matching pattern" |
| 247 |
visited_order=["A", "B", "C"], |
| 248 |
), |
| 249 |
"max number of delegations": DelegationsTestCase( |
| 250 |
delegations=[ |
| 251 |
TestDelegation("targets", "A"), |
| 252 |
TestDelegation("targets", "B"), |
| 253 |
TestDelegation("targets", "C"), |
| 254 |
TestDelegation("C", "D"), |
| 255 |
TestDelegation("C", "E"), |
| 256 |
], |
| 257 |
# "E" is skipped, max_delegations is 4 |
| 258 |
visited_order=["A", "B", "C", "D"], |
| 259 |
), |
| 260 |
} |
| 261 |
|
| 262 |
@utils.run_sub_tests_with_dataset(graphs) |
| 263 |
def test_graph_traversal(self, test_data: DelegationsTestCase) -> None: |
| 264 |
"""Test that delegated roles are traversed in the order of appearance |
| 265 |
in the delegator's metadata, using pre-order depth-first search""" |
| 266 |
|
| 267 |
try: |
| 268 |
exp_files = [*TOP_LEVEL_ROLE_NAMES, *test_data.visited_order] |
| 269 |
exp_calls = [(role, 1) for role in test_data.visited_order] |
| 270 |
|
| 271 |
self._init_repo(test_data) |
| 272 |
self.setup_subtest() |
| 273 |
|
| 274 |
updater = self._init_updater() |
| 275 |
# restrict the max number of delegations to simplify the test |
| 276 |
updater.config.max_delegations = 4 |
| 277 |
# Call explicitly refresh to simplify the expected_calls list |
| 278 |
updater.refresh() |
| 279 |
self.sim.fetch_tracker.metadata.clear() |
| 280 |
# Check that metadata dir contains only top-level roles |
| 281 |
self._assert_files_exist(TOP_LEVEL_ROLE_NAMES) |
| 282 |
|
| 283 |
# Looking for a non-existing targetpath forces updater |
| 284 |
# to visit all possible delegated roles |
| 285 |
targetfile = updater.get_targetinfo("missingpath") |
| 286 |
self.assertIsNone(targetfile) |
| 287 |
# Check that the delegated roles were visited in the expected |
| 288 |
# order and the corresponding metadata files were persisted |
| 289 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 290 |
self._assert_files_exist(exp_files) |
| 291 |
finally: |
| 292 |
self.teardown_subtest() |
| 293 |
|
| 294 |
invalid_metadata = { |
| 295 |
"unsigned delegated role": DelegationsTestCase( |
| 296 |
delegations=[ |
| 297 |
TestDelegation("targets", "invalid"), |
| 298 |
TestDelegation("targets", "B"), |
| 299 |
TestDelegation("invalid", "C"), |
| 300 |
], |
| 301 |
# The traversal stops after visiting an invalid role |
| 302 |
visited_order=["invalid"], |
| 303 |
) |
| 304 |
} |
| 305 |
|
| 306 |
@utils.run_sub_tests_with_dataset(invalid_metadata) |
| 307 |
def test_invalid_metadata(self, test_data: DelegationsTestCase) -> None: |
| 308 |
try: |
| 309 |
self._init_repo(test_data) |
| 310 |
# The invalid role is the last visited |
| 311 |
invalid_role = test_data.visited_order[-1] |
| 312 |
self.sim.signers[invalid_role].clear() |
| 313 |
|
| 314 |
self.setup_subtest() |
| 315 |
# The invalid role metadata must not be persisted |
| 316 |
exp_files = [*TOP_LEVEL_ROLE_NAMES, *test_data.visited_order[:-1]] |
| 317 |
exp_calls = [(role, 1) for role in test_data.visited_order] |
| 318 |
|
| 319 |
updater = self._init_updater() |
| 320 |
# Call explicitly refresh to simplify the expected_calls list |
| 321 |
updater.refresh() |
| 322 |
self.sim.fetch_tracker.metadata.clear() |
| 323 |
|
| 324 |
with self.assertRaises(UnsignedMetadataError): |
| 325 |
updater.get_targetinfo("missingpath") |
| 326 |
# Check that there were no visited roles after the invalid one |
| 327 |
# and only the valid metadata files were persisted |
| 328 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 329 |
self._assert_files_exist(exp_files) |
| 330 |
finally: |
| 331 |
self.teardown_subtest() |
| 332 |
|
| 333 |
def test_safely_encoded_rolenames(self) -> None: |
| 334 |
"""Test that delegated roles names are safely encoded in the filenames |
| 335 |
and URLs. |
| 336 |
""" |
| 337 |
|
| 338 |
roles_to_filenames = { |
| 339 |
"../a": "..%2Fa.json", |
| 340 |
".": "..json", |
| 341 |
"/": "%2F.json", |
| 342 |
"รถ": "%C3%B6.json", |
| 343 |
} |
| 344 |
|
| 345 |
delegations = [] |
| 346 |
for rolename in roles_to_filenames: |
| 347 |
delegations.append(TestDelegation("targets", rolename)) |
| 348 |
|
| 349 |
delegated_rolenames = DelegationsTestCase(delegations) |
| 350 |
self._init_repo(delegated_rolenames) |
| 351 |
updater = self._init_updater() |
| 352 |
updater.refresh() |
| 353 |
|
| 354 |
# trigger updater to fetch the delegated metadata |
| 355 |
self.sim.fetch_tracker.metadata.clear() |
| 356 |
updater.get_targetinfo("anything") |
| 357 |
|
| 358 |
# assert that local delegated metadata filenames are expected |
| 359 |
local_metadata = os.listdir(self.metadata_dir) |
| 360 |
for fname in roles_to_filenames.values(): |
| 361 |
self.assertTrue(fname in local_metadata) |
| 362 |
|
| 363 |
# assert that requested URLs are quoted without extension |
| 364 |
exp_calls = [(quoted[:-5], 1) for quoted in roles_to_filenames.values()] |
| 365 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 366 |
|
| 367 |
hash_bins_graph = { |
| 368 |
"delegations": DelegationsTestCase( |
| 369 |
delegations=[ |
| 370 |
TestDelegation( |
| 371 |
"targets", |
| 372 |
"role1", |
| 373 |
paths=None, |
| 374 |
path_hash_prefixes=["8", "9", "a", "b"], |
| 375 |
), |
| 376 |
TestDelegation( |
| 377 |
"targets", |
| 378 |
"role2", |
| 379 |
paths=None, |
| 380 |
path_hash_prefixes=["0", "1", "2", "3"], |
| 381 |
), |
| 382 |
TestDelegation( |
| 383 |
"targets", |
| 384 |
"role3", |
| 385 |
paths=None, |
| 386 |
path_hash_prefixes=["c", "d", "e", "f"], |
| 387 |
), |
| 388 |
], |
| 389 |
visited_order=["role1", "role2", "role3"], |
| 390 |
), |
| 391 |
} |
| 392 |
|
| 393 |
@utils.run_sub_tests_with_dataset(hash_bins_graph) |
| 394 |
def test_hash_bins_graph_traversal( |
| 395 |
self, test_data: DelegationsTestCase |
| 396 |
) -> None: |
| 397 |
"""Test that delegated roles are traversed in the order of appearance |
| 398 |
in the delegator's metadata, using pre-order depth-first search and that |
| 399 |
they correctly refer to the corresponding hash bin prefixes""" |
| 400 |
|
| 401 |
try: |
| 402 |
exp_files = [*TOP_LEVEL_ROLE_NAMES, *test_data.visited_order] |
| 403 |
exp_calls = [(role, 1) for role in test_data.visited_order] |
| 404 |
|
| 405 |
self._init_repo(test_data) |
| 406 |
self.setup_subtest() |
| 407 |
|
| 408 |
updater = self._init_updater() |
| 409 |
# Call explicitly refresh to simplify the expected_calls list |
| 410 |
updater.refresh() |
| 411 |
self.sim.fetch_tracker.metadata.clear() |
| 412 |
# Check that metadata dir contains only top-level roles |
| 413 |
self._assert_files_exist(TOP_LEVEL_ROLE_NAMES) |
| 414 |
|
| 415 |
# Looking for a non-existing targetpath forces updater |
| 416 |
# to visit a correspondning delegated role |
| 417 |
targetfile = updater.get_targetinfo("missingpath") |
| 418 |
self.assertIsNone(targetfile) |
| 419 |
targetfile = updater.get_targetinfo("othermissingpath") |
| 420 |
self.assertIsNone(targetfile) |
| 421 |
targetfile = updater.get_targetinfo("thirdmissingpath") |
| 422 |
self.assertIsNone(targetfile) |
| 423 |
# Check that the delegated roles were visited in the expected |
| 424 |
# order and the corresponding metadata files were persisted |
| 425 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 426 |
self._assert_files_exist(exp_files) |
| 427 |
finally: |
| 428 |
self.teardown_subtest() |
| 429 |
|
| 430 |
@dataclass |
| 431 |
class SuccinctRolesTestCase: |
| 432 |
bit_length: int |
| 433 |
target_path: str |
| 434 |
expected_target_bin: str |
| 435 |
|
| 436 |
# By setting the bit_length the total number of bins is 2^bit_length. |
| 437 |
# In each test case target_path is a path to a random target we want to |
| 438 |
# fetch and expected_target_bin is the bin we are expecting to visit. |
| 439 |
succinct_bins_graph = { |
| 440 |
"bin amount = 2, target bin index 0": SuccinctRolesTestCase( |
| 441 |
bit_length=1, |
| 442 |
target_path="boo", |
| 443 |
expected_target_bin="bin-0", |
| 444 |
), |
| 445 |
"bin amount = 2, target bin index 1": SuccinctRolesTestCase( |
| 446 |
bit_length=1, |
| 447 |
target_path="too", |
| 448 |
expected_target_bin="bin-1", |
| 449 |
), |
| 450 |
"bin amount = 4, target bin index 0": SuccinctRolesTestCase( |
| 451 |
bit_length=2, |
| 452 |
target_path="foo", |
| 453 |
expected_target_bin="bin-0", |
| 454 |
), |
| 455 |
"bin amount = 4, target bin index 1": SuccinctRolesTestCase( |
| 456 |
bit_length=2, |
| 457 |
target_path="doo", |
| 458 |
expected_target_bin="bin-1", |
| 459 |
), |
| 460 |
"bin amount = 4, target bin index 2": SuccinctRolesTestCase( |
| 461 |
bit_length=2, |
| 462 |
target_path="too", |
| 463 |
expected_target_bin="bin-2", |
| 464 |
), |
| 465 |
"bin amount = 4, target bin index 3": SuccinctRolesTestCase( |
| 466 |
bit_length=2, |
| 467 |
target_path="bar", |
| 468 |
expected_target_bin="bin-3", |
| 469 |
), |
| 470 |
"bin amount = 256, target bin index fc": SuccinctRolesTestCase( |
| 471 |
bit_length=8, |
| 472 |
target_path="bar", |
| 473 |
expected_target_bin="bin-fc", |
| 474 |
), |
| 475 |
} |
| 476 |
|
| 477 |
@utils.run_sub_tests_with_dataset(succinct_bins_graph) |
| 478 |
def test_succinct_roles_graph_traversal( |
| 479 |
self, test_data: SuccinctRolesTestCase |
| 480 |
) -> None: |
| 481 |
# Test traversing the delegation tree when succinct roles is used. For a |
| 482 |
# successful traversal all top level metadata files plus the expected |
| 483 |
# bin should exist locally and only one bin must be downloaded. |
| 484 |
|
| 485 |
try: |
| 486 |
exp_files = [*TOP_LEVEL_ROLE_NAMES, test_data.expected_target_bin] |
| 487 |
exp_calls = [(test_data.expected_target_bin, 1)] |
| 488 |
|
| 489 |
self.sim = RepositorySimulator() |
| 490 |
self.sim.add_succinct_roles("targets", test_data.bit_length, "bin") |
| 491 |
self.sim.update_snapshot() |
| 492 |
|
| 493 |
self.setup_subtest() |
| 494 |
|
| 495 |
updater = self._init_updater() |
| 496 |
# Call explicitly refresh to simplify the expected_calls list. |
| 497 |
updater.refresh() |
| 498 |
self.sim.fetch_tracker.metadata.clear() |
| 499 |
# Check that metadata dir contains only top-level roles |
| 500 |
self._assert_files_exist(TOP_LEVEL_ROLE_NAMES) |
| 501 |
|
| 502 |
# Looking for a non-existing targetpath forces updater |
| 503 |
# to visit a corresponding delegated role. |
| 504 |
targetfile = updater.get_targetinfo(test_data.target_path) |
| 505 |
self.assertIsNone(targetfile) |
| 506 |
|
| 507 |
# Check that the delegated roles were visited in the expected |
| 508 |
# order and the corresponding metadata files were persisted. |
| 509 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 510 |
self._assert_files_exist(exp_files) |
| 511 |
|
| 512 |
finally: |
| 513 |
self.teardown_subtest() |
| 514 |
|
| 515 |
|
| 516 |
class TestTargetFileSearch(TestDelegations): |
| 517 |
r""" |
| 518 |
Create a single repository with the following delegations: |
| 519 |
|
| 520 |
targets |
| 521 |
*.doc, *md / \ release/*/* |
| 522 |
A B |
| 523 |
release/x/* / \ release/y/*.zip |
| 524 |
C D |
| 525 |
|
| 526 |
Test that Updater successfully finds the target files metadata, |
| 527 |
traversing the delegations as expected. |
| 528 |
""" |
| 529 |
|
| 530 |
delegations_tree = DelegationsTestCase( |
| 531 |
delegations=[ |
| 532 |
TestDelegation("targets", "A", paths=["*.doc", "*.md"]), |
| 533 |
TestDelegation("targets", "B", paths=["releases/*/*"]), |
| 534 |
TestDelegation("B", "C", paths=["releases/x/*"]), |
| 535 |
TestDelegation("B", "D", paths=["releases/y/*.zip"]), |
| 536 |
], |
| 537 |
target_files=[ |
| 538 |
TestTarget("targets", b"targetfile content", "targetfile"), |
| 539 |
TestTarget("A", b"README by A", "README.md"), |
| 540 |
TestTarget("C", b"x release by C", "releases/x/x_v1"), |
| 541 |
TestTarget("D", b"y release by D", "releases/y/y_v1.zip"), |
| 542 |
TestTarget("D", b"z release by D", "releases/z/z_v1.zip"), |
| 543 |
], |
| 544 |
) |
| 545 |
|
| 546 |
def setUp(self) -> None: |
| 547 |
super().setUp() |
| 548 |
self._init_repo(self.delegations_tree) |
| 549 |
|
| 550 |
# fmt: off |
| 551 |
targets = { |
| 552 |
"no delegations": |
| 553 |
TargetTestCase("targetfile", True, []), |
| 554 |
"targetpath matches wildcard": |
| 555 |
TargetTestCase("README.md", True, ["A"]), |
| 556 |
"targetpath with separators x": |
| 557 |
TargetTestCase("releases/x/x_v1", True, ["B", "C"]), |
| 558 |
"targetpath with separators y": |
| 559 |
TargetTestCase("releases/y/y_v1.zip", True, ["B", "D"]), |
| 560 |
"targetpath is not delegated by all roles in the chain": |
| 561 |
TargetTestCase("releases/z/z_v1.zip", False, ["B"]), |
| 562 |
} |
| 563 |
# fmt: on |
| 564 |
|
| 565 |
@utils.run_sub_tests_with_dataset(targets) |
| 566 |
def test_targetfile_search(self, test_data: TargetTestCase) -> None: |
| 567 |
try: |
| 568 |
self.setup_subtest() |
| 569 |
exp_files = [*TOP_LEVEL_ROLE_NAMES, *test_data.visited_order] |
| 570 |
exp_calls = [(role, 1) for role in test_data.visited_order] |
| 571 |
exp_target = self.sim.target_files[test_data.targetpath].target_file |
| 572 |
|
| 573 |
updater = self._init_updater() |
| 574 |
# Call explicitly refresh to simplify the expected_calls list |
| 575 |
updater.refresh() |
| 576 |
self.sim.fetch_tracker.metadata.clear() |
| 577 |
target = updater.get_targetinfo(test_data.targetpath) |
| 578 |
if target is not None: |
| 579 |
# Confirm that the expected TargetFile is found |
| 580 |
self.assertTrue(test_data.found) |
| 581 |
self.assertDictEqual(target.to_dict(), exp_target.to_dict()) |
| 582 |
else: |
| 583 |
self.assertFalse(test_data.found) |
| 584 |
# Check that the delegated roles were visited in the expected |
| 585 |
# order and the corresponding metadata files were persisted |
| 586 |
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls) |
| 587 |
self._assert_files_exist(exp_files) |
| 588 |
finally: |
| 589 |
self.teardown_subtest() |
| 590 |
|
| 591 |
|
| 592 |
if __name__ == "__main__": |
| 593 |
if "--dump" in sys.argv: |
| 594 |
TestDelegations.dump_dir = tempfile.mkdtemp() |
| 595 |
print(f"Repository Simulator dumps in {TestDelegations.dump_dir}") |
| 596 |
sys.argv.remove("--dump") |
| 597 |
|
| 598 |
utils.configure_test_logging(sys.argv) |
| 599 |
unittest.main() |