2323 Metadata ,
2424 MetaFile ,
2525 Role ,
26- Root ,
27- Snapshot ,
2826 TargetFile ,
29- Targets ,
30- Timestamp ,
3127)
3228
3329
@@ -41,34 +37,76 @@ def setUpClass(cls) -> None:
4137 cls .repo_dir = os .path .join (
4238 utils .TESTS_DIR , "repository_data" , "repository" , "metadata"
4339 )
44- cls .metadata = {}
40+
41+ # Store class instances in this dict instead of creating them inside the
42+ # test function in order to escape the need for reinitialization of the
43+ # instances on each run of the test function.
44+ cls .objects = {}
4545 for md in TOP_LEVEL_ROLE_NAMES :
4646 with open (os .path .join (cls .repo_dir , f"{ md } .json" ), "rb" ) as f :
47- cls .metadata [md ] = f .read ()
47+ data = f .read ()
48+ cls .objects [md .capitalize ()] = Metadata .from_bytes (data ).signed
49+
50+ cls .objects ["Metadata" ] = Metadata (cls .objects ["Timestamp" ], {})
51+ cls .objects ["Signed" ] = cls .objects ["Timestamp" ]
52+ cls .objects ["Key" ] = Key (
53+ "id" , "rsa" , "rsassa-pss-sha256" , {"public" : "foo" }
54+ )
55+ cls .objects ["Role" ] = Role (["keyid1" , "keyid2" ], 3 )
56+ cls .objects ["MetaFile" ] = MetaFile (1 , 12 , {"sha256" : "abc" })
57+ cls .objects ["DelegatedRole" ] = DelegatedRole ("a" , [], 1 , False , ["d" ])
58+ cls .objects ["Delegations" ] = Delegations (
59+ {"keyid" : cls .objects ["Key" ]}, {"a" : cls .objects ["DelegatedRole" ]}
60+ )
61+ cls .objects ["TargetFile" ] = TargetFile (
62+ 1 , {"sha256" : "abc" }, "file1.txt"
63+ )
64+
65+ # Keys are class names.
66+ # Values are dictionaries containing attribute names and their new values.
67+ classes_attributes_modifications : utils .DataSet = {
68+ "Metadata" : {"signed" : None , "signatures" : None },
69+ "Signed" : {"version" : - 1 , "spec_version" : "0.0.0" },
70+ "Key" : {"keyid" : "a" , "keytype" : "foo" , "scheme" : "b" , "keyval" : "b" },
71+ "Role" : {"keyids" : [], "threshold" : 10 },
72+ "Root" : {"consistent_snapshot" : None , "keys" : {}},
73+ "MetaFile" : {"version" : None , "length" : None , "hashes" : {}},
74+ "Timestamp" : {"snapshot_meta" : None },
75+ "Snapshot" : {"meta" : None },
76+ "DelegatedRole" : {
77+ "name" : "" ,
78+ "terminating" : None ,
79+ "paths" : ["" ],
80+ "path_hash_prefixes" : ["" ],
81+ },
82+ "Delegations" : {"keys" : {}, "roles" : {}},
83+ "TargetFile" : {"length" : 0 , "hashes" : {}, "path" : "" },
84+ "Targets" : {"targets" : {}, "delegations" : []},
85+ }
86+
87+ @utils .run_sub_tests_with_dataset (classes_attributes_modifications )
88+ def test_classes_eq_ (self , test_case_data : Dict [str , Any ]) -> None :
89+ obj = self .objects [self .case_name ] # pylint: disable=no-member
4890
49- def copy_and_simple_assert (self , obj : Any ) -> Any :
5091 # Assert that obj is not equal to an object from another type
5192 self .assertNotEqual (obj , "" )
52- result_obj = copy .deepcopy (obj )
93+ obj_2 = copy .deepcopy (obj )
5394 # Assert that __eq__ works for equal objects.
54- self .assertEqual (obj , result_obj )
55- return result_obj
56-
57- def test_metadata_eq_ (self ) -> None :
58- md = Metadata .from_bytes (self .metadata ["snapshot" ])
59- md_2 : Metadata = self .copy_and_simple_assert (md )
95+ self .assertEqual (obj , obj_2 )
6096
61- for attr , value in [("signed" , None ), ("signatures" , None )]:
62- setattr (md_2 , attr , value )
63- self .assertNotEqual (md , md_2 , f"Failed case: { attr } " )
97+ for attr , value in test_case_data .items ():
98+ original_value = getattr (obj_2 , attr )
99+ setattr (obj_2 , attr , value )
100+ # Assert that the original object != modified one.
101+ self .assertNotEqual (obj , obj_2 , f"Failed case: { attr } " )
64102 # Restore the old value of the attribute.
65- setattr (md_2 , attr , getattr ( md , attr ) )
103+ setattr (obj_2 , attr , original_value )
66104
67105 def test_md_eq_signatures_reversed_order (self ) -> None :
68106 # Test comparing objects with same signatures but different order.
69107
70108 # Remove all signatures and create new ones.
71- md = Metadata . from_bytes ( self .metadata [ "snapshot" ])
109+ md : Metadata = self .objects [ "Metadata" ]
72110 md .signatures = {"a" : Signature ("a" , "a" ), "b" : Signature ("b" , "b" )}
73111 md_2 = copy .deepcopy (md )
74112 # Reverse signatures order in md_2.
@@ -83,7 +121,7 @@ def test_md_eq_signatures_reversed_order(self) -> None:
83121
84122 def test_md_eq_special_signatures_tests (self ) -> None :
85123 # Test that metadata objects with different signatures are not equal.
86- md = Metadata . from_bytes ( self .metadata [ "snapshot" ])
124+ md : Metadata = self .objects [ "Metadata" ]
87125 md_2 = copy .deepcopy (md )
88126 md_2 .signatures = {}
89127 self .assertNotEqual (md , md_2 )
@@ -96,171 +134,6 @@ def test_md_eq_special_signatures_tests(self) -> None:
96134 md_2 .signatures = "" # type: ignore
97135 self .assertNotEqual (md , md_2 )
98136
99- def test_signed_eq_ (self ) -> None :
100- md = Metadata .from_bytes (self .metadata ["snapshot" ])
101- md_2 : Metadata = self .copy_and_simple_assert (md )
102-
103- # We don't need to make "signed" = None as that was done when testing
104- # metadata attribute modifications.
105- for attr , value in [("version" , - 1 ), ("spec_version" , "0.0.0" )]:
106- setattr (md_2 .signed , attr , value )
107- self .assertNotEqual (md .signed , md_2 .signed , f"Failed case: { attr } " )
108- # Restore the old value of the attribute.
109- setattr (md_2 .signed , attr , getattr (md .signed , attr ))
110-
111- def test_key_eq_ (self ) -> None :
112- key_dict = {
113- "keytype" : "rsa" ,
114- "scheme" : "rsassa-pss-sha256" ,
115- "keyval" : {"public" : "foo" },
116- }
117- key = Key .from_dict ("12sa12" , key_dict )
118- key_2 : Key = self .copy_and_simple_assert (key )
119- for attr , value in [
120- ("keyid" , "a" ),
121- ("keytype" , "foo" ),
122- ("scheme" , "b" ),
123- ("keytype" , "b" ),
124- ]:
125- setattr (key_2 , attr , value )
126- self .assertNotEqual (key , key_2 , f"Failed case: { attr } " )
127- # Restore the old value of the attribute.
128- setattr (key_2 , attr , getattr (key , attr ))
129-
130- def test_role_eq_ (self ) -> None :
131- role_dict = {
132- "keyids" : ["keyid1" , "keyid2" ],
133- "threshold" : 3 ,
134- }
135- role = Role .from_dict (role_dict )
136- role_2 : Role = self .copy_and_simple_assert (role )
137-
138- for attr , value in [("keyids" , []), ("threshold" , 10 )]:
139- setattr (role_2 , attr , value )
140- self .assertNotEqual (role , role_2 , f"Failed case: { attr } " )
141- # Restore the old value of the attribute.
142- setattr (role_2 , attr , getattr (role , attr ))
143-
144- def test_root_eq_ (self ) -> None :
145- md = Metadata .from_bytes (self .metadata ["root" ])
146- signed_copy : Root = self .copy_and_simple_assert (md .signed )
147-
148- # Common attributes between Signed and Root doesn't need testing.
149- # Ignore mypy request for type annotations on attr and value
150- for attr , value in [ # type: ignore
151- ("consistent_snapshot" , None ),
152- ("keys" , {}),
153- ("roles" , {}),
154- ]:
155-
156- setattr (signed_copy , attr , value )
157- self .assertNotEqual (md .signed , signed_copy , f"Failed case: { attr } " )
158- # Restore the old value of the attribute.
159- setattr (signed_copy , attr , getattr (md .signed , attr ))
160-
161- def test_metafile_eq_ (self ) -> None :
162- metafile_dict = {
163- "version" : 1 ,
164- "length" : 12 ,
165- "hashes" : {"sha256" : "abc" },
166- }
167- metafile = MetaFile .from_dict (metafile_dict )
168- metafile_2 : MetaFile = self .copy_and_simple_assert (metafile )
169-
170- # Ignore mypy request for type annotations on attr and value
171- for attr , value in [ # type: ignore
172- ("version" , None ),
173- ("length" , None ),
174- ("hashes" , {}),
175- ]:
176- setattr (metafile_2 , attr , value )
177- self .assertNotEqual (metafile , metafile_2 , f"Failed case: { attr } " )
178- # Restore the old value of the attribute.
179- setattr (metafile_2 , attr , getattr (metafile , attr ))
180-
181- def test_timestamp_eq_ (self ) -> None :
182- md = Metadata .from_bytes (self .metadata ["timestamp" ])
183- signed_copy : Timestamp = self .copy_and_simple_assert (md .signed )
184-
185- # Common attributes between Signed and Timestamp doesn't need testing.
186- setattr (signed_copy , "snapshot_meta" , None )
187- self .assertNotEqual (md .signed , signed_copy )
188-
189- def test_snapshot_eq_ (self ) -> None :
190- md = Metadata .from_bytes (self .metadata ["snapshot" ])
191- signed_copy : Snapshot = self .copy_and_simple_assert (md .signed )
192-
193- # Common attributes between Signed and Snapshot doesn't need testing.
194- setattr (signed_copy , "meta" , None )
195- self .assertNotEqual (md .signed , signed_copy )
196-
197- def test_delegated_role_eq_ (self ) -> None :
198- delegated_role_dict = {
199- "keyids" : ["keyid" ],
200- "name" : "a" ,
201- "terminating" : False ,
202- "threshold" : 1 ,
203- "paths" : ["fn1" , "fn2" ],
204- }
205- delegated_role = DelegatedRole .from_dict (delegated_role_dict )
206- delegated_role_2 : DelegatedRole = self .copy_and_simple_assert (
207- delegated_role
208- )
209-
210- # Common attributes between DelegatedRole and Role doesn't need testing.
211- for attr , value in [
212- ("name" , "" ),
213- ("terminating" , None ),
214- ("paths" , ["" ]),
215- ("path_hash_prefixes" , ["" ]),
216- ]:
217- setattr (delegated_role_2 , attr , value )
218- msg = f"Failed case: { attr } "
219- self .assertNotEqual (delegated_role , delegated_role_2 , msg )
220- # Restore the old value of the attribute.
221- setattr (delegated_role_2 , attr , getattr (delegated_role , attr ))
222-
223- def test_delegations_eq_ (self ) -> None :
224- delegations_dict = {
225- "keys" : {
226- "keyid2" : {
227- "keytype" : "ed25519" ,
228- "scheme" : "ed25519" ,
229- "keyval" : {"public" : "bar" },
230- }
231- },
232- "roles" : [
233- {
234- "keyids" : ["keyid2" ],
235- "name" : "b" ,
236- "terminating" : True ,
237- "paths" : ["fn2" ],
238- "threshold" : 4 ,
239- }
240- ],
241- }
242- delegations = Delegations .from_dict (delegations_dict )
243- delegations_2 : Delegations = self .copy_and_simple_assert (delegations )
244- # Ignore mypy request for type annotations on attr and value
245- for attr , value in [("keys" , {}), ("roles" , {})]: # type: ignore
246- setattr (delegations_2 , attr , value )
247- msg = f"Failed case: { attr } "
248- self .assertNotEqual (delegations , delegations_2 , msg )
249- # Restore the old value of the attribute.
250- setattr (delegations_2 , attr , getattr (delegations , attr ))
251-
252- def test_targetfile_eq_ (self ) -> None :
253- targetfile_dict = {
254- "length" : 12 ,
255- "hashes" : {"sha256" : "abc" },
256- }
257- targetfile = TargetFile .from_dict (targetfile_dict , "file1.txt" )
258- targetfile_2 : TargetFile = self .copy_and_simple_assert (targetfile )
259-
260- # Common attr between TargetFile and MetaFile doesn't need testing.
261- setattr (targetfile_2 , "path" , "" )
262- self .assertNotEqual (targetfile , targetfile_2 )
263-
264137 def test_delegations_eq_roles_reversed_order (self ) -> None :
265138 # Test comparing objects with same delegated roles but different order.
266139 role_one_dict = {
@@ -303,18 +176,6 @@ def test_delegations_eq_roles_reversed_order(self) -> None:
303176
304177 self .assertEqual (delegations , delegations_2 )
305178
306- def test_targets_eq_ (self ) -> None :
307- md = Metadata .from_bytes (self .metadata ["targets" ])
308- signed_copy : Targets = self .copy_and_simple_assert (md .signed )
309-
310- # Common attributes between Targets and Signed doesn't need testing.
311- # Ignore mypy request for type annotations on attr and value
312- for attr , value in [("targets" , {}), ("delegations" , [])]: # type: ignore
313- setattr (signed_copy , attr , value )
314- self .assertNotEqual (md .signed , signed_copy , f"Failed case: { attr } " )
315- # Restore the old value of the attribute.
316- setattr (signed_copy , attr , getattr (md .signed , attr ))
317-
318179
319180# Run unit test.
320181if __name__ == "__main__" :
0 commit comments