Skip to content

Commit

Permalink
Test JSONSerializer.serialize with validate=True
Browse files Browse the repository at this point in the history
Add a simple test verifying that by calling JSONSerializer.serialize()
on JSONSerializer with validate = True will fail if an attribute has a
not allowed value.

Comprehensive testing is not needed here as the validation used inside
JSONSerializer.serialize() relies on the validation that is done during
metadata objects initialization which is tested in detail.

Additionally, I added validation when calling JSONSerializer.serialize()
in the modify_metadata() inside the test_trusted_metadata_set.py module
as this function modifies an existing object and I wanted to make
sure it does that without compromising it.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
  • Loading branch information
MVrachev committed Jan 18, 2022
1 parent 42a0764 commit 5d5a0b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ def test_md__eq__(self) -> None:
md_obj = Metadata.from_file(f"{self.repo_dir}/metadata/{role}.json")
self.assertEqualRecursively(md_obj)

def test_serialize_with_validate(self) -> None:
# Assert that by changing one required attribute validation will fail.
root = Metadata.from_file(f"{self.repo_dir}/metadata/root.json")
root.signed.version = 0
with self.assertRaises(ValueError):
root.to_bytes(JSONSerializer(validate=True))

def test_to_from_bytes(self) -> None:
for metadata in TOP_LEVEL_ROLE_NAMES:
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_trusted_metadata_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Targets,
Timestamp,
)
from tuf.api.serialization.json import JSONSerializer
from tuf.ngclient._internal.trusted_metadata_set import TrustedMetadataSet

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,7 +50,7 @@ def modify_metadata(
metadata = Metadata.from_bytes(cls.metadata[rolename])
modification_func(metadata.signed)
metadata.sign(cls.keystore[rolename])
return metadata.to_bytes()
return metadata.to_bytes(JSONSerializer(validate=True))

@classmethod
def setUpClass(cls) -> None:
Expand Down

0 comments on commit 5d5a0b8

Please sign in to comment.