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/metadata.py.""" 15 16import unittest 17 18from pw_software_update import metadata 19from pw_software_update.tuf_pb2 import HashFunction 20 21 22class GenTargetsMetadataTest(unittest.TestCase): 23 """Test the generation of targets metadata.""" 24 25 def test_multiple_targets(self): 26 """Checks that multiple targets generates multiple TargetFiles.""" 27 target_payloads = { 28 'foo': b'\x1e\xe7', 29 'bar': b'\x12\x34', 30 } 31 targets_metadata = metadata.gen_targets_metadata( 32 target_payloads, (HashFunction.SHA256,), version=42 33 ) 34 self.assertEqual(2, len(targets_metadata.target_files)) 35 self.assertEqual( 36 metadata.RoleType.TARGETS.value, 37 targets_metadata.common_metadata.role, 38 ) 39 self.assertEqual(42, targets_metadata.common_metadata.version) 40 41 42class GenHashesTest(unittest.TestCase): 43 """Test the generation of hashes.""" 44 45 def test_sha256(self): 46 """Checks that SHA256 hashes are computed and stored properly.""" 47 data = b'\x1e\xe7' 48 sha256_hash = metadata.gen_hashes(data, (HashFunction.SHA256,))[0] 49 self.assertEqual( 50 '9f36ce605a3b28110d2a25ec36bdfff86059086cbd53c9efc1428ef01070515d', 51 sha256_hash.hash.hex(), 52 ) 53 54 55if __name__ == '__main__': 56 unittest.main() 57