|
10 | 10 | import os
|
11 | 11 | import sys
|
12 | 12 | import unittest
|
13 |
| -from typing import Any, ClassVar, Dict |
| 13 | +from typing import Any, ClassVar, Dict, List |
| 14 | + |
| 15 | +from securesystemslib.signer import SSlibSigner |
14 | 16 |
|
15 | 17 | from tests import utils
|
| 18 | +from tests.repository_simulator import RepositorySimulator |
16 | 19 | from tuf.api.metadata import (
|
17 | 20 | TOP_LEVEL_ROLE_NAMES,
|
18 | 21 | DelegatedRole,
|
@@ -60,6 +63,35 @@ def test_metadata_eq_(self, test_case_data: Any) -> None:
|
60 | 63 | setattr(md_2, self.case_name, test_case_data)
|
61 | 64 | self.assertNotEqual(md, md_2)
|
62 | 65 |
|
| 66 | + def test_md_eq_signatures_reversed_order(self) -> None: |
| 67 | + # Test comparing objects with same signatures but different order. |
| 68 | + |
| 69 | + # Remove all signatures and create new ones. |
| 70 | + md = Metadata.from_bytes(self.metadata["snapshot"]) |
| 71 | + md.signatures = {} |
| 72 | + md_2 = copy.deepcopy(md) |
| 73 | + signatures: List[SSlibSigner] = [] |
| 74 | + # Assign the new signatures to the md object in the straight order. |
| 75 | + for i in ["0", "1", "2"]: |
| 76 | + _, signer = RepositorySimulator.create_key() |
| 77 | + signature = signer.sign(md.to_bytes()) |
| 78 | + signatures.append(signature) |
| 79 | + md.signatures[i] = signature |
| 80 | + |
| 81 | + # Assign the new signatures in a reverse order for the md_2 object. |
| 82 | + for j in [2, 1, 0]: |
| 83 | + md_2.signatures[str(j)] = signatures[j] |
| 84 | + |
| 85 | + # Assert that both objects are not the same because of signatures order. |
| 86 | + self.assertNotEqual(md, md_2) |
| 87 | + |
| 88 | + # but if we fix the signatures order they will be equal |
| 89 | + md_2.signatures = {} |
| 90 | + for j in [0, 1, 2]: |
| 91 | + md_2.signatures[str(j)] = signatures[j] |
| 92 | + |
| 93 | + self.assertEqual(md, md_2) |
| 94 | + |
63 | 95 | def test_md_eq_special_signatures_tests(self) -> None:
|
64 | 96 | # Test that metadata objects with different signatures are not equal.
|
65 | 97 | md = Metadata.from_bytes(self.metadata["snapshot"])
|
@@ -284,6 +316,64 @@ def test_delegations_eq_(self, test_case_data: Any) -> None:
|
284 | 316 | setattr(delegations_2, self.case_name, test_case_data)
|
285 | 317 | self.assertNotEqual(delegations, delegations_2)
|
286 | 318 |
|
| 319 | + def test_delegations_eq_roles_reversed_order(self) -> None: |
| 320 | + # Test comparing objects with same delegated roles but different order. |
| 321 | + role_one_dict = { |
| 322 | + "keyids": ["keyid1"], |
| 323 | + "name": "a", |
| 324 | + "terminating": False, |
| 325 | + "paths": ["fn1"], |
| 326 | + "threshold": 1, |
| 327 | + } |
| 328 | + role_two_dict = { |
| 329 | + "keyids": ["keyid2"], |
| 330 | + "name": "b", |
| 331 | + "terminating": True, |
| 332 | + "paths": ["fn2"], |
| 333 | + "threshold": 4, |
| 334 | + } |
| 335 | + role_one = DelegatedRole.from_dict(role_one_dict) |
| 336 | + role_two = DelegatedRole.from_dict(role_two_dict) |
| 337 | + |
| 338 | + delegations_dict = { |
| 339 | + "keys": { |
| 340 | + "keyid2": { |
| 341 | + "keytype": "ed25519", |
| 342 | + "scheme": "ed25519", |
| 343 | + "keyval": {"public": "bar"}, |
| 344 | + } |
| 345 | + }, |
| 346 | + "roles": [], |
| 347 | + } |
| 348 | + delegations = Delegations.from_dict(delegations_dict) |
| 349 | + delegations.roles["a"] = role_one |
| 350 | + delegations.roles["b"] = role_two |
| 351 | + |
| 352 | + # Create a second delegations obj with reversed roles order |
| 353 | + delegations_dict_2 = { |
| 354 | + "keys": { |
| 355 | + "keyid2": { |
| 356 | + "keytype": "ed25519", |
| 357 | + "scheme": "ed25519", |
| 358 | + "keyval": {"public": "bar"}, |
| 359 | + } |
| 360 | + }, |
| 361 | + "roles": [], |
| 362 | + } |
| 363 | + delegations_2 = Delegations.from_dict(delegations_dict_2) |
| 364 | + delegations.roles["b"] = copy.deepcopy(role_two) |
| 365 | + delegations.roles["a"] = copy.deepcopy(role_one) |
| 366 | + |
| 367 | + # Both objects are not the equal because of delegated roles order. |
| 368 | + self.assertNotEqual(delegations, delegations_2) |
| 369 | + |
| 370 | + # but if we fix the delegated roles order they will be equal |
| 371 | + delegations_2.roles = {} |
| 372 | + delegations_2.roles["a"] = copy.deepcopy(role_one) |
| 373 | + delegations_2.roles["b"] = copy.deepcopy(role_two) |
| 374 | + |
| 375 | + self.assertEqual(delegations, delegations_2) |
| 376 | + |
287 | 377 | # Common attributes between TargetFile and MetaFile doesn't need testing.
|
288 | 378 | targetfile_attributes_modifications: utils.DataSet = {
|
289 | 379 | "path": "",
|
|
0 commit comments