|
25 | 25 | Metadata,
|
26 | 26 | Snapshot,
|
27 | 27 | Timestamp,
|
28 |
| - Targets |
| 28 | + Targets, |
| 29 | + Key, |
| 30 | + Role |
29 | 31 | )
|
30 | 32 |
|
31 | 33 | from tuf.api.serialization import (
|
@@ -292,6 +294,69 @@ def test_metadata_timestamp(self):
|
292 | 294 | self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
|
293 | 295 |
|
294 | 296 |
|
| 297 | + def test_key_class(self): |
| 298 | + keys = { |
| 299 | + "59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{ |
| 300 | + "keytype": "ed25519", |
| 301 | + "keyval": { |
| 302 | + "public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd" |
| 303 | + }, |
| 304 | + "scheme": "ed25519" |
| 305 | + }, |
| 306 | + } |
| 307 | + for key_dict in keys.values(): |
| 308 | + # Testing that the workflow of deserializing and serializing |
| 309 | + # a key dictionary doesn't change the content. |
| 310 | + test_key_dict = key_dict.copy() |
| 311 | + key_obj = Key.from_dict(test_key_dict) |
| 312 | + self.assertEqual(key_dict, key_obj.to_dict()) |
| 313 | + # Test creating an instance without a required attribute. |
| 314 | + for key in key_dict.keys(): |
| 315 | + test_key_dict = key_dict.copy() |
| 316 | + del test_key_dict[key] |
| 317 | + with self.assertRaises(KeyError): |
| 318 | + Key.from_dict(test_key_dict) |
| 319 | + # Test creating a Key instance with wrong keyval format. |
| 320 | + key_dict["keyval"] = {} |
| 321 | + with self.assertRaises(ValueError): |
| 322 | + Key.from_dict(key_dict) |
| 323 | + |
| 324 | + |
| 325 | + def test_role_class(self): |
| 326 | + roles = { |
| 327 | + "root": { |
| 328 | + "keyids": [ |
| 329 | + "4e777de0d275f9d28588dd9a1606cc748e548f9e22b6795b7cb3f63f98035fcb" |
| 330 | + ], |
| 331 | + "threshold": 1 |
| 332 | + }, |
| 333 | + "snapshot": { |
| 334 | + "keyids": [ |
| 335 | + "59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d" |
| 336 | + ], |
| 337 | + "threshold": 1 |
| 338 | + }, |
| 339 | + } |
| 340 | + for role_dict in roles.values(): |
| 341 | + # Testing that the workflow of deserializing and serializing |
| 342 | + # a role dictionary doesn't change the content. |
| 343 | + test_role_dict = role_dict.copy() |
| 344 | + role_obj = Role.from_dict(test_role_dict) |
| 345 | + self.assertEqual(role_dict, role_obj.to_dict()) |
| 346 | + # Test creating an instance without a required attribute. |
| 347 | + for role_attr in role_dict.keys(): |
| 348 | + test_role_dict = role_dict.copy() |
| 349 | + del test_role_dict[role_attr] |
| 350 | + with self.assertRaises(KeyError): |
| 351 | + Key.from_dict(test_role_dict) |
| 352 | + # Test creating a Role instance with keyid dublicates. |
| 353 | + # for keyid in role_dict["keyids"]: |
| 354 | + role_dict["keyids"].append(role_dict["keyids"][0]) |
| 355 | + test_role_dict = role_dict.copy() |
| 356 | + with self.assertRaises(ValueError): |
| 357 | + Role.from_dict(test_role_dict) |
| 358 | + |
| 359 | + |
295 | 360 | def test_metadata_root(self):
|
296 | 361 | root_path = os.path.join(
|
297 | 362 | self.repo_dir, 'metadata', 'root.json')
|
|
0 commit comments