From 7b5c0fb96bc7653d817b84fc5bf53177c5d23fc0 Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Fri, 21 Jul 2023 11:14:03 +0800 Subject: [PATCH] ci(build_system): add a test case for bootloader override --- tools/test_build_system/test_bootloader.py | 25 ++++++++++++++++++- .../test_build_system_helpers/__init__.py | 6 ++--- .../test_build_system_helpers/idf_utils.py | 11 ++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tools/test_build_system/test_bootloader.py b/tools/test_build_system/test_bootloader.py index 991e5e119c..4f90d7d004 100644 --- a/tools/test_build_system/test_bootloader.py +++ b/tools/test_build_system/test_bootloader.py @@ -5,7 +5,7 @@ import shutil from pathlib import Path from typing import Union -from test_build_system_helpers import EnvDict, IdfPyFunc, file_contains +from test_build_system_helpers import EnvDict, IdfPyFunc, bin_file_contains, file_contains, replace_in_file def get_two_header_bytes(file_path: Union[str, Path]) -> str: @@ -31,6 +31,29 @@ def test_bootloader_custom_overrides_original(test_app_copy: Path, idf_py: IdfPy str(test_app_copy / 'components' / 'bootloader' / 'subproject' / 'main' / 'bootloader_start.c')) +def test_bootloader_custom_ignores_extra_component(test_app_copy: Path, idf_py: IdfPyFunc, default_idf_env: EnvDict) -> None: + logging.info('Custom bootloader can ignore extra components') + idf_path = Path(default_idf_env.get('IDF_PATH')) + # Import the main bootloader component that overrides the default one + shutil.copytree(idf_path / 'examples' / 'custom_bootloader' / 'bootloader_override' / 'bootloader_components', + test_app_copy / 'bootloader_components') + # Compile it normally and make sure the bootloader is overridden for now + idf_py('bootloader') + # Search for 'Custom bootloader message defined in the KConfig file.' in the resulting binary + # which is the default value for EXAMPLE_BOOTLOADER_WELCOME_MESSAGE Kconfig option. + default_message = bytes('Custom bootloader message defined in the KConfig file.', 'ascii') + assert bin_file_contains(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin', default_message) + # Clean the project + idf_py('fullclean') + # Ignore bootloader component and rebuild + replace_in_file(test_app_copy / 'CMakeLists.txt', + '# placeholder_after_include_project_cmake', + 'set(BOOTLOADER_IGNORE_EXTRA_COMPONENT "main")') + idf_py('bootloader') + # The overridden message must not be present anymore + assert not bin_file_contains(test_app_copy / 'build' / 'bootloader' / 'bootloader.bin', default_message) + + def test_bootloader_correctly_set_image_header(test_app_copy: Path, idf_py: IdfPyFunc) -> None: logging.info('Flash size is correctly set in the bootloader image header') # Build with the default 2MB setting diff --git a/tools/test_build_system/test_build_system_helpers/__init__.py b/tools/test_build_system/test_build_system_helpers/__init__.py index e8f8794167..9f7616cdc0 100644 --- a/tools/test_build_system/test_build_system_helpers/__init__.py +++ b/tools/test_build_system/test_build_system_helpers/__init__.py @@ -2,8 +2,8 @@ # SPDX-License-Identifier: Apache-2.0 from .build_constants import ALL_ARTIFACTS, APP_BINS, BOOTLOADER_BINS, JSON_METADATA, PARTITION_BIN from .editing import append_to_file, replace_in_file -from .idf_utils import (EXT_IDF_PATH, EnvDict, IdfPyFunc, file_contains, find_python, get_idf_build_env, run_cmake, - run_cmake_and_build, run_idf_py) +from .idf_utils import (EXT_IDF_PATH, EnvDict, IdfPyFunc, bin_file_contains, file_contains, find_python, + get_idf_build_env, run_cmake, run_cmake_and_build, run_idf_py) from .snapshot import Snapshot, get_snapshot __all__ = [ @@ -11,5 +11,5 @@ __all__ = [ 'get_idf_build_env', 'run_idf_py', 'EXT_IDF_PATH', 'EnvDict', 'IdfPyFunc', 'Snapshot', 'get_snapshot', 'run_cmake', 'APP_BINS', 'BOOTLOADER_BINS', 'PARTITION_BIN', 'JSON_METADATA', 'ALL_ARTIFACTS', - 'run_cmake_and_build', 'find_python', 'file_contains' + 'run_cmake_and_build', 'find_python', 'file_contains', 'bin_file_contains' ] diff --git a/tools/test_build_system/test_build_system_helpers/idf_utils.py b/tools/test_build_system/test_build_system_helpers/idf_utils.py index 5332a61d3c..c4eacdd12a 100644 --- a/tools/test_build_system/test_build_system_helpers/idf_utils.py +++ b/tools/test_build_system/test_build_system_helpers/idf_utils.py @@ -149,3 +149,14 @@ def file_contains(filename: Union[str, Path], what: Union[str, Pattern]) -> bool return what in data else: return re.search(what, data) is not None + + +def bin_file_contains(filename: Union[str, Path], what: bytearray) -> bool: + """ + Returns true if the binary file contains the given string + :param filename: path to file where lookup is executed + :param what: searched bytes + """ + with open(filename, 'rb') as f: + data = f.read() + return data.find(what) != -1