From f8cb539e296401cd4bc723ec8c53f130c14c76e5 Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Tue, 8 Jul 2025 09:40:49 +0200 Subject: [PATCH] feat(heap): Update test to CMake and use Catch2 component --- .gitignore | 2 - .gitlab/ci/host-test.yml | 6 --- .../.build-test-rules.yml | 3 ++ .../heap/test_multi_heap_host/CMakeLists.txt | 12 +++++ components/heap/test_multi_heap_host/Makefile | 54 ------------------- .../heap/test_multi_heap_host/README.md | 2 + components/heap/test_multi_heap_host/main.cpp | 8 --- .../test_multi_heap_host/main/CMakeLists.txt | 13 +++++ .../main/idf_component.yml | 2 + .../{ => main}/test_multi_heap.cpp | 54 +++++++++++++------ .../pytest_multi_heap_linux.py | 19 +++++++ .../sdkconfig.ci.comprehensive_poisoning | 3 ++ .../sdkconfig.ci.light_poisoning | 3 ++ .../sdkconfig.ci.no_poisoning | 3 ++ .../test_multi_heap_host/sdkconfig.defaults | 1 + .../test_multi_heap_host/test_all_configs.sh | 20 ------- tools/ci/executable-list.txt | 1 - 17 files changed, 99 insertions(+), 107 deletions(-) create mode 100644 components/heap/test_multi_heap_host/.build-test-rules.yml create mode 100644 components/heap/test_multi_heap_host/CMakeLists.txt delete mode 100644 components/heap/test_multi_heap_host/Makefile create mode 100644 components/heap/test_multi_heap_host/README.md delete mode 100644 components/heap/test_multi_heap_host/main.cpp create mode 100644 components/heap/test_multi_heap_host/main/CMakeLists.txt create mode 100644 components/heap/test_multi_heap_host/main/idf_component.yml rename components/heap/test_multi_heap_host/{ => main}/test_multi_heap.cpp (94%) create mode 100644 components/heap/test_multi_heap_host/pytest_multi_heap_linux.py create mode 100644 components/heap/test_multi_heap_host/sdkconfig.ci.comprehensive_poisoning create mode 100644 components/heap/test_multi_heap_host/sdkconfig.ci.light_poisoning create mode 100644 components/heap/test_multi_heap_host/sdkconfig.ci.no_poisoning create mode 100644 components/heap/test_multi_heap_host/sdkconfig.defaults delete mode 100755 components/heap/test_multi_heap_host/test_all_configs.sh diff --git a/.gitignore b/.gitignore index 0e2e4e1fd4..511ee125b4 100644 --- a/.gitignore +++ b/.gitignore @@ -66,8 +66,6 @@ build_summary_*.xml coverage.info coverage_report/ -test_multi_heap_host - # VS Code Settings .vscode/ diff --git a/.gitlab/ci/host-test.yml b/.gitlab/ci/host-test.yml index fbc172163d..be2a3658e5 100644 --- a/.gitlab/ci/host-test.yml +++ b/.gitlab/ci/host-test.yml @@ -77,12 +77,6 @@ test_fatfsgen_on_host: - ./test_wl_fatfsgen.py - ./test_fatfsparse.py -test_multi_heap_on_host: - extends: .host_test_template - script: - - cd components/heap/test_multi_heap_host - - ./test_all_configs.sh - test_certificate_bundle_on_host: extends: .host_test_template script: diff --git a/components/heap/test_multi_heap_host/.build-test-rules.yml b/components/heap/test_multi_heap_host/.build-test-rules.yml new file mode 100644 index 0000000000..6a9f1e856f --- /dev/null +++ b/components/heap/test_multi_heap_host/.build-test-rules.yml @@ -0,0 +1,3 @@ +components/heap/test_multi_heap_host: + enable: + - if: IDF_TARGET == "linux" diff --git a/components/heap/test_multi_heap_host/CMakeLists.txt b/components/heap/test_multi_heap_host/CMakeLists.txt new file mode 100644 index 0000000000..2c8fc998cf --- /dev/null +++ b/components/heap/test_multi_heap_host/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.16) + +add_compile_options(-m32) +add_link_options(-m32) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) + +# This test app doesn't require FreeRTOS, using mock instead +list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") + +project(multi_heap_test) diff --git a/components/heap/test_multi_heap_host/Makefile b/components/heap/test_multi_heap_host/Makefile deleted file mode 100644 index dadfad3160..0000000000 --- a/components/heap/test_multi_heap_host/Makefile +++ /dev/null @@ -1,54 +0,0 @@ -TEST_PROGRAM=test_multi_heap -all: $(TEST_PROGRAM) - -ifneq ($(filter clean,$(MAKECMDGOALS)),) -.NOTPARALLEL: # prevent make clean racing the other targets -endif - -SOURCE_FILES = $(abspath \ - test_multi_heap.cpp \ - ../multi_heap_poisoning.c \ - ../multi_heap.c \ - ../tlsf/tlsf.c \ - main.cpp \ - ) - -INCLUDE_FLAGS = -I../include -I../../../tools/catch -I../tlsf -I../tlsf/include - -GCOV ?= gcov - -CPPFLAGS += $(INCLUDE_FLAGS) -D CONFIG_LOG_DEFAULT_LEVEL -g -fstack-protector-all -m32 -CFLAGS += -Wall -Werror -fprofile-arcs -ftest-coverage -CXXFLAGS += -std=c++11 -Wall -Werror -fprofile-arcs -ftest-coverage -LDFLAGS += -lstdc++ -fprofile-arcs -ftest-coverage -m32 - -OBJ_FILES = $(filter %.o, $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)) - -COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) - -$(TEST_PROGRAM): $(OBJ_FILES) - g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES) - -$(OUTPUT_DIR): - mkdir -p $(OUTPUT_DIR) - -test: $(TEST_PROGRAM) - ./$(TEST_PROGRAM) - -$(COVERAGE_FILES): $(TEST_PROGRAM) test - -coverage.info: $(COVERAGE_FILES) - find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} + - lcov --capture --directory $(abspath ../) --no-external --output-file coverage.info --gcov-tool $(GCOV) - -coverage_report: coverage.info - genhtml coverage.info --output-directory coverage_report - @echo "Coverage report is in coverage_report/index.html" - -clean: - rm -f $(OBJ_FILES) $(TEST_PROGRAM) - rm -f $(COVERAGE_FILES) *.gcov - rm -rf coverage_report/ - rm -f coverage.info - -.PHONY: clean all test diff --git a/components/heap/test_multi_heap_host/README.md b/components/heap/test_multi_heap_host/README.md new file mode 100644 index 0000000000..37c142df16 --- /dev/null +++ b/components/heap/test_multi_heap_host/README.md @@ -0,0 +1,2 @@ +| Supported Targets | Linux | +| ----------------- | ----- | diff --git a/components/heap/test_multi_heap_host/main.cpp b/components/heap/test_multi_heap_host/main.cpp deleted file mode 100644 index f1d2676c06..0000000000 --- a/components/heap/test_multi_heap_host/main.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#define CATCH_CONFIG_MAIN -#include "catch.hpp" diff --git a/components/heap/test_multi_heap_host/main/CMakeLists.txt b/components/heap/test_multi_heap_host/main/CMakeLists.txt new file mode 100644 index 0000000000..a993af2905 --- /dev/null +++ b/components/heap/test_multi_heap_host/main/CMakeLists.txt @@ -0,0 +1,13 @@ +idf_component_register(SRCS "test_multi_heap.cpp" + "../../multi_heap_poisoning.c" + "../../multi_heap.c" + "../../tlsf/tlsf.c" + INCLUDE_DIRS + "../../include" + "../../tlsf" + "../../tlsf/include" + WHOLE_ARCHIVE) + +# Currently 'main' for IDF_TARGET=linux is defined in freertos component. +# Since we are using a freertos mock here, need to let Catch2 provide 'main'. +target_link_libraries(${COMPONENT_LIB} PRIVATE Catch2WithMain) diff --git a/components/heap/test_multi_heap_host/main/idf_component.yml b/components/heap/test_multi_heap_host/main/idf_component.yml new file mode 100644 index 0000000000..f7982136b9 --- /dev/null +++ b/components/heap/test_multi_heap_host/main/idf_component.yml @@ -0,0 +1,2 @@ +dependencies: + espressif/catch2: "^3.4.0" diff --git a/components/heap/test_multi_heap_host/test_multi_heap.cpp b/components/heap/test_multi_heap_host/main/test_multi_heap.cpp similarity index 94% rename from components/heap/test_multi_heap_host/test_multi_heap.cpp rename to components/heap/test_multi_heap_host/main/test_multi_heap.cpp index 6bc66cb6e2..467a29380c 100644 --- a/components/heap/test_multi_heap_host/test_multi_heap.cpp +++ b/components/heap/test_multi_heap_host/main/test_multi_heap.cpp @@ -1,10 +1,10 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ -#include "catch.hpp" +#include "catch2/catch_test_macros.hpp" #include "multi_heap.h" #include "../multi_heap_config.h" @@ -42,11 +42,28 @@ static void __free__(void *ptr) #undef realloc #define realloc #error +static uint8_t s_small_heap[4 * 1024]; +static multi_heap_handle_t s_small_heap_hdl = NULL; + +void get_small_heap(uint8_t **heap, multi_heap_handle_t *heap_hdl, size_t *heap_size) +{ + if (s_small_heap_hdl == NULL) { + memset(s_small_heap, 0, sizeof(s_small_heap)); + s_small_heap_hdl = multi_heap_register(s_small_heap, sizeof(s_small_heap)); + } + + *heap = s_small_heap; + *heap_hdl = s_small_heap_hdl; + *heap_size = sizeof(s_small_heap); + multi_heap_dump(*heap_hdl); +} + TEST_CASE("multi_heap simple allocations", "[multi_heap]") { - uint8_t small_heap[4 * 1024]; - - multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap)); + uint8_t *small_heap = NULL; + multi_heap_handle_t heap = NULL; + size_t small_heap_size = 0; + get_small_heap(&small_heap, &heap, &small_heap_size); size_t test_alloc_size = (multi_heap_free_size(heap) + 4) / 2; @@ -59,7 +76,7 @@ TEST_CASE("multi_heap simple allocations", "[multi_heap]") printf("small_heap %p buf %p\n", small_heap, buf); REQUIRE( buf != NULL ); REQUIRE((intptr_t)buf >= (intptr_t)small_heap); - REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + sizeof(small_heap))); + REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + small_heap_size)); REQUIRE( multi_heap_get_allocated_size(heap, buf) >= test_alloc_size ); printf("test alloc size %d\n", test_alloc_size); @@ -82,12 +99,12 @@ TEST_CASE("multi_heap simple allocations", "[multi_heap]") REQUIRE( multi_heap_free_size(heap) > multi_heap_minimum_free_size(heap) ); } - TEST_CASE("multi_heap fragmentation", "[multi_heap]") { - const size_t HEAP_SIZE = 4 * 1024; - uint8_t small_heap[HEAP_SIZE]; - multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap)); + uint8_t *small_heap = NULL; + multi_heap_handle_t heap = NULL; + size_t small_heap_size = 0; + get_small_heap(&small_heap, &heap, &small_heap_size); const size_t alloc_size = 500; @@ -135,13 +152,15 @@ TEST_CASE("multi_heap fragmentation", "[multi_heap]") /* Test that malloc/free does not leave free space fragmented */ TEST_CASE("multi_heap defrag", "[multi_heap]") { + uint8_t *small_heap = NULL; + multi_heap_handle_t heap = NULL; + size_t small_heap_size = 0; + get_small_heap(&small_heap, &heap, &small_heap_size); + void *p[4]; - uint8_t small_heap[4 * 1024]; multi_heap_info_t info, info2; - multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap)); printf("0 ---\n"); - multi_heap_dump(heap); REQUIRE( multi_heap_check(heap, true) ); multi_heap_get_info(heap, &info); REQUIRE( 0 == info.allocated_blocks ); @@ -149,9 +168,13 @@ TEST_CASE("multi_heap defrag", "[multi_heap]") printf("1 ---\n"); p[0] = multi_heap_malloc(heap, 128); - p[1] = multi_heap_malloc(heap, 32); - multi_heap_dump(heap); + REQUIRE(p[0] != NULL); REQUIRE( multi_heap_check(heap, true) ); + multi_heap_dump(heap); + p[1] = multi_heap_malloc(heap, 32); + REQUIRE(p[1] != NULL); + REQUIRE( multi_heap_check(heap, true) ); + multi_heap_dump(heap); printf("2 ---\n"); multi_heap_free(heap, p[0]); @@ -388,7 +411,6 @@ TEST_CASE("multi_heap minimum-size allocations", "[multi_heap]") uint8_t heapdata[4096]; void *p[sizeof(heapdata) / sizeof(void *)] = {NULL}; const size_t NUM_P = sizeof(p) / sizeof(void *); - size_t allocated_size = 0; multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata)); size_t before_free = multi_heap_free_size(heap); diff --git a/components/heap/test_multi_heap_host/pytest_multi_heap_linux.py b/components/heap/test_multi_heap_host/pytest_multi_heap_linux.py new file mode 100644 index 0000000000..0883d9ff1d --- /dev/null +++ b/components/heap/test_multi_heap_host/pytest_multi_heap_linux.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import pytest +from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize + + +@pytest.mark.host_test +@pytest.mark.parametrize( + 'config', + [ + 'no_poisoning', + 'light_poisoning', + 'comprehensive_poisoning', + ], +) +@idf_parametrize('target', ['linux'], indirect=['target']) +def test_multi_heap_linux(dut: Dut) -> None: + dut.expect_exact('All tests passed', timeout=180) diff --git a/components/heap/test_multi_heap_host/sdkconfig.ci.comprehensive_poisoning b/components/heap/test_multi_heap_host/sdkconfig.ci.comprehensive_poisoning new file mode 100644 index 0000000000..ccb4f41e0b --- /dev/null +++ b/components/heap/test_multi_heap_host/sdkconfig.ci.comprehensive_poisoning @@ -0,0 +1,3 @@ +CONFIG_HEAP_POISONING_COMPREHENSIVE=y +CONFIG_HEAP_POISONING_LIGHT=n +CONFIG_HEAP_POISONING_DISABLED=n diff --git a/components/heap/test_multi_heap_host/sdkconfig.ci.light_poisoning b/components/heap/test_multi_heap_host/sdkconfig.ci.light_poisoning new file mode 100644 index 0000000000..40ae6bb235 --- /dev/null +++ b/components/heap/test_multi_heap_host/sdkconfig.ci.light_poisoning @@ -0,0 +1,3 @@ +CONFIG_HEAP_POISONING_COMPREHENSIVE=n +CONFIG_HEAP_POISONING_LIGHT=y +CONFIG_HEAP_POISONING_DISABLED=n diff --git a/components/heap/test_multi_heap_host/sdkconfig.ci.no_poisoning b/components/heap/test_multi_heap_host/sdkconfig.ci.no_poisoning new file mode 100644 index 0000000000..e572cf7d69 --- /dev/null +++ b/components/heap/test_multi_heap_host/sdkconfig.ci.no_poisoning @@ -0,0 +1,3 @@ +CONFIG_HEAP_POISONING_COMPREHENSIVE=n +CONFIG_HEAP_POISONING_LIGHT=n +CONFIG_HEAP_POISONING_DISABLED=y diff --git a/components/heap/test_multi_heap_host/sdkconfig.defaults b/components/heap/test_multi_heap_host/sdkconfig.defaults new file mode 100644 index 0000000000..e4bfc208a5 --- /dev/null +++ b/components/heap/test_multi_heap_host/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_ESP_TASK_WDT_EN=n diff --git a/components/heap/test_multi_heap_host/test_all_configs.sh b/components/heap/test_multi_heap_host/test_all_configs.sh deleted file mode 100755 index ce144d0d6d..0000000000 --- a/components/heap/test_multi_heap_host/test_all_configs.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash -# -# Run the test suite with all configurations enabled -# - -FAIL=0 - -for FLAGS in "CONFIG_HEAP_POISONING_NONE" "CONFIG_HEAP_POISONING_LIGHT" "CONFIG_HEAP_POISONING_COMPREHENSIVE" ; do - echo "==== Testing with config: ${FLAGS} ====" - CPPFLAGS="-D${FLAGS}" make clean test || FAIL=1 -done - -make clean - -if [ $FAIL == 0 ]; then - echo "All configurations passed" -else - echo "Some configurations failed, see log." - exit 1 -fi diff --git a/tools/ci/executable-list.txt b/tools/ci/executable-list.txt index 797a740293..93f786554a 100644 --- a/tools/ci/executable-list.txt +++ b/tools/ci/executable-list.txt @@ -12,7 +12,6 @@ components/fatfs/test_fatfsgen/test_fatfsgen.py components/fatfs/test_fatfsgen/test_fatfsparse.py components/fatfs/test_fatfsgen/test_wl_fatfsgen.py components/fatfs/wl_fatfsgen.py -components/heap/test_multi_heap_host/test_all_configs.sh components/mbedtls/esp_crt_bundle/gen_crt_bundle.py components/mbedtls/esp_crt_bundle/test_gen_crt_bundle/test_gen_crt_bundle.py components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py