Skip to content

Commit

Permalink
Make length and hashes optional in Timestamp
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 29, 2021
1 parent 284893b commit 71aa27a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
24 changes: 24 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ 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('role2', 3)
fileinfo['role2.json'] = {'version': 3}
self.assertEqual(snapshot.signed.meta, fileinfo)

# Test from_dict and to_dict without hashes and length.
snapshot_dict = snapshot.to_dict()
test_dict = snapshot_dict['signed'].copy()
del test_dict['meta']['role1.json']['length']
del test_dict['meta']['role1.json']['hashes']
snapshot = Snapshot.from_dict(test_dict)
self.assertEqual(snapshot_dict['signed'], snapshot.to_dict())

def test_metadata_timestamp(self):
timestamp_path = os.path.join(
Expand Down Expand Up @@ -295,6 +307,18 @@ def test_metadata_timestamp(self):
timestamp.signed.update(2, 520, hashes)
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)

# Test from_dict and to_dict without hashes and length.
timestamp_dict = timestamp.to_dict()
test_dict = timestamp_dict['signed'].copy()
del test_dict['meta']['snapshot.json']['length']
del test_dict['meta']['snapshot.json']['hashes']
timestamp_test = Timestamp.from_dict(test_dict)
self.assertEqual(timestamp_dict['signed'], timestamp_test.to_dict())

# 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_key_class(self):
keys = {
Expand Down
19 changes: 12 additions & 7 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,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 @@ -643,14 +643,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 71aa27a

Please sign in to comment.