os/security/crypto/weakcryptospi/test/tcryptospi/src/randomnumbergeneratorgeneralusagestep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 <cryptospi/cryptorandomapi.h>
    25 
    26 #include "securityerr.h"
    27 #include "randomnumbergeneratorgeneralusagestep.h"
    28 
    29 
    30 using namespace CryptoSpi;
    31 
    32 CRandomNumberGeneratorGeneralUsageStep::~CRandomNumberGeneratorGeneralUsageStep()
    33 	{
    34 	}
    35 
    36 CRandomNumberGeneratorGeneralUsageStep::CRandomNumberGeneratorGeneralUsageStep()
    37 	{
    38 	SetTestStepName(KRandomNumberGeneratorGeneralUsageStep);
    39 	}
    40 
    41 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPreambleL()
    42 	{
    43 	SetTestStepResult(EPass);
    44 	return TestStepResult();
    45 	}
    46 
    47 
    48 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepL()
    49 	{
    50 	if (TestStepResult()==EPass)
    51 		{
    52 		
    53 		//Assume faliure, unless all is successful
    54 		SetTestStepResult(EFail);
    55 		
    56 		INFO_PRINTF1(_L("*** Random Number Generator - General Usage ***"));
    57 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    58 		
    59 		TVariantPtrC algorithmUid;
    60 		
    61 		//Extract the necessary parameters from the specified INI file
    62 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid))
    63 			{
    64 			ERR_PRINTF1(_L("** Error: Algorithm Uid Not Found **"));
    65 			SetTestStepResult(EFail);
    66 			}
    67 		else
    68 			{
    69 			//Create a pointer for the Random Implementation Object
    70 			CryptoSpi::CRandom* randomImpl = NULL;
    71 			
    72 			//Retrieve a Random Factory Object						
    73 			TRAPD(err,CRandomFactory::CreateRandomL(randomImpl,
    74 													algorithmUid,
    75 													NULL)); 
    76 				
    77 			if(randomImpl && (err == KErrNone))
    78 				{
    79 				
    80 				TBuf8<50> randomStr(50);
    81 				
    82 				INFO_PRINTF2(_L("*** randomStr Length: %d ***"),randomStr.Length());
    83 				INFO_PRINTF2(_L("*** randomStr MaxLength: %d ***"),randomStr.MaxLength());
    84 				
    85 				TBuf<50> originalState;
    86 				originalState.Copy(randomStr);
    87 				
    88 				INFO_PRINTF2(_L("*** Original Content: %S ***"),&originalState);
    89 				
    90 				TRAP(err,randomImpl->GenerateRandomBytesL(randomStr));
    91 				
    92 				//Copy the 8bit descriptor to 16bit using using a conversion macro
    93 				TBuf<50> randomResult;
    94 				randomResult.Copy(randomStr);
    95 					
    96 				if((randomResult != KEmptyString) && (randomResult != originalState) && ((err == KErrNone) || (err == KErrNotSecure)))
    97 					{
    98 					INFO_PRINTF1(_L("*** Random Number Generator - General Usage : PASS ***"));
    99 					SetTestStepResult(EPass);	
   100 					}
   101 				else
   102 					{
   103 					ERR_PRINTF1(_L("*** FAIL: Failed to Fill Random String ***"));
   104 					SetTestStepResult(EFail);	
   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 	}