1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Unit tests for pw_software_update/root_metadata.py.""" 15 16import unittest 17 18from pw_software_update import metadata 19from pw_software_update.root_metadata import ( 20 RootKeys, 21 TargetsKeys, 22 gen_root_metadata, 23) 24 25 26class GenRootMetadataTest(unittest.TestCase): 27 """Test the generation of root metadata.""" 28 29 def setUp(self): 30 self.root_key_public = ( 31 b'-----BEGIN PUBLIC KEY-----\n' 32 b'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE4XWOT3o27TNqeh7YF7P+2ErLzzFm' 33 b'c/VItYABCqw7Hh5z8wtNjGyo0GnUSBWeISg3LMs/WjOkCiwjawjqmI8OrQ==' 34 b'\n-----END PUBLIC KEY-----\n' 35 ) 36 37 self.targets_key_public = ( 38 b'-----BEGIN PUBLIC KEY-----\n' 39 b'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE9UM6qRZJ0gIWwLjo8tjbrrBTlKXg' 40 b'ukwVjOlnguSSiYMrN4MDqMlNDnaJgLvcCuiNUKHu9Oj1DG1i6ckNdE4VTA==' 41 b'\n-----END PUBLIC KEY-----\n' 42 ) 43 44 def test_multiple_keys(self) -> None: 45 """Checks that multiple keys generates multiple KeyMappings and 46 SignatureRequirements.""" 47 root_metadata = gen_root_metadata( 48 RootKeys([self.root_key_public]), 49 TargetsKeys([self.targets_key_public]), 50 version=42, 51 ) 52 53 self.assertEqual(len(root_metadata.keys), 2) 54 self.assertEqual( 55 len(root_metadata.root_signature_requirement.key_ids), 1 56 ) 57 self.assertEqual(root_metadata.root_signature_requirement.threshold, 1) 58 self.assertEqual( 59 len(root_metadata.targets_signature_requirement.key_ids), 1 60 ) 61 self.assertEqual( 62 root_metadata.targets_signature_requirement.threshold, 1 63 ) 64 self.assertEqual(root_metadata.common_metadata.version, 42) 65 self.assertEqual( 66 root_metadata.common_metadata.role, metadata.RoleType.ROOT.value 67 ) 68 69 70if __name__ == '__main__': 71 unittest.main() 72