Skip to content

Commit 506f420

Browse files
committed
Add affinity test for no registered callbacks
1 parent 3655e5c commit 506f420

2 files changed

Lines changed: 175 additions & 11 deletions

File tree

test/wh_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ int whTest_Unit(void)
9393
#ifndef WOLFHSM_CFG_NO_CRYPTO
9494
/* Crypto Tests */
9595
WH_TEST_ASSERT(0 == whTest_Crypto());
96+
97+
#ifdef WOLF_CRYPTO_CB
9698
WH_TEST_ASSERT(0 == whTest_CryptoAffinity());
99+
#endif
97100

98101
#if defined(WOLFHSM_CFG_SERVER_IMG_MGR) && !defined(WOLFHSM_CFG_NO_CRYPTO)
99102
/* Image Manager Tests */

test/wh_test_crypto_affinity.c

Lines changed: 172 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
#include "wolfhsm/wh_settings.h"
2727

2828
/* Only compile if we have crypto, client, server, and crypto callbacks */
29-
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(WOLFHSM_CFG_ENABLE_CLIENT) && \
30-
defined(WOLFHSM_CFG_ENABLE_SERVER) && defined(WOLF_CRYPTO_CB)
29+
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(WOLF_CRYPTO_CB)
3130

3231
#include <stdint.h>
3332
#include <stdio.h>
@@ -94,7 +93,7 @@ static int _cryptoAffinityTestConnectCb(void* context,
9493
}
9594

9695

97-
int whTest_CryptoAffinity(void)
96+
static int whTest_CryptoAffinityWithCb(void)
9897
{
9998
int rc = 0;
10099
int32_t server_rc = 0;
@@ -178,7 +177,7 @@ int whTest_CryptoAffinity(void)
178177

179178
cryptoAffinityTestServerCtx = server;
180179

181-
WH_TEST_PRINT("Testing crypto affinity...\n");
180+
WH_TEST_PRINT(" whTest_CryptoAffinityWithCb...");
182181

183182
/* Initialize wolfCrypt and register our test crypto callback */
184183
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
@@ -303,19 +302,181 @@ int whTest_CryptoAffinity(void)
303302
wc_CryptoCb_UnRegisterDevice(TEST_DEV_ID);
304303
wolfCrypt_Cleanup();
305304

306-
WH_TEST_PRINT("Crypto affinity test passed\n");
305+
WH_TEST_PRINT("PASS\n");
307306
return WH_ERROR_OK;
308307
}
309308

310-
#else /* No crypto, client, server, or crypto callbacks */
311309

312-
#include "wh_test_crypto_affinity.h"
310+
static int whTest_CryptoAffinityNoCb(void)
311+
{
312+
int rc = 0;
313+
int32_t server_rc = 0;
314+
uint32_t affinity = 0;
315+
316+
/* Transport memory configuration */
317+
uint8_t req[BUFFER_SIZE] = {0};
318+
uint8_t resp[BUFFER_SIZE] = {0};
319+
whTransportMemConfig tmcf[1] = {{
320+
.req = (whTransportMemCsr*)req,
321+
.req_size = sizeof(req),
322+
.resp = (whTransportMemCsr*)resp,
323+
.resp_size = sizeof(resp),
324+
}};
325+
326+
/* Client configuration/contexts */
327+
whTransportClientCb tccb[1] = {WH_TRANSPORT_MEM_CLIENT_CB};
328+
whTransportMemClientContext tmcc[1] = {0};
329+
whCommClientConfig cc_conf[1] = {{
330+
.transport_cb = tccb,
331+
.transport_context = (void*)tmcc,
332+
.transport_config = (void*)tmcf,
333+
.client_id = 1,
334+
.connect_cb = _cryptoAffinityTestConnectCb,
335+
}};
336+
whClientConfig c_conf[1] = {{
337+
.comm = cc_conf,
338+
}};
339+
whClientContext client[1] = {0};
340+
341+
/* Server configuration/contexts */
342+
whTransportServerCb tscb[1] = {WH_TRANSPORT_MEM_SERVER_CB};
343+
whTransportMemServerContext tmsc[1] = {0};
344+
whCommServerConfig cs_conf[1] = {{
345+
.transport_cb = tscb,
346+
.transport_context = (void*)tmsc,
347+
.transport_config = (void*)tmcf,
348+
.server_id = 123,
349+
}};
350+
351+
/* Flash/NVM configuration */
352+
uint8_t flash_memory[FLASH_RAM_SIZE] = {0};
353+
whFlashRamsimCtx fc[1] = {0};
354+
whFlashRamsimCfg fc_conf[1] = {{
355+
.size = FLASH_RAM_SIZE,
356+
.sectorSize = FLASH_SECTOR_SIZE,
357+
.pageSize = FLASH_PAGE_SIZE,
358+
.erasedByte = ~(uint8_t)0,
359+
.memory = flash_memory,
360+
}};
361+
const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB};
362+
363+
whNvmFlashContext nfc[1] = {0};
364+
whNvmFlashConfig nf_conf[1] = {{
365+
.cb = fcb,
366+
.context = fc,
367+
.config = fc_conf,
368+
}};
369+
370+
371+
whNvmCb nfcb[1] = {WH_NVM_FLASH_CB};
372+
whNvmConfig n_conf[1] = {{
373+
.cb = nfcb,
374+
.context = nfc,
375+
.config = nf_conf,
376+
}};
377+
whNvmContext nvm[1] = {0};
378+
379+
/* Crypto context - configure with INVALID_DEVID (no HW crypto) */
380+
whServerCryptoContext crypto[1] = {{
381+
.devId = INVALID_DEVID,
382+
}};
383+
384+
whServerConfig s_conf[1] = {{
385+
.comm_config = cs_conf,
386+
.nvm = nvm,
387+
.crypto = crypto,
388+
.devId = INVALID_DEVID,
389+
}};
390+
whServerContext server[1] = {0};
391+
392+
cryptoAffinityTestServerCtx = server;
393+
394+
WH_TEST_PRINT(" whTest_CryptoAffinityNoCb...");
395+
396+
/* Initialize wolfCrypt */
397+
WH_TEST_RETURN_ON_FAIL(wolfCrypt_Init());
398+
399+
/* Initialize NVM */
400+
WH_TEST_RETURN_ON_FAIL(wh_Nvm_Init(nvm, n_conf));
401+
402+
/* Initialize RNG */
403+
WH_TEST_RETURN_ON_FAIL(wc_InitRng_ex(crypto->rng, NULL, INVALID_DEVID));
404+
405+
/* Initialize server and client */
406+
WH_TEST_RETURN_ON_FAIL(wh_Server_Init(server, s_conf));
407+
WH_TEST_RETURN_ON_FAIL(wh_Client_Init(client, c_conf));
408+
409+
/* Check that the server side is ready to recv */
410+
WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY ==
411+
wh_Server_HandleRequestMessage(server));
412+
413+
/* Send comm init */
414+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitRequest(client));
415+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
416+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommInitResponse(client, NULL, NULL));
417+
418+
/* Verify initial state - should be SW since we configured with INVALID_DEVID
419+
*/
420+
WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID);
421+
WH_TEST_ASSERT_RETURN(server->crypto->configDevId == INVALID_DEVID);
422+
423+
/* Test 1: Set SW affinity - should succeed */
424+
WH_TEST_RETURN_ON_FAIL(
425+
wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW));
426+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
427+
WH_TEST_RETURN_ON_FAIL(
428+
wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity));
429+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
430+
WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW);
431+
WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID);
432+
433+
/* Test 2: Set HW affinity - should fail with BADCONFIG since no HW
434+
* configured */
435+
WH_TEST_RETURN_ON_FAIL(
436+
wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_HW));
437+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
438+
WH_TEST_RETURN_ON_FAIL(
439+
wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity));
440+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_BADCONFIG);
441+
/* Affinity should remain SW after failed HW request */
442+
WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW);
443+
WH_TEST_ASSERT_RETURN(server->crypto->devId == INVALID_DEVID);
444+
445+
/* Test 3: Verify SW affinity still works after failed HW request */
446+
WH_TEST_RETURN_ON_FAIL(
447+
wh_Client_SetCryptoAffinityRequest(client, WH_CRYPTO_AFFINITY_SW));
448+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
449+
WH_TEST_RETURN_ON_FAIL(
450+
wh_Client_SetCryptoAffinityResponse(client, &server_rc, &affinity));
451+
WH_TEST_ASSERT_RETURN(server_rc == WH_ERROR_OK);
452+
WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_SW);
453+
454+
/* Cleanup */
455+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseRequest(client));
456+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
457+
WH_TEST_RETURN_ON_FAIL(wh_Client_CommCloseResponse(client));
458+
459+
WH_TEST_RETURN_ON_FAIL(wh_Server_Cleanup(server));
460+
WH_TEST_RETURN_ON_FAIL(wh_Client_Cleanup(client));
461+
462+
wc_FreeRng(crypto->rng);
463+
wh_Nvm_Cleanup(nvm);
464+
wolfCrypt_Cleanup();
465+
466+
(void)rc;
467+
468+
WH_TEST_PRINT("PASS\n");
469+
return WH_ERROR_OK;
470+
}
471+
313472

314473
int whTest_CryptoAffinity(void)
315474
{
316-
/* Test not applicable without crypto callback support */
317-
return 0;
475+
WH_TEST_PRINT("Testing Crypto Affinity...\n");
476+
WH_TEST_RETURN_ON_FAIL(whTest_CryptoAffinityWithCb());
477+
WH_TEST_RETURN_ON_FAIL(whTest_CryptoAffinityNoCb());
478+
479+
return WH_ERROR_OK;
318480
}
319481

320-
#endif /* !WOLFHSM_CFG_NO_CRYPTO && WOLFHSM_CFG_ENABLE_CLIENT && \
321-
WOLFHSM_CFG_ENABLE_SERVER && WOLF_CRYPTO_CB */
482+
#endif /* !WOLFHSM_CFG_NO_CRYPTO && WOLF_CRYPTO_CB */

0 commit comments

Comments
 (0)