• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 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/cli.py."""
15
16import unittest
17from pw_software_update import cli, metadata, update_bundle
18from pw_software_update.tuf_pb2 import TargetsMetadata
19
20
21class AddFileToBundleTest(unittest.TestCase):
22    """Test adding a target file to an existing bundle"""
23
24    def test_adding_file_to_bundle(self):
25        """Adds a file to bundle"""
26
27        bundle = update_bundle.gen_empty_update_bundle()
28
29        target_payloads = {
30            'foo': b'foo contents',
31            'bar': b'bar contents',
32        }
33
34        for name, contents in target_payloads.items():
35            bundle = cli.add_file_to_bundle(
36                bundle=bundle, file_name=name, file_contents=contents
37            )
38
39        # Checks for existence of target in target payloads
40        self.assertEqual(target_payloads['foo'], bundle.target_payloads['foo'])
41        self.assertEqual(
42            len(bundle.target_payloads['foo']), len(target_payloads['foo'])
43        )
44
45        self.assertEqual(target_payloads['bar'], bundle.target_payloads['bar'])
46        self.assertEqual(
47            len(bundle.target_payloads['bar']), len(target_payloads['bar'])
48        )
49
50    def test_adding_duplicate_file_fails(self):
51        """Test for adding a duplicate target name to bundle"""
52
53        bundle = update_bundle.gen_empty_update_bundle()
54
55        target_payloads = {
56            'foo': b'foo contents',
57        }
58
59        for name, contents in target_payloads.items():
60            bundle = cli.add_file_to_bundle(
61                bundle=bundle, file_name=name, file_contents=contents
62            )
63
64        # Checks for raised exceptions when adding a duplicate file name
65        # in target payload
66        with self.assertRaises(Exception):
67            bundle = cli.add_file_to_bundle(
68                bundle=bundle, file_name='foo', file_contents=b'does not matter'
69            )
70
71    def test_adding_duplicate_target_file_fails(self):
72        """Test for adding a duplicate target file to bundle"""
73
74        bundle = update_bundle.gen_empty_update_bundle()
75
76        target_payloads = {'foo': b'asrgfdasrgfdasrgfdasrgfd'}
77
78        for name, contents in target_payloads.items():
79            bundle = cli.add_file_to_bundle(
80                bundle=bundle, file_name=name, file_contents=contents
81            )
82
83        # Adding a target file with no matching target_payload file name
84        target_file = metadata.gen_target_file('shoo', b'cvbfbzbz')
85
86        signed_targets_metadata = bundle.targets_metadata['targets']
87        targets_metadata = TargetsMetadata().FromString(
88            signed_targets_metadata.serialized_targets_metadata
89        )
90
91        targets_metadata.target_files.append(target_file)
92        bundle.targets_metadata[
93            'targets'
94        ].serialized_targets_metadata = targets_metadata.SerializeToString()
95
96        # Checks for raised exception for duplicate target file
97        with self.assertRaises(Exception):
98            bundle = cli.add_file_to_bundle(
99                bundle=bundle, file_name='shoo', file_contents=b'cvbfbzbz'
100            )
101
102
103if __name__ == '__main__':
104    unittest.main()
105