From a8944958d797434b3c560e56e53d11c6277a94ec Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Fri, 6 Aug 2021 21:00:24 +0300 Subject: [PATCH] Add more use cases and address Jussi's comments Signed-off-by: Martin Vrachev --- tests/test_metadata_serialization.py | 67 ++++++++++++++++------------ 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/tests/test_metadata_serialization.py b/tests/test_metadata_serialization.py index 0f5ebb8975..834cbd30e0 100644 --- a/tests/test_metadata_serialization.py +++ b/tests/test_metadata_serialization.py @@ -155,8 +155,12 @@ def test_role_serialization(self, test_case_data: str): valid_roots: DataSet = { "all": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ "expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ - "keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ - "roles": { "targets": {"keyids": ["keyid"], "threshold": 3}} \ + "keys": { \ + "keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ + "keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ + "roles": { \ + "targets": {"keyids": ["keyid1"], "threshold": 1}, \ + "snapshot": {"keyids": ["keyid2"], "threshold": 1}} \ }', "no consistent_snapshot": '{ "_type": "root", "spec_version": "1.0.0", "version": 1, \ "expires": "2030-01-01T00:00:00Z", \ @@ -168,25 +172,6 @@ def test_role_serialization(self, test_case_data: str): "keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ "roles": { "targets": {"keyids": ["keyid"], "threshold": 3}}, \ "foo": "bar"}', - "many keys": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ - "expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ - "keys": { \ - "keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ - "keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ - "roles": { "targets": {"keyids": ["keyid"], "threshold": 3}} \ - }', - "many roles": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ - "expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ - "keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ - "roles": { \ - "targets": {"keyids": ["keyid1"], "threshold": 1}, \ - "snapshot": {"keyids": ["keyid2"], "threshold": 1}} \ - }', - "many role keyids": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ - "expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ - "keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ - "roles": {"targets": {"keyids": ["keyid1", "keyid2"], "threshold": 3}} \ - }', } @run_sub_tests_with_dataset(valid_roots) @@ -203,7 +188,6 @@ def test_root_serialization(self, test_case_data: str): "empty hashes dict": '{"version": 1, "length": 1, "hashes": {}}', "hashes wrong type": '{"version": 1, "length": 1, "hashes": 1}', "hashes values wrong type": '{"version": 1, "length": 1, "hashes": {"sha256": 1}}', - "zero hashes": '{"version": 1, "length": 1, "hashes": {}}', } @run_sub_tests_with_dataset(invalid_metafiles) @@ -227,6 +211,21 @@ def test_metafile_serialization(self, test_case_data: str): metafile = MetaFile.from_dict(copy.copy(case_dict)) self.assertDictEqual(case_dict, metafile.to_dict()) + invalid_timestamps: DataSet = { + "no metafile": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z"}', + "multiple metafiles": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ + "meta": { \ + "snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}, \ + "file1.txt": {"hashes": {"sha256" : "cde"}, "version": 1} \ + }}', + } + + @run_sub_tests_with_dataset(invalid_timestamps) + def test_invalid_timestamp_serialization(self, test_case_data: Dict[str, str]): + case_dict = json.loads(test_case_data) + with self.assertRaises((ValueError, KeyError)): + Timestamp.from_dict(copy.deepcopy(case_dict)) + valid_timestamps: DataSet = { "all": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ @@ -244,7 +243,10 @@ def test_timestamp_serialization(self, test_case_data: str): valid_snapshots: DataSet = { "all": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ - "meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}}', + "meta": { \ + "file1.txt": {"hashes": {"sha256" : "abc"}, "version": 1}, \ + "file2.txt": {"hashes": {"sha256" : "cde"}, "version": 1} \ + }}', "unrecognized field": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ "meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}, "foo": "bar"}', } @@ -292,6 +294,9 @@ def test_delegated_role_serialization(self, test_case_data: str): "keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ "roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ] \ }', + "empty keys and roles": '{"keys": {}, \ + "roles": [] \ + }', } @run_sub_tests_with_dataset(valid_delegations) @@ -332,11 +337,17 @@ def test_targetfile_serialization(self, test_case_data: str): valid_targets: DataSet = { "all attributes": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ - "targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } }, \ - "delegations": {"keys": {"keyid" : {"keytype": "rsa", \ - "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \ - "roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]} \ - }', + "targets": { \ + "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} }, \ + "file2.txt": {"length": 50, "hashes": {"sha256" : "cde"} } }, \ + "delegations": { \ + "keys": { \ + "keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ + "keyid2": {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ + "roles": [ \ + {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3}, \ + {"keyids": ["keyid2"], "name": "b", "terminating": true, "threshold": 4} ] \ + }}', "empty targets": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ "targets": {}, \ "delegations": {"keys": {"keyid" : {"keytype": "rsa", \