Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/test_metadata_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,43 @@ def test_delegation_serialization(self, test_case_data: str):
delegation = Delegations.from_dict(copy.deepcopy(case_dict))
self.assertDictEqual(case_dict, delegation.to_dict())

valid_delegations: utils.DataSet = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use the dataset defined at line 321

invalid_delegations: utils.DataSet = {

and the test function defined in 336
def test_invalid_delegation_serialization(self, test_case_data: str):

instead of defining a valid_delegations and a new test function?

We aim to group all valid tests related to a specific class in one dataset and test function.
Also, the name valid_delegations is not correct.
Those are actually cases of invalid_delegations as when you call Delegations.from_dict(copy.deepcopy(case_dict)) it causes ValueError as you catch it inside your the test function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a concern about how to make sure that the ValueError is the correct unit we want.
I mean, we test the ValueError raised, but we don't check if the message comes from the part we want to cover.
Unless we don't want to go deep in this level, it is ok.

Copy link
Copy Markdown
Collaborator

@MVrachev MVrachev Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a more general question that probably is worth adressing.
I think for sure we want to have all invalid tests for Delegations in one single place, but I understand what you mean.
Maybe we can check the error message and type inside the with?
What do you think @jku?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed. If agreed in checking the type and error message, we can address it in another issue.

"using root as delegate role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
{"keyids": ["keyid"], "name": "root", "terminating": true, "paths": ["fn1"], "threshold": 3}] \
Comment thread
kairoaraujo marked this conversation as resolved.
Outdated
}',
"using snapshot as delegate role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
{"keyids": ["keyid"], "name": "snapshot", "terminating": true, "paths": ["fn1"], "threshold": 3}] \
}',
"using targets as delegate role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
{"keyids": ["keyid"], "name": "targets", "terminating": true, "paths": ["fn1"], "threshold": 3}] \
}',
"using timestamp as delegate role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
{"keyids": ["keyid"], "name": "root", "terminating": true, "paths": ["fn1"], "threshold": 3}] \
Comment thread
kairoaraujo marked this conversation as resolved.
Outdated
}',
"using valid and top-level role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \
"roles": [ \
{"keyids": ["keyid"], "name": "b", "terminating": true, "paths": ["fn1"], "threshold": 3}, \
Comment thread
kairoaraujo marked this conversation as resolved.
Outdated
{"keyids": ["keyid2"], "name": "root", "terminating": true, "paths": ["fn2"], "threshold": 4} ] \
}',
}

@utils.run_sub_tests_with_dataset(valid_delegations)
def test_delegation_top_role_names(self, test_case_data: str):
case_dict = json.loads(test_case_data)
with self.assertRaises(ValueError):
Delegations.from_dict(copy.deepcopy(case_dict))


invalid_targetfiles: utils.DataSet = {
"no hashes": '{"length": 1}',
"no length": '{"hashes": {"sha256": "abc"}}'
Expand Down
6 changes: 6 additions & 0 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,12 @@ def __init__(
unrecognized_fields: Optional[Mapping[str, Any]] = None,
):
self.keys = keys
if [role for role in set(roles) if role in TOP_LEVEL_ROLE_NAMES]:
raise ValueError(
"A delegated role can not use top-level role names ("
f"{', '.join(TOP_LEVEL_ROLE_NAMES)})"
)
Comment thread
kairoaraujo marked this conversation as resolved.
Outdated

self.roles = roles
self.unrecognized_fields = unrecognized_fields or {}

Expand Down