Skip to content

Commit

Permalink
new API: test containers for zero or more elements
Browse files Browse the repository at this point in the history
Test metadata (de)serialization with input data containing containers
with zero or more elements.

Here is the status for the different use cases:
Root keys:
- many keys: added
Root roles:
- many roles: added
Root role keyids:
- many keids: already added in #1481
MetaFile hashes:
- many hashes: already tested
- zero hashes: added. Testing as invalid test case.
Delegation keys:
- many keys: added
Delegation role keyids:
- many keyids: added
Delegation role paths:
- many paths: already tested
Delegation role path_hash_prefixes:
- many path_hash_path_prefixes: already tested

According to the spec having an empty container for any of these cases:
- Root keys
- Root roles
- Root role keyids
- Delegation keys
- DelegationRole keyids
- DelegationRole paths
- DelegationRole path_hash_prefixes
is not allowed, but for the purpose of interactive object construction
we don't block those use-cases.
We don't want to add tests, because we don't want to
advertise this behavior.

In the future, we are going to add validation that those cases don't
occur which will be called when serializing the object back to
bytes/dictionary/file.

Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev committed Jul 28, 2021
1 parent 8e9677d commit 41c661e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/test_metadata_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ 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)
Expand All @@ -184,6 +203,7 @@ 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)
Expand All @@ -198,6 +218,7 @@ def test_invalid_metafile_serialization(self, test_case_data: Dict[str, str]):
"no length": '{"hashes": {"sha256" : "abc"}, "version": 1 }',
"no hashes": '{"length": 12, "version": 1}',
"unrecognized field": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1, "foo": "bar"}',
"many hashes": '{"hashes": {"sha256" : "abc", "sha512": "cde"}, "length": 12, "version": 1}',
}

@run_sub_tests_with_dataset(valid_metafiles)
Expand Down Expand Up @@ -246,6 +267,9 @@ def test_snapshot_serialization(self, test_case_data: str):
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3}',
"unrecognized field":
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3, "foo": "bar"}',
"many keyids":
'{"keyids": ["keyid1", "keyid2"], "name": "a", "paths": ["fn1", "fn2"], \
"terminating": false, "threshold": 1}',
}

@run_sub_tests_with_dataset(valid_delegated_roles)
Expand All @@ -262,6 +286,12 @@ def test_delegated_role_serialization(self, test_case_data: str):
'{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ], \
"foo": "bar"}',
"many keys":
'{"keys": { \
"keyid1" : {"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} ] \
}',
}

@run_sub_tests_with_dataset(valid_delegations)
Expand Down

0 comments on commit 41c661e

Please sign in to comment.