Skip to content

Commit

Permalink
New API Timestamp: make length and hashes optional
Browse files Browse the repository at this point in the history
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 theupdateframework#1031) and we should
implement it in the new API.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed Dec 14, 2020
1 parent 3db1fa3 commit 98fab06
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 8 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def test_metadata_snapshot(self):
self.assertNotEqual(snapshot.signed.meta, fileinfo)
snapshot.signed.update('role1', 2, 123, hashes)
self.assertEqual(snapshot.signed.meta, fileinfo)
# Update only version. Length and hashes are optional.
snapshot.signed.update('role1', 3)
fileinfo['role1.json'] = {'version': 3}
self.assertEqual(snapshot.signed.meta, fileinfo)


def test_metadata_timestamp(self):
Expand Down Expand Up @@ -267,6 +271,10 @@ def test_metadata_timestamp(self):
self.assertNotEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
timestamp.signed.update(2, 520, hashes)
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
# Update only version. Length and hashes are optional.
timestamp.signed.update(3)
fileinfo = {'version': 3}
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)


def test_metadata_root(self):
Expand Down
17 changes: 10 additions & 7 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class Timestamp(Signed):
'<HASH ALGO 1>': '<SNAPSHOT METADATA FILE HASH 1>',
'<HASH ALGO 2>': '<SNAPSHOT METADATA FILE HASH 2>',
...
}
} // optional
}
}
Expand All @@ -455,13 +455,16 @@ def to_dict(self) -> JsonDict:


# Modification.
def update(self, version: int, length: int, hashes: JsonDict) -> None:
def update(self, version: int, length: Optional[int] = None,
hashes: Optional[JsonDict] = None) -> None:
"""Assigns passed info about snapshot metadata to meta dict. """
self.meta['snapshot.json'] = {
'version': version,
'length': length,
'hashes': hashes
}

self.meta['snapshot.json'] = {'version': version}
if length is not None:
self.meta['snapshot.json']['length'] = length

if hashes is not None:
self.meta['snapshot.json']['hashes'] = hashes


class Snapshot(Signed):
Expand Down

0 comments on commit 98fab06

Please sign in to comment.