|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
@@ -37,17 +37,27 @@ using namespace std::literals; |
37 | 37 |
|
38 | 38 | namespace { |
39 | 39 |
|
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 | +}; |
51 | 61 |
|
52 | 62 | auto make_response(socks_transport_error_t response) |
53 | 63 | { |
@@ -140,14 +150,23 @@ TEST_CASE("Requests to Proxy", "[Requests]") |
140 | 150 | auto *p_addr_info = &addr_info; |
141 | 151 | lwip_getaddrinfo_ReturnThruPtr_res(&p_addr_info); |
142 | 152 | 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) { |
144 | 163 | 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))); |
149 | 168 | return len; |
150 | | - })); |
| 169 | + }); |
151 | 170 |
|
152 | 171 | SECTION("Successful connection request") { |
153 | 172 |
|
|
0 commit comments