|
13 | 13 | import sys
|
14 | 14 | import tempfile
|
15 | 15 | import unittest
|
| 16 | +from copy import copy |
16 | 17 | from datetime import datetime, timedelta
|
17 |
| -from typing import ClassVar, Dict |
| 18 | +from typing import Any, ClassVar, Dict |
18 | 19 |
|
19 | 20 | from securesystemslib import hash as sslib_hash
|
20 | 21 | from securesystemslib.interface import (
|
@@ -51,7 +52,7 @@ class TestMetadata(unittest.TestCase):
|
51 | 52 | temporary_directory: ClassVar[str]
|
52 | 53 | repo_dir: ClassVar[str]
|
53 | 54 | keystore_dir: ClassVar[str]
|
54 |
| - keystore: ClassVar[Dict[str, str]] |
| 55 | + keystore: ClassVar[Dict[str, Dict[str, Any]]] |
55 | 56 |
|
56 | 57 | @classmethod
|
57 | 58 | def setUpClass(cls) -> None:
|
@@ -126,6 +127,16 @@ def test_generic_read(self) -> None:
|
126 | 127 |
|
127 | 128 | os.remove(bad_metadata_path)
|
128 | 129 |
|
| 130 | + def test_md_read_write_file_exceptions(self) -> None: |
| 131 | + # Test writing to a file with bad filename |
| 132 | + with self.assertRaises(exceptions.StorageError): |
| 133 | + Metadata.from_file("bad-metadata.json") |
| 134 | + |
| 135 | + # Test serializing to a file with bad filename |
| 136 | + with self.assertRaises(exceptions.StorageError): |
| 137 | + md = Metadata.from_file(f"{self.repo_dir}/metadata/root.json") |
| 138 | + md.to_file("") |
| 139 | + |
129 | 140 | def test_compact_json(self) -> None:
|
130 | 141 | path = os.path.join(self.repo_dir, "metadata", "targets.json")
|
131 | 142 | md_obj = Metadata.from_file(path)
|
@@ -212,6 +223,17 @@ def test_sign_verify(self) -> None:
|
212 | 223 | with self.assertRaises(exceptions.UnsignedMetadataError):
|
213 | 224 | targets_key.verify_signature(md_obj)
|
214 | 225 |
|
| 226 | + def test_sign_failures(self) -> None: |
| 227 | + # Test throwing UnsignedMetadataError because of signing problems |
| 228 | + # related to bad information in the signer. |
| 229 | + md = Metadata.from_file(f"{self.repo_dir}/metadata/snapshot.json") |
| 230 | + key_dict = copy(self.keystore[Snapshot.type]) |
| 231 | + key_dict["keytype"] = "rsa" |
| 232 | + key_dict["scheme"] = "bad_scheme" |
| 233 | + sslib_signer = SSlibSigner(key_dict) |
| 234 | + with self.assertRaises(exceptions.UnsignedMetadataError): |
| 235 | + md.sign(sslib_signer) |
| 236 | + |
215 | 237 | def test_verify_failures(self) -> None:
|
216 | 238 | root_path = os.path.join(self.repo_dir, "metadata", "root.json")
|
217 | 239 | root = Metadata[Root].from_file(root_path).signed
|
|
0 commit comments