|
29 | 29 | Targets,
|
30 | 30 | Key,
|
31 | 31 | Role,
|
| 32 | + MetaFile, |
| 33 | + TargetFile, |
32 | 34 | Delegations,
|
33 | 35 | DelegatedRole,
|
34 | 36 | )
|
@@ -241,33 +243,74 @@ def test_metadata_base(self):
|
241 | 243 | self.assertFalse(is_expired)
|
242 | 244 | md.signed.expires = expires
|
243 | 245 |
|
| 246 | + |
| 247 | + def test_metafile_class(self): |
| 248 | + # Test from_dict and to_dict with all attributes. |
| 249 | + data = { |
| 250 | + "hashes": { |
| 251 | + "sha256": "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab" |
| 252 | + }, |
| 253 | + "length": 515, |
| 254 | + "version": 1 |
| 255 | + } |
| 256 | + metafile_obj = MetaFile.from_dict(copy.copy(data)) |
| 257 | + self.assertEqual(metafile_obj.to_dict(), data) |
| 258 | + |
| 259 | + # Test from_dict and to_dict without length. |
| 260 | + del data["length"] |
| 261 | + metafile_obj = MetaFile.from_dict(copy.copy(data)) |
| 262 | + self.assertEqual(metafile_obj.to_dict(), data) |
| 263 | + |
| 264 | + # Test from_dict and to_dict without length and hashes. |
| 265 | + del data["hashes"] |
| 266 | + metafile_obj = MetaFile.from_dict(copy.copy(data)) |
| 267 | + self.assertEqual(metafile_obj.to_dict(), data) |
| 268 | + |
| 269 | + |
| 270 | + def test_targetfile_class(self): |
| 271 | + # Test from_dict and to_dict with all attributes. |
| 272 | + data = { |
| 273 | + "custom": { |
| 274 | + "file_permissions": "0644" |
| 275 | + }, |
| 276 | + "hashes": { |
| 277 | + "sha256": "65b8c67f51c993d898250f40aa57a317d854900b3a04895464313e48785440da", |
| 278 | + "sha512": "467430a68afae8e9f9c0771ea5d78bf0b3a0d79a2d3d3b40c69fde4dd42c461448aef76fcef4f5284931a1ffd0ac096d138ba3a0d6ca83fa8d7285a47a296f77" |
| 279 | + }, |
| 280 | + "length": 31 |
| 281 | + } |
| 282 | + targetfile_obj = TargetFile.from_dict(copy.copy(data)) |
| 283 | + self.assertEqual(targetfile_obj.to_dict(), data) |
| 284 | + |
| 285 | + # Test from_dict and to_dict without custom. |
| 286 | + del data["custom"] |
| 287 | + targetfile_obj = TargetFile.from_dict(copy.copy(data)) |
| 288 | + self.assertEqual(targetfile_obj.to_dict(), data) |
| 289 | + |
| 290 | + |
244 | 291 | def test_metadata_snapshot(self):
|
245 | 292 | snapshot_path = os.path.join(
|
246 | 293 | self.repo_dir, 'metadata', 'snapshot.json')
|
247 | 294 | snapshot = Metadata.from_file(snapshot_path)
|
248 | 295 |
|
249 |
| - # Create a dict representing what we expect the updated data to be |
250 |
| - fileinfo = copy.deepcopy(snapshot.signed.meta) |
| 296 | + # Create a MetaFile instance representing what we expect |
| 297 | + # the updated data to be. |
251 | 298 | hashes = {'sha256': 'c2986576f5fdfd43944e2b19e775453b96748ec4fe2638a6d2f32f1310967095'}
|
252 |
| - fileinfo['role1.json']['version'] = 2 |
253 |
| - fileinfo['role1.json']['hashes'] = hashes |
254 |
| - fileinfo['role1.json']['length'] = 123 |
| 299 | + fileinfo = MetaFile(2, 123, hashes) |
255 | 300 |
|
256 |
| - |
257 |
| - self.assertNotEqual(snapshot.signed.meta, fileinfo) |
258 |
| - snapshot.signed.update('role1', 2, 123, hashes) |
259 |
| - self.assertEqual(snapshot.signed.meta, fileinfo) |
260 |
| - |
261 |
| - # Update only version. Length and hashes are optional. |
262 |
| - snapshot.signed.update('role2', 3) |
263 |
| - fileinfo['role2.json'] = {'version': 3} |
264 |
| - self.assertEqual(snapshot.signed.meta, fileinfo) |
| 301 | + self.assertNotEqual( |
| 302 | + snapshot.signed.meta['role1.json'].to_dict(), fileinfo.to_dict() |
| 303 | + ) |
| 304 | + snapshot.signed.update('role1', fileinfo) |
| 305 | + self.assertEqual( |
| 306 | + snapshot.signed.meta['role1.json'].to_dict(), fileinfo.to_dict() |
| 307 | + ) |
265 | 308 |
|
266 | 309 | # Test from_dict and to_dict without hashes and length.
|
267 | 310 | snapshot_dict = snapshot.to_dict()
|
268 |
| - test_dict = snapshot_dict['signed'].copy() |
269 |
| - del test_dict['meta']['role1.json']['length'] |
270 |
| - del test_dict['meta']['role1.json']['hashes'] |
| 311 | + del snapshot_dict['signed']['meta']['role1.json']['length'] |
| 312 | + del snapshot_dict['signed']['meta']['role1.json']['hashes'] |
| 313 | + test_dict = copy.deepcopy(snapshot_dict['signed']) |
271 | 314 | snapshot = Snapshot.from_dict(test_dict)
|
272 | 315 | self.assertEqual(snapshot_dict['signed'], snapshot.to_dict())
|
273 | 316 |
|
@@ -295,29 +338,27 @@ def test_metadata_timestamp(self):
|
295 | 338 | timestamp.signed.bump_expiration(delta)
|
296 | 339 | self.assertEqual(timestamp.signed.expires, datetime(2036, 1, 3, 0, 0))
|
297 | 340 |
|
| 341 | + # Create a MetaFile instance representing what we expect |
| 342 | + # the updated data to be. |
298 | 343 | hashes = {'sha256': '0ae9664468150a9aa1e7f11feecb32341658eb84292851367fea2da88e8a58dc'}
|
299 |
| - fileinfo = copy.deepcopy(timestamp.signed.meta['snapshot.json']) |
300 |
| - fileinfo['hashes'] = hashes |
301 |
| - fileinfo['version'] = 2 |
302 |
| - fileinfo['length'] = 520 |
| 344 | + fileinfo = MetaFile(2, 520, hashes) |
303 | 345 |
|
304 |
| - self.assertNotEqual(timestamp.signed.meta['snapshot.json'], fileinfo) |
305 |
| - timestamp.signed.update(2, 520, hashes) |
306 |
| - self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo) |
| 346 | + self.assertNotEqual( |
| 347 | + timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict() |
| 348 | + ) |
| 349 | + timestamp.signed.update(fileinfo) |
| 350 | + self.assertEqual( |
| 351 | + timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict() |
| 352 | + ) |
307 | 353 |
|
308 | 354 | # Test from_dict and to_dict without hashes and length.
|
309 | 355 | timestamp_dict = timestamp.to_dict()
|
310 |
| - test_dict = timestamp_dict['signed'].copy() |
311 |
| - del test_dict['meta']['snapshot.json']['length'] |
312 |
| - del test_dict['meta']['snapshot.json']['hashes'] |
| 356 | + del timestamp_dict['signed']['meta']['snapshot.json']['length'] |
| 357 | + del timestamp_dict['signed']['meta']['snapshot.json']['hashes'] |
| 358 | + test_dict = copy.deepcopy(timestamp_dict['signed']) |
313 | 359 | timestamp_test = Timestamp.from_dict(test_dict)
|
314 | 360 | self.assertEqual(timestamp_dict['signed'], timestamp_test.to_dict())
|
315 | 361 |
|
316 |
| - # Update only version. Length and hashes are optional. |
317 |
| - timestamp.signed.update(3) |
318 |
| - fileinfo = {'version': 3} |
319 |
| - self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo) |
320 |
| - |
321 | 362 | def test_key_class(self):
|
322 | 363 | keys = {
|
323 | 364 | "59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
|
@@ -531,22 +572,23 @@ def test_metadata_targets(self):
|
531 | 572 | "sha512": "ef5beafa16041bcdd2937140afebd485296cd54f7348ecd5a4d035c09759608de467a7ac0eb58753d0242df873c305e8bffad2454aa48f44480f15efae1cacd0"
|
532 | 573 | },
|
533 | 574 |
|
534 |
| - fileinfo = { |
535 |
| - 'hashes': hashes, |
536 |
| - 'length': 28 |
537 |
| - } |
| 575 | + fileinfo = TargetFile(length=28, hashes=hashes) |
538 | 576 |
|
539 | 577 | # Assert that data is not aleady equal
|
540 |
| - self.assertNotEqual(targets.signed.targets[filename], fileinfo) |
| 578 | + self.assertNotEqual( |
| 579 | + targets.signed.targets[filename].to_dict(), fileinfo.to_dict() |
| 580 | + ) |
541 | 581 | # Update an already existing fileinfo
|
542 | 582 | targets.signed.update(filename, fileinfo)
|
543 | 583 | # Verify that data is updated
|
544 |
| - self.assertEqual(targets.signed.targets[filename], fileinfo) |
| 584 | + self.assertEqual( |
| 585 | + targets.signed.targets[filename].to_dict(), fileinfo.to_dict() |
| 586 | + ) |
545 | 587 |
|
546 | 588 | # Test from_dict/to_dict Targets without delegations
|
547 | 589 | targets_dict = targets.to_dict()
|
548 | 590 | del targets_dict["signed"]["delegations"]
|
549 |
| - tmp_dict = targets_dict["signed"].copy() |
| 591 | + tmp_dict = copy.deepcopy(targets_dict["signed"]) |
550 | 592 | targets_obj = Targets.from_dict(tmp_dict)
|
551 | 593 | self.assertEqual(targets_dict["signed"], targets_obj.to_dict())
|
552 | 594 |
|
|
0 commit comments