Update contrib.
2 * Copyright (c) 2008-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 "hash_incremental_step.h"
25 #include <cryptospi/cryptohashapi.h>
26 #include <cryptospi/plugincharacteristics.h>
28 using namespace CryptoSpi;
30 CHashIncrementalStep::~CHashIncrementalStep()
35 CHashIncrementalStep::CHashIncrementalStep()
37 SetTestStepName(KHashIncrementalStep);
41 TVerdict CHashIncrementalStep::doTestStepPreambleL()
47 TVerdict CHashIncrementalStep::doTestStepL()
49 //Assume faliure, unless all is successful
50 SetTestStepResult(EFail);
52 INFO_PRINTF1(_L("*** Hash - Incremental ***"));
53 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
55 TVariantPtrC algorithmUid;
59 //Extract the Test Case ID parameter from the specified INI file
60 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
61 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
62 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
64 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
65 SetTestStepResult(EFail);
69 //Create a pointer for the Hash Implementation Object
70 CHash* hashImpl = NULL;
72 //Retrieve a Hash Factory Object
73 TRAPD(err,CHashFactory::CreateHashL(hashImpl,
80 //Push the Hash Implementation Object onto the Cleanup Stack
81 CleanupStack::PushL(hashImpl);
84 CleanupClosePushL(fsSession);
86 //Create a connection to the file server
87 err = fsSession.Connect();
91 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
92 SetTestStepResult(EFail);
97 CleanupClosePushL(sourceFile);
99 //Open the specified source file
100 err = sourceFile.Open(fsSession,sourcePath, EFileRead);
104 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
105 SetTestStepResult(EFail);
110 TInt sourceLength = 0;
111 TInt readPosition = 0;
112 TInt readIncrement = 0;
113 TBool hashComplete = EFalse;
116 User::LeaveIfError(sourceFile.Size(sourceLength));
118 //Divide the total size of the source file up into individual equal sized blocks to read
119 //over several increments
120 readIncrement = sourceLength/KDataReadBlocks;
122 if (readIncrement == 0)
124 ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
125 User::LeaveIfError(KErrNotSupported);
130 //Create a heap based descriptor to store the data
131 HBufC8* sourceData = HBufC8::NewL(readIncrement);
132 CleanupStack::PushL(sourceData);
133 TPtr8 sourcePtr = sourceData->Des();
135 //Read in a block of data from the source file from the current position
136 User::LeaveIfError(sourceFile.Read(readPosition,sourcePtr,readIncrement));
138 //Update the read position by adding the number of bytes read
139 readPosition += readIncrement;
141 if(readPosition == readIncrement)
143 //Read in the first block from the data file into the Hash implementation object
144 hashImpl->Hash(*sourceData);
145 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
147 else if(readPosition >= sourceLength)
149 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
150 hashStr.Set(hashImpl->Final(*sourceData));
152 //Sets the Complete Flag to ETrue in order to drop out of the loop
153 hashComplete = ETrue;
155 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
156 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
160 //Update the message data within the Hash object with the new block
161 hashImpl->Update(*sourceData);
162 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
165 CleanupStack::PopAndDestroy(sourceData);
167 }while(hashComplete == EFalse);
169 //Create a NULL TCharacteristics pointer
170 const TCharacteristics* charsPtr(NULL);
172 //Retrieve the characteristics for the hash implementation object
173 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
175 //Static cast the characteristics to type THashCharacteristics
176 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
178 //The hash output size is returned in Bits, divide by 8 to get the Byte size
179 TInt hashSize = hashCharsPtr->iOutputSize/8;
181 //Retrieve the final 8bit hash value and convert to 16bit
182 HBufC* hashData = HBufC::NewLC(hashSize);
183 TPtr hashPtr = hashData->Des();
185 //Copy the hashed content into the heap based descriptor
186 hashPtr.Copy(hashStr);
188 //Take the 16bit descriptor and convert the string to hexadecimal
189 TVariantPtrC convertHash;
190 convertHash.Set(hashPtr);
191 HBufC* hashResult = convertHash.HexStringLC();
193 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
194 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
196 //If the returned hash value matches the expected hash, Pass the test
197 if(*hashResult == expectedHash)
199 INFO_PRINTF1(_L("*** Hash - Incremental Hash : PASS ***"));
200 SetTestStepResult(EPass);
204 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
205 SetTestStepResult(EFail);
208 CleanupStack::PopAndDestroy(2, hashData); // hashResult, hashData
211 //Cleanup the Source RFile
212 CleanupStack::PopAndDestroy();
215 CleanupStack::PopAndDestroy(&fsSession);
219 CleanupStack::PopAndDestroy(hashImpl);
223 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
224 SetTestStepResult(EFail);
228 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
229 return TestStepResult();
233 TVerdict CHashIncrementalStep::doTestStepPostambleL()
235 return TestStepResult();