Skip to content

Commit

Permalink
Merge branch 'ci/migrate_mbedtls_unit_test_app' into 'master'
Browse files Browse the repository at this point in the history
ci: Migrate mbedtls unit tests from unit-test-app to component-test-app

Closes IDF-5593

See merge request espressif/esp-idf!19783
  • Loading branch information
mahavirj committed Sep 19, 2022
2 parents cb48a7b + 83504f9 commit 64129c2
Show file tree
Hide file tree
Showing 54 changed files with 211 additions and 60 deletions.
25 changes: 7 additions & 18 deletions .gitlab/ci/target-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ test_app_test_esp32_generic:

UT_001:
extends: .unit_test_esp32_template
parallel: 50
parallel: 44
tags:
- ESP32_IDF
- UT_T1_1
Expand All @@ -880,7 +880,7 @@ UT_001:

UT_002:
extends: .unit_test_esp32_template
parallel: 21
parallel: 16
tags:
- ESP32_IDF
- UT_T1_1
Expand Down Expand Up @@ -915,18 +915,10 @@ UT_006:

UT_007:
extends: .unit_test_esp32_template
parallel: 2
tags:
- ESP32_IDF
- UT_T1_1

UT_008:
extends: .unit_test_esp32_template
tags:
- ESP32_IDF
- UT_T1_GPIO
- psram

UT_014:
extends: .unit_test_esp32_template
tags:
Expand Down Expand Up @@ -978,14 +970,13 @@ UT_033:

UT_034:
extends: .unit_test_esp32_template
parallel: 2
tags:
- ESP32_IDF
- UT_T1_ESP_FLASH

UT_035:
extends: .unit_test_esp32s2_template
parallel: 50
parallel: 44
tags:
- ESP32S2_IDF
- UT_T1_1
Expand All @@ -999,7 +990,6 @@ UT_036:

UT_038:
extends: .unit_test_esp32s2_template
parallel: 2
tags:
- ESP32S2_IDF
- UT_T1_ESP_FLASH
Expand Down Expand Up @@ -1087,7 +1077,7 @@ UT_S2_SDSPI:

UT_C2:
extends: .unit_test_esp32c2_template
parallel: 22
parallel: 19
tags:
- ESP32C2_IDF
- UT_T1_1
Expand Down Expand Up @@ -1119,14 +1109,13 @@ UT_C2_FLASH:

UT_C3:
extends: .unit_test_esp32c3_template
parallel: 31
parallel: 27
tags:
- ESP32C3_IDF
- UT_T1_1

UT_C3_FLASH:
extends: .unit_test_esp32c3_template
parallel: 2
tags:
- ESP32C3_IDF
- UT_T1_ESP_FLASH
Expand Down Expand Up @@ -1157,14 +1146,14 @@ UT_C3_SDSPI:

UT_S3:
extends: .unit_test_esp32s3_template
parallel: 29
parallel: 25
tags:
- ESP32S3_IDF
- UT_T1_1

UT_S3_QUAD_PSRAM:
extends: .unit_test_esp32s3_template
parallel: 4
parallel: 2
tags:
- ESP32S3_IDF
- UT_T1_1
Expand Down
7 changes: 7 additions & 0 deletions components/mbedtls/test_apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#This is the project CMakeLists.txt file for the test subproject
cmake_minimum_required(VERSION 3.16)

set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mbedtls_test)
2 changes: 2 additions & 0 deletions components/mbedtls/test_apps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- |
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ set(TEST_CRTS "crts/server_cert_chain.pem"

idf_component_register(SRC_DIRS "."
PRIV_INCLUDE_DIRS "."
PRIV_REQUIRES cmock test_utils mbedtls esp_timer
EMBED_TXTFILES ${TEST_CRTS})
PRIV_REQUIRES cmock test_utils mbedtls esp_timer unity
EMBED_TXTFILES ${TEST_CRTS}
WHOLE_ARCHIVE)
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

idf_component_get_property(mbedtls mbedtls COMPONENT_LIB)
Expand Down
46 changes: 46 additions & 0 deletions components/mbedtls/test_apps/main/app_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "unity.h"
#include "memory_checks.h"

/* setUp runs before every test */
void setUp(void)
{
test_utils_record_free_mem();
test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL);
test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_LWIP);
}

/* tearDown runs after every test */
void tearDown(void)
{
/* some FreeRTOS stuff is cleaned up by idle task */
vTaskDelay(5);

/* clean up some of the newlib's lazy allocations */
esp_reent_cleanup();

/* check if unit test has caused heap corruption in any heap */
TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");

test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));

}

static void test_task(void *pvParameters)
{
vTaskDelay(2); /* Delay a bit to let the main task be deleted */
unity_run_menu();
}

void app_main(void)
{
xTaskCreatePinnedToCore(test_task, "testTask", CONFIG_UNITY_FREERTOS_STACK_SIZE, NULL, CONFIG_UNITY_FREERTOS_PRIORITY, NULL, CONFIG_UNITY_FREERTOS_CPU);
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* mbedTLS AES test
*/
#include <string.h>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* mbedTLS AES performance test
*/
#include <string.h>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* Implementation of utility functions to verify
unit tests aren't performing SMP-unsafe DPORT reads.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* Utility functions to test that APB access is still safe
while the other CPU performs some set of DPORT accesses
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* mbedTLS self-tests as unit tests
Focus on testing functionality where we use ESP32 hardware
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "freertos/FreeRTOS.h"
#include "unity.h"
#include "test_utils.h"
#include "memory_checks.h"
#include "ccomp_timer.h"

#define PRINT_DEBUG_INFO
Expand Down Expand Up @@ -423,23 +424,24 @@ static void print_rsa_details(mbedtls_rsa_context *rsa)
}
#endif

/** NOTE:
* For ESP32-S3, CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG is enabled
* by default; allocating a lock of 92 bytes, which is never freed.
*
* MR !18574 adds the MPI crypto lock for S3 increasing the leakage by
* 92 bytes. This caused the RSA UT to fail with a leakage more than
* 1024 bytes.
*
* The allocations made by ESP32-S2 (944 bytes) and ESP32-S3 are the same,
* except for the JTAG lock (92 + 944 > 1024).
*/
#if CONFIG_FREERTOS_SMP // IDF-5260
TEST_CASE("test performance RSA key operations", "[bignum][leaks=1088][timeout=60]")
TEST_CASE("test performance RSA key operations", "[bignum][timeout=60]")
#else
TEST_CASE("test performance RSA key operations", "[bignum][leaks=1088]")
TEST_CASE("test performance RSA key operations", "[bignum]")
#endif
{
/** NOTE:
* For ESP32-S3, CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG is enabled
* by default; allocating a lock of 92 bytes, which is never freed.
*
* MR !18574 adds the MPI crypto lock for S3 increasing the leakage by
* 92 bytes. This caused the RSA UT to fail with a leakage more than
* 1024 bytes.
*
* The allocations made by ESP32-S2 (944 bytes) and ESP32-S3 are the same,
* except for the JTAG lock (92 + 944 > 1024).
*/
TEST_ESP_OK(test_utils_set_leak_level(1088, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
for (int keysize = 2048; keysize <= SOC_RSA_MAX_BIT_LEN; keysize += 1024) {
rsa_key_operations(keysize, true, false);
}
Expand Down
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions components/mbedtls/test_apps/pytest_mbedtls_ut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0

import pytest
from pytest_embedded import Dut


@pytest.mark.supported_targets
@pytest.mark.generic
def test_mbedtls(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=120)


@pytest.mark.esp32
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.esp32c3
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
[
'aes_no_hw',
],
indirect=True,
)
def test_mbedtls_aes_no_hw(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=120)


@pytest.mark.esp32
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
[
'psram',
],
indirect=True,
)
def test_mbedtls_psram(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=120)


@pytest.mark.esp32
@pytest.mark.generic
@pytest.mark.parametrize(
'config',
[
'psram_esp32',
'psram_all_ext',
],
indirect=True,
)
def test_mbedtls_psram_esp32(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('*')
dut.expect_unity_test_output(timeout=120)
2 changes: 2 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.ci.aes_no_hw
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_MBEDTLS_HARDWARE_AES=n
CONFIG_MBEDTLS_MPI_USE_INTERRUPT=n
Empty file.
1 change: 1 addition & 0 deletions components/mbedtls/test_apps/sdkconfig.ci.psram
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_SPIRAM=y
4 changes: 4 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.ci.psram_all_ext
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG_SPIRAM=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0
3 changes: 3 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.ci.psram_esp32
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_SPIRAM=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=800
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY=y
9 changes: 9 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# General options for additional checks
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
CONFIG_COMPILER_STACK_CHECK=y

CONFIG_ESP_TASK_WDT=n
3 changes: 3 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults.esp32
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_XTAL_FREQ_AUTO=y
CONFIG_SPI_FLASH_SHARE_SPI1_BUS=y
2 changes: 2 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults.esp32c2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y
1 change: 1 addition & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults.esp32c3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
2 changes: 2 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults.esp32s2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
2 changes: 2 additions & 0 deletions components/mbedtls/test_apps/sdkconfig.defaults.esp32s3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=n
5 changes: 5 additions & 0 deletions tools/unit-test-app/components/test_utils/memory_checks.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ static void check_leak(size_t before_free,
if (leaked <= warn_threshold) {
return;
}
else {
printf("The test leaked more memory than warn threshold: Leaked: %d Warn threshold: -%u \n",
leaked,
warn_threshold);
}

printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
type,
Expand Down
1 change: 1 addition & 0 deletions tools/unit-test-app/components/test_utils/test_runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void setUp(void)
test_utils_record_free_mem();
test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL);
test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_LWIP);
}

typedef enum {
Expand Down
5 changes: 0 additions & 5 deletions tools/unit-test-app/configs/aes_no_hw

This file was deleted.

5 changes: 0 additions & 5 deletions tools/unit-test-app/configs/aes_no_hw_c3

This file was deleted.

0 comments on commit 64129c2

Please sign in to comment.