Skip to content

Commit 7cfd100

Browse files
committed
New API: Timestamp - make length and hashes optional
As per the specification (v1.0.1) length and hashes fields in timestamp metadata are optional. We have implement this in the older API (see #1031) and we should implement it in the new API. Signed-off-by: Martin Vrachev <[email protected]>
1 parent a141580 commit 7cfd100

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

tests/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ def test_metadata_snapshot(self):
232232
self.assertNotEqual(snapshot.signed.meta, fileinfo)
233233
snapshot.signed.update('role1', 2, 123, hashes)
234234
self.assertEqual(snapshot.signed.meta, fileinfo)
235+
# Update only version. Length and hashes are optional.
236+
snapshot.signed.update('role1', 3)
237+
fileinfo['role1.json'] = {'version': 3}
238+
self.assertEqual(snapshot.signed.meta, fileinfo)
235239

236240

237241
def test_metadata_timestamp(self):
@@ -267,6 +271,10 @@ def test_metadata_timestamp(self):
267271
self.assertNotEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
268272
timestamp.signed.update(2, 520, hashes)
269273
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
274+
# Update only version. Length and hashes are optional.
275+
timestamp.signed.update(3)
276+
fileinfo = {'version': 3}
277+
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
270278

271279

272280
def test_metadata_root(self):

tuf/api/metadata.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ class Timestamp(Signed):
431431
'<HASH ALGO 1>': '<SNAPSHOT METADATA FILE HASH 1>',
432432
'<HASH ALGO 2>': '<SNAPSHOT METADATA FILE HASH 2>',
433433
...
434-
}
434+
} // optional
435435
}
436436
}
437437
@@ -455,13 +455,16 @@ def to_dict(self) -> JsonDict:
455455

456456

457457
# Modification.
458-
def update(self, version: int, length: int, hashes: JsonDict) -> None:
458+
def update(self, version: int, length: Optional[int] = None,
459+
hashes: Optional[JsonDict] = None) -> None:
459460
"""Assigns passed info about snapshot metadata to meta dict. """
460-
self.meta['snapshot.json'] = {
461-
'version': version,
462-
'length': length,
463-
'hashes': hashes
464-
}
461+
462+
self.meta['snapshot.json'] = {'version': version}
463+
if length is not None:
464+
self.meta['snapshot.json']['length'] = length
465+
466+
if hashes is not None:
467+
self.meta['snapshot.json']['hashes'] = hashes
465468

466469

467470
class Snapshot(Signed):

0 commit comments

Comments
 (0)