@@ -71,7 +71,7 @@ def setUpClass(cls):
71
71
72
72
# Load keys into memory
73
73
cls .keystore = {}
74
- for role in ["delegation" , "snapshot" , "targets" , "timestamp" ]:
74
+ for role in ["delegation" , Snapshot . type , Targets . type , Timestamp . type ]:
75
75
cls .keystore [role ] = import_ed25519_privatekey_from_file (
76
76
os .path .join (cls .keystore_dir , role + "_key" ),
77
77
password = "password" ,
@@ -85,10 +85,10 @@ def tearDownClass(cls):
85
85
86
86
def test_generic_read (self ):
87
87
for metadata , inner_metadata_cls in [
88
- ("root" , Root ),
89
- ("snapshot" , Snapshot ),
90
- ("timestamp" , Timestamp ),
91
- ("targets" , Targets ),
88
+ (Root . type , Root ),
89
+ (Snapshot . type , Snapshot ),
90
+ (Timestamp . type , Timestamp ),
91
+ (Targets . type , Targets ),
92
92
]:
93
93
94
94
# Load JSON-formatted metdata of each supported type from file
@@ -129,7 +129,7 @@ def test_compact_json(self):
129
129
)
130
130
131
131
def test_read_write_read_compare (self ):
132
- for metadata in ["root" , "snapshot" , "timestamp" , "targets" ]:
132
+ for metadata in [Root . type , Snapshot . type , Timestamp . type , Targets . type ]:
133
133
path = os .path .join (self .repo_dir , "metadata" , metadata + ".json" )
134
134
md_obj = Metadata .from_file (path )
135
135
@@ -141,7 +141,7 @@ def test_read_write_read_compare(self):
141
141
os .remove (path_2 )
142
142
143
143
def test_to_from_bytes (self ):
144
- for metadata in ["root" , "snapshot" , "timestamp" , "targets" ]:
144
+ for metadata in [Root . type , Snapshot . type , Timestamp . type , Targets . type ]:
145
145
path = os .path .join (self .repo_dir , "metadata" , metadata + ".json" )
146
146
with open (path , "rb" ) as f :
147
147
metadata_bytes = f .read ()
@@ -162,11 +162,11 @@ def test_sign_verify(self):
162
162
root = Metadata [Root ].from_file (root_path ).signed
163
163
164
164
# Locate the public keys we need from root
165
- targets_keyid = next (iter (root .roles ["targets" ].keyids ))
165
+ targets_keyid = next (iter (root .roles [Targets . type ].keyids ))
166
166
targets_key = root .keys [targets_keyid ]
167
- snapshot_keyid = next (iter (root .roles ["snapshot" ].keyids ))
167
+ snapshot_keyid = next (iter (root .roles [Snapshot . type ].keyids ))
168
168
snapshot_key = root .keys [snapshot_keyid ]
169
- timestamp_keyid = next (iter (root .roles ["timestamp" ].keyids ))
169
+ timestamp_keyid = next (iter (root .roles [Timestamp . type ].keyids ))
170
170
timestamp_key = root .keys [timestamp_keyid ]
171
171
172
172
# Load sample metadata (targets) and assert ...
@@ -185,7 +185,7 @@ def test_sign_verify(self):
185
185
with self .assertRaises (exceptions .UnsignedMetadataError ):
186
186
targets_key .verify_signature (md_obj , JSONSerializer ())
187
187
188
- sslib_signer = SSlibSigner (self .keystore ["snapshot" ])
188
+ sslib_signer = SSlibSigner (self .keystore [Snapshot . type ])
189
189
# Append a new signature with the unrelated key and assert that ...
190
190
sig = md_obj .sign (sslib_signer , append = True )
191
191
# ... there are now two signatures, and
@@ -196,7 +196,7 @@ def test_sign_verify(self):
196
196
# ... the returned (appended) signature is for snapshot key
197
197
self .assertEqual (sig .keyid , snapshot_keyid )
198
198
199
- sslib_signer = SSlibSigner (self .keystore ["timestamp" ])
199
+ sslib_signer = SSlibSigner (self .keystore [Timestamp . type ])
200
200
# Create and assign (don't append) a new signature and assert that ...
201
201
md_obj .sign (sslib_signer , append = False )
202
202
# ... there now is only one signature,
@@ -211,7 +211,7 @@ def test_verify_failures(self):
211
211
root = Metadata [Root ].from_file (root_path ).signed
212
212
213
213
# Locate the timestamp public key we need from root
214
- timestamp_keyid = next (iter (root .roles ["timestamp" ].keyids ))
214
+ timestamp_keyid = next (iter (root .roles [Timestamp . type ].keyids ))
215
215
timestamp_key = root .keys [timestamp_keyid ]
216
216
217
217
# Load sample metadata (timestamp)
@@ -362,20 +362,20 @@ def test_metadata_verify_delegate(self):
362
362
role2 = Metadata [Targets ].from_file (role2_path )
363
363
364
364
# test the expected delegation tree
365
- root .verify_delegate ("root" , root )
366
- root .verify_delegate ("snapshot" , snapshot )
367
- root .verify_delegate ("targets" , targets )
365
+ root .verify_delegate (Root . type , root )
366
+ root .verify_delegate (Snapshot . type , snapshot )
367
+ root .verify_delegate (Targets . type , targets )
368
368
targets .verify_delegate ("role1" , role1 )
369
369
role1 .verify_delegate ("role2" , role2 )
370
370
371
371
# only root and targets can verify delegates
372
372
with self .assertRaises (TypeError ):
373
- snapshot .verify_delegate ("snapshot" , snapshot )
373
+ snapshot .verify_delegate (Snapshot . type , snapshot )
374
374
# verify fails for roles that are not delegated by delegator
375
375
with self .assertRaises (ValueError ):
376
376
root .verify_delegate ("role1" , role1 )
377
377
with self .assertRaises (ValueError ):
378
- targets .verify_delegate ("targets" , targets )
378
+ targets .verify_delegate (Targets . type , targets )
379
379
# verify fails when delegator has no delegations
380
380
with self .assertRaises (ValueError ):
381
381
role2 .verify_delegate ("role1" , role1 )
@@ -384,31 +384,31 @@ def test_metadata_verify_delegate(self):
384
384
expires = snapshot .signed .expires
385
385
snapshot .signed .bump_expiration ()
386
386
with self .assertRaises (exceptions .UnsignedMetadataError ):
387
- root .verify_delegate ("snapshot" , snapshot )
387
+ root .verify_delegate (Snapshot . type , snapshot )
388
388
snapshot .signed .expires = expires
389
389
390
390
# verify fails if roles keys do not sign the metadata
391
391
with self .assertRaises (exceptions .UnsignedMetadataError ):
392
- root .verify_delegate ("timestamp" , snapshot )
392
+ root .verify_delegate (Timestamp . type , snapshot )
393
393
394
394
# Add a key to snapshot role, make sure the new sig fails to verify
395
- ts_keyid = next (iter (root .signed .roles ["timestamp" ].keyids ))
396
- root .signed .add_key ("snapshot" , root .signed .keys [ts_keyid ])
395
+ ts_keyid = next (iter (root .signed .roles [Timestamp . type ].keyids ))
396
+ root .signed .add_key (Snapshot . type , root .signed .keys [ts_keyid ])
397
397
snapshot .signatures [ts_keyid ] = Signature (ts_keyid , "ff" * 64 )
398
398
399
399
# verify succeeds if threshold is reached even if some signatures
400
400
# fail to verify
401
- root .verify_delegate ("snapshot" , snapshot )
401
+ root .verify_delegate (Snapshot . type , snapshot )
402
402
403
403
# verify fails if threshold of signatures is not reached
404
- root .signed .roles ["snapshot" ].threshold = 2
404
+ root .signed .roles [Snapshot . type ].threshold = 2
405
405
with self .assertRaises (exceptions .UnsignedMetadataError ):
406
- root .verify_delegate ("snapshot" , snapshot )
406
+ root .verify_delegate (Snapshot . type , snapshot )
407
407
408
408
# verify succeeds when we correct the new signature and reach the
409
409
# threshold of 2 keys
410
- snapshot .sign (SSlibSigner (self .keystore ["timestamp" ]), append = True )
411
- root .verify_delegate ("snapshot" , snapshot )
410
+ snapshot .sign (SSlibSigner (self .keystore [Timestamp . type ]), append = True )
411
+ root .verify_delegate (Snapshot . type , snapshot )
412
412
413
413
def test_key_class (self ):
414
414
# Test if from_securesystemslib_key removes the private key from keyval
@@ -434,44 +434,44 @@ def test_root_add_key_and_remove_key(self):
434
434
)
435
435
436
436
# Assert that root does not contain the new key
437
- self .assertNotIn (keyid , root .signed .roles ["root" ].keyids )
437
+ self .assertNotIn (keyid , root .signed .roles [Root . type ].keyids )
438
438
self .assertNotIn (keyid , root .signed .keys )
439
439
440
440
# Add new root key
441
- root .signed .add_key ("root" , key_metadata )
441
+ root .signed .add_key (Root . type , key_metadata )
442
442
443
443
# Assert that key is added
444
- self .assertIn (keyid , root .signed .roles ["root" ].keyids )
444
+ self .assertIn (keyid , root .signed .roles [Root . type ].keyids )
445
445
self .assertIn (keyid , root .signed .keys )
446
446
447
447
# Confirm that the newly added key does not break
448
448
# the object serialization
449
449
root .to_dict ()
450
450
451
451
# Try adding the same key again and assert its ignored.
452
- pre_add_keyid = root .signed .roles ["root" ].keyids .copy ()
453
- root .signed .add_key ("root" , key_metadata )
454
- self .assertEqual (pre_add_keyid , root .signed .roles ["root" ].keyids )
452
+ pre_add_keyid = root .signed .roles [Root . type ].keyids .copy ()
453
+ root .signed .add_key (Root . type , key_metadata )
454
+ self .assertEqual (pre_add_keyid , root .signed .roles [Root . type ].keyids )
455
455
456
456
# Add the same key to targets role as well
457
- root .signed .add_key ("targets" , key_metadata )
457
+ root .signed .add_key (Targets . type , key_metadata )
458
458
459
459
# Add the same key to a nonexistent role.
460
460
with self .assertRaises (ValueError ):
461
461
root .signed .add_key ("nosuchrole" , key_metadata )
462
462
463
463
# Remove the key from root role (targets role still uses it)
464
- root .signed .remove_key ("root" , keyid )
465
- self .assertNotIn (keyid , root .signed .roles ["root" ].keyids )
464
+ root .signed .remove_key (Root . type , keyid )
465
+ self .assertNotIn (keyid , root .signed .roles [Root . type ].keyids )
466
466
self .assertIn (keyid , root .signed .keys )
467
467
468
468
# Remove the key from targets as well
469
- root .signed .remove_key ("targets" , keyid )
470
- self .assertNotIn (keyid , root .signed .roles ["targets" ].keyids )
469
+ root .signed .remove_key (Targets . type , keyid )
470
+ self .assertNotIn (keyid , root .signed .roles [Targets . type ].keyids )
471
471
self .assertNotIn (keyid , root .signed .keys )
472
472
473
473
with self .assertRaises (ValueError ):
474
- root .signed .remove_key ("root" , "nosuchkey" )
474
+ root .signed .remove_key (Root . type , "nosuchkey" )
475
475
with self .assertRaises (ValueError ):
476
476
root .signed .remove_key ("nosuchrole" , keyid )
477
477
@@ -662,7 +662,7 @@ def test_length_and_hash_validation(self):
662
662
targets_path = os .path .join (self .repo_dir , "metadata" , "targets.json" )
663
663
targets = Metadata [Targets ].from_file (targets_path )
664
664
file1_targetfile = targets .signed .targets ["file1.txt" ]
665
- filepath = os .path .join (self .repo_dir , "targets" , "file1.txt" )
665
+ filepath = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
666
666
667
667
with open (filepath , "rb" ) as file1 :
668
668
file1_targetfile .verify_length_and_hashes (file1 )
@@ -680,7 +680,7 @@ def test_length_and_hash_validation(self):
680
680
681
681
def test_targetfile_from_file (self ):
682
682
# Test with an existing file and valid hash algorithm
683
- file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
683
+ file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
684
684
targetfile_from_file = TargetFile .from_file (
685
685
file_path , file_path , ["sha256" ]
686
686
)
@@ -689,20 +689,20 @@ def test_targetfile_from_file(self):
689
689
targetfile_from_file .verify_length_and_hashes (file )
690
690
691
691
# Test with a non-existing file
692
- file_path = os .path .join (self .repo_dir , "targets" , "file123.txt" )
692
+ file_path = os .path .join (self .repo_dir , Targets . type , "file123.txt" )
693
693
with self .assertRaises (FileNotFoundError ):
694
694
TargetFile .from_file (
695
695
file_path , file_path , [sslib_hash .DEFAULT_HASH_ALGORITHM ]
696
696
)
697
697
698
698
# Test with an unsupported algorithm
699
- file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
699
+ file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
700
700
with self .assertRaises (exceptions .UnsupportedAlgorithmError ):
701
701
TargetFile .from_file (file_path , file_path , ["123" ])
702
702
703
703
def test_targetfile_from_data (self ):
704
704
data = b"Inline test content"
705
- target_file_path = os .path .join (self .repo_dir , "targets" , "file1.txt" )
705
+ target_file_path = os .path .join (self .repo_dir , Targets . type , "file1.txt" )
706
706
707
707
# Test with a valid hash algorithm
708
708
targetfile_from_data = TargetFile .from_data (
0 commit comments