Skip to content

Commit a9fdf1f

Browse files
author
Michael Schwarcz
committed
Add assertions and tests
1 parent e7341a9 commit a9fdf1f

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

tools/psa/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
from os.path import basename
2+
from tools.resources import FileType
23

34

4-
def find_secure_image(notify, resources, ns_image_path, configured_s_image_path, image_type):
5+
def find_secure_image(notify, resources, ns_image_path, configured_s_image_filename, image_type):
56
""" Find secure image. """
7+
8+
assert ns_image_path and configured_s_image_filename, 'ns_image_path and configured_s_image_path are mandatory'
9+
assert image_type in [FileType.BIN, FileType.HEX], 'image_type must be of type BIN or HEX'
10+
611
image_files = resources.get_file_paths(image_type)
7-
secure_image = next((f for f in image_files if basename(f) == configured_s_image_path), None)
12+
assert image_files, 'No image files found for this target'
13+
14+
secure_image = next((f for f in image_files if basename(f) == configured_s_image_filename), None)
815
secure_image = next((f for f in image_files if basename(f) == basename(ns_image_path)), secure_image)
916

1017
if secure_image:
1118
notify.debug("Secure image file found: %s." % secure_image)
1219
else:
13-
notify.debug("Secure image file %s not found. Aborting." % configured_s_image_path)
20+
notify.debug("Secure image file %s not found. Aborting." % configured_s_image_filename)
1421
raise Exception("Required secure image not found.")
1522

1623
return secure_image
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2019 ARM Limited
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import pytest
18+
import os
19+
from tools.notifier.mock import MockNotifier
20+
from tools.resources import Resources, FileType
21+
from tools.psa import find_secure_image
22+
23+
24+
def test_find_secure_image():
25+
mock_notifier = MockNotifier()
26+
mock_resources = Resources(mock_notifier)
27+
ns_image_path = os.path.join('BUILD', 'TARGET_NS', 'app.bin')
28+
ns_test_path = os.path.join('BUILD', 'TARGET_NS', 'test.bin')
29+
config_s_image_name = 'target_config.bin'
30+
default_bin = os.path.join('prebuilt', config_s_image_name)
31+
test_bin = os.path.join('prebuilt', 'test.bin')
32+
33+
with pytest.raises(Exception, match='ns_image_path and configured_s_image_path are mandatory'):
34+
find_secure_image(mock_notifier, mock_resources, None, None, FileType.BIN)
35+
find_secure_image(mock_notifier, mock_resources, ns_image_path, None, FileType.BIN)
36+
find_secure_image(mock_notifier, mock_resources, None, config_s_image_name, FileType.BIN)
37+
38+
with pytest.raises(Exception, match='image_type must be of type BIN or HEX'):
39+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, None)
40+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.C_SRC)
41+
42+
with pytest.raises(Exception, match='No image files found for this target'):
43+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
44+
45+
dummy_bin = os.path.join('path', 'to', 'dummy.bin')
46+
mock_resources.add_file_ref(FileType.BIN, dummy_bin, dummy_bin)
47+
48+
with pytest.raises(Exception, match='Required secure image not found'):
49+
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
50+
51+
mock_resources.add_file_ref(FileType.BIN, default_bin, default_bin)
52+
mock_resources.add_file_ref(FileType.BIN, test_bin, test_bin)
53+
secure_image = find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN)
54+
assert secure_image == default_bin
55+
56+
secure_image = find_secure_image(mock_notifier, mock_resources, ns_test_path, config_s_image_name, FileType.BIN)
57+
assert secure_image == test_bin

0 commit comments

Comments
 (0)