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