Skip to content

Commit 97da5ab

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1416 from MVrachev/comprehensive-testing
New API: Comprehensive serialization testing
2 parents b860ad8 + d0828bd commit 97da5ab

File tree

2 files changed

+204
-167
lines changed

2 files changed

+204
-167
lines changed

tests/test_api.py

Lines changed: 1 addition & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -274,49 +274,6 @@ def test_metadata_base(self):
274274
Metadata.from_dict(data)
275275

276276

277-
def test_metafile_class(self):
278-
# Test from_dict and to_dict with all attributes.
279-
data = {
280-
"hashes": {
281-
"sha256": "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
282-
},
283-
"length": 515,
284-
"version": 1
285-
}
286-
metafile_obj = MetaFile.from_dict(copy.copy(data))
287-
self.assertEqual(metafile_obj.to_dict(), data)
288-
289-
# Test from_dict and to_dict without length.
290-
del data["length"]
291-
metafile_obj = MetaFile.from_dict(copy.copy(data))
292-
self.assertEqual(metafile_obj.to_dict(), data)
293-
294-
# Test from_dict and to_dict without length and hashes.
295-
del data["hashes"]
296-
metafile_obj = MetaFile.from_dict(copy.copy(data))
297-
self.assertEqual(metafile_obj.to_dict(), data)
298-
299-
300-
def test_targetfile_class(self):
301-
# Test from_dict and to_dict with all attributes.
302-
data = {
303-
"custom": {
304-
"file_permissions": "0644"
305-
},
306-
"hashes": {
307-
"sha256": "65b8c67f51c993d898250f40aa57a317d854900b3a04895464313e48785440da",
308-
"sha512": "467430a68afae8e9f9c0771ea5d78bf0b3a0d79a2d3d3b40c69fde4dd42c461448aef76fcef4f5284931a1ffd0ac096d138ba3a0d6ca83fa8d7285a47a296f77"
309-
},
310-
"length": 31
311-
}
312-
targetfile_obj = TargetFile.from_dict(copy.copy(data))
313-
self.assertEqual(targetfile_obj.to_dict(), data)
314-
315-
# Test from_dict and to_dict without custom.
316-
del data["custom"]
317-
targetfile_obj = TargetFile.from_dict(copy.copy(data))
318-
self.assertEqual(targetfile_obj.to_dict(), data)
319-
320277
def test_metadata_snapshot(self):
321278
snapshot_path = os.path.join(
322279
self.repo_dir, 'metadata', 'snapshot.json')
@@ -335,13 +292,6 @@ def test_metadata_snapshot(self):
335292
snapshot.signed.meta['role1.json'].to_dict(), fileinfo.to_dict()
336293
)
337294

338-
# Test from_dict and to_dict without hashes and length.
339-
snapshot_dict = snapshot.to_dict()
340-
del snapshot_dict['signed']['meta']['role1.json']['length']
341-
del snapshot_dict['signed']['meta']['role1.json']['hashes']
342-
test_dict = copy.deepcopy(snapshot_dict['signed'])
343-
snapshot = Snapshot.from_dict(test_dict)
344-
self.assertEqual(snapshot_dict['signed'], snapshot.to_dict())
345295

346296
def test_metadata_timestamp(self):
347297
timestamp_path = os.path.join(
@@ -380,13 +330,6 @@ def test_metadata_timestamp(self):
380330
timestamp.signed.meta['snapshot.json'].to_dict(), fileinfo.to_dict()
381331
)
382332

383-
# Test from_dict and to_dict without hashes and length.
384-
timestamp_dict = timestamp.to_dict()
385-
del timestamp_dict['signed']['meta']['snapshot.json']['length']
386-
del timestamp_dict['signed']['meta']['snapshot.json']['hashes']
387-
test_dict = copy.deepcopy(timestamp_dict['signed'])
388-
timestamp_test = Timestamp.from_dict(test_dict)
389-
self.assertEqual(timestamp_dict['signed'], timestamp_test.to_dict())
390333

391334

392335
def test_key_class(self):
@@ -400,21 +343,12 @@ def test_key_class(self):
400343
},
401344
}
402345
for key_dict in keys.values():
403-
# Testing that the workflow of deserializing and serializing
404-
# a key dictionary doesn't change the content.
405-
test_key_dict = key_dict.copy()
406-
key_obj = Key.from_dict("id", test_key_dict)
407-
self.assertEqual(key_dict, key_obj.to_dict())
408346
# Test creating an instance without a required attribute.
409347
for key in key_dict.keys():
410348
test_key_dict = key_dict.copy()
411349
del test_key_dict[key]
412350
with self.assertRaises(KeyError):
413351
Key.from_dict("id", test_key_dict)
414-
# Test creating a Key instance with wrong keyval format.
415-
key_dict["keyval"] = {}
416-
with self.assertRaises(KeyError):
417-
Key.from_dict("id", key_dict)
418352

419353

420354
def test_role_class(self):
@@ -433,23 +367,12 @@ def test_role_class(self):
433367
},
434368
}
435369
for role_dict in roles.values():
436-
# Testing that the workflow of deserializing and serializing
437-
# a role dictionary doesn't change the content.
438-
test_role_dict = role_dict.copy()
439-
role_obj = Role.from_dict(test_role_dict)
440-
self.assertEqual(role_dict, role_obj.to_dict())
441370
# Test creating an instance without a required attribute.
442371
for role_attr in role_dict.keys():
443372
test_role_dict = role_dict.copy()
444373
del test_role_dict[role_attr]
445374
with self.assertRaises(KeyError):
446-
Key.from_dict("id", test_role_dict)
447-
# Test creating a Role instance with keyid dublicates.
448-
# for keyid in role_dict["keyids"]:
449-
role_dict["keyids"].append(role_dict["keyids"][0])
450-
test_role_dict = role_dict.copy()
451-
with self.assertRaises(ValueError):
452-
Role.from_dict(test_role_dict)
375+
Role.from_dict(test_role_dict)
453376

454377

455378
def test_metadata_root(self):
@@ -496,84 +419,8 @@ def test_metadata_root(self):
496419
with self.assertRaises(KeyError):
497420
root.signed.remove_key('root', 'nosuchkey')
498421

499-
# Test serializing and deserializing without consistent_snapshot.
500-
root_dict = root.to_dict()
501-
del root_dict["signed"]["consistent_snapshot"]
502-
root = Root.from_dict(copy.deepcopy(root_dict["signed"]))
503-
self.assertEqual(root_dict["signed"], root.to_dict())
504-
505-
def test_delegated_role_class(self):
506-
roles = [
507-
{
508-
"keyids": [
509-
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
510-
],
511-
"name": "role1",
512-
"paths": [
513-
"file3.txt"
514-
],
515-
"terminating": False,
516-
"threshold": 1
517-
}
518-
]
519-
for role in roles:
520-
# Testing that the workflow of deserializing and serializing
521-
# a delegation role dictionary doesn't change the content.
522-
key_obj = DelegatedRole.from_dict(role.copy())
523-
self.assertEqual(role, key_obj.to_dict())
524-
525-
# Test creating a DelegatedRole object with both "paths" and
526-
# "path_hash_prefixes" set.
527-
role["path_hash_prefixes"] = "foo"
528-
with self.assertRaises(ValueError):
529-
DelegatedRole.from_dict(role.copy())
530-
531-
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
532-
del role["paths"]
533-
role["path_hash_prefixes"] = []
534-
role_obj = DelegatedRole.from_dict(role.copy())
535-
self.assertEqual(role_obj.to_dict(), role)
536-
537-
# Test creating DelegatedRole only with "paths" (now an empty one)
538-
del role["path_hash_prefixes"]
539-
role["paths"] = []
540-
role_obj = DelegatedRole.from_dict(role.copy())
541-
self.assertEqual(role_obj.to_dict(), role)
542-
543-
# Test creating DelegatedRole without "paths" and
544-
# "path_hash_prefixes" set
545-
del role["paths"]
546-
role_obj = DelegatedRole.from_dict(role.copy())
547-
self.assertEqual(role_obj.to_dict(), role)
548-
549422

550423
def test_delegation_class(self):
551-
roles = [
552-
{
553-
"keyids": [
554-
"c8022fa1e9b9cb239a6b362bbdffa9649e61ad2cb699d2e4bc4fdf7930a0e64a"
555-
],
556-
"name": "role1",
557-
"paths": [
558-
"file3.txt"
559-
],
560-
"terminating": False,
561-
"threshold": 1
562-
}
563-
]
564-
keys = {
565-
"59a4df8af818e9ed7abe0764c0b47b4240952aa0d179b5b78346c470ac30278d":{
566-
"keytype": "ed25519",
567-
"keyval": {
568-
"public": "edcd0a32a07dce33f7c7873aaffbff36d20ea30787574ead335eefd337e4dacd"
569-
},
570-
"scheme": "ed25519"
571-
},
572-
}
573-
delegations_dict = {"keys": keys, "roles": roles}
574-
delegations = Delegations.from_dict(copy.deepcopy(delegations_dict))
575-
self.assertEqual(delegations_dict, delegations.to_dict())
576-
577424
# empty keys and roles
578425
delegations_dict = {"keys":{}, "roles":[]}
579426
delegations = Delegations.from_dict(delegations_dict.copy())
@@ -615,19 +462,6 @@ def test_metadata_targets(self):
615462
targets.signed.targets[filename].to_dict(), fileinfo.to_dict()
616463
)
617464

618-
# Test from_dict/to_dict Targets with empty targets.
619-
targets_dict = copy.deepcopy(targets.to_dict())
620-
targets_dict["signed"]["targets"] = {}
621-
tmp_dict = copy.deepcopy(targets_dict["signed"])
622-
targets_obj = Targets.from_dict(tmp_dict)
623-
self.assertEqual(targets_dict["signed"], targets_obj.to_dict())
624-
625-
# Test from_dict/to_dict Targets without delegations
626-
targets_dict = targets.to_dict()
627-
del targets_dict["signed"]["delegations"]
628-
tmp_dict = copy.deepcopy(targets_dict["signed"])
629-
targets_obj = Targets.from_dict(tmp_dict)
630-
self.assertEqual(targets_dict["signed"], targets_obj.to_dict())
631465

632466
def setup_dict_with_unrecognized_field(self, file_path, field, value):
633467
json_dict = {}

0 commit comments

Comments
 (0)