os/security/crypto/weakcryptospi/test/tcryptospi/src/randomnumbergeneratorgeneralusagestep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/randomnumbergeneratorgeneralusagestep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include <cryptospi/cryptorandomapi.h>
    1.28 +
    1.29 +#include "securityerr.h"
    1.30 +#include "randomnumbergeneratorgeneralusagestep.h"
    1.31 +
    1.32 +
    1.33 +using namespace CryptoSpi;
    1.34 +
    1.35 +CRandomNumberGeneratorGeneralUsageStep::~CRandomNumberGeneratorGeneralUsageStep()
    1.36 +	{
    1.37 +	}
    1.38 +
    1.39 +CRandomNumberGeneratorGeneralUsageStep::CRandomNumberGeneratorGeneralUsageStep()
    1.40 +	{
    1.41 +	SetTestStepName(KRandomNumberGeneratorGeneralUsageStep);
    1.42 +	}
    1.43 +
    1.44 +TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPreambleL()
    1.45 +	{
    1.46 +	SetTestStepResult(EPass);
    1.47 +	return TestStepResult();
    1.48 +	}
    1.49 +
    1.50 +
    1.51 +TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepL()
    1.52 +	{
    1.53 +	if (TestStepResult()==EPass)
    1.54 +		{
    1.55 +		
    1.56 +		//Assume faliure, unless all is successful
    1.57 +		SetTestStepResult(EFail);
    1.58 +		
    1.59 +		INFO_PRINTF1(_L("*** Random Number Generator - General Usage ***"));
    1.60 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.61 +		
    1.62 +		TVariantPtrC algorithmUid;
    1.63 +		
    1.64 +		//Extract the necessary parameters from the specified INI file
    1.65 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid))
    1.66 +			{
    1.67 +			ERR_PRINTF1(_L("** Error: Algorithm Uid Not Found **"));
    1.68 +			SetTestStepResult(EFail);
    1.69 +			}
    1.70 +		else
    1.71 +			{
    1.72 +			//Create a pointer for the Random Implementation Object
    1.73 +			CryptoSpi::CRandom* randomImpl = NULL;
    1.74 +			
    1.75 +			//Retrieve a Random Factory Object						
    1.76 +			TRAPD(err,CRandomFactory::CreateRandomL(randomImpl,
    1.77 +													algorithmUid,
    1.78 +													NULL)); 
    1.79 +				
    1.80 +			if(randomImpl && (err == KErrNone))
    1.81 +				{
    1.82 +				
    1.83 +				TBuf8<50> randomStr(50);
    1.84 +				
    1.85 +				INFO_PRINTF2(_L("*** randomStr Length: %d ***"),randomStr.Length());
    1.86 +				INFO_PRINTF2(_L("*** randomStr MaxLength: %d ***"),randomStr.MaxLength());
    1.87 +				
    1.88 +				TBuf<50> originalState;
    1.89 +				originalState.Copy(randomStr);
    1.90 +				
    1.91 +				INFO_PRINTF2(_L("*** Original Content: %S ***"),&originalState);
    1.92 +				
    1.93 +				TRAP(err,randomImpl->GenerateRandomBytesL(randomStr));
    1.94 +				
    1.95 +				//Copy the 8bit descriptor to 16bit using using a conversion macro
    1.96 +				TBuf<50> randomResult;
    1.97 +				randomResult.Copy(randomStr);
    1.98 +					
    1.99 +				if((randomResult != KEmptyString) && (randomResult != originalState) && ((err == KErrNone) || (err == KErrNotSecure)))
   1.100 +					{
   1.101 +					INFO_PRINTF1(_L("*** Random Number Generator - General Usage : PASS ***"));
   1.102 +					SetTestStepResult(EPass);	
   1.103 +					}
   1.104 +				else
   1.105 +					{
   1.106 +					ERR_PRINTF1(_L("*** FAIL: Failed to Fill Random String ***"));
   1.107 +					SetTestStepResult(EFail);	
   1.108 +					}	
   1.109 +				}
   1.110 +			else
   1.111 +				{
   1.112 +				ERR_PRINTF2(_L("*** Random Object Load Failure : %d ***"), err);
   1.113 +				User::Leave(err);	
   1.114 +				}
   1.115 +			
   1.116 +			//Free up the memory allocated to the Random object and set the point to NULL	
   1.117 +			delete randomImpl;
   1.118 +			randomImpl = NULL;
   1.119 +			
   1.120 +			}
   1.121 +		
   1.122 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.123 +		
   1.124 +		}
   1.125 +	return TestStepResult();
   1.126 +	}
   1.127 +
   1.128 +
   1.129 +TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPostambleL()
   1.130 +	{
   1.131 +	return TestStepResult();
   1.132 +	}