os/security/crypto/weakcryptospi/test/tcryptospi/src/randomnumbergeneratorgeneralusagestep.cpp
Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Example CTestStep derived implementation
24 #include <cryptospi/cryptorandomapi.h>
26 #include "securityerr.h"
27 #include "randomnumbergeneratorgeneralusagestep.h"
30 using namespace CryptoSpi;
32 CRandomNumberGeneratorGeneralUsageStep::~CRandomNumberGeneratorGeneralUsageStep()
36 CRandomNumberGeneratorGeneralUsageStep::CRandomNumberGeneratorGeneralUsageStep()
38 SetTestStepName(KRandomNumberGeneratorGeneralUsageStep);
41 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPreambleL()
43 SetTestStepResult(EPass);
44 return TestStepResult();
48 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepL()
50 if (TestStepResult()==EPass)
53 //Assume faliure, unless all is successful
54 SetTestStepResult(EFail);
56 INFO_PRINTF1(_L("*** Random Number Generator - General Usage ***"));
57 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
59 TVariantPtrC algorithmUid;
61 //Extract the necessary parameters from the specified INI file
62 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid))
64 ERR_PRINTF1(_L("** Error: Algorithm Uid Not Found **"));
65 SetTestStepResult(EFail);
69 //Create a pointer for the Random Implementation Object
70 CryptoSpi::CRandom* randomImpl = NULL;
72 //Retrieve a Random Factory Object
73 TRAPD(err,CRandomFactory::CreateRandomL(randomImpl,
77 if(randomImpl && (err == KErrNone))
80 TBuf8<50> randomStr(50);
82 INFO_PRINTF2(_L("*** randomStr Length: %d ***"),randomStr.Length());
83 INFO_PRINTF2(_L("*** randomStr MaxLength: %d ***"),randomStr.MaxLength());
85 TBuf<50> originalState;
86 originalState.Copy(randomStr);
88 INFO_PRINTF2(_L("*** Original Content: %S ***"),&originalState);
90 TRAP(err,randomImpl->GenerateRandomBytesL(randomStr));
92 //Copy the 8bit descriptor to 16bit using using a conversion macro
93 TBuf<50> randomResult;
94 randomResult.Copy(randomStr);
96 if((randomResult != KEmptyString) && (randomResult != originalState) && ((err == KErrNone) || (err == KErrNotSecure)))
98 INFO_PRINTF1(_L("*** Random Number Generator - General Usage : PASS ***"));
99 SetTestStepResult(EPass);
103 ERR_PRINTF1(_L("*** FAIL: Failed to Fill Random String ***"));
104 SetTestStepResult(EFail);
109 ERR_PRINTF2(_L("*** Random Object Load Failure : %d ***"), err);
113 //Free up the memory allocated to the Random object and set the point to NULL
119 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
122 return TestStepResult();
126 TVerdict CRandomNumberGeneratorGeneralUsageStep::doTestStepPostambleL()
128 return TestStepResult();