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 and snapshot 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 Apr 28, 2021
1 parent 1ccf2d8 commit 7d87691
Show file tree
Hide file tree
Showing 2 changed files with 20 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 @@ -258,6 +258,10 @@ def test_metadata_snapshot(self):
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):
timestamp_path = os.path.join(
Expand Down Expand Up @@ -293,6 +297,10 @@ def test_metadata_timestamp(self):
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):
root_path = os.path.join(
Expand Down
19 changes: 12 additions & 7 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class Timestamp(Signed):
'<HASH ALGO 1>': '<SNAPSHOT METADATA FILE HASH 1>',
'<HASH ALGO 2>': '<SNAPSHOT METADATA FILE HASH 2>',
...
}
} // optional
}
}
Expand Down Expand Up @@ -557,14 +557,19 @@ def to_dict(self) -> Dict[str, Any]:

# Modification.
def update(
self, version: int, length: int, hashes: Mapping[str, Any]
self,
version: int,
length: Optional[int] = None,
hashes: Optional[Mapping[str, Any]] = 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 7d87691

Please sign in to comment.