From 1036f1b00efe2bbd2467cbfa3a4a97ab7f56591e Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 27 Jun 2024 16:12:18 +1000 Subject: fork esp-idf's dev console, in order to add some hooks --- lib/console/test_apps/console/CMakeLists.txt | 12 +++ lib/console/test_apps/console/README.md | 2 + lib/console/test_apps/console/main/CMakeLists.txt | 4 + lib/console/test_apps/console/main/test_app_main.c | 51 ++++++++++ lib/console/test_apps/console/main/test_console.c | 104 +++++++++++++++++++++ lib/console/test_apps/console/pytest_console.py | 96 +++++++++++++++++++ lib/console/test_apps/console/sdkconfig.defaults | 3 + 7 files changed, 272 insertions(+) create mode 100644 lib/console/test_apps/console/CMakeLists.txt create mode 100644 lib/console/test_apps/console/README.md create mode 100644 lib/console/test_apps/console/main/CMakeLists.txt create mode 100644 lib/console/test_apps/console/main/test_app_main.c create mode 100644 lib/console/test_apps/console/main/test_console.c create mode 100644 lib/console/test_apps/console/pytest_console.py create mode 100644 lib/console/test_apps/console/sdkconfig.defaults (limited to 'lib/console/test_apps') diff --git a/lib/console/test_apps/console/CMakeLists.txt b/lib/console/test_apps/console/CMakeLists.txt new file mode 100644 index 00000000..12837206 --- /dev/null +++ b/lib/console/test_apps/console/CMakeLists.txt @@ -0,0 +1,12 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +list(PREPEND SDKCONFIG_DEFAULTS "$ENV{IDF_PATH}/tools/test_apps/configs/sdkconfig.debug_helpers" "sdkconfig.defaults") + +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +set(COMPONENTS main) + +project(test_console) diff --git a/lib/console/test_apps/console/README.md b/lib/console/test_apps/console/README.md new file mode 100644 index 00000000..bf47d80e --- /dev/null +++ b/lib/console/test_apps/console/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/lib/console/test_apps/console/main/CMakeLists.txt b/lib/console/test_apps/console/main/CMakeLists.txt new file mode 100644 index 00000000..b253135b --- /dev/null +++ b/lib/console/test_apps/console/main/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_register(SRCS "test_app_main.c" "test_console.c" + INCLUDE_DIRS "." + PRIV_REQUIRES unity console + WHOLE_ARCHIVE) diff --git a/lib/console/test_apps/console/main/test_app_main.c b/lib/console/test_apps/console/main/test_app_main.c new file mode 100644 index 00000000..8dffdfd3 --- /dev/null +++ b/lib/console/test_apps/console/main/test_app_main.c @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "unity.h" +#include "unity_test_runner.h" +#include "esp_heap_caps.h" +#include + +// Some resources are lazy allocated (newlib locks) in the console code, the threshold is left for that case +#define TEST_MEMORY_LEAK_THRESHOLD (-150) + +static size_t before_free_8bit; +static size_t before_free_32bit; + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); + TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + + + +void tearDown(void) +{ + size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); +} + +void app_main(void) +{ + /* Preallocate some newlib locks to avoid it from + registering as memory leaks */ + + struct timeval tv = { 0 }; + gettimeofday(&tv, NULL); + + printf("Running console component tests\n"); + unity_run_menu(); +} diff --git a/lib/console/test_apps/console/main/test_console.c b/lib/console/test_apps/console/main/test_console.c new file mode 100644 index 00000000..db768502 --- /dev/null +++ b/lib/console/test_apps/console/main/test_console.c @@ -0,0 +1,104 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include "sdkconfig.h" +#include "unity.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" +#include "linenoise/linenoise.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +static int do_hello_cmd(int argc, char **argv) +{ + printf("Hello World\n"); + return 0; +} + +TEST_CASE("esp console init/deinit test", "[console]") +{ + esp_console_config_t console_config = ESP_CONSOLE_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_console_init(&console_config)); + const esp_console_cmd_t cmd = { + .command = "hello", + .help = "Print Hello World", + .hint = NULL, + .func = do_hello_cmd, + }; + TEST_ESP_OK(esp_console_cmd_register(&cmd)); + // re-register the same command, just for test + TEST_ESP_OK(esp_console_cmd_register(&cmd)); + TEST_ESP_OK(esp_console_deinit()); +} + +static esp_console_repl_t *s_repl = NULL; + +/* handle 'quit' command */ +static int do_cmd_quit(int argc, char **argv) +{ + printf("ByeBye\r\n"); + s_repl->del(s_repl); + + linenoiseHistoryFree(); // Free up memory + + return 0; +} + +static esp_console_cmd_t s_quit_cmd = { + .command = "quit", + .help = "Quit REPL environment", + .func = &do_cmd_quit +}; + +// Enter "quit" to exit REPL environment +/* Marked as ignore since it cannot run as a normal unity test case + ran separately in test_console_repl */ +TEST_CASE("esp console repl test", "[console][ignore]") +{ + esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); + esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl)); + + TEST_ESP_OK(esp_console_cmd_register(&s_quit_cmd)); + + TEST_ESP_OK(esp_console_start_repl(s_repl)); + vTaskDelay(pdMS_TO_TICKS(2000)); +} + +TEST_CASE("esp console help command", "[console][ignore]") +{ + esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); + esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); + TEST_ESP_OK(esp_console_new_repl_uart(&uart_config, &repl_config, &s_repl)); + + TEST_ESP_OK(esp_console_cmd_register(&s_quit_cmd)); + TEST_ESP_OK(esp_console_register_help_command()); + + TEST_ESP_OK(esp_console_start_repl(s_repl)); + vTaskDelay(pdMS_TO_TICKS(5000)); +} + +TEST_CASE("esp console init/deinit test, minimal config", "[console]") +{ + /* Test with minimal init config */ + esp_console_config_t console_config = { + .max_cmdline_length = 100, + }; + + TEST_ESP_OK(esp_console_init(&console_config)); + const esp_console_cmd_t cmd = { + .command = "hello", + .help = "Print Hello World", + .hint = NULL, + .func = do_hello_cmd, + }; + + TEST_ESP_OK(esp_console_cmd_register(&cmd)); + // re-register the same command, just for test + TEST_ESP_OK(esp_console_cmd_register(&cmd)); + TEST_ESP_OK(esp_console_deinit()); +} diff --git a/lib/console/test_apps/console/pytest_console.py b/lib/console/test_apps/console/pytest_console.py new file mode 100644 index 00000000..0dfdd135 --- /dev/null +++ b/lib/console/test_apps/console/pytest_console.py @@ -0,0 +1,96 @@ +# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: CC0-1.0 + +import pytest +from pytest_embedded import Dut + + +def do_test_quit(dut: Dut) -> None: + dut.expect_exact('Press ENTER to see the list of tests') + dut.write('"esp console repl test"') + + dut.expect_exact('esp>', timeout=5) + dut.write('quit') + + dut.expect_exact('ByeBye', timeout=5) + + +def do_test_help_generic(dut: Dut) -> None: + dut.expect_exact('Press ENTER to see the list of tests') + dut.write('"esp console help command"') + + dut.expect_exact('esp>', timeout=5) + dut.write('help') + + dut.expect_exact('quit', timeout=5) + dut.expect_exact('Quit REPL environment', timeout=5) + + dut.expect(r'help\s+\[\]', timeout=5) + + # Note: repl seems to do the line breaks by itself, this needs to be adjusted if repl changes its line width + dut.expect_exact('Print the summary of all registered commands if no arguments are given,', timeout=5) + dut.expect_exact('otherwise print summary of given command.', timeout=5) + dut.expect(r'\s+Name of command\s+esp>', timeout=5) + + +def do_test_help_quit(dut: Dut) -> None: + dut.expect_exact('Press ENTER to see the list of tests') + dut.write('"esp console help command"') + + dut.expect_exact('esp>', timeout=5) + dut.write('help quit') + + dut.expect(r'quit\s+Quit REPL environment\s+esp>', timeout=5) + + +@pytest.mark.generic +@pytest.mark.supported_targets +def test_console(dut: Dut) -> None: + dut.run_all_single_board_cases() + + +@pytest.mark.generic +@pytest.mark.supported_targets +def test_console_repl(dut: Dut) -> None: + do_test_quit(dut) + + +@pytest.mark.generic +@pytest.mark.supported_targets +def test_console_help_generic(dut: Dut) -> None: + do_test_help_generic(dut) + + +@pytest.mark.generic +@pytest.mark.supported_targets +def test_console_help_quit(dut: Dut) -> None: + do_test_help_quit(dut) + + +@pytest.mark.host_test +@pytest.mark.qemu +@pytest.mark.esp32 +@pytest.mark.esp32c3 +def test_console_qemu(dut: Dut) -> None: + dut.run_all_single_board_cases() + + +@pytest.mark.host_test +@pytest.mark.qemu +@pytest.mark.esp32 +def test_console_repl_qemu(dut: Dut) -> None: + do_test_quit(dut) + + +@pytest.mark.host_test +@pytest.mark.qemu +@pytest.mark.esp32 +def test_console_help_generic_qemu(dut: Dut) -> None: + do_test_help_generic(dut) + + +@pytest.mark.host_test +@pytest.mark.qemu +@pytest.mark.esp32 +def test_console_help_quit_qemu(dut: Dut) -> None: + do_test_help_quit(dut) diff --git a/lib/console/test_apps/console/sdkconfig.defaults b/lib/console/test_apps/console/sdkconfig.defaults new file mode 100644 index 00000000..ba19fc56 --- /dev/null +++ b/lib/console/test_apps/console/sdkconfig.defaults @@ -0,0 +1,3 @@ +# This "default" configuration is appended to all other configurations +# The contents of "sdkconfig.debug_helpers" is also appended to all other configurations (see CMakeLists.txt) +CONFIG_ESP_TASK_WDT_INIT=n -- cgit v1.2.3