Skip to content

Commit 177c542

Browse files
author
Guilherme Ferreira
committed
Merge branch 'fix/fix-test-socks-transport' into 'master'
fix(tcp_transport): Fix test for socks transport Closes IDFCI-2781 See merge request espressif/esp-idf!38045
2 parents 410e138 + c880002 commit 177c542

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

components/tcp_transport/host_test/main/test_socks_transport.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -37,17 +37,27 @@ using namespace std::literals;
3737

3838
namespace {
3939

40-
/*
41-
* Makes possible to pass a capturing lambda as a callback
42-
*/
43-
decltype(auto) capture_lambda(auto callable)
44-
{
45-
// make a static copy of the lambda to extend it's lifetime and avoid the capture.
46-
[[maybe_unused]]static auto call = callable;
47-
return []<typename... Args>(Args... args) {
48-
return call(args...);
49-
};
50-
}
40+
struct CallbackContext {
41+
esp_transport_handle_t test_parent;
42+
std::array<char, 9> expected_request;
43+
int timeout;
44+
};
45+
static CallbackContext context;
46+
47+
struct CallbackContextGuard {
48+
CallbackContext &context;
49+
50+
// Constructor to initialize the context
51+
CallbackContextGuard(CallbackContext &context): context(context) {}
52+
53+
// Destructor to clean up the context
54+
~CallbackContextGuard() {
55+
// Reset the context to avoid residual state
56+
context.test_parent = nullptr;
57+
context.expected_request.fill(0);
58+
context.timeout = 0;
59+
}
60+
};
5161

5262
auto make_response(socks_transport_error_t response)
5363
{
@@ -140,14 +150,23 @@ TEST_CASE("Requests to Proxy", "[Requests]")
140150
auto *p_addr_info = &addr_info;
141151
lwip_getaddrinfo_ReturnThruPtr_res(&p_addr_info);
142152
auto expected_request = std::array<char,9>{0x04, 0x01, 0x00, 0x50, 0x5a, 0x5a, 0x5a, 0x5a, 0x00 };
143-
mock_write_Stub(capture_lambda([&test_parent, expected_request, &timeout](esp_transport_handle_t transport, const char *request_sent, int len, int timeout_ms, [[maybe_unused]]int num_call) {
153+
154+
// Create and initialize the callback context
155+
context = {
156+
.test_parent = test_parent.get(),
157+
.expected_request = expected_request,
158+
.timeout = timeout,
159+
};
160+
CallbackContextGuard guard(context);
161+
162+
mock_write_Stub([](esp_transport_handle_t transport, const char *request_sent, int len, int timeout_ms, [[maybe_unused]]int num_call) {
144163
using namespace Catch::Matchers;
145-
REQUIRE(transport == test_parent.get());
146-
REQUIRE(len == expected_request.size());
147-
REQUIRE(timeout_ms == timeout);
148-
REQUIRE(std::equal(request_sent,request_sent+len, std::begin(expected_request), std::end(expected_request)));
164+
REQUIRE(transport == context.test_parent);
165+
REQUIRE(len == context.expected_request.size());
166+
REQUIRE(timeout_ms == context.timeout);
167+
REQUIRE(std::equal(request_sent, request_sent + len, std::begin(context.expected_request), std::end(context.expected_request)));
149168
return len;
150-
}));
169+
});
151170

152171
SECTION("Successful connection request") {
153172

0 commit comments

Comments
 (0)