Skip to content

Commit d0a25bf

Browse files
author
Martin Vrachev
committed
Metadata API: simplify testing unrecognized_fields
We have merged ADR 8 allowing for unrecognized fields and we have added tests for that which are too specific and not scalable. Now, I use table testing which we have used initially in #1416 to test unrecognized fields support in a cleaner and much more readable way. Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
1 parent e61055a commit d0a25bf

2 files changed

Lines changed: 34 additions & 53 deletions

File tree

tests/test_api.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -465,52 +465,6 @@ def test_metadata_targets(self):
465465
)
466466

467467

468-
def setup_dict_with_unrecognized_field(self, file_path, field, value):
469-
json_dict = {}
470-
with open(file_path) as f:
471-
json_dict = json.loads(f.read())
472-
# We are changing the json dict without changing the signature.
473-
# This could be a problem if we want to do verification on this dict.
474-
json_dict["signed"][field] = value
475-
return json_dict
476-
477-
def test_support_for_unrecognized_fields(self):
478-
for metadata in ["root", "timestamp", "snapshot", "targets"]:
479-
path = os.path.join(self.repo_dir, "metadata", metadata + ".json")
480-
dict1 = self.setup_dict_with_unrecognized_field(path, "f", "b")
481-
# Test that the metadata classes store unrecognized fields when
482-
# initializing and passes them when casting the instance to a dict.
483-
484-
# Add unrecognized fields to all metadata sub (helper) classes.
485-
if metadata == "root":
486-
for keyid in dict1["signed"]["keys"].keys():
487-
dict1["signed"]["keys"][keyid]["d"] = "c"
488-
for role_str in dict1["signed"]["roles"].keys():
489-
dict1["signed"]["roles"][role_str]["e"] = "g"
490-
elif metadata == "targets" and dict1["signed"].get("delegations"):
491-
for keyid in dict1["signed"]["delegations"]["keys"].keys():
492-
dict1["signed"]["delegations"]["keys"][keyid]["d"] = "c"
493-
new_roles = []
494-
for role in dict1["signed"]["delegations"]["roles"]:
495-
role["e"] = "g"
496-
new_roles.append(role)
497-
dict1["signed"]["delegations"]["roles"] = new_roles
498-
dict1["signed"]["delegations"]["foo"] = "bar"
499-
500-
temp_copy = copy.deepcopy(dict1)
501-
metadata_obj = Metadata.from_dict(temp_copy)
502-
503-
self.assertEqual(dict1["signed"], metadata_obj.signed.to_dict())
504-
505-
# Test that two instances of the same class could have different
506-
# unrecognized fields.
507-
dict2 = self.setup_dict_with_unrecognized_field(path, "f2", "b2")
508-
temp_copy2 = copy.deepcopy(dict2)
509-
metadata_obj2 = Metadata.from_dict(temp_copy2)
510-
self.assertNotEqual(
511-
metadata_obj.signed.to_dict(), metadata_obj2.signed.to_dict()
512-
)
513-
514468
def test_length_and_hash_validation(self):
515469

516470
# Test metadata files' hash and length verification.

tests/test_metadata_serialization.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ def wrapper(test_cls: "TestSerialization"):
5151
class TestSerialization(unittest.TestCase):
5252

5353
valid_keys: DataSet = {
54+
# Do we want to add unrecognized fields in the Key class?
5455
"all": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
5556
"keyval": {"public": "foo"}}',
57+
"unrecognized field": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
58+
"keyval": {"public": "foo"}, "foo": "bar"}',
59+
"unrecognized field in keyval": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
60+
"keyval": {"public": "foo", "foo": "bar"}}',
5661
}
5762

5863
@run_sub_tests_with_dataset(valid_keys)
@@ -63,7 +68,8 @@ def test_key_serialization(self, test_case_data: str):
6368

6469

6570
valid_roles: DataSet = {
66-
"all": '{"keyids": ["keyid"], "threshold": 3}'
71+
"all": '{"keyids": ["keyid"], "threshold": 3}',
72+
"unrecognized field": '{"keyids": ["keyid"], "threshold": 3, "foo": "bar"}',
6773
}
6874

6975
@run_sub_tests_with_dataset(valid_roles)
@@ -84,6 +90,11 @@ def test_role_serialization(self, test_case_data: str):
8490
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \
8591
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3} } \
8692
}',
93+
"unrecognized field": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
94+
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
95+
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
96+
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3}}, \
97+
"foo": "bar"}',
8798
}
8899

89100
@run_sub_tests_with_dataset(valid_roots)
@@ -95,7 +106,9 @@ def test_root_serialization(self, test_case_data: str):
95106
valid_metafiles: DataSet = {
96107
"all": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1}',
97108
"no length": '{"hashes": {"sha256" : "abc"}, "version": 1 }',
98-
"no hashes": '{"length": 12, "version": 1}'
109+
"no hashes": '{"length": 12, "version": 1}',
110+
"unrecognized field": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1, \
111+
"foo": "bar"}',
99112
}
100113

101114
@run_sub_tests_with_dataset(valid_metafiles)
@@ -107,7 +120,9 @@ def test_metafile_serialization(self, test_case_data: str):
107120

108121
valid_timestamps: DataSet = {
109122
"all": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
110-
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}'
123+
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}',
124+
"unrecognized field": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
125+
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}, "foo": "bar"}',
111126
}
112127

113128
@run_sub_tests_with_dataset(valid_timestamps)
@@ -119,7 +134,9 @@ def test_timestamp_serialization(self, test_case_data: str):
119134

120135
valid_snapshots: DataSet = {
121136
"all": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
122-
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}}'
137+
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}}',
138+
"unrecognized field": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
139+
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}, "foo": "bar"}',
123140
}
124141

125142
@run_sub_tests_with_dataset(valid_snapshots)
@@ -138,6 +155,8 @@ def test_snapshot_serialization(self, test_case_data: str):
138155
"path_hash_prefixes": ["h1", "h2"], "threshold": 99}',
139156
"no hash or path prefix":
140157
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3}',
158+
"unrecognized field":
159+
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3, "foo": "bar"}',
141160
}
142161

143162
@run_sub_tests_with_dataset(valid_delegated_roles)
@@ -149,7 +168,11 @@ def test_delegated_role_serialization(self, test_case_data: str):
149168

150169
valid_delegations: DataSet = {
151170
"all": '{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
152-
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]}'
171+
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ]}',
172+
"unrecognized field":
173+
'{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
174+
"roles": [ {"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3} ], \
175+
"foo": "bar"}',
153176
}
154177

155178
@run_sub_tests_with_dataset(valid_delegations)
@@ -162,7 +185,9 @@ def test_delegation_serialization(self, test_case_data: str):
162185
valid_targetfiles: DataSet = {
163186
"all": '{"length": 12, "hashes": {"sha256" : "abc"}, \
164187
"custom" : {"foo": "bar"} }',
165-
"no custom": '{"length": 12, "hashes": {"sha256" : "abc"}}'
188+
"no custom": '{"length": 12, "hashes": {"sha256" : "abc"}}',
189+
"unrecognized field": '{"length": 12, "hashes": {"sha256" : "abc"}, \
190+
"custom" : {"foo": "bar"}, "foo": "bar"}',
166191
}
167192

168193
@run_sub_tests_with_dataset(valid_targetfiles)
@@ -187,7 +212,9 @@ def test_targetfile_serialization(self, test_case_data: str):
187212
}',
188213
"no delegations": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
189214
"targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } } \
190-
}'
215+
}',
216+
"unrecognized_field": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
217+
"targets": {}, "foo": "bar"}',
191218
}
192219

193220
@run_sub_tests_with_dataset(valid_targets)

0 commit comments

Comments
 (0)