Skip to content

Commit 7cdd150

Browse files
author
Martin Vrachev
committed
Modify test_update_with_invalid_json
This addition was necessary because we weren't testing calling update_snapshot with invalid type or update_snapshot with invalid JSON, but instead an exception was thrown because snapshot hashes cannot be verified against timestamp hashes. Also, we forget to test calling update_root with an incorrect type. Signed-off-by: Martin Vrachev <[email protected]>
1 parent d624624 commit 7cdd150

1 file changed

Lines changed: 9 additions & 32 deletions

File tree

tests/test_trusted_metadata_set.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def test_update_with_invalid_json(self):
110110
with self.assertRaises(exceptions.RepositoryError):
111111
TrustedMetadataSet(json.dumps(root.to_dict()).encode())
112112

113+
# update_root called with the wrong metadata type
114+
with self.assertRaises(exceptions.RepositoryError):
115+
self.trusted_set.update_root(self.metadata["snapshot"])
116+
113117
self.trusted_set.root_update_finished()
114118

115119
top_level_md = [
@@ -118,11 +122,15 @@ def test_update_with_invalid_json(self):
118122
(self.metadata["targets"], self.trusted_set.update_targets),
119123
]
120124
for metadata, update_func in top_level_md:
125+
md = Metadata.from_bytes(metadata)
126+
if md.signed.type == "snapshot":
127+
# timestamp hashes and length intervene when testing snapshot
128+
self.trusted_set.timestamp.signed.meta["snapshot.json"].hashes = None
129+
self.trusted_set.timestamp.signed.meta["snapshot.json"].length = None
121130
# metadata is not json
122131
with self.assertRaises(exceptions.RepositoryError):
123132
update_func(b"")
124133
# metadata is invalid
125-
md = Metadata.from_bytes(metadata)
126134
md.signed.version += 1
127135
with self.assertRaises(exceptions.RepositoryError):
128136
update_func(json.dumps(md.to_dict()).encode())
@@ -228,23 +236,6 @@ def _calculate_modified_hashes(
228236
modified_hashes[algo] = observed_hash
229237
return modified_hashes
230238

231-
def test_update_snapshot_new_snapshot_invalid_type(self):
232-
self._setup_update_snapshot_or_timestamp_test()
233-
# new_snapshot data with invalid targets type
234-
invalid_type_data = json.loads(self.metadata["snapshot"])
235-
invalid_type_data["signed"]["_type"] = "targets"
236-
invalid_type_data["signed"]["targets"] = {}
237-
invalid_type_data = json.dumps(invalid_type_data).encode()
238-
timestamp_meta = self.trusted_set.timestamp.signed.meta["snapshot.json"]
239-
true_hashes = timestamp_meta.hashes or {}
240-
modified_hashes = self._calculate_modified_hashes(
241-
true_hashes, invalid_type_data
242-
)
243-
self.trusted_set.timestamp.signed.meta["snapshot.json"].hashes = modified_hashes
244-
245-
with self.assertRaises(exceptions.RepositoryError):
246-
self.trusted_set.update_snapshot(invalid_type_data)
247-
248239
def test_update_snapshot_after_targets_updated(self):
249240
self._setup_update_snapshot_or_timestamp_test()
250241
# cannot update snapshot after targets update completes or targets != None
@@ -253,20 +244,6 @@ def test_update_snapshot_after_targets_updated(self):
253244
with self.assertRaises(RuntimeError):
254245
self.trusted_set.update_snapshot(self.metadata["snapshot"])
255246

256-
def test_update_snapshot_with_invalid_json(self):
257-
self._setup_update_snapshot_or_timestamp_test()
258-
# Deserialization error - failed to decode the new_snapshot JSON.
259-
timestamp_meta = self.trusted_set.timestamp.signed.meta["snapshot.json"]
260-
true_hashes = timestamp_meta.hashes or {}
261-
262-
modified_hashes = self._calculate_modified_hashes(
263-
true_hashes, b'{""sig": }'
264-
)
265-
self.trusted_set.timestamp.signed.meta["snapshot.json"].hashes = modified_hashes
266-
with self.assertRaises(exceptions.RepositoryError):
267-
self.trusted_set.update_snapshot(b'{""sig": }')
268-
self.trusted_set.timestamp.signed.meta["snapshot.json"].hashes = true_hashes
269-
270247
def test_update_snapshot_cannot_verify_snapshot_with_threshold(self):
271248
self._setup_update_snapshot_or_timestamp_test()
272249
# root data with threshold which cannot be verified for new_snapshot

0 commit comments

Comments
 (0)