From 1ac3fc79b71dd6253237cfe2a6f51c940c6a6c60 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Aug 2022 18:09:45 +0200 Subject: [PATCH 1/4] unity: add linux port This allows using unity fixture in Linux host tests --- components/unity/CMakeLists.txt | 9 +++---- components/unity/unity_port_linux.c | 42 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 components/unity/unity_port_linux.c diff --git a/components/unity/CMakeLists.txt b/components/unity/CMakeLists.txt index 9659dbba89..ff521fa1d4 100644 --- a/components/unity/CMakeLists.txt +++ b/components/unity/CMakeLists.txt @@ -32,6 +32,7 @@ if(NOT "${target}" STREQUAL "linux") list(APPEND srcs "unity_port_esp32.c") list(APPEND srcs "port/esp/unity_utils_memory_esp.c") else() + list(APPEND srcs "unity_port_linux.c") list(APPEND srcs "port/linux/unity_utils_memory_linux.c") endif() @@ -43,10 +44,8 @@ if(CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER) idf_component_optional_requires(PRIVATE spi_flash) endif() -if(NOT "${target}" STREQUAL "linux") - target_compile_definitions(${COMPONENT_LIB} PUBLIC - -DUNITY_INCLUDE_CONFIG_H - ) -endif() +target_compile_definitions(${COMPONENT_LIB} PUBLIC + -DUNITY_INCLUDE_CONFIG_H +) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable) diff --git a/components/unity/unity_port_linux.c b/components/unity/unity_port_linux.c new file mode 100644 index 0000000000..a295879c33 --- /dev/null +++ b/components/unity/unity_port_linux.c @@ -0,0 +1,42 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include +#include +#include +#include "unity.h" +#include "sdkconfig.h" + + +static struct timeval s_test_start, s_test_stop; + +void unity_putc(int c) +{ + putc(c, stdout); +} + +void unity_flush(void) +{ + fflush(stdout); + fsync(fileno(stdout)); +} + +void unity_exec_time_start(void) +{ + gettimeofday(&s_test_start, NULL); +} + +void unity_exec_time_stop(void) +{ + gettimeofday(&s_test_stop, NULL); +} + +uint32_t unity_exec_time_get_ms(void) +{ + return (uint32_t) (((s_test_stop.tv_sec * 1000000ULL + s_test_stop.tv_usec) - + (s_test_start.tv_sec * 1000000ULL + s_test_start.tv_usec)) / 1000); +} From 91d0e016a118b30dc33a34973ddff7f657ae8fd1 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Aug 2022 18:12:04 +0200 Subject: [PATCH 2/4] spi_flash: fix build for Linux target CONFIG_MMU_PAGE_SIZE is not defined for the Linux target, add a fallback definition. --- components/spi_flash/partition.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/spi_flash/partition.c b/components/spi_flash/partition.c index 1ff38261aa..a161c484e5 100644 --- a/components/spi_flash/partition.c +++ b/components/spi_flash/partition.c @@ -30,6 +30,13 @@ #include "esp_private/partition_linux.h" #endif +#ifndef CONFIG_IDF_TARGET_LINUX +#define MMU_PAGE_SIZE CONFIG_MMU_PAGE_SIZE +#else +// No relation to the page size on Linux; assume the same value as on ESP32 +#define MMU_PAGE_SIZE 65536 +#endif // CONFIG_MMU_PAGE_SIZE + #ifndef NDEBUG // Enable built-in checks in queue.h in debug builds #define INVARIANTS @@ -78,7 +85,7 @@ static esp_err_t load_partitions(void) esp_rom_md5_init(&context); #endif - uint32_t partition_align_pg_size = (ESP_PARTITION_TABLE_OFFSET) & ~(CONFIG_MMU_PAGE_SIZE - 1); + uint32_t partition_align_pg_size = (ESP_PARTITION_TABLE_OFFSET) & ~(MMU_PAGE_SIZE - 1); uint32_t partition_pad = ESP_PARTITION_TABLE_OFFSET - partition_align_pg_size; #if CONFIG_IDF_TARGET_LINUX From ab02e156015ca551d7a92ef0db04fdbf03b41246 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Aug 2022 18:13:47 +0200 Subject: [PATCH 3/4] spi_flash: use Unity fixture in the partition API test --- .../partition_api_test/CMakeLists.txt | 2 + .../partition_api_test/main/CMakeLists.txt | 4 +- ...tion_api_test.cpp => partition_api_test.c} | 97 ++++++++++++------- .../partition_api_test/sdkconfig.defaults | 1 + 4 files changed, 65 insertions(+), 39 deletions(-) rename components/spi_flash/host_test/partition_api_test/main/{partition_api_test.cpp => partition_api_test.c} (55%) diff --git a/components/spi_flash/host_test/partition_api_test/CMakeLists.txt b/components/spi_flash/host_test/partition_api_test/CMakeLists.txt index 469a614a14..3986bdb314 100644 --- a/components/spi_flash/host_test/partition_api_test/CMakeLists.txt +++ b/components/spi_flash/host_test/partition_api_test/CMakeLists.txt @@ -7,3 +7,5 @@ set(COMPONENTS main) list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") project(partition_api_test) + +add_dependencies(partition_api_test.elf partition-table) diff --git a/components/spi_flash/host_test/partition_api_test/main/CMakeLists.txt b/components/spi_flash/host_test/partition_api_test/main/CMakeLists.txt index 408a6111e1..a43dde98bf 100644 --- a/components/spi_flash/host_test/partition_api_test/main/CMakeLists.txt +++ b/components/spi_flash/host_test/partition_api_test/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "partition_api_test.cpp" - REQUIRES spi_flash) +idf_component_register(SRCS "partition_api_test.c" + REQUIRES spi_flash unity) diff --git a/components/spi_flash/host_test/partition_api_test/main/partition_api_test.cpp b/components/spi_flash/host_test/partition_api_test/main/partition_api_test.c similarity index 55% rename from components/spi_flash/host_test/partition_api_test/main/partition_api_test.cpp rename to components/spi_flash/host_test/partition_api_test/main/partition_api_test.c index 4ee8849496..a4d5278c0a 100644 --- a/components/spi_flash/host_test/partition_api_test/main/partition_api_test.cpp +++ b/components/spi_flash/host_test/partition_api_test/main/partition_api_test.c @@ -10,64 +10,74 @@ #include "esp_err.h" #include "esp_partition.h" #include "esp_private/partition_linux.h" +#include "unity.h" +#include "unity_fixture.h" -int main(int argc, char **argv) + +TEST_GROUP(partition_api); + +TEST_SETUP(partition_api) { - printf("Partition API Linux emulation test: "); +} - //////////////////////////////////////// - //PARTITION LOOKUP: +TEST_TEAR_DOWN(partition_api) +{ +} - //1. esp_partition_find (label=STORAGE) +TEST(partition_api, test_partition_find_basic) +{ esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); - assert(iter); + TEST_ASSERT_NOT_NULL(iter); - //2. esp_partition_get (label=STORAGE) const esp_partition_t *part = esp_partition_get(iter); - assert(part); + TEST_ASSERT_NOT_NULL(part); - //3. esp_partition_iterator_release (label STORAGE iter): assumed OK esp_partition_iterator_release(iter); +} - //////////////////////////////////////// - //ITERATORS, PARTITION PROPERTIES: - - //4. esp_partition_find_first (type=APP, subtype=ANY) - const esp_partition_t *partition_app = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL); - assert(partition_app); - - //5. enumerate all APP partitions - iter = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL); - assert(iter); +TEST(partition_api, test_partition_find_app) +{ + esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL); + TEST_ASSERT_NOT_NULL(iter); size_t counter = 0; while (iter != NULL) { const esp_partition_t *part_data = esp_partition_get(iter); counter++; - assert(part_data); + TEST_ASSERT_NOT_NULL(part_data); iter = esp_partition_next(iter); } esp_partition_iterator_release(iter); +} - //6. enumerate all DATA partitions and print details for each - iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL); - assert(iter); - counter = 0; +TEST(partition_api, test_partition_find_data) +{ + esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, NULL); + TEST_ASSERT_NOT_NULL(iter); + size_t counter = 0; while (iter != NULL) { const esp_partition_t *part_data = esp_partition_get(iter); counter++; - assert(part_data); + TEST_ASSERT_NOT_NULL(part_data); iter = esp_partition_next(iter); } esp_partition_iterator_release(iter); +} + +TEST(partition_api, test_partition_find_first) +{ + const esp_partition_t *partition_app = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL); + TEST_ASSERT_NOT_NULL(partition_app); - //7. esp_partition_find_first (type=DATA, label=STORAGE) const esp_partition_t *partition_data = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); - assert(partition_data); + TEST_ASSERT_NOT_NULL(partition_data); +} - ///////////////////////////////////// - //OPERATIONS +TEST(partition_api, test_partition_ops) +{ + const esp_partition_t *partition_data = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); + TEST_ASSERT_NOT_NULL(partition_data); uint8_t buff[] = "ABCDEFGHIJKLMNOP"; size_t bufsize = sizeof(buff); @@ -75,12 +85,12 @@ int main(int argc, char **argv) //8. esp_partition_write/raw esp_err_t err = esp_partition_write(partition_data, off, (const void *)buff, bufsize); - assert(err == ESP_OK); + TEST_ESP_OK(err); //9. esp_partition_read/raw uint8_t buffout[32] = {0}; err = esp_partition_read(partition_data, off, (void *)buffout, bufsize); - assert(err == ESP_OK); + TEST_ESP_OK(err); //10. esp_partition_erase_range uint8_t buferase[bufsize]; @@ -90,17 +100,30 @@ int main(int argc, char **argv) err = esp_partition_erase_range(partition_data, sector_off, SPI_FLASH_SEC_SIZE); assert(esp_partition_read(partition_data, off, (void *)buffout, bufsize) == ESP_OK); - assert(err == ESP_OK && memcmp(buffout, buferase, bufsize) == 0); + TEST_ESP_OK(err); + TEST_ASSERT_EQUAL(0, memcmp(buffout, buferase, bufsize)); //11. esp_partition_verify (partition_data) const esp_partition_t *verified_partition = esp_partition_verify(partition_data); - assert(verified_partition != NULL); + TEST_ASSERT_NOT_NULL(verified_partition); +} - //12. release SPI FLASH emulation block from memory - err = esp_partition_file_munmap(); - assert(err == ESP_OK); +TEST_GROUP_RUNNER(partition_api) +{ + RUN_TEST_CASE(partition_api, test_partition_find_basic); + RUN_TEST_CASE(partition_api, test_partition_find_app); + RUN_TEST_CASE(partition_api, test_partition_find_data); + RUN_TEST_CASE(partition_api, test_partition_find_first); + RUN_TEST_CASE(partition_api, test_partition_ops); +} - printf("OK\n"); +static void run_all_tests(void) +{ + RUN_TEST_GROUP(partition_api); +} +int main(int argc, char **argv) +{ + UNITY_MAIN_FUNC(run_all_tests); return 0; } diff --git a/components/spi_flash/host_test/partition_api_test/sdkconfig.defaults b/components/spi_flash/host_test/partition_api_test/sdkconfig.defaults index 47666febd9..275e768c6c 100644 --- a/components/spi_flash/host_test/partition_api_test/sdkconfig.defaults +++ b/components/spi_flash/host_test/partition_api_test/sdkconfig.defaults @@ -1,6 +1,7 @@ CONFIG_IDF_TARGET="linux" CONFIG_COMPILER_CXX_EXCEPTIONS=y CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n +CONFIG_UNITY_ENABLE_FIXTURE=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partition_table.csv" CONFIG_ESPTOOLPY_FLASHSIZE="4MB" From 181105cddb376e41bd596e77bc9a7e6756b8fb28 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 15 Aug 2022 18:25:14 +0200 Subject: [PATCH 4/4] ci: run partition API test on host --- .gitlab/ci/host-test.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitlab/ci/host-test.yml b/.gitlab/ci/host-test.yml index b2333592ca..834de0ffe7 100644 --- a/.gitlab/ci/host-test.yml +++ b/.gitlab/ci/host-test.yml @@ -431,6 +431,14 @@ test_linux_example: - timeout 5 ./build/linux_host_app.elf >test.log || true - grep "Restarting" test.log +test_partition_api_host: + extends: .host_test_template + script: + - cd ${IDF_PATH}/components/spi_flash/host_test/partition_api_test + - idf.py build + - timeout 5 ./build/partition_api_test.elf >test.log + - grep " 0 Failures" test.log + test_gen_soc_caps_kconfig: extends: .host_test_template script: