@@ -239,18 +239,54 @@ def test_basic_metadata_hash_support(
239239 # Verify client accepts correct hashes
240240 assert client .refresh (init_data ) == 0
241241
242+
243+ def test_targets_hash_mismatch (client : ClientRunner , server : SimulatorServer ) -> None :
244+ """Verify that clients supports hashes for metadata"""
245+ init_data , repo = server .new_test (client .test_name )
246+
247+ # Construct repository with hashes in timestamp/snapshot
248+ repo .compute_metafile_hashes_length = True
249+ repo .publish ([Targets .type , Snapshot .type , Timestamp .type ]) # v2, v2, v2
250+
251+ assert client .init_client (init_data ) == 0
252+ assert client .refresh (init_data ) == 0
253+
242254 # Modify targets metadata, set hash in snapshot to wrong value
243255 repo .publish ([Targets .type ]) # v3
244256 assert repo .snapshot .meta ["targets.json" ].hashes
245- repo .snapshot .meta ["targets.json" ].hashes ["sha256" ] = (
246- "46419349341cfb2d95f6ae3d4cd5c3d3dd7f4673985dad42a45130be5e0531a0"
247- )
257+ repo .snapshot .meta ["targets.json" ].hashes ["sha256" ] = "0" * 64
248258 repo .publish ([Snapshot .type , Timestamp .type ]) # v3
249259
250260 # Verify client refuses targets v3 that does not match hashes
251261 assert client .refresh (init_data ) == 1
252262 assert client .version (Snapshot .type ) == 3
253- assert client .version (Targets .type ) == 2
263+ assert client .version (Targets .type ) != 3
264+
265+
266+ def test_snapshot_hash_mismatch (client : ClientRunner , server : SimulatorServer ) -> None :
267+ """Repository serves a snapshot with a hash that does not match the one in
268+ timestamp. Expect client to refuse the update.
269+ """
270+ init_data , repo = server .new_test (client .test_name )
271+
272+ # Construct repository with hashes in timestamp/snapshot
273+ repo .compute_metafile_hashes_length = True
274+ repo .publish ([Snapshot .type , Timestamp .type ]) # v2, v2
275+
276+ assert client .init_client (init_data ) == 0
277+ assert client .refresh (init_data ) == 0
278+
279+ # Create a new timestamp v3 with a bad hash for snapshot.
280+ repo .publish ([Snapshot .type ]) # v3
281+ assert repo .timestamp .snapshot_meta .hashes
282+ repo .timestamp .snapshot_meta .hashes ["sha256" ] = "0" * 64
283+ repo .publish ([Timestamp .type ]) # v3
284+
285+ # Client should refuse the update because snapshot hash does not match.
286+ # Client should have the new timestamp v3 but not the new snapshot
287+ assert client .refresh (init_data ) == 1
288+ assert client .version (Snapshot .type ) != 3
289+ assert client .version (Timestamp .type ) == 3
254290
255291
256292def test_metadata_bytes_match (client : ClientRunner , server : SimulatorServer ) -> None :
0 commit comments