os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithreplicatestep.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 "hashincrementalhashwithreplicatestep.h"
 
    26 #include <cryptospi/cryptohashapi.h>
 
    27 #include <cryptospi/plugincharacteristics.h>
 
    29 using namespace CryptoSpi;
 
    31 CHashIncrementalHashWithReplicateStep::~CHashIncrementalHashWithReplicateStep()
 
    36 CHashIncrementalHashWithReplicateStep::CHashIncrementalHashWithReplicateStep()
 
    38 	SetTestStepName(KHashIncrementalHashWithReplicateStep);
 
    42 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPreambleL()
 
    44 	SetTestStepResult(EPass);
 
    45 	return TestStepResult();
 
    49 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepL()
 
    51 	if (TestStepResult()==EPass)
 
    54 		//Assume faliure, unless all is successful
 
    55 		SetTestStepResult(EFail);
 
    57 		INFO_PRINTF1(_L("*** Hash - Incremental Hash with Replicate ***"));
 
    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 hashReplicated = EFalse;
 
   125 						CHash* hashReplicateImpl = 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 								if(hashReplicated == EFalse)
 
   151 									hashImpl->Hash(*sourceData);
 
   152 									INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
 
   156 									hashReplicateImpl->Hash(*sourceData);
 
   157 									INFO_PRINTF2(_L("Intial Hash (Replicate) - Bytes Read: %d"), readPosition);	
 
   160 							else if(readPosition >= sourceLength)
 
   162 								//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
 
   163 								hashStr.Set(hashReplicateImpl->Final(*sourceData));
 
   165 								//Sets the Complete Flag to ETrue in order to drop out of the loop
 
   166 								hashComplete = ETrue;
 
   168 								TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
 
   169 								INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
 
   171 							//If the read position is half the source length and the implementation
 
   172 							//object hasn't already been replicated
 
   173 							else if((readPosition >= sourceLength/2) && (hashReplicated == EFalse))
 
   175 								INFO_PRINTF1(_L("Replicating Hash Object..."));
 
   177 								//Create a Copy of the existing Hash Object with NO internal message state
 
   178 								hashReplicateImpl = hashImpl->ReplicateL();
 
   180 								hashReplicated = ETrue;
 
   182 								//Sets the read position back to 0 inorder to restart the file read from the beginning
 
   185 								INFO_PRINTF2(_L("*** HASH REPLICATE - Bytes Read: %d ***"), readPosition);
 
   189 								//Update the message data within the Hash object with the new block
 
   190 								if(hashReplicated == EFalse)
 
   192 									hashImpl->Update(*sourceData);
 
   193 									INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);		
 
   197 									hashReplicateImpl->Update(*sourceData);
 
   198 									INFO_PRINTF2(_L("Hash Update (Replicate) - Bytes Read: %d"), readPosition);		
 
   202 							CleanupStack::PopAndDestroy(sourceData);
 
   204 							}while(hashComplete == EFalse);
 
   206 						//Create a NULL TCharacteristics pointer
 
   207 						const TCharacteristics* charsPtr(NULL);
 
   209 						//Retrieve the characteristics for the hash implementation object
 
   210 						TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
 
   212 						//Static cast the characteristics to type THashCharacteristics
 
   213 						const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
 
   215 						//The hash output size is returned in Bits, divide by 8 to get the Byte size
 
   216 						TInt hashSize = hashCharsPtr->iOutputSize/8;
 
   218 						//Retrieve the final 8bit hash value and convert to 16bit												
 
   219 						HBufC* hashData = HBufC::NewLC(hashSize);
 
   220 						TPtr hashPtr = hashData->Des();
 
   222 						//Copy the hashed content into the heap based descriptor
 
   223 						hashPtr.Copy(hashStr);
 
   225 					 	//Take the 16bit descriptor and convert the string to hexadecimal
 
   226 						TVariantPtrC convertHash;
 
   227 						convertHash.Set(hashPtr);
 
   228 						HBufC* hashResult = convertHash.HexStringLC();
 
   230 						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
 
   231 						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
 
   233 						//If the returned hash value matches the expected hash, Pass the test
 
   234 						if(*hashResult == expectedHash)
 
   236 							INFO_PRINTF1(_L("*** Hash - Incremental Hash with Replicate : PASS ***"));
 
   237 							SetTestStepResult(EPass);	
 
   241 							ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
 
   242 							SetTestStepResult(EFail);	
 
   245 						CleanupStack::PopAndDestroy(hashResult);
 
   246 						CleanupStack::PopAndDestroy(hashData);						
 
   248 						delete hashReplicateImpl;
 
   251 					//Cleanup the Source RFile	
 
   252 					CleanupStack::PopAndDestroy();	
 
   257 				CleanupStack::PopAndDestroy(hashImpl);	
 
   261 				ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
 
   262 				SetTestStepResult(EFail);	
 
   267 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
 
   268 	return TestStepResult();
 
   272 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPostambleL()
 
   275 	return TestStepResult();