|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 wolfSSL Inc. |
| 3 | + * |
| 4 | + * This file is part of wolfHSM. |
| 5 | + * |
| 6 | + * wolfHSM is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * wolfHSM is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with wolfHSM. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +/* |
| 20 | + * test-refactor/misc/wh_test_comm.c |
| 21 | + * |
| 22 | + * Sequential mem-transport coverage from legacy |
| 23 | + * test/wh_test_comm.c::whTest_CommMem. The pthread mem/tcp/shmem |
| 24 | + * variants live in the POSIX port (legacy harness retains them). |
| 25 | + */ |
| 26 | + |
| 27 | +#include "wolfhsm/wh_settings.h" |
| 28 | + |
| 29 | +#if defined(WOLFHSM_CFG_ENABLE_CLIENT) && defined(WOLFHSM_CFG_ENABLE_SERVER) |
| 30 | + |
| 31 | +#include <stdint.h> |
| 32 | +#include <stdio.h> |
| 33 | +#include <string.h> |
| 34 | + |
| 35 | +#include "wolfhsm/wh_error.h" |
| 36 | +#include "wolfhsm/wh_comm.h" |
| 37 | +#include "wolfhsm/wh_transport_mem.h" |
| 38 | + |
| 39 | +#include "wh_test_common.h" |
| 40 | +#include "wh_test_list.h" |
| 41 | + |
| 42 | +#define BUFFER_SIZE 4096 |
| 43 | +#define REQ_SIZE 32 |
| 44 | +#define RESP_SIZE 64 |
| 45 | +#define REPEAT_COUNT 10 |
| 46 | + |
| 47 | + |
| 48 | +static int _whTest_CommMem(void) |
| 49 | +{ |
| 50 | + int ret = 0; |
| 51 | + |
| 52 | + /* Transport memory configuration */ |
| 53 | + uint8_t req[BUFFER_SIZE] = {0}; |
| 54 | + uint8_t resp[BUFFER_SIZE] = {0}; |
| 55 | + whTransportMemConfig tmcf[1] = {{ |
| 56 | + .req = (whTransportMemCsr*)req, |
| 57 | + .req_size = sizeof(req), |
| 58 | + .resp = (whTransportMemCsr*)resp, |
| 59 | + .resp_size = sizeof(resp), |
| 60 | + }}; |
| 61 | + |
| 62 | + |
| 63 | + /* Client configuration/contexts */ |
| 64 | + whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB}; |
| 65 | + whTransportMemClientContext tmcc[1] = {0}; |
| 66 | + whCommClientConfig c_conf[1] = {{ |
| 67 | + .transport_cb = tccb, |
| 68 | + .transport_context = (void*)tmcc, |
| 69 | + .transport_config = (void*)tmcf, |
| 70 | + .client_id = WH_TEST_DEFAULT_CLIENT_ID, |
| 71 | + }}; |
| 72 | + whCommClient client[1] = {0}; |
| 73 | + |
| 74 | + /* Server configuration/contexts */ |
| 75 | + whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB}; |
| 76 | + whTransportMemServerContext tmsc[1] = {0}; |
| 77 | + whCommServerConfig s_conf[1] = {{ |
| 78 | + .transport_cb = tscb, |
| 79 | + .transport_context = (void*)tmsc, |
| 80 | + .transport_config = (void*)tmcf, |
| 81 | + .server_id = 124, |
| 82 | + }}; |
| 83 | + whCommServer server[1] = {0}; |
| 84 | + |
| 85 | + int counter = 1; |
| 86 | + |
| 87 | + uint8_t tx_req[REQ_SIZE] = {0}; |
| 88 | + uint16_t tx_req_len = 0; |
| 89 | + uint16_t tx_req_flags = WH_COMM_MAGIC_NATIVE; |
| 90 | + uint16_t tx_req_type = 0; |
| 91 | + uint16_t tx_req_seq = 0; |
| 92 | + |
| 93 | + uint8_t rx_req[REQ_SIZE] = {0}; |
| 94 | + uint16_t rx_req_len = 0; |
| 95 | + uint16_t rx_req_flags = 0; |
| 96 | + uint16_t rx_req_type = 0; |
| 97 | + uint16_t rx_req_seq = 0; |
| 98 | + |
| 99 | + uint8_t tx_resp[RESP_SIZE] = {0}; |
| 100 | + uint16_t tx_resp_len = 0; |
| 101 | + |
| 102 | + uint8_t rx_resp[RESP_SIZE] = {0}; |
| 103 | + uint16_t rx_resp_len = 0; |
| 104 | + uint16_t rx_resp_flags = 0; |
| 105 | + uint16_t rx_resp_type = 0; |
| 106 | + uint16_t rx_resp_seq = 0; |
| 107 | + |
| 108 | + uint16_t seq_snapshot = 0; |
| 109 | + int rc = 0; |
| 110 | + |
| 111 | + /* BADARGS on uninitialized or NULL context */ |
| 112 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == |
| 113 | + wh_CommClient_IsRequestPending(NULL)); |
| 114 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == |
| 115 | + wh_CommClient_IsRequestPending(client)); |
| 116 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == wh_CommClient_AbortPending(NULL)); |
| 117 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == |
| 118 | + wh_CommClient_AbortPending(client)); |
| 119 | + |
| 120 | + /* Init client and server */ |
| 121 | + WH_TEST_RETURN_ON_FAIL(wh_CommClient_Init(client, c_conf)); |
| 122 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_Init(server, s_conf, NULL, NULL)); |
| 123 | + |
| 124 | + /* Fresh context: idle */ |
| 125 | + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); |
| 126 | + |
| 127 | + /* Check that neither side is ready to recv */ |
| 128 | + WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == |
| 129 | + wh_CommServer_RecvRequest(server, &rx_req_flags, |
| 130 | + &rx_req_type, &rx_req_seq, |
| 131 | + &rx_req_len, rx_req)); |
| 132 | + |
| 133 | + /* RecvResponse with no outstanding request short-circuits to NOTREADY |
| 134 | + * without touching the transport. */ |
| 135 | + WH_TEST_ASSERT_RETURN( |
| 136 | + WH_ERROR_NOTREADY == |
| 137 | + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, |
| 138 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 139 | + |
| 140 | + for (counter = 0; counter < REPEAT_COUNT; counter++) { |
| 141 | + (void)snprintf((char*)tx_req, sizeof(tx_req), "Request:%u", counter); |
| 142 | + tx_req_len = strlen((char*)tx_req); |
| 143 | + tx_req_type = counter * 2; |
| 144 | + WH_TEST_RETURN_ON_FAIL( |
| 145 | + wh_CommClient_SendRequest(client, tx_req_flags, tx_req_type, |
| 146 | + &tx_req_seq, tx_req_len, tx_req)); |
| 147 | + WH_TEST_DEBUG_PRINT("Client SendRequest:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", |
| 148 | + ret, tx_req_flags, tx_req_type, tx_req_seq, tx_req_len, tx_req); |
| 149 | + |
| 150 | + if (counter == 0) { |
| 151 | + WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == |
| 152 | + wh_CommClient_RecvResponse( |
| 153 | + client, &rx_resp_flags, &rx_resp_type, |
| 154 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 155 | + |
| 156 | + WH_TEST_ASSERT_RETURN( |
| 157 | + WH_ERROR_REQUEST_PENDING == |
| 158 | + wh_CommClient_SendRequest(client, tx_req_flags, tx_req_type, |
| 159 | + &tx_req_seq, tx_req_len, tx_req)); |
| 160 | + } |
| 161 | + |
| 162 | + WH_TEST_RETURN_ON_FAIL( |
| 163 | + wh_CommServer_RecvRequest(server, &rx_req_flags, &rx_req_type, |
| 164 | + &rx_req_seq, &rx_req_len, rx_req)); |
| 165 | + |
| 166 | + WH_TEST_DEBUG_PRINT("Server RecvRequest:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", |
| 167 | + ret, rx_req_flags, rx_req_type, rx_req_seq, rx_req_len, rx_req); |
| 168 | + |
| 169 | + (void)snprintf((char*)tx_resp, sizeof(tx_resp), "Response:%s", rx_req); |
| 170 | + tx_resp_len = strlen((char*)tx_resp); |
| 171 | + ret = wh_CommServer_SendResponse(server, rx_req_flags, rx_req_type, |
| 172 | + rx_req_seq, tx_resp_len, tx_resp); |
| 173 | + if (ret != 0) { |
| 174 | + WH_ERROR_PRINT("Server SendResponse:%d\n", ret); |
| 175 | + return ret; |
| 176 | + } |
| 177 | + |
| 178 | + WH_TEST_DEBUG_PRINT( |
| 179 | + "Server SendResponse:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", |
| 180 | + ret, rx_req_flags, rx_req_type, rx_req_seq, tx_resp_len, tx_resp); |
| 181 | + |
| 182 | + WH_TEST_RETURN_ON_FAIL( |
| 183 | + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, |
| 184 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 185 | + |
| 186 | + WH_TEST_DEBUG_PRINT( |
| 187 | + "Client RecvResponse:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", |
| 188 | + ret, rx_resp_flags, rx_resp_type, rx_resp_seq, rx_resp_len, |
| 189 | + rx_resp); |
| 190 | + } |
| 191 | + |
| 192 | + /* Pending tracking: context is idle after the loop */ |
| 193 | + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); |
| 194 | + |
| 195 | + /* Send a request: transitions to pending */ |
| 196 | + (void)snprintf((char*)tx_req, sizeof(tx_req), "PendingTest"); |
| 197 | + tx_req_len = (uint16_t)strlen((char*)tx_req); |
| 198 | + tx_req_type = 0xABCD; |
| 199 | + WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( |
| 200 | + client, tx_req_flags, tx_req_type, &tx_req_seq, tx_req_len, tx_req)); |
| 201 | + WH_TEST_ASSERT_RETURN(1 == wh_CommClient_IsRequestPending(client)); |
| 202 | + |
| 203 | + /* Stacking guard: second send rejected without touching seq or transport */ |
| 204 | + seq_snapshot = client->seq; |
| 205 | + rc = wh_CommClient_SendRequest(client, tx_req_flags, tx_req_type, |
| 206 | + &tx_req_seq, tx_req_len, tx_req); |
| 207 | + WH_TEST_ASSERT_RETURN(WH_ERROR_REQUEST_PENDING == rc); |
| 208 | + WH_TEST_ASSERT_RETURN(seq_snapshot == client->seq); |
| 209 | + WH_TEST_ASSERT_RETURN(1 == wh_CommClient_IsRequestPending(client)); |
| 210 | + |
| 211 | + /* Server completes the exchange */ |
| 212 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_RecvRequest( |
| 213 | + server, &rx_req_flags, &rx_req_type, &rx_req_seq, &rx_req_len, rx_req)); |
| 214 | + (void)snprintf((char*)tx_resp, sizeof(tx_resp), "Resp"); |
| 215 | + tx_resp_len = (uint16_t)strlen((char*)tx_resp); |
| 216 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_SendResponse( |
| 217 | + server, rx_req_flags, rx_req_type, rx_req_seq, tx_resp_len, tx_resp)); |
| 218 | + |
| 219 | + /* Successful recv clears pending */ |
| 220 | + WH_TEST_RETURN_ON_FAIL( |
| 221 | + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, |
| 222 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 223 | + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); |
| 224 | + |
| 225 | + /* Second Recv with no outstanding request again yields NOTREADY */ |
| 226 | + WH_TEST_ASSERT_RETURN( |
| 227 | + WH_ERROR_NOTREADY == |
| 228 | + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, |
| 229 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 230 | + |
| 231 | + /* Send, then manually abort. Seq must not advance. */ |
| 232 | + WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( |
| 233 | + client, tx_req_flags, tx_req_type, &tx_req_seq, tx_req_len, tx_req)); |
| 234 | + WH_TEST_ASSERT_RETURN(1 == wh_CommClient_IsRequestPending(client)); |
| 235 | + seq_snapshot = client->seq; |
| 236 | + WH_TEST_RETURN_ON_FAIL(wh_CommClient_AbortPending(client)); |
| 237 | + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); |
| 238 | + WH_TEST_ASSERT_RETURN(seq_snapshot == client->seq); |
| 239 | + |
| 240 | + /* Drain the abandoned exchange on the server side so the transport state |
| 241 | + * doesn't linger across tests. */ |
| 242 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_RecvRequest( |
| 243 | + server, &rx_req_flags, &rx_req_type, &rx_req_seq, &rx_req_len, rx_req)); |
| 244 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_SendResponse( |
| 245 | + server, rx_req_flags, rx_req_type, rx_req_seq, tx_resp_len, tx_resp)); |
| 246 | + |
| 247 | + /* After abort the late response is ignored - pending is 0 so |
| 248 | + * RecvResponse short-circuits before consulting the transport. */ |
| 249 | + WH_TEST_ASSERT_RETURN( |
| 250 | + WH_ERROR_NOTREADY == |
| 251 | + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, |
| 252 | + &rx_resp_seq, &rx_resp_len, rx_resp)); |
| 253 | + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); |
| 254 | + |
| 255 | + WH_TEST_RETURN_ON_FAIL(wh_CommServer_Cleanup(server)); |
| 256 | + WH_TEST_RETURN_ON_FAIL(wh_CommClient_Cleanup(client)); |
| 257 | + |
| 258 | + /* After Cleanup the context is no longer initialized; API reports BADARGS |
| 259 | + */ |
| 260 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == |
| 261 | + wh_CommClient_IsRequestPending(client)); |
| 262 | + WH_TEST_ASSERT_RETURN(WH_ERROR_BADARGS == |
| 263 | + wh_CommClient_AbortPending(client)); |
| 264 | + |
| 265 | + return ret; |
| 266 | +} |
| 267 | + |
| 268 | + |
| 269 | +int whTest_Comm(void* ctx) |
| 270 | +{ |
| 271 | + (void)ctx; |
| 272 | + |
| 273 | + WH_TEST_PRINT("Testing comms: mem...\n"); |
| 274 | + WH_TEST_RETURN_ON_FAIL(_whTest_CommMem()); |
| 275 | + |
| 276 | + return WH_ERROR_OK; |
| 277 | +} |
| 278 | + |
| 279 | +#endif /* WOLFHSM_CFG_ENABLE_CLIENT && WOLFHSM_CFG_ENABLE_SERVER */ |
0 commit comments