| 1 |
# Copyright New York University and the TUF contributors |
| 2 |
# SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 |
|
| 4 |
"""Unit tests testing tuf/api/metadata.py classes |
| 5 |
serialization and deserialization. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import copy |
| 10 |
import json |
| 11 |
import logging |
| 12 |
import sys |
| 13 |
import unittest |
| 14 |
|
| 15 |
from securesystemslib.signer import Signature |
| 16 |
|
| 17 |
from tests import utils |
| 18 |
from tuf.api.metadata import ( |
| 19 |
DelegatedRole, |
| 20 |
Delegations, |
| 21 |
Key, |
| 22 |
Metadata, |
| 23 |
MetaFile, |
| 24 |
Role, |
| 25 |
Root, |
| 26 |
Snapshot, |
| 27 |
SuccinctRoles, |
| 28 |
TargetFile, |
| 29 |
Targets, |
| 30 |
Timestamp, |
| 31 |
) |
| 32 |
from tuf.api.serialization import DeserializationError |
| 33 |
|
| 34 |
logger = logging.getLogger(__name__) |
| 35 |
|
| 36 |
|
| 37 |
class TestSerialization(unittest.TestCase): |
| 38 |
"""Test serialization for all classes in 'tuf/api/metadata.py'.""" |
| 39 |
|
| 40 |
invalid_metadata = { |
| 41 |
"no signatures field": b'{"signed": \ |
| 42 |
{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 43 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}} \ |
| 44 |
}', |
| 45 |
"non unique duplicating signatures": b'{"signed": \ |
| 46 |
{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 47 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}, \ |
| 48 |
"signatures": [{"keyid": "id", "sig": "b"}, {"keyid": "id", "sig": "b"}] \ |
| 49 |
}', |
| 50 |
} |
| 51 |
|
| 52 |
@utils.run_sub_tests_with_dataset(invalid_metadata) |
| 53 |
def test_invalid_metadata_serialization(self, test_data: bytes) -> None: |
| 54 |
# We expect a DeserializationError reraised from ValueError or KeyError. |
| 55 |
with self.assertRaises(DeserializationError): |
| 56 |
Metadata.from_bytes(test_data) |
| 57 |
|
| 58 |
valid_metadata = { |
| 59 |
"multiple signatures": b'{ \ |
| 60 |
"signed": \ |
| 61 |
{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 62 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}, \ |
| 63 |
"signatures": [{ "keyid": "id", "sig": "b"}, {"keyid": "id2", "sig": "d" }] \ |
| 64 |
}', |
| 65 |
"no signatures": b'{ \ |
| 66 |
"signed": \ |
| 67 |
{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 68 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}, \ |
| 69 |
"signatures": [] \ |
| 70 |
}', |
| 71 |
"unrecognized fields": b'{ \ |
| 72 |
"signed": \ |
| 73 |
{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 74 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}, \ |
| 75 |
"signatures": [{"keyid": "id", "sig": "b"}], \ |
| 76 |
"foo": "bar" \ |
| 77 |
}', |
| 78 |
} |
| 79 |
|
| 80 |
@utils.run_sub_tests_with_dataset(valid_metadata) |
| 81 |
def test_valid_metadata_serialization(self, test_case_data: bytes) -> None: |
| 82 |
md = Metadata.from_bytes(test_case_data) |
| 83 |
|
| 84 |
# Convert to a JSON and sort the keys the way we do in JSONSerializer. |
| 85 |
separators = (",", ":") |
| 86 |
test_json = json.loads(test_case_data) |
| 87 |
test_bytes = json.dumps( |
| 88 |
test_json, separators=separators, sort_keys=True |
| 89 |
).encode("utf-8") |
| 90 |
|
| 91 |
self.assertEqual(test_bytes, md.to_bytes()) |
| 92 |
|
| 93 |
invalid_signatures = { |
| 94 |
"missing keyid attribute in a signature": '{ "sig": "abc" }', |
| 95 |
"missing sig attribute in a signature": '{ "keyid": "id" }', |
| 96 |
} |
| 97 |
|
| 98 |
@utils.run_sub_tests_with_dataset(invalid_signatures) |
| 99 |
def test_invalid_signature_serialization(self, test_data: str) -> None: |
| 100 |
case_dict = json.loads(test_data) |
| 101 |
with self.assertRaises(KeyError): |
| 102 |
Signature.from_dict(case_dict) |
| 103 |
|
| 104 |
valid_signatures = { |
| 105 |
"all": '{ "keyid": "id", "sig": "b"}', |
| 106 |
"unrecognized fields": '{ "keyid": "id", "sig": "b", "foo": "bar"}', |
| 107 |
} |
| 108 |
|
| 109 |
@utils.run_sub_tests_with_dataset(valid_signatures) |
| 110 |
def test_signature_serialization(self, test_case_data: str) -> None: |
| 111 |
case_dict = json.loads(test_case_data) |
| 112 |
signature = Signature.from_dict(copy.copy(case_dict)) |
| 113 |
self.assertEqual(case_dict, signature.to_dict()) |
| 114 |
|
| 115 |
# Snapshot instances with meta = {} are valid, but for a full valid |
| 116 |
# repository it's required that meta has at least one element inside it. |
| 117 |
invalid_signed = { |
| 118 |
"no _type": '{"spec_version": "1.0.0", "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 119 |
"no spec_version": '{"_type": "snapshot", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 120 |
"no version": '{"_type": "snapshot", "spec_version": "1.0.0", "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 121 |
"no expires": '{"_type": "snapshot", "spec_version": "1.0.0", "version": 1, "meta": {}}', |
| 122 |
"empty str _type": '{"_type": "", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 123 |
"empty str spec_version": '{"_type": "snapshot", "spec_version": "", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 124 |
"_type wrong type": '{"_type": "foo", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 125 |
"version wrong type": '{"_type": "snapshot", "spec_version": "1.0.0", "version": "a", "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 126 |
"invalid spec_version str": '{"_type": "snapshot", "spec_version": "abc", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 127 |
"non-number spec_version": '{"_type": "snapshot", "spec_version": "1.2.a", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 128 |
"one part spec_version": '{"_type": "snapshot", "spec_version": "1", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 129 |
"different major spec_version": '{"_type": "snapshot", "spec_version": "0.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 130 |
"version 0": '{"_type": "snapshot", "spec_version": "1.0.0", "version": 0, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 131 |
"version below 0": '{"_type": "snapshot", "spec_version": "1.0.0", "version": -1, "expires": "2030-01-01T00:00:00Z", "meta": {}}', |
| 132 |
"wrong datetime string": '{"_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "abc", "meta": {}}', |
| 133 |
} |
| 134 |
|
| 135 |
@utils.run_sub_tests_with_dataset(invalid_signed) |
| 136 |
def test_invalid_signed_serialization(self, test_case_data: str) -> None: |
| 137 |
case_dict = json.loads(test_case_data) |
| 138 |
with self.assertRaises((KeyError, ValueError, TypeError)): |
| 139 |
Snapshot.from_dict(case_dict) |
| 140 |
|
| 141 |
valid_keys = { |
| 142 |
"all": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \ |
| 143 |
"keyval": {"public": "foo"}}', |
| 144 |
"unrecognized field": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \ |
| 145 |
"keyval": {"public": "foo"}, "foo": "bar"}', |
| 146 |
"unrecognized field in keyval": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", \ |
| 147 |
"keyval": {"public": "foo", "foo": "bar"}}', |
| 148 |
} |
| 149 |
|
| 150 |
@utils.run_sub_tests_with_dataset(valid_keys) |
| 151 |
def test_valid_key_serialization(self, test_case_data: str) -> None: |
| 152 |
case_dict = json.loads(test_case_data) |
| 153 |
key = Key.from_dict("id", copy.copy(case_dict)) |
| 154 |
self.assertDictEqual(case_dict, key.to_dict()) |
| 155 |
|
| 156 |
invalid_keys = { |
| 157 |
"no keyid": '{"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "abc"}}', |
| 158 |
"no keytype": '{"keyid": "id", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}', |
| 159 |
"no scheme": '{"keyid": "id", "keytype": "rsa", "keyval": {"public": "foo"}}', |
| 160 |
"no keyval": '{"keyid": "id", "keytype": "rsa", "scheme": "rsassa-pss-sha256"}', |
| 161 |
"keyid wrong type": '{"keyid": 1, "keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "abc"}}', |
| 162 |
"keytype wrong type": '{"keyid": "id", "keytype": 1, "scheme": "rsassa-pss-sha256", "keyval": {"public": "abc"}}', |
| 163 |
"scheme wrong type": '{"keyid": "id", "keytype": "rsa", "scheme": 1, "keyval": {"public": "abc"}}', |
| 164 |
"keyval wrong type": '{"keyid": "id", "keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": 1}', |
| 165 |
} |
| 166 |
|
| 167 |
@utils.run_sub_tests_with_dataset(invalid_keys) |
| 168 |
def test_invalid_key_serialization(self, test_case_data: str) -> None: |
| 169 |
case_dict = json.loads(test_case_data) |
| 170 |
with self.assertRaises((TypeError, KeyError, ValueError)): |
| 171 |
keyid = case_dict.pop("keyid") |
| 172 |
Key.from_dict(keyid, case_dict) |
| 173 |
|
| 174 |
invalid_roles = { |
| 175 |
"no threshold": '{"keyids": ["keyid"]}', |
| 176 |
"no keyids": '{"threshold": 3}', |
| 177 |
"wrong threshold type": '{"keyids": ["keyid"], "threshold": "a"}', |
| 178 |
"wrong keyids type": '{"keyids": 1, "threshold": 3}', |
| 179 |
"threshold below 1": '{"keyids": ["keyid"], "threshold": 0}', |
| 180 |
"duplicate keyids": '{"keyids": ["keyid", "keyid"], "threshold": 3}', |
| 181 |
} |
| 182 |
|
| 183 |
@utils.run_sub_tests_with_dataset(invalid_roles) |
| 184 |
def test_invalid_role_serialization(self, test_case_data: str) -> None: |
| 185 |
case_dict = json.loads(test_case_data) |
| 186 |
with self.assertRaises((KeyError, TypeError, ValueError)): |
| 187 |
Role.from_dict(case_dict) |
| 188 |
|
| 189 |
valid_roles = { |
| 190 |
"all": '{"keyids": ["keyid"], "threshold": 3}', |
| 191 |
"many keyids": '{"keyids": ["a", "b", "c", "d", "e"], "threshold": 1}', |
| 192 |
"ordered keyids": '{"keyids": ["c", "b", "a"], "threshold": 1}', |
| 193 |
"empty keyids": '{"keyids": [], "threshold": 1}', |
| 194 |
"unrecognized field": '{"keyids": ["keyid"], "threshold": 3, "foo": "bar"}', |
| 195 |
} |
| 196 |
|
| 197 |
@utils.run_sub_tests_with_dataset(valid_roles) |
| 198 |
def test_role_serialization(self, test_case_data: str) -> None: |
| 199 |
case_dict = json.loads(test_case_data) |
| 200 |
role = Role.from_dict(copy.deepcopy(case_dict)) |
| 201 |
self.assertDictEqual(case_dict, role.to_dict()) |
| 202 |
|
| 203 |
valid_roots = { |
| 204 |
"all": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 205 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 206 |
"keys": { \ |
| 207 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 208 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 209 |
"roles": { \ |
| 210 |
"root": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 211 |
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \ |
| 212 |
"targets": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 213 |
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \ |
| 214 |
}', |
| 215 |
"no consistent_snapshot": '{ "_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 216 |
"expires": "2030-01-01T00:00:00Z", \ |
| 217 |
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \ |
| 218 |
"roles": { \ |
| 219 |
"root": {"keyids": ["keyid"], "threshold": 1}, \ |
| 220 |
"timestamp": {"keyids": ["keyid"], "threshold": 1}, \ |
| 221 |
"targets": {"keyids": ["keyid"], "threshold": 1}, \ |
| 222 |
"snapshot": {"keyids": ["keyid"], "threshold": 1}} \ |
| 223 |
}', |
| 224 |
"empty keys": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 225 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 226 |
"keys": {}, \ |
| 227 |
"roles": { \ |
| 228 |
"root": {"keyids": [], "threshold": 1}, \ |
| 229 |
"timestamp": {"keyids": [], "threshold": 1}, \ |
| 230 |
"targets": {"keyids": [], "threshold": 1}, \ |
| 231 |
"snapshot": {"keyids": [], "threshold": 1}} \ |
| 232 |
}', |
| 233 |
"unrecognized field": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 234 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 235 |
"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 236 |
"roles": { \ |
| 237 |
"root": {"keyids": ["keyid"], "threshold": 1}, \ |
| 238 |
"timestamp": {"keyids": ["keyid"], "threshold": 1}, \ |
| 239 |
"targets": {"keyids": ["keyid"], "threshold": 1}, \ |
| 240 |
"snapshot": {"keyids": ["keyid"], "threshold": 1} \ |
| 241 |
}, \ |
| 242 |
"foo": "bar"}', |
| 243 |
} |
| 244 |
|
| 245 |
@utils.run_sub_tests_with_dataset(valid_roots) |
| 246 |
def test_root_serialization(self, test_case_data: str) -> None: |
| 247 |
case_dict = json.loads(test_case_data) |
| 248 |
root = Root.from_dict(copy.deepcopy(case_dict)) |
| 249 |
self.assertDictEqual(case_dict, root.to_dict()) |
| 250 |
|
| 251 |
invalid_roots = { |
| 252 |
"invalid role name": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 253 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 254 |
"keys": { \ |
| 255 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 256 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 257 |
"roles": { \ |
| 258 |
"bar": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 259 |
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \ |
| 260 |
"targets": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 261 |
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \ |
| 262 |
}', |
| 263 |
"missing root role": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 264 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 265 |
"keys": { \ |
| 266 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 267 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 268 |
"roles": { \ |
| 269 |
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \ |
| 270 |
"targets": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 271 |
"snapshot": {"keyids": ["keyid2"], "threshold": 1}} \ |
| 272 |
}', |
| 273 |
"one additional role": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 274 |
"expires": "2030-01-01T00:00:00Z", "consistent_snapshot": false, \ |
| 275 |
"keys": { \ |
| 276 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 277 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 278 |
"roles": { \ |
| 279 |
"root": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 280 |
"timestamp": {"keyids": ["keyid2"], "threshold": 1}, \ |
| 281 |
"targets": {"keyids": ["keyid1"], "threshold": 1}, \ |
| 282 |
"snapshot": {"keyids": ["keyid2"], "threshold": 1}, \ |
| 283 |
"foo": {"keyids": ["keyid2"], "threshold": 1}} \ |
| 284 |
}', |
| 285 |
"invalid expiry with microseconds": '{"_type": "root", "spec_version": "1.0.0", "version": 1, \ |
| 286 |
"expires": "2030-01-01T12:00:00.123456Z", "consistent_snapshot": false, \ |
| 287 |
"keys": {}, "roles": {"root": {}, "timestamp": {}, "targets": {}, "snapshot": {}}}', |
| 288 |
} |
| 289 |
|
| 290 |
@utils.run_sub_tests_with_dataset(invalid_roots) |
| 291 |
def test_invalid_root_serialization(self, test_case_data: str) -> None: |
| 292 |
case_dict = json.loads(test_case_data) |
| 293 |
with self.assertRaises(ValueError): |
| 294 |
Root.from_dict(case_dict) |
| 295 |
|
| 296 |
invalid_metafiles = { |
| 297 |
"wrong length type": '{"version": 1, "length": "a", "hashes": {"sha256" : "abc"}}', |
| 298 |
"version 0": '{"version": 0, "length": 1, "hashes": {"sha256" : "abc"}}', |
| 299 |
"length below 0": '{"version": 1, "length": -1, "hashes": {"sha256" : "abc"}}', |
| 300 |
"empty hashes dict": '{"version": 1, "length": 1, "hashes": {}}', |
| 301 |
"hashes wrong type": '{"version": 1, "length": 1, "hashes": 1}', |
| 302 |
"hashes values wrong type": '{"version": 1, "length": 1, "hashes": {"sha256": 1}}', |
| 303 |
} |
| 304 |
|
| 305 |
@utils.run_sub_tests_with_dataset(invalid_metafiles) |
| 306 |
def test_invalid_metafile_serialization(self, test_case_data: str) -> None: |
| 307 |
case_dict = json.loads(test_case_data) |
| 308 |
with self.assertRaises((TypeError, ValueError, AttributeError)): |
| 309 |
MetaFile.from_dict(case_dict) |
| 310 |
|
| 311 |
valid_metafiles = { |
| 312 |
"all": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1}', |
| 313 |
"no length": '{"hashes": {"sha256" : "abc"}, "version": 1 }', |
| 314 |
"length 0": '{"version": 1, "length": 0, "hashes": {"sha256" : "abc"}}', |
| 315 |
"no hashes": '{"length": 12, "version": 1}', |
| 316 |
"unrecognized field": '{"hashes": {"sha256" : "abc"}, "length": 12, "version": 1, "foo": "bar"}', |
| 317 |
"many hashes": '{"hashes": {"sha256" : "abc", "sha512": "cde"}, "length": 12, "version": 1}', |
| 318 |
} |
| 319 |
|
| 320 |
@utils.run_sub_tests_with_dataset(valid_metafiles) |
| 321 |
def test_metafile_serialization(self, test_case_data: str) -> None: |
| 322 |
case_dict = json.loads(test_case_data) |
| 323 |
metafile = MetaFile.from_dict(copy.copy(case_dict)) |
| 324 |
self.assertDictEqual(case_dict, metafile.to_dict()) |
| 325 |
|
| 326 |
invalid_timestamps = { |
| 327 |
"no metafile": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z"}', |
| 328 |
} |
| 329 |
|
| 330 |
@utils.run_sub_tests_with_dataset(invalid_timestamps) |
| 331 |
def test_invalid_timestamp_serialization(self, test_case_data: str) -> None: |
| 332 |
case_dict = json.loads(test_case_data) |
| 333 |
with self.assertRaises((ValueError, KeyError)): |
| 334 |
Timestamp.from_dict(case_dict) |
| 335 |
|
| 336 |
valid_timestamps = { |
| 337 |
"all": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 338 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}', |
| 339 |
"legacy spec_version": '{ "_type": "timestamp", "spec_version": "1.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 340 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}}', |
| 341 |
"unrecognized field": '{ "_type": "timestamp", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 342 |
"meta": {"snapshot.json": {"hashes": {"sha256" : "abc"}, "version": 1}}, "foo": "bar"}', |
| 343 |
} |
| 344 |
|
| 345 |
@utils.run_sub_tests_with_dataset(valid_timestamps) |
| 346 |
def test_timestamp_serialization(self, test_case_data: str) -> None: |
| 347 |
case_dict = json.loads(test_case_data) |
| 348 |
timestamp = Timestamp.from_dict(copy.deepcopy(case_dict)) |
| 349 |
self.assertDictEqual(case_dict, timestamp.to_dict()) |
| 350 |
|
| 351 |
valid_snapshots = { |
| 352 |
"all": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 353 |
"meta": { \ |
| 354 |
"file1.txt": {"hashes": {"sha256" : "abc"}, "version": 1}, \ |
| 355 |
"file2.txt": {"hashes": {"sha256" : "cde"}, "version": 1} \ |
| 356 |
}}', |
| 357 |
"empty meta": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 358 |
"meta": {} \ |
| 359 |
}', |
| 360 |
"unrecognized field": '{ "_type": "snapshot", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 361 |
"meta": { "file.txt": { "hashes": {"sha256" : "abc"}, "version": 1 }}, "foo": "bar"}', |
| 362 |
} |
| 363 |
|
| 364 |
@utils.run_sub_tests_with_dataset(valid_snapshots) |
| 365 |
def test_snapshot_serialization(self, test_case_data: str) -> None: |
| 366 |
case_dict = json.loads(test_case_data) |
| 367 |
snapshot = Snapshot.from_dict(copy.deepcopy(case_dict)) |
| 368 |
self.assertDictEqual(case_dict, snapshot.to_dict()) |
| 369 |
|
| 370 |
valid_delegated_roles = { |
| 371 |
# DelegatedRole inherits Role and some use cases can be found in the valid_roles. |
| 372 |
"no hash prefix attribute": '{"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], \ |
| 373 |
"terminating": false, "threshold": 1}', |
| 374 |
"no path attribute": '{"keyids": ["keyid"], "name": "a", "terminating": false, \ |
| 375 |
"path_hash_prefixes": ["h1", "h2"], "threshold": 99}', |
| 376 |
"empty paths": '{"keyids": ["keyid"], "name": "a", "paths": [], \ |
| 377 |
"terminating": false, "threshold": 1}', |
| 378 |
"empty path_hash_prefixes": '{"keyids": ["keyid"], "name": "a", "terminating": false, \ |
| 379 |
"path_hash_prefixes": [], "threshold": 99}', |
| 380 |
"unrecognized field": '{"keyids": ["keyid"], "name": "a", "terminating": true, "paths": ["fn1"], "threshold": 3, "foo": "bar"}', |
| 381 |
"many keyids": '{"keyids": ["keyid1", "keyid2"], "name": "a", "paths": ["fn1", "fn2"], \ |
| 382 |
"terminating": false, "threshold": 1}', |
| 383 |
"ordered keyids": '{"keyids": ["keyid2", "keyid1"], "name": "a", "paths": ["fn1", "fn2"], \ |
| 384 |
"terminating": false, "threshold": 1}', |
| 385 |
} |
| 386 |
|
| 387 |
@utils.run_sub_tests_with_dataset(valid_delegated_roles) |
| 388 |
def test_delegated_role_serialization(self, test_case_data: str) -> None: |
| 389 |
case_dict = json.loads(test_case_data) |
| 390 |
deserialized_role = DelegatedRole.from_dict(copy.copy(case_dict)) |
| 391 |
self.assertDictEqual(case_dict, deserialized_role.to_dict()) |
| 392 |
|
| 393 |
invalid_delegated_roles = { |
| 394 |
# DelegatedRole inherits Role and some use cases can be found in the invalid_roles. |
| 395 |
"missing hash prefixes and paths": '{"name": "a", "keyids": ["keyid"], "threshold": 1, "terminating": false}', |
| 396 |
"both hash prefixes and paths": '{"name": "a", "keyids": ["keyid"], "threshold": 1, "terminating": false, \ |
| 397 |
"paths": ["fn1", "fn2"], "path_hash_prefixes": ["h1", "h2"]}', |
| 398 |
"invalid path type": '{"keyids": ["keyid"], "name": "a", "paths": [1,2,3], \ |
| 399 |
"terminating": false, "threshold": 1}', |
| 400 |
"invalid path_hash_prefixes type": '{"keyids": ["keyid"], "name": "a", "path_hash_prefixes": [1,2,3], \ |
| 401 |
"terminating": false, "threshold": 1}', |
| 402 |
} |
| 403 |
|
| 404 |
@utils.run_sub_tests_with_dataset(invalid_delegated_roles) |
| 405 |
def test_invalid_delegated_role_serialization( |
| 406 |
self, test_case_data: str |
| 407 |
) -> None: |
| 408 |
case_dict = json.loads(test_case_data) |
| 409 |
with self.assertRaises(ValueError): |
| 410 |
DelegatedRole.from_dict(case_dict) |
| 411 |
|
| 412 |
valid_succinct_roles = { |
| 413 |
# SuccinctRoles inherits Role and some use cases can be found in the valid_roles. |
| 414 |
"standard succinct_roles information": '{"keyids": ["keyid"], "threshold": 1, \ |
| 415 |
"bit_length": 8, "name_prefix": "foo"}', |
| 416 |
"succinct_roles with unrecognized fields": '{"keyids": ["keyid"], "threshold": 1, \ |
| 417 |
"bit_length": 8, "name_prefix": "foo", "foo": "bar"}', |
| 418 |
} |
| 419 |
|
| 420 |
@utils.run_sub_tests_with_dataset(valid_succinct_roles) |
| 421 |
def test_succinct_roles_serialization(self, test_case_data: str) -> None: |
| 422 |
case_dict = json.loads(test_case_data) |
| 423 |
succinct_roles = SuccinctRoles.from_dict(copy.copy(case_dict)) |
| 424 |
self.assertDictEqual(case_dict, succinct_roles.to_dict()) |
| 425 |
|
| 426 |
invalid_succinct_roles = { |
| 427 |
# SuccinctRoles inherits Role and some use cases can be found in the invalid_roles. |
| 428 |
"missing bit_length from succinct_roles": '{"keyids": ["keyid"], "threshold": 1, "name_prefix": "foo"}', |
| 429 |
"missing name_prefix from succinct_roles": '{"keyids": ["keyid"], "threshold": 1, "bit_length": 8}', |
| 430 |
"succinct_roles with invalid bit_length type": '{"keyids": ["keyid"], "threshold": 1, "bit_length": "a", "name_prefix": "foo"}', |
| 431 |
"succinct_roles with invalid name_prefix type": '{"keyids": ["keyid"], "threshold": 1, "bit_length": 8, "name_prefix": 1}', |
| 432 |
"succinct_roles with high bit_length value": '{"keyids": ["keyid"], "threshold": 1, "bit_length": 50, "name_prefix": "foo"}', |
| 433 |
"succinct_roles with low bit_length value": '{"keyids": ["keyid"], "threshold": 1, "bit_length": 0, "name_prefix": "foo"}', |
| 434 |
} |
| 435 |
|
| 436 |
@utils.run_sub_tests_with_dataset(invalid_succinct_roles) |
| 437 |
def test_invalid_succinct_roles_serialization(self, test_data: str) -> None: |
| 438 |
case_dict = json.loads(test_data) |
| 439 |
with self.assertRaises((ValueError, KeyError, TypeError)): |
| 440 |
SuccinctRoles.from_dict(case_dict) |
| 441 |
|
| 442 |
invalid_delegations = { |
| 443 |
"empty delegations": "{}", |
| 444 |
"missing keys": '{ "roles": [ \ |
| 445 |
{"keyids": ["keyid"], "name": "a", "terminating": true, "paths": ["fn1"], "threshold": 3}, \ |
| 446 |
{"keyids": ["keyid2"], "name": "b", "terminating": true, "paths": ["fn2"], "threshold": 4} ] \ |
| 447 |
}', |
| 448 |
"missing roles": '{"keys": { \ |
| 449 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 450 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}}', |
| 451 |
"bad keys": '{"keys": "foo", \ |
| 452 |
"roles": [{"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": false, "threshold": 3}]}', |
| 453 |
"bad roles": '{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 454 |
"roles": ["foo"]}', |
| 455 |
"duplicate role names": '{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 456 |
"roles": [ \ |
| 457 |
{"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": false, "threshold": 3}, \ |
| 458 |
{"keyids": ["keyid2"], "name": "a", "paths": ["fn3"], "terminating": false, "threshold": 2} \ |
| 459 |
] \ |
| 460 |
}', |
| 461 |
"using empty string role name": '{"keys": { \ |
| 462 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 463 |
"roles": [ \ |
| 464 |
{"keyids": ["keyid1"], "name": "", "terminating": true, "paths": ["fn1"], "threshold": 3}] \ |
| 465 |
}', |
| 466 |
"using root as delegate role name": '{"keys": { \ |
| 467 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 468 |
"roles": [ \ |
| 469 |
{"keyids": ["keyid1"], "name": "root", "terminating": true, "paths": ["fn1"], "threshold": 3}] \ |
| 470 |
}', |
| 471 |
"using snapshot as delegate role name": '{"keys": { \ |
| 472 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 473 |
"roles": [ \ |
| 474 |
{"keyids": ["keyid1"], "name": "snapshot", "terminating": true, "paths": ["fn1"], "threshold": 3}] \ |
| 475 |
}', |
| 476 |
"using targets as delegate role name": '{"keys": { \ |
| 477 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 478 |
"roles": [ \ |
| 479 |
{"keyids": ["keyid1"], "name": "targets", "terminating": true, "paths": ["fn1"], "threshold": 3}] \ |
| 480 |
}', |
| 481 |
"using timestamp as delegate role name": '{"keys": { \ |
| 482 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 483 |
"roles": [ \ |
| 484 |
{"keyids": ["keyid1"], "name": "timestamp", "terminating": true, "paths": ["fn1"], "threshold": 3}] \ |
| 485 |
}', |
| 486 |
"using valid and top-level role name": '{"keys": { \ |
| 487 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 488 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 489 |
"roles": [ \ |
| 490 |
{"keyids": ["keyid1"], "name": "b", "terminating": true, "paths": ["fn1"], "threshold": 3}, \ |
| 491 |
{"keyids": ["keyid2"], "name": "root", "terminating": true, "paths": ["fn2"], "threshold": 4} ] \ |
| 492 |
}', |
| 493 |
"roles and succinct_roles set": '{"keys": { \ |
| 494 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 495 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 496 |
"roles": [ \ |
| 497 |
{"keyids": ["keyid"], "name": "a", "terminating": true, "paths": ["fn1"], "threshold": 3}, \ |
| 498 |
{"keyids": ["keyid2"], "name": "b", "terminating": true, "paths": ["fn2"], "threshold": 4} ], \ |
| 499 |
"succinct_roles": {"keyids": ["keyid"], "threshold": 1, "bit_length": 8, "name_prefix": "foo"}}', |
| 500 |
} |
| 501 |
|
| 502 |
@utils.run_sub_tests_with_dataset(invalid_delegations) |
| 503 |
def test_invalid_delegation_serialization( |
| 504 |
self, test_case_data: str |
| 505 |
) -> None: |
| 506 |
case_dict = json.loads(test_case_data) |
| 507 |
with self.assertRaises((ValueError, KeyError, AttributeError)): |
| 508 |
Delegations.from_dict(case_dict) |
| 509 |
|
| 510 |
valid_delegations = { |
| 511 |
"with roles": '{"keys": { \ |
| 512 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 513 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 514 |
"roles": [ \ |
| 515 |
{"keyids": ["keyid"], "name": "a", "terminating": true, "paths": ["fn1"], "threshold": 3}, \ |
| 516 |
{"keyids": ["keyid2"], "name": "b", "terminating": true, "paths": ["fn2"], "threshold": 4} ] \ |
| 517 |
}', |
| 518 |
"with succinct_roles": '{"keys": { \ |
| 519 |
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 520 |
"keyid2" : {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 521 |
"succinct_roles": {"keyids": ["keyid"], "threshold": 1, "bit_length": 8, "name_prefix": "foo"}}', |
| 522 |
"unrecognized field": '{"keys": {"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \ |
| 523 |
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ], \ |
| 524 |
"foo": "bar"}', |
| 525 |
"empty keys and roles": '{"keys": {}, \ |
| 526 |
"roles": [] \ |
| 527 |
}', |
| 528 |
} |
| 529 |
|
| 530 |
@utils.run_sub_tests_with_dataset(valid_delegations) |
| 531 |
def test_delegation_serialization(self, test_case_data: str) -> None: |
| 532 |
case_dict = json.loads(test_case_data) |
| 533 |
delegation = Delegations.from_dict(copy.deepcopy(case_dict)) |
| 534 |
self.assertDictEqual(case_dict, delegation.to_dict()) |
| 535 |
|
| 536 |
invalid_targetfiles = { |
| 537 |
"no hashes": '{"length": 1}', |
| 538 |
"no length": '{"hashes": {"sha256": "abc"}}', |
| 539 |
# The remaining cases are the same as for invalid_hashes and |
| 540 |
# invalid_length datasets. |
| 541 |
} |
| 542 |
|
| 543 |
@utils.run_sub_tests_with_dataset(invalid_targetfiles) |
| 544 |
def test_invalid_targetfile_serialization( |
| 545 |
self, test_case_data: str |
| 546 |
) -> None: |
| 547 |
case_dict = json.loads(test_case_data) |
| 548 |
with self.assertRaises(KeyError): |
| 549 |
TargetFile.from_dict(case_dict, "file1.txt") |
| 550 |
|
| 551 |
valid_targetfiles = { |
| 552 |
"all": '{"length": 12, "hashes": {"sha256" : "abc"}, \ |
| 553 |
"custom" : {"foo": "bar"} }', |
| 554 |
"no custom": '{"length": 12, "hashes": {"sha256" : "abc"}}', |
| 555 |
"unrecognized field": '{"length": 12, "hashes": {"sha256" : "abc"}, \ |
| 556 |
"custom" : {"foo": "bar"}, "foo": "bar"}', |
| 557 |
} |
| 558 |
|
| 559 |
@utils.run_sub_tests_with_dataset(valid_targetfiles) |
| 560 |
def test_targetfile_serialization(self, test_case_data: str) -> None: |
| 561 |
case_dict = json.loads(test_case_data) |
| 562 |
target_file = TargetFile.from_dict(copy.copy(case_dict), "file1.txt") |
| 563 |
self.assertDictEqual(case_dict, target_file.to_dict()) |
| 564 |
|
| 565 |
valid_targets = { |
| 566 |
"all attributes": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 567 |
"targets": { \ |
| 568 |
"file.txt": {"length": 12, "hashes": {"sha256" : "abc"} }, \ |
| 569 |
"file2.txt": {"length": 50, "hashes": {"sha256" : "cde"} } }, \ |
| 570 |
"delegations": { \ |
| 571 |
"keys": { \ |
| 572 |
"keyid" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}, \ |
| 573 |
"keyid2": {"keytype": "ed25519", "scheme": "ed25519", "keyval": {"public": "bar"}}}, \ |
| 574 |
"roles": [ \ |
| 575 |
{"keyids": ["keyid"], "name": "a", "terminating": true, "paths": ["fn1"], "threshold": 3}, \ |
| 576 |
{"keyids": ["keyid2"], "name": "b", "terminating": true, "paths": ["fn2"], "threshold": 4} ] \ |
| 577 |
}}', |
| 578 |
"empty targets": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 579 |
"targets": {}, \ |
| 580 |
"delegations": {"keys": {"keyid" : {"keytype": "rsa", \ |
| 581 |
"scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"} }}, \ |
| 582 |
"roles": [ {"keyids": ["keyid"], "name": "a", "paths": ["fn1", "fn2"], "terminating": true, "threshold": 3} ]} \ |
| 583 |
}', |
| 584 |
"no delegations": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 585 |
"targets": { "file.txt": {"length": 12, "hashes": {"sha256" : "abc"} } } \ |
| 586 |
}', |
| 587 |
"unrecognized_field": '{"_type": "targets", "spec_version": "1.0.0", "version": 1, "expires": "2030-01-01T00:00:00Z", \ |
| 588 |
"targets": {}, "foo": "bar"}', |
| 589 |
} |
| 590 |
|
| 591 |
@utils.run_sub_tests_with_dataset(valid_targets) |
| 592 |
def test_targets_serialization(self, test_case_data: str) -> None: |
| 593 |
case_dict = json.loads(test_case_data) |
| 594 |
targets = Targets.from_dict(copy.deepcopy(case_dict)) |
| 595 |
self.assertDictEqual(case_dict, targets.to_dict()) |
| 596 |
|
| 597 |
|
| 598 |
# Run unit test. |
| 599 |
if __name__ == "__main__": |
| 600 |
utils.configure_test_logging(sys.argv) |
| 601 |
unittest.main() |