Skip to content

Commit dd9a392

Browse files
committed
fix(asio): Add a simple unit test to check the leaks
1 parent d5db1fc commit dd9a392

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is the project CMakeLists.txt file for the test subproject
2+
cmake_minimum_required(VERSION 3.16)
3+
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
6+
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "6.0")
7+
set(test_component_dir $ENV{IDF_PATH}/tools/test_apps/components)
8+
else()
9+
set(test_component_dir $ENV{IDF_PATH}/tools/unit-test-app/components)
10+
endif()
11+
12+
set(EXTRA_COMPONENT_DIRS ../..
13+
${test_component_dir})
14+
15+
project(asio_ssl_unit_test)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(SRCS "test_asio_ssl.cpp"
2+
# REQUIRES test_utils
3+
INCLUDE_DIRS ".")
4+
# PRIV_REQUIRES unity asio)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies:
2+
idf: ">=5.1"
3+
espressif/asio:
4+
version: "^1.14.1"
5+
override_path: "../../../"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#include "unity.h"
8+
#include "unity_fixture.h"
9+
#include "test_utils.h"
10+
#include "memory_checks.h"
11+
#include "esp_netif.h"
12+
#include "esp_event.h"
13+
14+
#include "asio.hpp"
15+
#include "asio/ssl.hpp"
16+
17+
static void create_stream_and_attempt_handshake()
18+
{
19+
asio::io_context io;
20+
asio::ssl::context ctx(asio::ssl::context::tlsv12_client);
21+
asio::ssl::stream<asio::ip::tcp::socket> stream(io, ctx);
22+
stream.set_verify_mode(asio::ssl::verify_none);
23+
24+
asio::error_code ec;
25+
stream.handshake(asio::ssl::stream_base::client, ec);
26+
}
27+
28+
29+
TEST_GROUP(asio_ssl);
30+
31+
TEST_SETUP(asio_ssl)
32+
{
33+
// call once to allocate all one time inits
34+
create_stream_and_attempt_handshake();
35+
test_utils_record_free_mem();
36+
TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
37+
}
38+
39+
TEST_TEAR_DOWN(asio_ssl)
40+
{
41+
}
42+
43+
44+
TEST(asio_ssl, ssl_stream_lifecycle_no_leak)
45+
{
46+
test_case_uses_tcpip();
47+
create_stream_and_attempt_handshake();
48+
TEST_ASSERT_TRUE(true);
49+
}
50+
51+
TEST_GROUP_RUNNER(asio_ssl)
52+
{
53+
RUN_TEST_CASE(asio_ssl, ssl_stream_lifecycle_no_leak)
54+
}
55+
56+
extern "C" void app_main(void)
57+
{
58+
esp_netif_init();
59+
UNITY_MAIN(asio_ssl);
60+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from pytest_embedded import Dut
5+
6+
7+
def test_asio_ssl_unit(dut: Dut) -> None:
8+
dut.expect_unity_test_output()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_COMPILER_CXX_EXCEPTIONS=y
2+
CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n
3+
CONFIG_UNITY_ENABLE_FIXTURE=y
4+
CONFIG_ASIO_SSL_SUPPORT=y

0 commit comments

Comments
 (0)