@@ -95,6 +95,113 @@ static uint16_t* cancelSeqP;
9595 WOLFHSM_CFG_ENABLE_SERVER && WOLFHSM_CFG_CANCEL_API */
9696#endif /* WOLFHSM_CFG_IS_TEST_SERVER */
9797
98+ /*
99+ * Utility function tests
100+ * These test the basic utility functions in wh_utils.c
101+ */
102+ #include "wolfhsm/wh_utils.h"
103+
104+ static int whTest_Utils (void )
105+ {
106+ int ret = 0 ;
107+ uint16_t val16 , swap16 ;
108+ uint32_t val32 , swap32 ;
109+ uint64_t val64 , swap64 ;
110+ uint8_t buffer [32 ];
111+ int i ;
112+
113+ printf ("Testing utility functions...\n" );
114+
115+ /* Test Swap16 */
116+ val16 = 0x1234 ;
117+ swap16 = wh_Utils_Swap16 (val16 );
118+ if (swap16 != 0x3412 ) {
119+ printf ("Failed wh_Utils_Swap16: expected 0x3412, got 0x%04x\n" , swap16 );
120+ return WH_ERROR_ABORTED ;
121+ }
122+ /* Verify double swap returns original */
123+ if (wh_Utils_Swap16 (swap16 ) != val16 ) {
124+ printf ("Failed wh_Utils_Swap16 double swap\n" );
125+ return WH_ERROR_ABORTED ;
126+ }
127+
128+ /* Test Swap32 */
129+ val32 = 0x12345678ul ;
130+ swap32 = wh_Utils_Swap32 (val32 );
131+ if (swap32 != 0x78563412ul ) {
132+ printf ("Failed wh_Utils_Swap32: expected 0x78563412, got 0x%08x\n" ,
133+ (unsigned int )swap32 );
134+ return WH_ERROR_ABORTED ;
135+ }
136+ /* Verify double swap returns original */
137+ if (wh_Utils_Swap32 (swap32 ) != val32 ) {
138+ printf ("Failed wh_Utils_Swap32 double swap\n" );
139+ return WH_ERROR_ABORTED ;
140+ }
141+
142+ /* Test Swap64 */
143+ val64 = 0x123456789ABCDEF0ull ;
144+ swap64 = wh_Utils_Swap64 (val64 );
145+ if (swap64 != 0xF0DEBC9A78563412ull ) {
146+ printf ("Failed wh_Utils_Swap64: expected 0xF0DEBC9A78563412, got 0x%016llx\n" ,
147+ (unsigned long long )swap64 );
148+ return WH_ERROR_ABORTED ;
149+ }
150+ /* Verify double swap returns original */
151+ if (wh_Utils_Swap64 (swap64 ) != val64 ) {
152+ printf ("Failed wh_Utils_Swap64 double swap\n" );
153+ return WH_ERROR_ABORTED ;
154+ }
155+
156+ /* Test htonl and ntohl (network byte order conversion) */
157+ val32 = 0x12345678ul ;
158+ swap32 = wh_Utils_htonl (val32 );
159+ /* Result depends on endianness, but ntohl should reverse it */
160+ if (wh_Utils_ntohl (swap32 ) != val32 ) {
161+ printf ("Failed wh_Utils_htonl/ntohl round trip\n" );
162+ return WH_ERROR_ABORTED ;
163+ }
164+
165+ /* Test memeqzero with all zeros */
166+ memset (buffer , 0 , sizeof (buffer ));
167+ ret = wh_Utils_memeqzero (buffer , sizeof (buffer ));
168+ if (ret != 1 ) {
169+ printf ("Failed wh_Utils_memeqzero: expected 1 for all zeros, got %d\n" , ret );
170+ return WH_ERROR_ABORTED ;
171+ }
172+
173+ /* Test memeqzero with non-zero byte at various positions
174+ * Note: memeqzero checks from the end backwards and stops at index 1,
175+ * so it doesn't check buffer[0] */
176+ for (i = 1 ; i < (int )sizeof (buffer ); i ++ ) {
177+ memset (buffer , 0 , sizeof (buffer ));
178+ buffer [i ] = 0x42 ;
179+ ret = wh_Utils_memeqzero (buffer , sizeof (buffer ));
180+ if (ret != 0 ) {
181+ printf ("Failed wh_Utils_memeqzero: expected 0 with non-zero byte at pos %d, got %d\n" ,
182+ i , ret );
183+ return WH_ERROR_ABORTED ;
184+ }
185+ }
186+
187+ /* Test memeqzero with size 0 and 1 */
188+ ret = wh_Utils_memeqzero (buffer , 0 );
189+ if (ret != 1 ) {
190+ printf ("Failed wh_Utils_memeqzero: expected 1 for size 0, got %d\n" , ret );
191+ return WH_ERROR_ABORTED ;
192+ }
193+
194+ memset (buffer , 0 , sizeof (buffer ));
195+ ret = wh_Utils_memeqzero (buffer , 1 );
196+ if (ret != 1 ) {
197+ printf ("Failed wh_Utils_memeqzero: expected 1 for single zero byte, got %d\n" , ret );
198+ return WH_ERROR_ABORTED ;
199+ }
200+
201+ printf ("Utility function tests passed\n" );
202+ return WH_ERROR_OK ;
203+ }
204+
98205#if defined(WOLFHSM_CFG_TEST_VERBOSE ) && defined(WOLFHSM_CFG_ENABLE_CLIENT )
99206static int whTest_ShowNvmAvailable (whClientContext * ctx )
100207{
@@ -3719,6 +3826,9 @@ static int wh_ClientServer_MemThreadTest(void)
37193826 defined(WOLFHSM_CFG_ENABLE_SERVER )
37203827int whTest_Crypto (void )
37213828{
3829+ /* Test utility functions first (no setup required) */
3830+ WH_TEST_RETURN_ON_FAIL (whTest_Utils ());
3831+
37223832 printf ("Testing crypto: (pthread) mem...\n" );
37233833 WH_TEST_RETURN_ON_FAIL (wh_ClientServer_MemThreadTest ());
37243834 return 0 ;
0 commit comments