@@ -557,6 +557,114 @@ static void TestSftpBufferSendPendingOutput(void)
557557#endif /* WOLFSSH_SFTP */
558558
559559
560+ #ifdef WOLFSSH_KEYBOARD_INTERACTIVE
561+ static int KbPreparePacketFailUserAuth (byte authType , WS_UserAuthData * authData ,
562+ void * ctx )
563+ {
564+ static byte * responses [1 ];
565+ static word32 responseLens [1 ];
566+ static byte response [] = "regress" ;
567+
568+ (void )ctx ;
569+
570+ if (authType != WOLFSSH_USERAUTH_KEYBOARD || authData == NULL ) {
571+ return WOLFSSH_USERAUTH_INVALID_AUTHTYPE ;
572+ }
573+
574+ if (authData -> sf .keyboard .promptCount != 1 ||
575+ authData -> sf .keyboard .prompts == NULL ) {
576+ return WOLFSSH_USERAUTH_INVALID_PASSWORD ;
577+ }
578+
579+ responses [0 ] = response ;
580+ responseLens [0 ] = (word32 )sizeof (response ) - 1 ;
581+ authData -> sf .keyboard .responseCount = 1 ;
582+ authData -> sf .keyboard .responseLengths = responseLens ;
583+ authData -> sf .keyboard .responses = responses ;
584+
585+ return WOLFSSH_USERAUTH_SUCCESS ;
586+ }
587+
588+ static void TestKeyboardResponsePreparePacketFailure (WOLFSSH * ssh ,
589+ WOLFSSH_CTX * ctx )
590+ {
591+ byte * prompt ;
592+ byte * * prompts ;
593+ byte * promptEcho ;
594+ int ret ;
595+ byte * savedBuffer ;
596+
597+ AssertNotNull (ssh );
598+ AssertNotNull (ctx );
599+
600+ ResetSession (ssh );
601+ wolfSSH_SetUserAuth (ctx , KbPreparePacketFailUserAuth );
602+
603+ prompt = (byte * )WMALLOC (9 , ctx -> heap , DYNTYPE_STRING ); /* "Password" */
604+ prompts = (byte * * )WMALLOC (sizeof (byte * ), ctx -> heap , DYNTYPE_STRING );
605+ promptEcho = (byte * )WMALLOC (1 , ctx -> heap , DYNTYPE_STRING );
606+ AssertNotNull (prompt );
607+ AssertNotNull (prompts );
608+ AssertNotNull (promptEcho );
609+
610+ WMEMCPY (prompt , "Password" , 8 );
611+ prompt [8 ] = '\0' ;
612+ prompts [0 ] = prompt ;
613+ promptEcho [0 ] = 0 ;
614+
615+ ssh -> kbAuth .promptCount = 1 ;
616+ ssh -> kbAuth .prompts = prompts ;
617+ ssh -> kbAuth .promptEcho = promptEcho ;
618+ ssh -> kbAuth .promptName = NULL ;
619+ ssh -> kbAuth .promptInstruction = NULL ;
620+ ssh -> kbAuth .promptLanguage = NULL ;
621+
622+ /* Force PreparePacket() to fail with WS_OVERFLOW_E. */
623+ ssh -> outputBuffer .length = 0 ;
624+ ssh -> outputBuffer .idx = 1 ;
625+
626+ savedBuffer = ssh -> outputBuffer .buffer ;
627+ ssh -> outputBuffer .buffer = NULL ;
628+
629+ ret = SendUserAuthKeyboardResponse (ssh );
630+ AssertIntEQ (ret , WS_OVERFLOW_E );
631+
632+ /* Ensure packet purge/reset happened cleanly. */
633+ AssertIntEQ (ssh -> outputBuffer .idx , 0 );
634+ AssertIntEQ (ssh -> outputBuffer .length , 0 );
635+
636+ /* Restore known-good buffer pointer for subsequent tests. */
637+ if (ssh -> outputBuffer .buffer == NULL ) {
638+ ssh -> outputBuffer .buffer = savedBuffer ;
639+ }
640+
641+ /* Ownership was transferred and freed by SendUserAuthKeyboardResponse(). */
642+ ssh -> kbAuth .promptCount = 0 ;
643+ ssh -> kbAuth .prompts = NULL ;
644+ ssh -> kbAuth .promptEcho = NULL ;
645+ }
646+
647+ static void TestKeyboardResponseNoUserAuthCallback (WOLFSSH * ssh ,
648+ WOLFSSH_CTX * ctx )
649+ {
650+ int ret ;
651+
652+ AssertNotNull (ssh );
653+ AssertNotNull (ctx );
654+
655+ ResetSession (ssh );
656+ wolfSSH_SetUserAuth (ctx , NULL );
657+
658+ ret = SendUserAuthKeyboardResponse (ssh );
659+ AssertIntEQ (ret , WS_INVALID_STATE_E );
660+
661+ /* No packet should have been started. */
662+ AssertIntEQ (ssh -> outputBuffer .length , 0 );
663+ AssertIntEQ (ssh -> outputBuffer .idx , 0 );
664+ }
665+ #endif /* WOLFSSH_KEYBOARD_INTERACTIVE */
666+
667+
560668int main (int argc , char * * argv )
561669{
562670 WOLFSSH_CTX * ctx ;
@@ -594,6 +702,11 @@ int main(int argc, char** argv)
594702 TestSftpBufferSendPendingOutput ();
595703#endif
596704
705+ #ifdef WOLFSSH_KEYBOARD_INTERACTIVE
706+ TestKeyboardResponsePreparePacketFailure (ssh , ctx );
707+ TestKeyboardResponseNoUserAuthCallback (ssh , ctx );
708+ #endif
709+
597710 /* TODO: add app-level regressions that simulate stdin EOF/password
598711 * prompts and mid-session socket closes once the test harness can
599712 * drive the wolfssh client without real sockets/tty. */
0 commit comments