3636#include "wolfhsm/wh_error.h"
3737#include "wolfhsm/wh_log.h"
3838#include "wolfhsm/wh_log_ringbuf.h"
39+ #include "wolfhsm/wh_log_printf.h"
3940
4041#include "wh_test_common.h"
4142#include "wh_test_list.h"
@@ -1034,6 +1035,80 @@ static int whTest_LogRingbuf_Generic(void)
10341035 return whTest_LogBackend_RunAll (& testCfg );
10351036}
10361037
1038+ /* Printf backend tests. The printf backend is a write-only sink: it
1039+ * implements only Init and AddEntry, so the remaining frontend ops
1040+ * report NOTIMPL. Exercises both the "log always" and "drop unless
1041+ * debug" config paths and the bad-args/uninitialized rejections. */
1042+ static int whTest_LogPrintf (void )
1043+ {
1044+ whLogContext logCtx ;
1045+ whLogPrintfContext printfCtx ;
1046+ whLogPrintfConfig printfCfg ;
1047+ whLogConfig logConfig ;
1048+ whLogCb printfCb = WH_LOG_PRINTF_CB ;
1049+ whLogEntry entry = {0 };
1050+ int iterate_count = 0 ;
1051+
1052+ memset (& printfCtx , 0 , sizeof (printfCtx ));
1053+
1054+ /* Direct backend bad-args rejections */
1055+ WH_TEST_ASSERT_RETURN (whLogPrintf_Init (NULL , NULL ) == WH_ERROR_BADARGS );
1056+ WH_TEST_ASSERT_RETURN (whLogPrintf_AddEntry (NULL , & entry ) ==
1057+ WH_ERROR_BADARGS );
1058+ WH_TEST_ASSERT_RETURN (whLogPrintf_AddEntry (& printfCtx , NULL ) ==
1059+ WH_ERROR_BADARGS );
1060+
1061+ /* Adding to an uninitialized backend is rejected */
1062+ WH_TEST_ASSERT_RETURN (whLogPrintf_AddEntry (& printfCtx , & entry ) ==
1063+ WH_ERROR_ABORTED );
1064+
1065+ /* Init with NULL config uses defaults (logIfNotDebug = 0) */
1066+ memset (& logCtx , 0 , sizeof (logCtx ));
1067+ memset (& printfCtx , 0 , sizeof (printfCtx ));
1068+ logConfig .cb = & printfCb ;
1069+ logConfig .context = & printfCtx ;
1070+ logConfig .config = NULL ;
1071+ WH_TEST_RETURN_ON_FAIL (wh_Log_Init (& logCtx , & logConfig ));
1072+ WH_TEST_ASSERT_RETURN (printfCtx .initialized == 1 );
1073+ WH_TEST_ASSERT_RETURN (printfCtx .logIfNotDebug == 0 );
1074+
1075+ /* With logIfNotDebug = 0 and a non-debug build, entries are dropped */
1076+ WH_LOG (& logCtx , WH_LOG_LEVEL_INFO , "Dropped unless debug build" );
1077+
1078+ /* Re-init with logIfNotDebug = 1 so entries are always printed */
1079+ memset (& logCtx , 0 , sizeof (logCtx ));
1080+ memset (& printfCtx , 0 , sizeof (printfCtx ));
1081+ printfCfg .logIfNotDebug = 1 ;
1082+ logConfig .config = & printfCfg ;
1083+ WH_TEST_RETURN_ON_FAIL (wh_Log_Init (& logCtx , & logConfig ));
1084+ WH_TEST_ASSERT_RETURN (printfCtx .logIfNotDebug == 1 );
1085+
1086+ /* Exercise the print path for each known level */
1087+ WH_LOG (& logCtx , WH_LOG_LEVEL_INFO , "Printf info" );
1088+ WH_LOG (& logCtx , WH_LOG_LEVEL_ERROR , "Printf error" );
1089+ WH_LOG (& logCtx , WH_LOG_LEVEL_SECEVENT , "Printf secevent" );
1090+
1091+ /* An out-of-range level exercises the level-to-string default */
1092+ entry .timestamp = 1 ;
1093+ entry .level = (whLogLevel )999 ;
1094+ entry .file = __FILE__ ;
1095+ entry .function = __func__ ;
1096+ entry .line = __LINE__ ;
1097+ entry .msg_len = 0 ;
1098+ WH_TEST_RETURN_ON_FAIL (wh_Log_AddEntry (& logCtx , & entry ));
1099+
1100+ /* The printf backend implements no store, so the remaining frontend
1101+ * operations report NOTIMPL */
1102+ WH_TEST_ASSERT_RETURN (wh_Log_Cleanup (& logCtx ) == WH_ERROR_NOTIMPL );
1103+ WH_TEST_ASSERT_RETURN (wh_Log_Export (& logCtx , NULL ) == WH_ERROR_NOTIMPL );
1104+ WH_TEST_ASSERT_RETURN (
1105+ wh_Log_Iterate (& logCtx , iterateCallbackCount , & iterate_count ) ==
1106+ WH_ERROR_NOTIMPL );
1107+ WH_TEST_ASSERT_RETURN (wh_Log_Clear (& logCtx ) == WH_ERROR_NOTIMPL );
1108+
1109+ return WH_ERROR_OK ;
1110+ }
1111+
10371112/* Portable log test entry point (Misc group) */
10381113int whTest_Log (void * ctx )
10391114{
@@ -1057,6 +1132,9 @@ int whTest_Log(void* ctx)
10571132 WH_TEST_PRINT ("Testing ring buffer backend...\n" );
10581133 WH_TEST_RETURN_ON_FAIL (whTest_LogRingbuf ());
10591134
1135+ WH_TEST_PRINT ("Testing printf backend...\n" );
1136+ WH_TEST_RETURN_ON_FAIL (whTest_LogPrintf ());
1137+
10601138 return WH_ERROR_OK ;
10611139}
10621140
0 commit comments