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 "hashbasichashofdatastep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/plugincharacteristics.h>
29 using namespace CryptoSpi;
41 THashParameters::~THashParameters()
46 TInt CreatorThreadEntryPoint(TAny* aParameters)
48 CTrapCleanup* cleanup = CTrapCleanup::New();
50 User::Exit(KErrNoMemory);
53 THashParameters* params=static_cast<THashParameters*>(aParameters);
55 TRAPD(result,CHashFactory::CreateHashL(params->iHash,
56 params->iAlgorithmUid,
57 params->iOperationUid,
66 CHashBasicHashOfDataStep::~CHashBasicHashOfDataStep()
71 CHashBasicHashOfDataStep::CHashBasicHashOfDataStep()
73 SetTestStepName(KHashBasicHashOfDataStep);
77 TVerdict CHashBasicHashOfDataStep::doTestStepPreambleL()
79 SetTestStepResult(EPass);
80 return TestStepResult();
84 TVerdict CHashBasicHashOfDataStep::doTestStepL()
86 if (TestStepResult()==EPass)
89 //Assume faliure, unless all is successful
90 SetTestStepResult(EFail);
92 INFO_PRINTF1(_L("*** Hash - Basic Hash of Data ***"));
93 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
95 TVariantPtrC algorithmUid;
96 TVariantPtrC operationModeUid;
100 //Extract the Test Case ID parameter from the specified INI file
101 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
102 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
103 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
104 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
106 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
107 SetTestStepResult(EFail);
112 CHash* hashImpl=NULL;
114 THashParameters params;
115 RThread creatorThread;
117 CleanupClosePushL(creatorThread);
119 if (GetStringFromConfig(ConfigSection(),KConfigThreadName,threadName))
122 params.iAlgorithmUid=algorithmUid;
123 params.iOperationUid=operationModeUid;
127 User::LeaveIfError(creatorThread.Create(threadName, CreatorThreadEntryPoint, KDefaultStackSize, NULL, (TAny*)¶ms));
128 TRequestStatus status=KRequestPending;
129 creatorThread.Logon(status);
130 creatorThread.Resume();
131 User::WaitForRequest(status);
132 //creatorThread.Close();
133 hashImpl=params.iHash;
138 //Retrieve a Hash Factory Object
139 TRAP(err,CHashFactory::CreateHashL(hashImpl,
144 params.iHash=hashImpl;
147 if(hashImpl && (err == KErrNone))
150 //Push the Hash Implementation Object onto the Cleanup Stack
151 //CleanupStack::PushL(hashImpl);
155 //Create a connection to the file server
156 err = fsSession.Connect();
160 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
161 SetTestStepResult(EFail);
166 CleanupClosePushL(sourceFile);
168 //Open the specified source file
169 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
173 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
174 SetTestStepResult(EFail);
178 TInt sourceLength = 0;
179 User::LeaveIfError(sourceFile.Size(sourceLength));
181 //Create a heap based descriptor to store the data
182 HBufC8* sourceData = HBufC8::NewL(sourceLength);
183 CleanupStack::PushL(sourceData);
184 TPtr8 sourcePtr = sourceData->Des();
186 sourceFile.Read(sourcePtr);
188 if(sourcePtr.Length() != sourceLength)
190 ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
191 SetTestStepResult(EFail);
195 //Create a NULL TCharacteristics pointer
196 const TCharacteristics* charsPtr(NULL);
198 //Retrieve the characteristics for the hash implementation object
199 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
201 //Static cast the characteristics to type THashCharacteristics
202 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
204 //The hash output size is returned in Bits, divide by 8 to get the Byte size
205 TInt hashSize = hashCharsPtr->iOutputSize/8;
207 //Retrieve the final 8bit hash value and convert to 16bit
208 HBufC* hashData = HBufC::NewLC(hashSize);
209 TPtr hashPtr = hashData->Des();
211 //Copy the hashed content into the heap based descriptor
212 hashPtr.Copy(hashImpl->Hash(*sourceData));
214 //Take the 16bit descriptor and convert the string to hexadecimal
215 TVariantPtrC convertHash;
216 convertHash.Set(hashPtr);
217 HBufC* hashResult = convertHash.HexStringLC();
219 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
220 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
222 //If the returned hash value matches the expected hash, Pass the test
223 if(*hashResult == expectedHash)
225 INFO_PRINTF1(_L("*** Hash - Basic Hash of Data : PASS ***"));
226 SetTestStepResult(EPass);
230 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
231 SetTestStepResult(EFail);
234 CleanupStack::PopAndDestroy(hashResult);
235 CleanupStack::PopAndDestroy(hashData);
236 CleanupStack::PopAndDestroy(sourceData);
240 //Cleanup the Source RFile
241 CleanupStack::PopAndDestroy();
246 //CleanupStack::PopAndDestroy(hashImpl);
250 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
251 SetTestStepResult(EFail);
254 CleanupStack::PopAndDestroy(&creatorThread);
258 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
260 return TestStepResult();
264 TVerdict CHashBasicHashOfDataStep::doTestStepPostambleL()
267 return TestStepResult();