@@ -28976,13 +28976,14 @@ static int test_wc_CryptoCb(void)
2897628976 (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519))
2897728977 int tlsVer;
2897828978#endif
28979- /* TODO: Add crypto callback API tests */
28980-
28981- /* Test wc_CryptoCb_GetDevice() lookup behavior . */
28979+ /* Exercise the exposed CryptoCb device-management API:
28980+ * wc_CryptoCb_RegisterDevice / UnRegisterDevice / GetDevice /
28981+ * DefaultDevID (and InfoString when DEBUG_CRYPTOCB is enabled) . */
2898228982 {
2898328983 int getDevId = 1234;
2898428984 int getDevCtx = 0;
2898528985 CryptoCb* dev = NULL;
28986+ int i, n, rc;
2898628987
2898728988 /* Unregistered devId is not found. */
2898828989 ExpectNull(wc_CryptoCb_GetDevice(getDevId));
@@ -28999,9 +29000,50 @@ static int test_wc_CryptoCb(void)
2899929000 /* A different, unregistered devId is still not found. */
2900029001 ExpectNull(wc_CryptoCb_GetDevice(getDevId + 1));
2900129002
29003+ /* Re-registering an already-registered devId is rejected. */
29004+ ExpectIntEQ(wc_CryptoCb_RegisterDevice(getDevId, NULL, &getDevCtx),
29005+ WC_NO_ERR_TRACE(ALREADY_E));
29006+
29007+ /* wc_CryptoCb_DefaultDevID behavior depends on the build config. */
29008+ #ifdef WC_NO_DEFAULT_DEVID
29009+ ExpectIntEQ(wc_CryptoCb_DefaultDevID(), INVALID_DEVID);
29010+ #elif !defined(WOLFSSL_CAAM_DEVID) && !defined(HAVE_ARIA) && \
29011+ !defined(WC_USE_DEVID)
29012+ /* "first available" mode: a device is registered, so one is returned. */
29013+ ExpectIntNE(wc_CryptoCb_DefaultDevID(), INVALID_DEVID);
29014+ #endif
29015+
2900229016 /* After unregistering, the device is no longer found. */
2900329017 wc_CryptoCb_UnRegisterDevice(getDevId);
2900429018 ExpectNull(wc_CryptoCb_GetDevice(getDevId));
29019+
29020+ /* Unregistering is a harmless no-op for unknown or invalid devIds. */
29021+ wc_CryptoCb_UnRegisterDevice(getDevId); /* already removed */
29022+ wc_CryptoCb_UnRegisterDevice(INVALID_DEVID); /* never a real devId */
29023+ ExpectNull(wc_CryptoCb_GetDevice(getDevId));
29024+
29025+ /* The device table is finite: once full, registration returns
29026+ * BUFFER_E. Register unique devIds until that happens, then clean up.
29027+ * The cap (256) just guards against an unexpectedly large table. */
29028+ rc = 0;
29029+ for (i = 0; rc == 0 && i < 256; i++) {
29030+ rc = wc_CryptoCb_RegisterDevice(0x5000 + i, NULL, NULL);
29031+ }
29032+ ExpectIntEQ(rc, WC_NO_ERR_TRACE(BUFFER_E));
29033+ /* Every id except the final failed attempt registered; unregister them. */
29034+ for (n = 0; n + 1 < i; n++) {
29035+ wc_CryptoCb_UnRegisterDevice(0x5000 + n);
29036+ }
29037+
29038+ #ifdef DEBUG_CRYPTOCB
29039+ /* wc_CryptoCb_InfoString smoke test: must not dereference bad memory. */
29040+ {
29041+ wc_CryptoInfo info;
29042+ XMEMSET(&info, 0, sizeof(info));
29043+ info.algo_type = WC_ALGO_TYPE_NONE;
29044+ wc_CryptoCb_InfoString(&info);
29045+ }
29046+ #endif
2900529047 }
2900629048
2900729049#ifdef HAVE_IO_TESTS_DEPENDENCIES
0 commit comments