crypto/weakcryptospi/test/tcryptospi/src/randomnumbergeneratorgeneralusagestep.cpp
changeset 8 35751d3474b7
child 43 9b5a3a9fddf8
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Example CTestStep derived implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23 */
       
    24 #include "randomnumbergeneratorgeneralusagestep.h"
       
    25 
       
    26 #include <cryptospi/cryptorandomapi.h>
       
    27 
       
    28 
       
    29 using namespace CryptoSpi;
       
    30 
       
    31 CRandomNumberGeneratorGeneralUsageStep::~CRandomNumberGeneratorGeneralUsageStep()
       
    32 	{
       
    33 	}
       
    34 
       
    35 CRandomNumberGeneratorGeneralUsageStep::CRandomNumberGeneratorGeneralUsageStep()
       
    36 	{
       
    37 	SetTestStepName(KRandomNumberGeneratorGeneralUsageStep);
       
    38 	}
       
    39 
       
    40 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPreambleL()
       
    41 	{
       
    42 	SetTestStepResult(EPass);
       
    43 	return TestStepResult();
       
    44 	}
       
    45 
       
    46 
       
    47 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepL()
       
    48 	{
       
    49 	if (TestStepResult()==EPass)
       
    50 		{
       
    51 		
       
    52 		//Assume faliure, unless all is successful
       
    53 		SetTestStepResult(EFail);
       
    54 		
       
    55 		INFO_PRINTF1(_L("*** Random Number Generator - General Usage ***"));
       
    56 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    57 		
       
    58 		TVariantPtrC algorithmUid;
       
    59 		
       
    60 		//Extract the necessary parameters from the specified INI file
       
    61 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid))
       
    62 			{
       
    63 			ERR_PRINTF1(_L("** Error: Algorithm Uid Not Found **"));
       
    64 			SetTestStepResult(EFail);
       
    65 			}
       
    66 		else
       
    67 			{
       
    68 			//Create a pointer for the Random Implementation Object
       
    69 			CryptoSpi::CRandom* randomImpl = NULL;
       
    70 			
       
    71 			//Retrieve a Random Factory Object						
       
    72 			TRAPD(err,CRandomFactory::CreateRandomL(randomImpl,
       
    73 													algorithmUid,
       
    74 													NULL)); 
       
    75 				
       
    76 			if(randomImpl && (err == KErrNone))
       
    77 				{
       
    78 				
       
    79 				TBuf8<50> randomStr(50);
       
    80 				
       
    81 				INFO_PRINTF2(_L("*** randomStr Length: %d ***"),randomStr.Length());
       
    82 				INFO_PRINTF2(_L("*** randomStr MaxLength: %d ***"),randomStr.MaxLength());
       
    83 				
       
    84 				TBuf<50> originalState;
       
    85 				originalState.Copy(randomStr);
       
    86 				
       
    87 				INFO_PRINTF2(_L("*** Original Content: %S ***"),&originalState);
       
    88 				
       
    89 				TRAP_LOG(err,randomImpl->GenerateRandomBytesL(randomStr));
       
    90 				
       
    91 				//Copy the 8bit descriptor to 16bit using using a conversion macro
       
    92 				TBuf<50> randomResult;
       
    93 				randomResult.Copy(randomStr);
       
    94 					
       
    95 				if((randomResult != KEmptyString) && (randomResult != originalState) && (err == KErrNone))
       
    96 					{
       
    97 					INFO_PRINTF1(_L("*** Random Number Generator - General Usage : PASS ***"));
       
    98 					SetTestStepResult(EPass);	
       
    99 					}
       
   100 				else
       
   101 					{
       
   102 					ERR_PRINTF1(_L("*** FAIL: Failed to Fill Random String ***"));
       
   103 					SetTestStepResult(EFail);	
       
   104 					}
       
   105 	
       
   106 				}
       
   107 			else
       
   108 				{
       
   109 				ERR_PRINTF2(_L("*** Random Object Load Failure : %d ***"), err);
       
   110 				User::Leave(err);	
       
   111 				}
       
   112 			
       
   113 			//Free up the memory allocated to the Random object and set the point to NULL	
       
   114 			delete randomImpl;
       
   115 			randomImpl = NULL;
       
   116 			
       
   117 			}
       
   118 		
       
   119 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   120 		
       
   121 		}
       
   122 	return TestStepResult();
       
   123 	}
       
   124 
       
   125 
       
   126 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPostambleL()
       
   127 	{
       
   128 	return TestStepResult();
       
   129 	}