@@ -451,21 +451,23 @@ def test_delegated_role_class(self):
451451 with self .assertRaises (ValueError ):
452452 DelegatedRole .from_dict (role .copy ())
453453
454- # Test creating DelegatedRole only with "path_hash_prefixes"
454+ # Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
455455 del role ["paths" ]
456- DelegatedRole .from_dict (role .copy ())
457- role ["paths" ] = "foo"
456+ role ["path_hash_prefixes" ] = []
457+ role_obj = DelegatedRole .from_dict (role .copy ())
458+ self .assertEqual (role_obj .to_dict (), role )
458459
459- # Test creating DelegatedRole only with "paths"
460+ # Test creating DelegatedRole only with "paths" (now an empty one)
460461 del role ["path_hash_prefixes" ]
461- DelegatedRole .from_dict (role .copy ())
462- role ["path_hash_prefixes" ] = "foo"
462+ role ["paths" ] = []
463+ role_obj = DelegatedRole .from_dict (role .copy ())
464+ self .assertEqual (role_obj .to_dict (), role )
463465
464466 # Test creating DelegatedRole without "paths" and
465467 # "path_hash_prefixes" set
466468 del role ["paths" ]
467- del role [ "path_hash_prefixes" ]
468- DelegatedRole . from_dict ( role )
469+ role_obj = DelegatedRole . from_dict ( role . copy ())
470+ self . assertEqual ( role_obj . to_dict (), role )
469471
470472
471473 def test_delegation_class (self ):
@@ -495,6 +497,21 @@ def test_delegation_class(self):
495497 delegations = Delegations .from_dict (copy .deepcopy (delegations_dict ))
496498 self .assertEqual (delegations_dict , delegations .to_dict ())
497499
500+ # empty keys and roles
501+ delegations_dict = {"keys" :{}, "roles" :[]}
502+ delegations = Delegations .from_dict (delegations_dict .copy ())
503+ self .assertEqual (delegations_dict , delegations .to_dict ())
504+
505+ # Test some basic missing or broken input
506+ invalid_delegations_dicts = [
507+ {},
508+ {"keys" :None , "roles" :None },
509+ {"keys" :{"foo" :0 }, "roles" :[]},
510+ {"keys" :{}, "roles" :["foo" ]},
511+ ]
512+ for d in invalid_delegations_dicts :
513+ with self .assertRaises ((KeyError , AttributeError )):
514+ Delegations .from_dict (d )
498515
499516 def test_metadata_targets (self ):
500517 targets_path = os .path .join (
0 commit comments