Skip to content

Commit

Permalink
ngtests: Add asserts for expected version
Browse files Browse the repository at this point in the history
Define _assert_version_equals for checking if the
local metadata file's version is as expected.

Signed-off-by: Teodora Sechkova <[email protected]>
  • Loading branch information
sechkova committed Nov 9, 2021
1 parent e51642a commit 8418d52
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions tests/test_updater_top_level_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def _assert_content_equals(
with open(os.path.join(self.metadata_dir, f"{role}.json"), "rb") as f:
self.assertEqual(f.read(), expected_content)

def _assert_version_equals(self, role: str, expected_version: int) -> None:
"""Assert that local metadata version is the expected"""
md = Metadata.from_file(os.path.join(self.metadata_dir, f"{role}.json"))
self.assertEqual(md.signed.version, expected_version)

def test_first_time_refresh(self) -> None:
# Metadata dir contains only the mandatory initial root.json
self._assert_files_exist(["root"])
Expand Down Expand Up @@ -164,18 +169,17 @@ def test_max_root_rotations(self) -> None:
self.sim.root.version += 1
self.sim.publish_root()

root_path = os.path.join(self.metadata_dir, "root.json")
md_root = Metadata.from_file(root_path)
md_root = Metadata.from_file(
os.path.join(self.metadata_dir, "root.json")
)
initial_root_version = md_root.signed.version

updater.refresh()

# Assert that root version was increased with no more
# than 'max_root_rotations'
md_root = Metadata.from_file(root_path)
self.assertEqual(
md_root.signed.version,
initial_root_version + updater.config.max_root_rotations,
self._assert_version_equals(
"root", initial_root_version + updater.config.max_root_rotations
)

def test_intermediate_root_incorrectly_signed(self) -> None:
Expand Down Expand Up @@ -286,10 +290,7 @@ def test_new_timestamp_version_rollback(self) -> None:
with self.assertRaises(ReplayedMetadataError):
self._run_refresh()

md_timestamp = Metadata.from_file(
os.path.join(self.metadata_dir, "timestamp.json")
)
self.assertEqual(md_timestamp.signed.version, 2)
self._assert_version_equals("timestamp", 2)

def test_new_timestamp_snapshot_rollback(self) -> None:
# Check for a rollback attack.
Expand All @@ -304,10 +305,7 @@ def test_new_timestamp_snapshot_rollback(self) -> None:
with self.assertRaises(ReplayedMetadataError):
self._run_refresh()

md_timestamp = Metadata.from_file(
os.path.join(self.metadata_dir, "timestamp.json")
)
self.assertEqual(md_timestamp.signed.version, 2)
self._assert_version_equals("timestamp", 2)

def test_new_timestamp_expired(self) -> None:
# Check for a freeze attack
Expand Down Expand Up @@ -338,15 +336,8 @@ def test_new_snapshot_hash_mismatch(self) -> None:
with self.assertRaises(RepositoryError):
self._run_refresh()

md_timestamp = Metadata.from_file(
os.path.join(self.metadata_dir, "timestamp.json")
)
self.assertEqual(md_timestamp.signed.version, 3)

md_snapshot = Metadata.from_file(
os.path.join(self.metadata_dir, "snapshot.json")
)
self.assertEqual(md_snapshot.signed.version, 1)
self._assert_version_equals("timestamp", 3)
self._assert_version_equals("snapshot", 1)

def test_new_snapshot_unsigned(self) -> None:
# Check for an arbitrary software attack
Expand Down Expand Up @@ -382,10 +373,7 @@ def test_new_snapshot_version_rollback(self) -> None:
with self.assertRaises(ReplayedMetadataError):
self._run_refresh()

md_snapshot = Metadata.from_file(
os.path.join(self.metadata_dir, "snapshot.json")
)
self.assertEqual(md_snapshot.signed.version, 2)
self._assert_version_equals("snapshot", 2)

def test_new_snapshot_expired(self) -> None:
# Check for a freeze attack
Expand Down Expand Up @@ -417,15 +405,8 @@ def test_new_targets_hash_mismatch(self) -> None:
with self.assertRaises(RepositoryError):
self._run_refresh()

md_snapshot = Metadata.from_file(
os.path.join(self.metadata_dir, "snapshot.json")
)
self.assertEqual(md_snapshot.signed.version, 3)

md_targets = Metadata.from_file(
os.path.join(self.metadata_dir, "targets.json")
)
self.assertEqual(md_targets.signed.version, 1)
self._assert_version_equals("snapshot", 3)
self._assert_version_equals("targets", 1)

def test_new_targets_unsigned(self) -> None:
# Check for an arbitrary software attack
Expand Down

0 comments on commit 8418d52

Please sign in to comment.