third_party.pigweed.src/pw_software_update/py/root_metadata_test.py
Ali Zhang 31a3f6f007 pw_software_update: Support multiple signing keys
Allow adding multiple root and targets keys when generating a root
metadata.

Bug: b/205623081

No-Docs-Update-Reason: module in early development

Change-Id: Ia6d023506edfd95d4633348c2e5f2896d7ff7050
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/68088
Pigweed-Auto-Submit: Ali Zhang <alizhang@google.com>
Reviewed-by: Joe Ethier <jethier@google.com>
Reviewed-by: David Rogers <davidrogers@google.com>
Commit-Queue: Ali Zhang <alizhang@google.com>
2021-11-11 00:04:25 +00:00

58 lines
2.4 KiB
Python

# Copyright 2021 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Unit tests for pw_software_update/root_metadata.py."""
import unittest
from pw_software_update.root_metadata import (RootKeys, TargetsKeys,
gen_root_metadata)
class GenRootMetadataTest(unittest.TestCase):
"""Test the generation of root metadata."""
def setUp(self):
self.root_key_public = (
b'-----BEGIN PUBLIC KEY-----\n'
b'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4XWOT3o27TNqeh7YF7P+2ErLzzFm'
b'c/VItYABCqw7Hh5z8wtNjGyo0GnUSBWeISg3LMs/WjOkCiwjawjqmI8OrQ=='
b'\n-----END PUBLIC KEY-----\n')
self.targets_key_public = (
b'-----BEGIN PUBLIC KEY-----\n'
b'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE9UM6qRZJ0gIWwLjo8tjbrrBTlKXg'
b'ukwVjOlnguSSiYMrN4MDqMlNDnaJgLvcCuiNUKHu9Oj1DG1i6ckNdE4VTA=='
b'\n-----END PUBLIC KEY-----\n')
def test_multiple_keys(self) -> None:
"""Checks that multiple keys generates multiple KeyMappings and
SignatureRequirements."""
root_metadata = gen_root_metadata(RootKeys([self.root_key_public]),
TargetsKeys(
[self.targets_key_public]),
version=42)
self.assertEqual(len(root_metadata.keys), 2)
self.assertEqual(len(root_metadata.root_signature_requirement.key_ids),
1)
self.assertEqual(root_metadata.root_signature_requirement.threshold, 1)
self.assertEqual(
len(root_metadata.targets_signature_requirement.key_ids), 1)
self.assertEqual(root_metadata.targets_signature_requirement.threshold,
1)
self.assertEqual(root_metadata.common_metadata.version, 42)
if __name__ == '__main__':
unittest.main()