Skip to content

Commit 135fc69

Browse files
committed
Metadata API: add timestamp meta validation
Signed-off-by: Martin Vrachev <[email protected]>
1 parent c04f712 commit 135fc69

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tests/test_metadata_serialization.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ def test_metafile_serialization(self, test_case_data: str):
205205
metafile = MetaFile.from_dict(copy.copy(case_dict))
206206
self.assertDictEqual(case_dict, metafile.to_dict())
207207

208+
invalid_timestamp: DataSet = {
209+
"no meta": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z"}',
210+
"empty meta":
211+
'{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}',
212+
"meta with wrong key":
213+
'{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
214+
"meta": {"f.title": {"hashes": {"sha256" : "abc"}, "version": 1}}}'
215+
}
216+
217+
@run_sub_tests_with_dataset(invalid_timestamp)
218+
def test_invalid_timestamp_serialization(self, test_case_data: Dict[str, str]):
219+
case_dict = json.loads(test_case_data)
220+
with self.assertRaises(KeyError):
221+
Timestamp.from_dict(copy.deepcopy(case_dict))
222+
208223

209224
valid_timestamps: DataSet = {
210225
"all": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \

tuf/api/metadata.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,23 @@ class Timestamp(Signed):
855855

856856
_signed_type = "timestamp"
857857

858+
@property
859+
def meta(self):
860+
return self._meta
861+
862+
@meta.setter
863+
def meta(self, new_meta: Dict[str, MetaFile]):
864+
if len(new_meta) != 1 or new_meta.get("snapshot.json") is None:
865+
raise ValueError(
866+
f"meta should contain information only about snapshot.json, "
867+
f"instead got {new_meta}"
868+
)
869+
if not isinstance(new_meta["snapshot.json"], MetaFile):
870+
raise TypeError(
871+
'meta["snapshot.json"] should be from type MetaFile!'
872+
)
873+
self._meta = new_meta
874+
858875
@property
859876
def snapshot_meta(self):
860877
return self.meta["snapshot.json"]

0 commit comments

Comments
 (0)