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 "hashincrementalhashstep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/plugincharacteristics.h>
29 using namespace CryptoSpi;
31 CHashIncrementalHashStep::~CHashIncrementalHashStep()
36 CHashIncrementalHashStep::CHashIncrementalHashStep()
38 SetTestStepName(KHashIncrementalHashStep);
42 TVerdict CHashIncrementalHashStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CHashIncrementalHashStep::doTestStepL()
51 if (TestStepResult()==EPass)
54 //Assume faliure, unless all is successful
55 SetTestStepResult(EFail);
57 INFO_PRINTF1(_L("*** Hash - Incremental Hash ***"));
58 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
60 TVariantPtrC algorithmUid;
61 TVariantPtrC operationModeUid;
65 //Extract the Test Case ID parameter from the specified INI file
66 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
67 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
68 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
69 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
71 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
72 SetTestStepResult(EFail);
76 //Create a pointer for the Hash Implementation Object
77 CHash* hashImpl = NULL;
79 //Retrieve a Hash Factory Object
80 TRAPD(err,CHashFactory::CreateHashL(hashImpl,
86 if(hashImpl && (err==KErrNone))
89 //Push the Hash Implementation Object onto the Cleanup Stack
90 CleanupStack::PushL(hashImpl);
94 //Create a connection to the file server
95 err = fsSession.Connect();
99 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
100 SetTestStepResult(EFail);
105 CleanupClosePushL(sourceFile);
107 //Open the specified source file
108 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
112 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
113 SetTestStepResult(EFail);
118 TInt sourceLength = 0;
119 TInt readPosition = 0;
120 TInt readIncrement = 0;
121 TBool hashComplete = EFalse;
124 User::LeaveIfError(sourceFile.Size(sourceLength));
126 //Divide the total size of the source file up into individual equal sized blocks to read
127 //over several increments
128 readIncrement = sourceLength/KDataReadBlocks;
132 //Create a heap based descriptor to store the data
133 HBufC8* sourceData = HBufC8::NewL(readIncrement);
134 CleanupStack::PushL(sourceData);
135 TPtr8 sourcePtr = sourceData->Des();
137 //Read in a block of data from the source file from the current position
138 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
140 //Update the read position by adding the number of bytes read
141 readPosition += readIncrement;
143 if(readPosition == readIncrement)
145 //Read in the first block from the data file into the Hash implementation object
146 hashImpl->Hash(*sourceData);
147 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
149 else if(readPosition >= sourceLength)
151 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
152 hashStr.Set(hashImpl->Final(*sourceData));
154 //Sets the Complete Flag to ETrue in order to drop out of the loop
155 hashComplete = ETrue;
157 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
158 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
162 //Update the message data within the Hash object with the new block
163 hashImpl->Update(*sourceData);
164 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
167 CleanupStack::PopAndDestroy(sourceData);
169 }while(hashComplete == EFalse);
171 //Create a NULL TCharacteristics pointer
172 const TCharacteristics* charsPtr(NULL);
174 //Retrieve the characteristics for the hash implementation object
175 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
177 //Static cast the characteristics to type THashCharacteristics
178 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
180 //The hash output size is returned in Bits, divide by 8 to get the Byte size
181 TInt hashSize = hashCharsPtr->iOutputSize/8;
183 //Retrieve the final 8bit hash value and convert to 16bit
184 HBufC* hashData = HBufC::NewLC(hashSize);
185 TPtr hashPtr = hashData->Des();
187 //Copy the hashed content into the heap based descriptor
188 hashPtr.Copy(hashStr);
190 //Take the 16bit descriptor and convert the string to hexadecimal
191 TVariantPtrC convertHash;
192 convertHash.Set(hashPtr);
193 HBufC* hashResult = convertHash.HexStringLC();
195 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
196 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
198 //If the returned hash value matches the expected hash, Pass the test
199 if(*hashResult == expectedHash)
201 INFO_PRINTF1(_L("*** Hash - Incremental Hash : PASS ***"));
202 SetTestStepResult(EPass);
206 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
207 SetTestStepResult(EFail);
210 CleanupStack::PopAndDestroy(hashResult);
211 CleanupStack::PopAndDestroy(hashData);
214 //Cleanup the Source RFile
215 CleanupStack::PopAndDestroy();
220 CleanupStack::PopAndDestroy(hashImpl);
224 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
225 SetTestStepResult(EFail);
230 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
231 return TestStepResult();
235 TVerdict CHashIncrementalHashStep::doTestStepPostambleL()
237 return TestStepResult();