feat(esp_local_ctrl): adds basic test app

This commit is contained in:
Ashish Sharma
2026-04-12 18:19:20 +08:00
parent 1d4b776775
commit a5898a2004
8 changed files with 235 additions and 21 deletions
@@ -0,0 +1,8 @@
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
components/esp_local_ctrl/test_apps:
disable:
- if: IDF_TARGET not in ["esp32c3"]
reason: Testing on one target is enough
depends_components:
- esp_local_ctrl
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.22)
set(COMPONENTS main)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_local_ctrl_test)
@@ -0,0 +1,2 @@
| Supported Targets | ESP32-C3 |
| ----------------- | -------- |
@@ -0,0 +1,8 @@
idf_component_register(
SRCS "app_main.c" "test_esp_local_ctrl.c"
# Access the private transport struct so we can define a null transport
# without requiring BLE or HTTPD hardware in unit tests.
PRIV_INCLUDE_DIRS "$ENV{IDF_PATH}/components/esp_local_ctrl/src"
PRIV_REQUIRES unity esp_local_ctrl
WHOLE_ARCHIVE
)
@@ -0,0 +1,45 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "unity.h"
#include "unity_test_runner.h"
#include "esp_heap_caps.h"
#define TEST_MEMORY_LEAK_THRESHOLD (-512)
static size_t s_before_free_8bit;
static size_t s_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)
{
s_before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
s_before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}
void tearDown(void)
{
/* Catch heap corruption that would be caused by a double-free */
TEST_ASSERT_MESSAGE(heap_caps_check_integrity(MALLOC_CAP_INVALID, true),
"heap integrity check failed (possible double-free or corruption)");
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(s_before_free_8bit, after_free_8bit, "8BIT");
check_leak(s_before_free_32bit, after_free_32bit, "32BIT");
}
void app_main(void)
{
printf("Running esp_local_ctrl component tests\n");
unity_run_menu();
}
@@ -0,0 +1,139 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "unity.h"
#include "esp_local_ctrl.h"
#include "esp_local_ctrl_priv.h" /* full definition of esp_local_ctrl_transport */
#include "esp_heap_caps.h"
/*
* Minimal transport with all function pointers NULL.
*
* esp_local_ctrl_start() guards every transport callback with an `if` before
* calling it, so a zero-initialised transport lets the service start with an
* in-memory protocomm instance only — no BLE or HTTPD hardware required.
*/
static const esp_local_ctrl_transport_t s_null_transport = { 0 };
static esp_err_t dummy_get_prop_values(size_t props_count,
const esp_local_ctrl_prop_t props[],
esp_local_ctrl_prop_val_t prop_values[],
void *usr_ctx)
{
return ESP_OK;
}
static esp_err_t dummy_set_prop_values(size_t props_count,
const esp_local_ctrl_prop_t props[],
const esp_local_ctrl_prop_val_t prop_values[],
void *usr_ctx)
{
return ESP_OK;
}
static esp_local_ctrl_config_t make_config(size_t max_properties)
{
esp_local_ctrl_config_t cfg = {
.transport = &s_null_transport,
.transport_config = { .httpd = NULL },
.proto_sec = { .version = PROTOCOM_SEC0 },
.handlers = {
.get_prop_values = dummy_get_prop_values,
.set_prop_values = dummy_set_prop_values,
},
.max_properties = max_properties,
};
return cfg;
}
/*
* Test: remove a non-last property, then stop.
*
* 1. Add prop_a (slot 0) and prop_b (slot 1). props_count = 2.
* 2. Remove prop_a:
* - prop_a is freed.
* - Compaction shifts prop_b into slot 0.
* 3. esp_local_ctrl_stop() must free prop_b exactly once (slot 0).
*/
TEST_CASE("remove_property then stop does not double-free", "[esp_local_ctrl]")
{
esp_local_ctrl_config_t cfg = make_config(5);
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
const esp_local_ctrl_prop_t prop_a = { .name = "prop_a" };
const esp_local_ctrl_prop_t prop_b = { .name = "prop_b" };
TEST_ESP_OK(esp_local_ctrl_add_property(&prop_a));
TEST_ESP_OK(esp_local_ctrl_add_property(&prop_b));
/* Removing the first property triggers compaction. */
TEST_ESP_OK(esp_local_ctrl_remove_property("prop_a"));
/* prop_b must still be reachable after compaction */
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("prop_b"));
TEST_ESP_OK(esp_local_ctrl_stop());
}
/* Test: remove the only property then stop — no compaction, basic sanity. */
TEST_CASE("remove sole property then stop is clean", "[esp_local_ctrl]")
{
esp_local_ctrl_config_t cfg = make_config(3);
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
const esp_local_ctrl_prop_t prop = { .name = "only_prop" };
TEST_ESP_OK(esp_local_ctrl_add_property(&prop));
TEST_ESP_OK(esp_local_ctrl_remove_property("only_prop"));
/* After removing the sole property, stop must find nothing to free. */
TEST_ESP_OK(esp_local_ctrl_stop());
}
/* Test: multiple compaction passes (remove head repeatedly) then stop. */
TEST_CASE("repeated head removal then stop is clean", "[esp_local_ctrl]")
{
esp_local_ctrl_config_t cfg = make_config(5);
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
const esp_local_ctrl_prop_t p0 = { .name = "p0" };
const esp_local_ctrl_prop_t p1 = { .name = "p1" };
const esp_local_ctrl_prop_t p2 = { .name = "p2" };
TEST_ESP_OK(esp_local_ctrl_add_property(&p0));
TEST_ESP_OK(esp_local_ctrl_add_property(&p1));
TEST_ESP_OK(esp_local_ctrl_add_property(&p2));
/* First removal: p1→slot0, p2→slot1, slot2 stale (fixed to NULL). */
TEST_ESP_OK(esp_local_ctrl_remove_property("p0"));
/* Second removal: p2→slot0, slot1 stale (fixed to NULL). */
TEST_ESP_OK(esp_local_ctrl_remove_property("p1"));
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("p2"));
TEST_ESP_OK(esp_local_ctrl_stop());
}
/* Test: add/remove/re-add cycle — the slot vacated by a remove must accept a
* new property without corrupting internal bookkeeping. */
TEST_CASE("add remove add cycle is clean", "[esp_local_ctrl]")
{
esp_local_ctrl_config_t cfg = make_config(3);
TEST_ESP_OK(esp_local_ctrl_start(&cfg));
const esp_local_ctrl_prop_t pa = { .name = "a" };
const esp_local_ctrl_prop_t pb = { .name = "b" };
const esp_local_ctrl_prop_t pc = { .name = "c" };
TEST_ESP_OK(esp_local_ctrl_add_property(&pa));
TEST_ESP_OK(esp_local_ctrl_add_property(&pb));
TEST_ESP_OK(esp_local_ctrl_remove_property("a"));
/* Reuse the freed slot capacity for a new property. */
TEST_ESP_OK(esp_local_ctrl_add_property(&pc));
TEST_ASSERT_NULL(esp_local_ctrl_get_property("a"));
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("b"));
TEST_ASSERT_NOT_NULL(esp_local_ctrl_get_property("c"));
TEST_ESP_OK(esp_local_ctrl_stop());
}
@@ -0,0 +1,4 @@
# Enable Security Version 0 (no encryption) so esp_local_ctrl_start() succeeds
# with a minimal null transport (no BLE/Wi-Fi hardware required).
CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_0=y
CONFIG_ESP_TASK_WDT_INIT=n