mirror of
https://github.com/theupdateframework/python-tuf
synced 2026-05-24 10:08:28 +00:00
Merge pull request #1490 from MVrachev/metadata-to-bytes
Metadata API: add Metadata.to_bytes()
This commit is contained in:
commit
69dca08b9a
2 changed files with 47 additions and 7 deletions
|
|
@ -159,6 +159,28 @@ def test_read_write_read_compare(self):
|
|||
os.remove(path_2)
|
||||
|
||||
|
||||
def test_to_from_bytes(self):
|
||||
for metadata in ["root", "snapshot", "timestamp", "targets"]:
|
||||
path = os.path.join(self.repo_dir, 'metadata', metadata + '.json')
|
||||
with open(path, 'rb') as f:
|
||||
metadata_bytes = f.read()
|
||||
metadata_obj = Metadata.from_bytes(metadata_bytes)
|
||||
# Comparate that from_bytes/to_bytes doesn't change the content
|
||||
# for two cases for the serializer: noncompact and compact.
|
||||
|
||||
# Case 1: test noncompact by overriding the default serializer.
|
||||
self.assertEqual(
|
||||
metadata_obj.to_bytes(JSONSerializer()), metadata_bytes
|
||||
)
|
||||
|
||||
# Case 2: test compact by using the default serializer.
|
||||
obj_bytes = metadata_obj.to_bytes()
|
||||
metadata_obj_2 = Metadata.from_bytes(obj_bytes)
|
||||
self.assertEqual(
|
||||
metadata_obj_2.to_bytes(), obj_bytes
|
||||
)
|
||||
|
||||
|
||||
def test_sign_verify(self):
|
||||
root_path = os.path.join(self.repo_dir, 'metadata', 'root.json')
|
||||
root:Root = Metadata.from_file(root_path).signed
|
||||
|
|
|
|||
|
|
@ -185,6 +185,29 @@ def from_bytes(
|
|||
|
||||
return deserializer.deserialize(data)
|
||||
|
||||
def to_bytes(
|
||||
self, serializer: Optional[MetadataSerializer] = None
|
||||
) -> bytes:
|
||||
"""Return the serialized TUF file format as bytes.
|
||||
|
||||
Arguments:
|
||||
serializer: A MetadataSerializer instance that implements the
|
||||
desired serialization format. Default is JSONSerializer.
|
||||
|
||||
Raises:
|
||||
tuf.api.serialization.SerializationError:
|
||||
The metadata object cannot be serialized.
|
||||
"""
|
||||
|
||||
if serializer is None:
|
||||
# Use local scope import to avoid circular import errors
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from tuf.api.serialization.json import JSONSerializer
|
||||
|
||||
serializer = JSONSerializer(compact=True)
|
||||
|
||||
return serializer.serialize(self)
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
"""Returns the dict representation of self."""
|
||||
|
||||
|
|
@ -214,15 +237,10 @@ def to_file(
|
|||
The file cannot be written.
|
||||
"""
|
||||
|
||||
if serializer is None:
|
||||
# Use local scope import to avoid circular import errors
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from tuf.api.serialization.json import JSONSerializer
|
||||
|
||||
serializer = JSONSerializer(compact=True)
|
||||
bytes_data = self.to_bytes(serializer)
|
||||
|
||||
with tempfile.TemporaryFile() as temp_file:
|
||||
temp_file.write(serializer.serialize(self))
|
||||
temp_file.write(bytes_data)
|
||||
persist_temp_file(temp_file, filename, storage_backend)
|
||||
|
||||
# Signatures.
|
||||
|
|
|
|||
Loading…
Reference in a new issue