@@ -78,7 +78,7 @@ def setUpClass(cls) -> None:
78
78
79
79
# Load keys into memory
80
80
cls .keystore = {}
81
- for role in ["delegation" , "snapshot" , "targets" , "timestamp" ]:
81
+ for role in ["delegation" , Snapshot . type , Targets . type , Timestamp . type ]:
82
82
cls .keystore [role ] = import_ed25519_privatekey_from_file (
83
83
os .path .join (cls .keystore_dir , role + "_key" ),
84
84
password = "password" ,
@@ -92,10 +92,10 @@ def tearDownClass(cls) -> None:
92
92
93
93
def test_generic_read (self ) -> None :
94
94
for metadata , inner_metadata_cls in [
95
- ("root" , Root ),
96
- ("snapshot" , Snapshot ),
97
- ("timestamp" , Timestamp ),
98
- ("targets" , Targets ),
95
+ (Root . type , Root ),
96
+ (Snapshot . type , Snapshot ),
97
+ (Timestamp . type , Timestamp ),
98
+ (Targets . type , Targets ),
99
99
]:
100
100
101
101
# Load JSON-formatted metdata of each supported type from file
@@ -136,7 +136,7 @@ def test_compact_json(self) -> None:
136
136
)
137
137
138
138
def test_read_write_read_compare (self ) -> None :
139
- for metadata in ["root" , "snapshot" , "timestamp" , "targets" ]:
139
+ for metadata in [Root . type , Snapshot . type , Timestamp . type , Targets . type ]:
140
140
path = os .path .join (self .repo_dir , "metadata" , metadata + ".json" )
141
141
md_obj = Metadata .from_file (path )
142
142
@@ -148,7 +148,7 @@ def test_read_write_read_compare(self) -> None:
148
148
os .remove (path_2 )
149
149
150
150
def test_to_from_bytes (self ) -> None :
151
- for metadata in ["root" , "snapshot" , "timestamp" , "targets" ]:
151
+ for metadata in [Root . type , Snapshot . type , Timestamp . type , Targets . type ]:
152
152
path = os .path .join (self .repo_dir , "metadata" , metadata + ".json" )
153
153
with open (path , "rb" ) as f :
154
154
metadata_bytes = f .read ()
@@ -169,11 +169,11 @@ def test_sign_verify(self) -> None:
169
169
root = Metadata [Root ].from_file (root_path ).signed
170
170
171
171
# Locate the public keys we need from root
172
- targets_keyid = next (iter (root .roles ["targets" ].keyids ))
172
+ targets_keyid = next (iter (root .roles [Targets . type ].keyids ))
173
173
targets_key = root .keys [targets_keyid ]
174
- snapshot_keyid = next (iter (root .roles ["snapshot" ].keyids ))
174
+ snapshot_keyid = next (iter (root .roles [Snapshot . type ].keyids ))
175
175
snapshot_key = root .keys [snapshot_keyid ]
176
- timestamp_keyid = next (iter (root .roles ["timestamp" ].keyids ))
176
+ timestamp_keyid = next (iter (root .roles [Timestamp . type ].keyids ))
177
177
timestamp_key = root .keys [timestamp_keyid ]
178
178
179
179
# Load sample metadata (targets) and assert ...
@@ -192,7 +192,7 @@ def test_sign_verify(self) -> None:
192
192
with self .assertRaises (exceptions .UnsignedMetadataError ):
193
193
targets_key .verify_signature (md_obj , JSONSerializer ()) # type: ignore[arg-type]
194
194
195
- sslib_signer = SSlibSigner (self .keystore ["snapshot" ])
195
+ sslib_signer = SSlibSigner (self .keystore [Snapshot . type ])
196
196
# Append a new signature with the unrelated key and assert that ...
197
197
sig = md_obj .sign (sslib_signer , append = True )
198
198
# ... there are now two signatures, and
@@ -203,7 +203,7 @@ def test_sign_verify(self) -> None:
203
203
# ... the returned (appended) signature is for snapshot key
204
204
self .assertEqual (sig .keyid , snapshot_keyid )
205
205
206
- sslib_signer = SSlibSigner (self .keystore ["timestamp" ])
206
+ sslib_signer = SSlibSigner (self .keystore [Timestamp . type ])
207
207
# Create and assign (don't append) a new signature and assert that ...
208
208
md_obj .sign (sslib_signer , append = False )
209
209
# ... there now is only one signature,
@@ -218,7 +218,7 @@ def test_verify_failures(self) -> None:
218
218
root = Metadata [Root ].from_file (root_path ).signed
219
219
220
220
# Locate the timestamp public key we need from root
221
- timestamp_keyid = next (iter (root .roles ["timestamp" ].keyids ))
221
+ timestamp_keyid = next (iter (root .roles [Timestamp . type ].keyids ))
222
222
timestamp_key = root .keys [timestamp_keyid ]
223
223
224
224
# Load sample metadata (timestamp)
@@ -369,20 +369,20 @@ def test_metadata_verify_delegate(self) -> None:
369
369
role2 = Metadata [Targets ].from_file (role2_path )
370
370
371
371
# test the expected delegation tree
372
- root .verify_delegate ("root" , root )
373
- root .verify_delegate ("snapshot" , snapshot )
374
- root .verify_delegate ("targets" , targets )
372
+ root .verify_delegate (Root . type , root )
373
+ root .verify_delegate (Snapshot . type , snapshot )
374
+ root .verify_delegate (Targets . type , targets )
375
375
targets .verify_delegate ("role1" , role1 )
376
376
role1 .verify_delegate ("role2" , role2 )
377
377
378
378
# only root and targets can verify delegates
379
379
with self .assertRaises (TypeError ):
380
- snapshot .verify_delegate ("snapshot" , snapshot )
380
+ snapshot .verify_delegate (Snapshot . type , snapshot )
381
381
# verify fails for roles that are not delegated by delegator
382
382
with self .assertRaises (ValueError ):
383
383
root .verify_delegate ("role1" , role1 )
384
384
with self .assertRaises (ValueError ):
385
- targets .verify_delegate ("targets" , targets )
385
+ targets .verify_delegate (Targets . type , targets )
386
386
# verify fails when delegator has no delegations
387
387
with self .assertRaises (ValueError ):
388
388
role2 .verify_delegate ("role1" , role1 )
@@ -391,31 +391,31 @@ def test_metadata_verify_delegate(self) -> None:
391
391
expires = snapshot .signed .expires
392
392
snapshot .signed .bump_expiration ()
393
393
with self .assertRaises (exceptions .UnsignedMetadataError ):
394
- root .verify_delegate ("snapshot" , snapshot )
394
+ root .verify_delegate (Snapshot . type , snapshot )
395
395
snapshot .signed .expires = expires
396
396
397
397
# verify fails if roles keys do not sign the metadata
398
398
with self .assertRaises (exceptions .UnsignedMetadataError ):
399
- root .verify_delegate ("timestamp" , snapshot )
399
+ root .verify_delegate (Timestamp . type , snapshot )
400
400
401
401
# Add a key to snapshot role, make sure the new sig fails to verify
402
- ts_keyid = next (iter (root .signed .roles ["timestamp" ].keyids ))
403
- root .signed .add_key ("snapshot" , root .signed .keys [ts_keyid ])
402
+ ts_keyid = next (iter (root .signed .roles [Timestamp . type ].keyids ))
403
+ root .signed .add_key (Snapshot . type , root .signed .keys [ts_keyid ])
404
404
snapshot .signatures [ts_keyid ] = Signature (ts_keyid , "ff" * 64 )
405
405
406
406
# verify succeeds if threshold is reached even if some signatures
407
407
# fail to verify
408
- root .verify_delegate ("snapshot" , snapshot )
408
+ root .verify_delegate (Snapshot . type , snapshot )
409
409
410
410
# verify fails if threshold of signatures is not reached
411
- root .signed .roles ["snapshot" ].threshold = 2
411
+ root .signed .roles [Snapshot . type ].threshold = 2
412
412
with self .assertRaises (exceptions .UnsignedMetadataError ):
413
- root .verify_delegate ("snapshot" , snapshot )
413
+ root .verify_delegate (Snapshot . type , snapshot )
414
414
415
415
# verify succeeds when we correct the new signature and reach the
416
416
# threshold of 2 keys
417
- snapshot .sign (SSlibSigner (self .keystore ["timestamp" ]), append = True )
418
- root .verify_delegate ("snapshot" , snapshot )
417
+ snapshot .sign (SSlibSigner (self .keystore [Timestamp . type ]), append = True )
418
+ root .verify_delegate (Snapshot . type , snapshot )
419
419
420
420
def test_key_class (self ) -> None :
421
421
# Test if from_securesystemslib_key removes the private key from keyval
@@ -441,44 +441,44 @@ def test_root_add_key_and_remove_key(self) -> None:
441
441
)
442
442
443
443
# Assert that root does not contain the new key
444
- self .assertNotIn (keyid , root .signed .roles ["root" ].keyids )
444
+ self .assertNotIn (keyid , root .signed .roles [Root . type ].keyids )
445
445
self .assertNotIn (keyid , root .signed .keys )
446
446
447
447
# Add new root key
448
- root .signed .add_key ("root" , key_metadata )
448
+ root .signed .add_key (Root . type , key_metadata )
449
449
450
450
# Assert that key is added
451
- self .assertIn (keyid , root .signed .roles ["root" ].keyids )
451
+ self .assertIn (keyid , root .signed .roles [Root . type ].keyids )
452
452
self .assertIn (keyid , root .signed .keys )
453
453
454
454
# Confirm that the newly added key does not break
455
455
# the object serialization
456
456
root .to_dict ()
457
457
458
458
# Try adding the same key again and assert its ignored.
459
- pre_add_keyid = root .signed .roles ["root" ].keyids .copy ()
460
- root .signed .add_key ("root" , key_metadata )
461
- self .assertEqual (pre_add_keyid , root .signed .roles ["root" ].keyids )
459
+ pre_add_keyid = root .signed .roles [Root . type ].keyids .copy ()
460
+ root .signed .add_key (Root . type , key_metadata )
461
+ self .assertEqual (pre_add_keyid , root .signed .roles [Root . type ].keyids )
462
462
463
463
# Add the same key to targets role as well
464
- root .signed .add_key ("targets" , key_metadata )
464
+ root .signed .add_key (Targets . type , key_metadata )
465
465
466
466
# Add the same key to a nonexistent role.
467
467
with self .assertRaises (ValueError ):
468
468
root .signed .add_key ("nosuchrole" , key_metadata )
469
469
470
470
# Remove the key from root role (targets role still uses it)
471
- root .signed .remove_key ("root" , keyid )
472
- self .assertNotIn (keyid , root .signed .roles ["root" ].keyids )
471
+ root .signed .remove_key (Root . type , keyid )
472
+ self .assertNotIn (keyid , root .signed .roles [Root . type ].keyids )
473
473
self .assertIn (keyid , root .signed .keys )
474
474
475
475
# Remove the key from targets as well
476
- root .signed .remove_key ("targets" , keyid )
477
- self .assertNotIn (keyid , root .signed .roles ["targets" ].keyids )
476
+ root .signed .remove_key (Targets . type , keyid )
477
+ self .assertNotIn (keyid , root .signed .roles [Targets . type ].keyids )
478
478
self .assertNotIn (keyid , root .signed .keys )
479
479
480
480
with self .assertRaises (ValueError ):
481
- root .signed .remove_key ("root" , "nosuchkey" )
481
+ root .signed .remove_key (Root . type , "nosuchkey" )
482
482
with self .assertRaises (ValueError ):
483
483
root .signed .remove_key ("nosuchrole" , keyid )
484
484
@@ -670,7 +670,7 @@ def test_length_and_hash_validation(self) -> None:
670
670
targets_path = os .path .join (self .repo_dir , "metadata" , "targets.json" )
671
671
targets = Metadata [Targets ].from_file (targets_path )
672
672
file1_targetfile = targets .signed .targets ["file1.txt" ]
673
- filepath = os .path .join (self .repo_dir , "targets" , "file1.txt" )
673
+ filepath = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
674
674
675
675
with open (filepath , "rb" ) as file1 :
676
676
file1_targetfile .verify_length_and_hashes (file1 )
@@ -688,7 +688,7 @@ def test_length_and_hash_validation(self) -> None:
688
688
689
689
def test_targetfile_from_file (self ) -> None :
690
690
# Test with an existing file and valid hash algorithm
691
- file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
691
+ file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
692
692
targetfile_from_file = TargetFile .from_file (
693
693
file_path , file_path , ["sha256" ]
694
694
)
@@ -697,20 +697,20 @@ def test_targetfile_from_file(self) -> None:
697
697
targetfile_from_file .verify_length_and_hashes (file )
698
698
699
699
# Test with a non-existing file
700
- file_path = os .path .join (self .repo_dir , "targets" , "file123.txt" )
700
+ file_path = os .path .join (self .repo_dir , Targets . type , "file123.txt" )
701
701
with self .assertRaises (FileNotFoundError ):
702
702
TargetFile .from_file (
703
703
file_path , file_path , [sslib_hash .DEFAULT_HASH_ALGORITHM ]
704
704
)
705
705
706
706
# Test with an unsupported algorithm
707
- file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
707
+ file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
708
708
with self .assertRaises (exceptions .UnsupportedAlgorithmError ):
709
709
TargetFile .from_file (file_path , file_path , ["123" ])
710
710
711
711
def test_targetfile_from_data (self ) -> None :
712
712
data = b"Inline test content"
713
- target_file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
713
+ target_file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
714
714
715
715
# Test with a valid hash algorithm
716
716
targetfile_from_data = TargetFile .from_data (
0 commit comments