
  /*
   *  Object %name    : %
   *  State           :  %state%
   *  Creation date   :  Mon Jul 23 07:42:53 2007
   *  Last modified   :  %modify_time%
   */
  /** @file
   *  \brief A brief description of this module
   *
   *  \version KMNG_ATP_TST_main.c#1:csrc:1
   *  \author doronp
   *  \remarks Copyright (C) 2007 by Discretix Technologies Ltd.
   *           All Rights reserved
   */

/* disabling pc lint warning regarding to constant condition :
   'Constant value Boolean' */
/*lint --e{506} */

/************* Include Files **************************************/
#include "DX_VOS_Stdio.h"
#include "DX_VOS_Utils.h"
#include "DX_VOS_File.h"
#include "MW_ATP_UTIL_funcs.h"
#include "KMNG_ATP_TST_Defines.h"
#include "KMNG_ATP_TST_ManageOp.h"
#include "KMNG_ATP_TST_KeysOp.h"
#include "KMNG_TST_Utils.h"
#include "MW_ATP_define_flags.h"
#include "KMNG_API.h"

/************************ Defines **********************************/
/************************ Enums ************************************/
/************************ Typedefs *********************************/
/************************ Global Data ******************************/
#if KMNG_TST_LOG_ENABLE 
DxVosFile  TST_main_log;
char      *TST_KMNG_log_fname_ptr = DX_NULL;
#endif


/*************** Private function prototype ************************/
void KMNG_ATP_TST_GetVersion( void );
DxError_t KMNG_ATP_TST_Run_All_ATP_Tests( void );
/************************ Public Functions *************************/

 int KMNG_ATP_menu(void)
 {
    /********* TST Variables Definitions ************/
    DxUint32_t          TST_index = 0;
    DxChar              TST_Var;
    DxStatus            TST_TestStatus = DX_SUCCESS;
    DxError_t           TST_Status = DX_SUCCESS;
    DxBool_t            TST_Endflag = DX_FALSE;    
    
    DxChar* TST_TestsNameStrings[] = 
    {
      "Key Ring Management test",
      "Key Ring Keys Operations test",
      "Key Ring DH key operations",
      "Key Ring Get DH Public Key",
      "Key Ring List test",
      "Key Ring two Key Rings test",
      "Key Ring save Key Ring to flash test",
      "Key Ring Get Key Info",      
      "all Key Management tests"      
    };

    
#if _DEBUG
    DX_VOS_DbgPrint_SetPrintFunc(DX_VOS_DebugFilePrint);
#endif

   
    TST_TestStatus = KMNG_Init();
    if(TST_TestStatus != DX_SUCCESS)
    {
		ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"Failed KMNG_Init -Error:%X\n",TST_TestStatus));
		return KMNG_TEST_FAIL;
    }
    
 
    while(1)
    {
      /******** Main Menu - User chooses how to run the tests ********/
      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"\n-------------- DISCRETIX LTD 2007  ------------\n"));
      KMNG_ATP_TST_GetVersion();
      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"\n Please enter test number :\n"));
      for (TST_index = 0; TST_index < TST_MAX_NUM_OF_KMNG_ATP_TESTS; TST_index++)
      {
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"[%d] - Run %s\n",TST_index,TST_TestsNameStrings[TST_index]));
      }
      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"[Z] - End of Key Management Tests \n"));
      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"\n------------------------------------------------\n"));
    
      GET_NON_WHITE_SPACE_CHAR(TST_Var);
      
      switch(TST_Var)
      {
        case '0':
          TST_Status = KMNG_ATP_TST_Key_Ring_Management();  
          break;
        case '1':
          TST_Status = KMNG_ATP_TST_KeysOperation();
          break;
        case '2':
          #ifndef TST_DH_NOT_SUPPORTED
          TST_Status = KMNG_ATP_TST_DH_KeysOperation();
          #else
          ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"DH Key Operations not supported\n"));
          #endif
          break;
        case '3':
          #ifndef TST_DH_NOT_SUPPORTED
          TST_Status = KMNG_ATP_TST_DH_GetPubKey();
          #else
          ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"DH Key Operations not supported\n"));
          #endif
          break;
        case '4':
          TST_Status = KMNG_ATP_TST_Key_Ring_List();
          break;
        case '5':
          TST_Status = KMNG_ATP_TST_Operations_2_Key_Ring();
          break;
        case '6':
          TST_Status = KMNG_ATP_TST_Save_Key_Ring_To_Flash();
          break;  
        case '7':
          TST_Status = KMNG_ATP_TST_Get_Keys_Data();
          break;
        case '8':
          TST_Status = KMNG_ATP_TST_Run_All_ATP_Tests();
          break;
        case 'Z':

          DX_VOS_Printf("\n End of KMNG tests \n");
          TST_Endflag = DX_TRUE;
          break ;
        default:
          ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"User choice is illegal !!\n"));        
          continue;
      }
      if (TST_Endflag == DX_TRUE)
        break;
    
      if (TST_Status == DX_SUCCESS)
      {
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"***********************************************************\n"));
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"%s PASSED SUCCESSFULLY\n",TST_TestsNameStrings[TST_Var-'0']));
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"***********************************************************\n"));
      }
      else
      {
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"***********************************************************\n"));
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"%s FAILED\n",TST_TestsNameStrings[TST_Var-'0']));
        ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"***********************************************************\n"));
      }
    }
    
    
    #if KMNG_TST_LOG_ENABLE
    DX_VOS_FClose(TST_main_log);
    #endif
    return 0;
 }/* End of KMNG_ATP_TST_main*/

/************************ Private Functions *************************/

 /*****************************************************************************
 * Function Name:                                                           
 *  KMNG_ATP_TST_GetVersion      
 *  
 * Inputs:
 *  None
 *
 * Outputs: 
 *  None                                    
 *
 * Description:                                                                 
 *  This function gets the KMNG version and prints it
 *  
 ******************************************************************************/
void KMNG_ATP_TST_GetVersion(void)  
{
  KMNG_Version_t DX_TST_version;

  KMNG_GetVersion(&DX_TST_version);
  KMNG_ATP_TST_PrintVersion(&DX_TST_version);
}

/*****************************************************************************
* Function Name:                                                           
*  KMNG_ATP_TST_Run_All_ATP_Tests      
*  
* Inputs:
*  None
*
* Outputs:
*  DxError_t - Function error return
*
* Description:                                                                 
*  This function runs all the tests
*  
******************************************************************************/
DxError_t KMNG_ATP_TST_Run_All_ATP_Tests( void )
{
  /********* TST Variables Definitions ************/
  DxError_t          TST_Status = DX_SUCCESS, TST_TempError = DX_SUCCESS;

  /* Initializing the KMNG in case this function is called w/o the menu
  multiple calls to KMNG_Init are assumed to have no effect.	        */
  TST_Status = KMNG_Init();

  TST_Status = KMNG_TST_PrintCheckErrorReturn(TST_Status,
											   "KMNG_Init",
											   "!!!!!!!!!",
											   "!!!!!!!!!");
  if (TST_Status != DX_SUCCESS)
	return KMNG_TEST_FAIL;

  ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******************************************************* \n"));
  ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************** Run All ATP tests ********************** \n"));      
  ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******************************************************* \n \n"));

  TST_Status = KMNG_ATP_TST_Key_Ring_Management();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"********************************************************************* \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Key_Ring_Management Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"********************************************************************* \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Key_Ring_Management Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  TST_Status = KMNG_ATP_TST_KeysOperation();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_KeysOperation Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_KeysOperation Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  #ifndef TST_DH_NOT_SUPPORTED
	  TST_Status = KMNG_ATP_TST_DH_KeysOperation();
	  if (TST_Status == DX_SUCCESS)
	  {
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n"));
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_DH_KeysOperation Passed Successfully **** \n"));      
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n \n"));
	  }
	  else
	  {
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n"));
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_DH_KeysOperation Failed **** \n"));      
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n \n"));
	  }

	  TST_TempError = TST_TempError + TST_Status;
	  
	  TST_Status = KMNG_ATP_TST_DH_GetPubKey();
	  if (TST_Status == DX_SUCCESS)
	  {
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n"));
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_DH_GetPubKey Passed Successfully **** \n"));      
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n \n"));
	  }
	  else
	  {
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n"));
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_DH_GetPubKey Failed **** \n"));      
	      ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n \n"));
	  }

	  TST_TempError = TST_TempError + TST_Status;
  #endif

  TST_Status = KMNG_ATP_TST_Key_Ring_List();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Key_Ring_List Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*************************************************************** \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Key_Ring_List Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  TST_Status = KMNG_ATP_TST_Operations_2_Key_Ring();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Operations_2_Key_Ring Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************************** \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"********************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Operations_2_Key_Ring Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"********************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  TST_Status = KMNG_ATP_TST_Save_Key_Ring_To_Flash();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************************************ \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Save_Key_Ring_To_Flash Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************************************ \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Save_Key_Ring_To_Flash Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  TST_Status = KMNG_ATP_TST_Get_Keys_Data();
  if (TST_Status == DX_SUCCESS)
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************************************ \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Get_Keys_Data Passed Successfully ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"************************************************************************ \n \n"));
  }
  else
  {
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************** \n"));
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"******** KMNG_ATP_TST_Get_Keys_Data Failed ******* \n"));      
    ATP_LOG_TST_PRINT((KMNG_ATP_TST_MSG,"*********************************************************** \n \n"));
  }

  TST_TempError = TST_TempError + TST_Status;

  return TST_TempError;
}
