-
Notifications
You must be signed in to change notification settings - Fork 3k
PSA tools: Find secure image at post-build #9894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from os.path import basename | ||
from tools.resources import FileType | ||
|
||
|
||
def find_secure_image(notify, resources, ns_image_path, configured_s_image_filename, image_type): | ||
""" Find secure image. """ | ||
|
||
assert ns_image_path and configured_s_image_filename, 'ns_image_path and configured_s_image_path are mandatory' | ||
assert image_type in [FileType.BIN, FileType.HEX], 'image_type must be of type BIN or HEX' | ||
|
||
image_files = resources.get_file_paths(image_type) | ||
assert image_files, 'No image files found for this target' | ||
|
||
secure_image = next((f for f in image_files if basename(f) == configured_s_image_filename), None) | ||
secure_image = next((f for f in image_files if basename(f) == basename(ns_image_path)), secure_image) | ||
|
||
if secure_image: | ||
notify.debug("Secure image file found: %s." % secure_image) | ||
else: | ||
notify.debug("Secure image file %s not found. Aborting." % configured_s_image_filename) | ||
raise Exception("Required secure image not found.") | ||
|
||
return secure_image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2019 ARM Limited | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
|
||
import pytest | ||
import os | ||
from tools.notifier.mock import MockNotifier | ||
from tools.resources import Resources, FileType | ||
from tools.psa import find_secure_image | ||
|
||
|
||
def test_find_secure_image(): | ||
mock_notifier = MockNotifier() | ||
mock_resources = Resources(mock_notifier) | ||
ns_image_path = os.path.join('BUILD', 'TARGET_NS', 'app.bin') | ||
ns_test_path = os.path.join('BUILD', 'TARGET_NS', 'test.bin') | ||
config_s_image_name = 'target_config.bin' | ||
default_bin = os.path.join('prebuilt', config_s_image_name) | ||
test_bin = os.path.join('prebuilt', 'test.bin') | ||
|
||
with pytest.raises(Exception, match='ns_image_path and configured_s_image_path are mandatory'): | ||
find_secure_image(mock_notifier, mock_resources, None, None, FileType.BIN) | ||
find_secure_image(mock_notifier, mock_resources, ns_image_path, None, FileType.BIN) | ||
find_secure_image(mock_notifier, mock_resources, None, config_s_image_name, FileType.BIN) | ||
|
||
with pytest.raises(Exception, match='image_type must be of type BIN or HEX'): | ||
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, None) | ||
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.C_SRC) | ||
|
||
with pytest.raises(Exception, match='No image files found for this target'): | ||
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) | ||
|
||
dummy_bin = os.path.join('path', 'to', 'dummy.bin') | ||
mock_resources.add_file_ref(FileType.BIN, dummy_bin, dummy_bin) | ||
|
||
with pytest.raises(Exception, match='Required secure image not found'): | ||
find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) | ||
|
||
mock_resources.add_file_ref(FileType.BIN, default_bin, default_bin) | ||
mock_resources.add_file_ref(FileType.BIN, test_bin, test_bin) | ||
secure_image = find_secure_image(mock_notifier, mock_resources, ns_image_path, config_s_image_name, FileType.BIN) | ||
assert secure_image == default_bin | ||
|
||
secure_image = find_secure_image(mock_notifier, mock_resources, ns_test_path, config_s_image_name, FileType.BIN) | ||
assert secure_image == test_bin |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing license header here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done