os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithcopystep.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 "hashincrementalhashwithcopystep.h"
26 #include <cryptospi/cryptohashapi.h>
27 #include <cryptospi/plugincharacteristics.h>
29 using namespace CryptoSpi;
31 CHashIncrementalHashWithCopyStep::~CHashIncrementalHashWithCopyStep()
36 CHashIncrementalHashWithCopyStep::CHashIncrementalHashWithCopyStep()
38 SetTestStepName(KHashIncrementalHashWithCopyStep);
42 TVerdict CHashIncrementalHashWithCopyStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
49 TVerdict CHashIncrementalHashWithCopyStep::doTestStepL()
51 if (TestStepResult()==EPass)
54 //Assume faliure, unless all is successful
55 SetTestStepResult(EFail);
57 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy ***"));
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;
122 TBool hashCopied = EFalse;
125 CHash* hashCopyImpl = NULL;
127 User::LeaveIfError(sourceFile.Size(sourceLength));
129 //Divide the total size of the source file up into individual equal sized blocks to read
130 //over several increments
131 readIncrement = sourceLength/KDataReadBlocks;
135 //Create a heap based descriptor to store the data
136 HBufC8* sourceData = HBufC8::NewL(readIncrement);
137 CleanupStack::PushL(sourceData);
138 TPtr8 sourcePtr = sourceData->Des();
140 //Read in a block of data from the source file from the current position
141 err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
143 //Update the read position by adding the number of bytes read
144 readPosition += readIncrement;
146 if(readPosition == readIncrement)
148 //Read in the first block from the data file into the Hash implementation object
149 hashImpl->Hash(*sourceData);
150 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
152 else if(readPosition >= sourceLength)
154 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
155 hashStr.Set(hashCopyImpl->Final(*sourceData));
157 //Sets the Complete Flag to ETrue in order to drop out of the loop
158 hashComplete = ETrue;
160 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
161 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
163 //If the read position is half the source length and the implementation
164 //object hasn't already been copied
165 else if((readPosition >= sourceLength/2) && (hashCopied == EFalse))
167 //Update the hash message before copying
168 hashImpl->Update(*sourceData);
170 INFO_PRINTF1(_L("Copying Hash Object..."));
172 //Create a Copy of the existing Hash Object and all internal state of the message digest
173 hashCopyImpl = hashImpl->CopyL();
177 INFO_PRINTF2(_L("*** HASH COPY - Bytes Read: %d ***"), readPosition);
181 //Update the message data within the Hash object with the new block
182 if(hashCopied == EFalse)
184 hashImpl->Update(*sourceData);
185 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
189 hashCopyImpl->Update(*sourceData);
190 INFO_PRINTF2(_L("Hash Update (Copy) - Bytes Read: %d"), readPosition);
194 CleanupStack::PopAndDestroy(sourceData);
197 }while(hashComplete == EFalse);
199 //Create a NULL TCharacteristics pointer
200 const TCharacteristics* charsPtr(NULL);
202 //Retrieve the characteristics for the hash implementation object
203 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
205 //Static cast the characteristics to type THashCharacteristics
206 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
208 //The hash output size is returned in Bits, divide by 8 to get the Byte size
209 TInt hashSize = hashCharsPtr->iOutputSize/8;
211 //Retrieve the final 8bit hash value and convert to 16bit
212 HBufC* hashData = HBufC::NewLC(hashSize);
213 TPtr hashPtr = hashData->Des();
215 //Copy the hashed content into the heap based descriptor
216 hashPtr.Copy(hashStr);
218 //Take the 16bit descriptor and convert the string to hexadecimal
219 TVariantPtrC convertHash;
220 convertHash.Set(hashPtr);
221 HBufC* hashResult = convertHash.HexStringLC();
223 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
224 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
226 //If the returned hash value matches the expected hash, Pass the test
227 if(*hashResult == expectedHash)
229 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy : PASS ***"));
230 SetTestStepResult(EPass);
234 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
235 SetTestStepResult(EFail);
238 CleanupStack::PopAndDestroy(hashResult);
239 CleanupStack::PopAndDestroy(hashData);
244 //Cleanup the Source RFile
245 CleanupStack::PopAndDestroy();
250 CleanupStack::PopAndDestroy(hashImpl);
254 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
255 SetTestStepResult(EFail);
261 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
262 return TestStepResult();
266 TVerdict CHashIncrementalHashWithCopyStep::doTestStepPostambleL()
269 return TestStepResult();