@@ -80,21 +80,6 @@ def hashes_length_modifier(timestamp: Timestamp) -> None:
80
80
def setUp (self ) -> None :
81
81
self .trusted_set = TrustedMetadataSet (self .metadata ["root" ])
82
82
83
- def _root_updated_and_update_timestamp (
84
- self , timestamp_bytes : Optional [bytes ] = None
85
- ) -> None :
86
- """Finsh root update and update timestamp with passed timestamp_bytes.
87
-
88
- Args:
89
- timestamp_bytes:
90
- Bytes used when calling trusted_set.update_timestamp().
91
- Default self.metadata["timestamp"].
92
-
93
- """
94
- timestamp_bytes = timestamp_bytes or self .metadata ["timestamp" ]
95
- self .trusted_set .root_update_finished ()
96
- self .trusted_set .update_timestamp (timestamp_bytes )
97
-
98
83
99
84
def _update_all_besides_targets (
100
85
self ,
@@ -112,13 +97,14 @@ def _update_all_besides_targets(
112
97
Default self.metadata["snapshot"].
113
98
114
99
"""
115
- self ._root_updated_and_update_timestamp (timestamp_bytes )
100
+
101
+ timestamp_bytes = timestamp_bytes or self .metadata ["timestamp" ]
102
+ self .trusted_set .update_timestamp (timestamp_bytes )
116
103
snapshot_bytes = snapshot_bytes or self .metadata ["snapshot" ]
117
104
self .trusted_set .update_snapshot (snapshot_bytes )
118
105
119
106
120
107
def test_update (self ):
121
- self .trusted_set .root_update_finished ()
122
108
self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
123
109
self .trusted_set .update_snapshot (self .metadata ["snapshot" ])
124
110
self .trusted_set .update_targets (self .metadata ["targets" ])
@@ -139,24 +125,16 @@ def test_update(self):
139
125
self .assertTrue (count , 6 )
140
126
141
127
def test_out_of_order_ops (self ):
142
- # Update timestamp before root is finished
143
- with self .assertRaises (RuntimeError ):
144
- self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
145
-
146
- self .trusted_set .root_update_finished ()
147
- with self .assertRaises (RuntimeError ):
148
- self .trusted_set .root_update_finished ()
149
-
150
- # Update root after a previous successful root update
151
- with self .assertRaises (RuntimeError ):
152
- self .trusted_set .update_root (self .metadata ["root" ])
153
-
154
128
# Update snapshot before timestamp
155
129
with self .assertRaises (RuntimeError ):
156
130
self .trusted_set .update_snapshot (self .metadata ["snapshot" ])
157
131
158
132
self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
159
133
134
+ # Update root after timestamp
135
+ with self .assertRaises (RuntimeError ):
136
+ self .trusted_set .update_root (self .metadata ["root" ])
137
+
160
138
# Update targets before snapshot
161
139
with self .assertRaises (RuntimeError ):
162
140
self .trusted_set .update_targets (self .metadata ["targets" ])
@@ -198,8 +176,6 @@ def test_update_with_invalid_json(self):
198
176
with self .assertRaises (exceptions .RepositoryError ):
199
177
self .trusted_set .update_root (self .metadata ["snapshot" ])
200
178
201
- self .trusted_set .root_update_finished ()
202
-
203
179
top_level_md = [
204
180
(self .metadata ["timestamp" ], self .trusted_set .update_timestamp ),
205
181
(self .metadata ["snapshot" ], self .trusted_set .update_snapshot ),
@@ -241,15 +217,16 @@ def test_update_root_new_root_ver_same_as_trusted_root_ver(self):
241
217
with self .assertRaises (exceptions .ReplayedMetadataError ):
242
218
self .trusted_set .update_root (self .metadata ["root" ])
243
219
244
- def test_root_update_finished_expired (self ):
220
+ def test_root_expired_final_root (self ):
245
221
def root_expired_modifier (root : Root ) -> None :
246
222
root .expires = datetime (1970 , 1 , 1 )
247
223
224
+ # intermediate root can be expired
248
225
root = self .modify_metadata ("root" , root_expired_modifier )
249
226
tmp_trusted_set = TrustedMetadataSet (root )
250
- # call root_update_finished when trusted root has expired
227
+ # update timestamp to trigger final root expiry check
251
228
with self .assertRaises (exceptions .ExpiredMetadataError ):
252
- tmp_trusted_set .root_update_finished ( )
229
+ tmp_trusted_set .update_timestamp ( self . metadata [ "timestamp" ] )
253
230
254
231
255
232
def test_update_timestamp_new_timestamp_ver_below_trusted_ver (self ):
@@ -258,7 +235,7 @@ def version_modifier(timestamp: Timestamp) -> None:
258
235
timestamp .version = 3
259
236
260
237
timestamp = self .modify_metadata ("timestamp" , version_modifier )
261
- self ._root_updated_and_update_timestamp (timestamp )
238
+ self .trusted_set . update_timestamp (timestamp )
262
239
with self .assertRaises (exceptions .ReplayedMetadataError ):
263
240
self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
264
241
@@ -268,35 +245,38 @@ def bump_snapshot_version(timestamp: Timestamp) -> None:
268
245
269
246
# set current known snapshot.json version to 2
270
247
timestamp = self .modify_metadata ("timestamp" , bump_snapshot_version )
271
- self ._root_updated_and_update_timestamp (timestamp )
248
+ self .trusted_set . update_timestamp (timestamp )
272
249
273
250
# newtimestamp.meta["snapshot.json"].version < trusted_timestamp.meta["snapshot.json"].version
274
251
with self .assertRaises (exceptions .ReplayedMetadataError ):
275
252
self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
276
253
277
254
def test_update_timestamp_expired (self ):
278
- self .trusted_set .root_update_finished ()
279
255
# new_timestamp has expired
280
256
def timestamp_expired_modifier (timestamp : Timestamp ) -> None :
281
257
timestamp .expires = datetime (1970 , 1 , 1 )
282
258
259
+ # intermediate timestamp is allowed to be expired
283
260
timestamp = self .modify_metadata ("timestamp" , timestamp_expired_modifier )
261
+ self .trusted_set .update_timestamp (timestamp )
262
+
263
+ # update snapshot to trigger final timestamp expiry check
284
264
with self .assertRaises (exceptions .ExpiredMetadataError ):
285
- self .trusted_set .update_timestamp ( timestamp )
265
+ self .trusted_set .update_snapshot ( self . metadata [ "snapshot" ] )
286
266
287
267
def test_update_snapshot_length_or_hash_mismatch (self ):
288
268
def modify_snapshot_length (timestamp : Timestamp ) -> None :
289
269
timestamp .meta ["snapshot.json" ].length = 1
290
270
291
271
# set known snapshot.json length to 1
292
272
timestamp = self .modify_metadata ("timestamp" , modify_snapshot_length )
293
- self ._root_updated_and_update_timestamp (timestamp )
273
+ self .trusted_set . update_timestamp (timestamp )
294
274
295
275
with self .assertRaises (exceptions .RepositoryError ):
296
276
self .trusted_set .update_snapshot (self .metadata ["snapshot" ])
297
277
298
278
def test_update_snapshot_cannot_verify_snapshot_with_threshold (self ):
299
- self ._root_updated_and_update_timestamp (self .metadata ["timestamp" ])
279
+ self .trusted_set . update_timestamp (self .metadata ["timestamp" ])
300
280
snapshot = Metadata .from_bytes (self .metadata ["snapshot" ])
301
281
snapshot .signatures .clear ()
302
282
with self .assertRaises (exceptions .UnsignedMetadataError ):
@@ -307,46 +287,70 @@ def timestamp_version_modifier(timestamp: Timestamp) -> None:
307
287
timestamp .meta ["snapshot.json" ].version = 2
308
288
309
289
timestamp = self .modify_metadata ("timestamp" , timestamp_version_modifier )
310
- self ._root_updated_and_update_timestamp (timestamp )
311
- # new_snapshot.version != trusted timestamp.meta["snapshot"].version
312
- def snapshot_version_modifier ( snapshot : Snapshot ) -> None :
313
- snapshot . version = 3
290
+ self .trusted_set . update_timestamp (timestamp )
291
+
292
+ #intermediate snapshot is allowed to not match meta version
293
+ self . trusted_set . update_snapshot ( self . metadata [ "snapshot" ])
314
294
315
- snapshot = self . modify_metadata ( " snapshot" , snapshot_version_modifier )
295
+ # final snapshot must match meta version
316
296
with self .assertRaises (exceptions .BadVersionNumberError ):
317
- self .trusted_set .update_snapshot (snapshot )
297
+ self .trusted_set .update_targets (self .metadata ["targets" ])
298
+
318
299
319
- def test_update_snapshot_after_successful_update_new_snapshot_no_meta (self ):
300
+ def test_update_snapshot_file_removed_from_meta (self ):
320
301
self ._update_all_besides_targets (self .metadata ["timestamp" ])
321
- # Test removing a meta_file in new_snapshot compared to the old snapshot
322
- def no_meta_modifier (snapshot : Snapshot ) -> None :
323
- snapshot .meta = {}
302
+ def remove_file_from_meta (snapshot : Snapshot ) -> None :
303
+ del snapshot .meta ["targets.json" ]
324
304
325
- snapshot = self .modify_metadata ("snapshot" , no_meta_modifier )
305
+ # Test removing a meta_file in new_snapshot compared to the old snapshot
306
+ snapshot = self .modify_metadata ("snapshot" , remove_file_from_meta )
326
307
with self .assertRaises (exceptions .RepositoryError ):
327
308
self .trusted_set .update_snapshot (snapshot )
328
309
329
- def test_update_snapshot_after_succesfull_update_new_snapshot_meta_version_different (self ):
330
- self ._root_updated_and_update_timestamp (self .metadata ["timestamp" ])
331
- # snapshot.meta["project1"].version != new_snapshot.meta["project1"].version
310
+ def test_update_snapshot_meta_version_decreases (self ):
311
+ self .trusted_set . update_timestamp (self .metadata ["timestamp" ])
312
+
332
313
def version_meta_modifier (snapshot : Snapshot ) -> None :
333
- for metafile_path in snapshot .meta .keys ():
334
- snapshot .meta [metafile_path ].version += 1
314
+ snapshot .meta ["targets.json" ].version += 1
335
315
336
316
snapshot = self .modify_metadata ("snapshot" , version_meta_modifier )
337
317
self .trusted_set .update_snapshot (snapshot )
318
+
338
319
with self .assertRaises (exceptions .BadVersionNumberError ):
339
320
self .trusted_set .update_snapshot (self .metadata ["snapshot" ])
340
321
341
322
def test_update_snapshot_expired_new_snapshot (self ):
342
- self ._root_updated_and_update_timestamp (self .metadata ["timestamp" ])
343
- # new_snapshot has expired
323
+ self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
344
324
def snapshot_expired_modifier (snapshot : Snapshot ) -> None :
345
325
snapshot .expires = datetime (1970 , 1 , 1 )
346
326
327
+ # intermediate snapshot is allowed to be expired
347
328
snapshot = self .modify_metadata ("snapshot" , snapshot_expired_modifier )
329
+ self .trusted_set .update_snapshot (snapshot )
330
+
331
+ # update targets to trigger final snapshot expiry check
348
332
with self .assertRaises (exceptions .ExpiredMetadataError ):
349
- self .trusted_set .update_snapshot (snapshot )
333
+ self .trusted_set .update_targets (self .metadata ["targets" ])
334
+
335
+ def test_update_snapshot_successful_rollback_checks (self ):
336
+ def meta_version_bump (timestamp : Timestamp ) -> None :
337
+ timestamp .meta ["snapshot.json" ].version += 1
338
+
339
+ def version_bump (snapshot : Snapshot ) -> None :
340
+ snapshot .version += 1
341
+
342
+ # load a "local" timestamp, then update to newer one:
343
+ self .trusted_set .update_timestamp (self .metadata ["timestamp" ])
344
+ new_timestamp = self .modify_metadata ("timestamp" , meta_version_bump )
345
+ self .trusted_set .update_timestamp (new_timestamp )
346
+
347
+ # load a "local" snapshot, then update to newer one:
348
+ self .trusted_set .update_snapshot (self .metadata ["snapshot" ])
349
+ new_snapshot = self .modify_metadata ("snapshot" , version_bump )
350
+ self .trusted_set .update_snapshot (new_snapshot )
351
+
352
+ # update targets to trigger final snapshot meta version check
353
+ self .trusted_set .update_targets (self .metadata ["targets" ])
350
354
351
355
def test_update_targets_no_meta_in_snapshot (self ):
352
356
def no_meta_modifier (snapshot : Snapshot ) -> None :
0 commit comments