Skip to content

Commit a91a1ab

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1466 from MVrachev/unrecognized_fields
Metadata API: Simplify unrecognized fields testing
2 parents a71ec9d + f34cc7e commit a91a1ab

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

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: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class TestSerialization(unittest.TestCase):
5353
valid_keys: DataSet = {
5454
"all": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
5555
"keyval": {"public": "foo"}}',
56+
"unrecognized field": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
57+
"keyval": {"public": "foo"}, "foo": "bar"}',
58+
"unrecognized field in keyval": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \
59+
"keyval": {"public": "foo", "foo": "bar"}}',
5660
}
5761

5862
@run_sub_tests_with_dataset(valid_keys)
@@ -63,7 +67,8 @@ def test_key_serialization(self, test_case_data: str):
6367

6468

6569
valid_roles: DataSet = {
66-
"all": '{"keyids": ["keyid"], "threshold": 3}'
70+
"all": '{"keyids": ["keyid"], "threshold": 3}',
71+
"unrecognized field": '{"keyids": ["keyid"], "threshold": 3, "foo": "bar"}',
6772
}
6873

6974
@run_sub_tests_with_dataset(valid_roles)
@@ -84,6 +89,11 @@ def test_role_serialization(self, test_case_data: str):
8489
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \
8590
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3} } \
8691
}',
92+
"unrecognized field": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \
93+
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \
94+
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
95+
"roles": { "targets": {"keyids": ["keyid"], "threshold": 3}}, \
96+
"foo": "bar"}',
8797
}
8898

8999
@run_sub_tests_with_dataset(valid_roots)
@@ -95,7 +105,9 @@ def test_root_serialization(self, test_case_data: str):
95105
valid_metafiles: DataSet = {
96106
"all": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1}',
97107
"no length": '{"hashes": {"sha256" : "abc"}, "version": 1 }',
98-
"no hashes": '{"length": 12, "version": 1}'
108+
"no hashes": '{"length": 12, "version": 1}',
109+
"unrecognized field": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1, \
110+
"foo": "bar"}',
99111
}
100112

101113
@run_sub_tests_with_dataset(valid_metafiles)
@@ -107,7 +119,9 @@ def test_metafile_serialization(self, test_case_data: str):
107119

108120
valid_timestamps: DataSet = {
109121
"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}}}'
122+
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}',
123+
"unrecognized field": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
124+
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}, "foo": "bar"}',
111125
}
112126

113127
@run_sub_tests_with_dataset(valid_timestamps)
@@ -119,7 +133,9 @@ def test_timestamp_serialization(self, test_case_data: str):
119133

120134
valid_snapshots: DataSet = {
121135
"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 }}}'
136+
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}}',
137+
"unrecognized field": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
138+
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}, "foo": "bar"}',
123139
}
124140

125141
@run_sub_tests_with_dataset(valid_snapshots)
@@ -138,6 +154,8 @@ def test_snapshot_serialization(self, test_case_data: str):
138154
"path_hash_prefixes": ["h1", "h2"], "threshold": 99}',
139155
"no hash or path prefix":
140156
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3}',
157+
"unrecognized field":
158+
'{"keyids": ["keyid"], "name": "a", "terminating": true, "threshold": 3, "foo": "bar"}',
141159
}
142160

143161
@run_sub_tests_with_dataset(valid_delegated_roles)
@@ -149,7 +167,11 @@ def test_delegated_role_serialization(self, test_case_data: str):
149167

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

155177
@run_sub_tests_with_dataset(valid_delegations)
@@ -162,7 +184,9 @@ def test_delegation_serialization(self, test_case_data: str):
162184
valid_targetfiles: DataSet = {
163185
"all": '{"length": 12, "hashes": {"sha256" : "abc"}, \
164186
"custom" : {"foo": "bar"} }',
165-
"no custom": '{"length": 12, "hashes": {"sha256" : "abc"}}'
187+
"no custom": '{"length": 12, "hashes": {"sha256" : "abc"}}',
188+
"unrecognized field": '{"length": 12, "hashes": {"sha256" : "abc"}, \
189+
"custom" : {"foo": "bar"}, "foo": "bar"}',
166190
}
167191

168192
@run_sub_tests_with_dataset(valid_targetfiles)
@@ -187,7 +211,9 @@ def test_targetfile_serialization(self, test_case_data: str):
187211
}',
188212
"no delegations": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
189213
"targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } } \
190-
}'
214+
}',
215+
"unrecognized_field": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \
216+
"targets": {}, "foo": "bar"}',
191217
}
192218

193219
@run_sub_tests_with_dataset(valid_targets)

0 commit comments

Comments
 (0)