os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithcopystep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithcopystep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,270 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include "hashincrementalhashwithcopystep.h"
    1.28 +
    1.29 +#include <cryptospi/cryptohashapi.h>
    1.30 +#include <cryptospi/plugincharacteristics.h>
    1.31 +
    1.32 +using namespace CryptoSpi;
    1.33 +
    1.34 +CHashIncrementalHashWithCopyStep::~CHashIncrementalHashWithCopyStep()
    1.35 +	{
    1.36 +	}
    1.37 +
    1.38 +
    1.39 +CHashIncrementalHashWithCopyStep::CHashIncrementalHashWithCopyStep()
    1.40 +	{
    1.41 +	SetTestStepName(KHashIncrementalHashWithCopyStep);
    1.42 +	}
    1.43 +
    1.44 +
    1.45 +TVerdict CHashIncrementalHashWithCopyStep::doTestStepPreambleL()
    1.46 +	{
    1.47 +	SetTestStepResult(EPass);
    1.48 +	return TestStepResult();
    1.49 +	}
    1.50 +
    1.51 +
    1.52 +TVerdict CHashIncrementalHashWithCopyStep::doTestStepL()
    1.53 +	{
    1.54 +	if (TestStepResult()==EPass)
    1.55 +		{
    1.56 +		
    1.57 +		//Assume faliure, unless all is successful
    1.58 +		SetTestStepResult(EFail);
    1.59 +		
    1.60 +		INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy ***"));
    1.61 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.62 +		
    1.63 +		TVariantPtrC algorithmUid;
    1.64 +		TVariantPtrC operationModeUid;
    1.65 +		TPtrC sourcePath;
    1.66 +		TPtrC expectedHash;
    1.67 +		
    1.68 +		//Extract the Test Case ID parameter from the specified INI file
    1.69 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.70 +			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
    1.71 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    1.72 +			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
    1.73 +			{
    1.74 +			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    1.75 +			SetTestStepResult(EFail);
    1.76 +			}
    1.77 +		else
    1.78 +			{
    1.79 +			//Create a pointer for the Hash Implementation Object
    1.80 +			CHash* hashImpl = NULL;
    1.81 +			
    1.82 +			//Retrieve a Hash Factory Object					
    1.83 +			TRAPD(err,CHashFactory::CreateHashL(hashImpl,
    1.84 +												algorithmUid,
    1.85 +												operationModeUid,
    1.86 +												NULL,
    1.87 +												NULL));  				
    1.88 +	
    1.89 +			if(hashImpl && (err == KErrNone))
    1.90 +				{
    1.91 +				
    1.92 +				//Push the Hash Implementation Object onto the Cleanup Stack
    1.93 +				CleanupStack::PushL(hashImpl);
    1.94 +				
    1.95 +				RFs fsSession;
    1.96 +				
    1.97 +				//Create a connection to the file server	
    1.98 +				err = fsSession.Connect();
    1.99 +					
   1.100 +				if(err != KErrNone)
   1.101 +					{
   1.102 +					ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
   1.103 +					SetTestStepResult(EFail);
   1.104 +					}	
   1.105 +				else
   1.106 +					{
   1.107 +					RFile sourceFile;
   1.108 +					CleanupClosePushL(sourceFile);
   1.109 +	    			
   1.110 +	    			//Open the specified source file		
   1.111 +	    			err = sourceFile.Open(fsSession,sourcePath, EFileRead);
   1.112 +	    					
   1.113 +	    			if(err != KErrNone)
   1.114 +						{
   1.115 +						ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
   1.116 +						SetTestStepResult(EFail);
   1.117 +						}
   1.118 +					else
   1.119 +						{
   1.120 +						
   1.121 +						TInt sourceLength = 0;
   1.122 +						TInt readPosition = 0;
   1.123 +						TInt readIncrement = 0;
   1.124 +						TBool hashComplete = EFalse;
   1.125 +						TBool hashCopied = EFalse;
   1.126 +						TPtrC8 hashStr;
   1.127 +						
   1.128 +						CHash* hashCopyImpl = NULL;
   1.129 +						
   1.130 +						User::LeaveIfError(sourceFile.Size(sourceLength));
   1.131 +						
   1.132 +						//Divide the total size of the source file up into individual equal sized blocks to read
   1.133 +						//over several increments
   1.134 +						readIncrement = sourceLength/KDataReadBlocks;
   1.135 +						
   1.136 +						do 
   1.137 +							{							
   1.138 +							//Create a heap based descriptor to store the data
   1.139 +							HBufC8* sourceData = HBufC8::NewL(readIncrement);
   1.140 +							CleanupStack::PushL(sourceData);
   1.141 +							TPtr8 sourcePtr = sourceData->Des();
   1.142 +							
   1.143 +							//Read in a block of data from the source file from the current position
   1.144 +							err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
   1.145 +							
   1.146 +							//Update the read position by adding the number of bytes read
   1.147 +							readPosition += readIncrement;
   1.148 +							
   1.149 +							if(readPosition == readIncrement)
   1.150 +								{
   1.151 +								//Read in the first block from the data file into the Hash implementation object
   1.152 +								hashImpl->Hash(*sourceData);
   1.153 +								INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
   1.154 +								}
   1.155 +							else if(readPosition >= sourceLength)
   1.156 +								{
   1.157 +								//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
   1.158 +								hashStr.Set(hashCopyImpl->Final(*sourceData));
   1.159 +								
   1.160 +								//Sets the Complete Flag to ETrue in order to drop out of the loop
   1.161 +								hashComplete = ETrue;
   1.162 +								
   1.163 +								TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
   1.164 +								INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
   1.165 +								}
   1.166 +							//If the read position is half the source length and the implementation
   1.167 +							//object hasn't already been copied
   1.168 +							else if((readPosition >= sourceLength/2) && (hashCopied == EFalse))
   1.169 +								{
   1.170 +								//Update the hash message before copying
   1.171 +								hashImpl->Update(*sourceData);
   1.172 +								
   1.173 +								INFO_PRINTF1(_L("Copying Hash Object..."));
   1.174 +								
   1.175 +								//Create a Copy of the existing Hash Object and all internal state of the message digest
   1.176 +								hashCopyImpl = hashImpl->CopyL();
   1.177 +								
   1.178 +								hashCopied = ETrue;
   1.179 +								
   1.180 +								INFO_PRINTF2(_L("*** HASH COPY - Bytes Read: %d ***"), readPosition);
   1.181 +								}
   1.182 +							else
   1.183 +								{
   1.184 +								//Update the message data within the Hash object with the new block
   1.185 +								if(hashCopied == EFalse)
   1.186 +									{
   1.187 +									hashImpl->Update(*sourceData);
   1.188 +									INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);		
   1.189 +									}
   1.190 +								else
   1.191 +									{
   1.192 +									hashCopyImpl->Update(*sourceData);
   1.193 +									INFO_PRINTF2(_L("Hash Update (Copy) - Bytes Read: %d"), readPosition);		
   1.194 +									}
   1.195 +								}
   1.196 +							
   1.197 +							CleanupStack::PopAndDestroy(sourceData);
   1.198 +							
   1.199 +								
   1.200 +							}while(hashComplete == EFalse);
   1.201 +						
   1.202 +						//Create a NULL TCharacteristics pointer
   1.203 +						const TCharacteristics* charsPtr(NULL);
   1.204 +						
   1.205 +						//Retrieve the characteristics for the hash implementation object
   1.206 +						TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
   1.207 +						
   1.208 +						//Static cast the characteristics to type THashCharacteristics
   1.209 +						const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
   1.210 +						
   1.211 +						//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.212 +						TInt hashSize = hashCharsPtr->iOutputSize/8;
   1.213 +						
   1.214 +						//Retrieve the final 8bit hash value and convert to 16bit												
   1.215 +						HBufC* hashData = HBufC::NewLC(hashSize);
   1.216 +						TPtr hashPtr = hashData->Des();
   1.217 +						
   1.218 +						//Copy the hashed content into the heap based descriptor
   1.219 +						hashPtr.Copy(hashStr);
   1.220 +						
   1.221 +					 	//Take the 16bit descriptor and convert the string to hexadecimal
   1.222 +						TVariantPtrC convertHash;
   1.223 +						convertHash.Set(hashPtr);
   1.224 +						HBufC* hashResult = convertHash.HexStringLC();
   1.225 +						
   1.226 +						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
   1.227 +						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.228 +						
   1.229 +						//If the returned hash value matches the expected hash, Pass the test		
   1.230 +						if(*hashResult == expectedHash)
   1.231 +							{
   1.232 +							INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy : PASS ***"));
   1.233 +							SetTestStepResult(EPass);	
   1.234 +							}
   1.235 +						else
   1.236 +							{
   1.237 +							ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
   1.238 +							SetTestStepResult(EFail);	
   1.239 +							}
   1.240 +								
   1.241 +						CleanupStack::PopAndDestroy(hashResult);
   1.242 +						CleanupStack::PopAndDestroy(hashData);						
   1.243 +						
   1.244 +						delete hashCopyImpl;
   1.245 +						}
   1.246 +					
   1.247 +					//Cleanup the Source RFile	
   1.248 +					CleanupStack::PopAndDestroy();	
   1.249 +					}
   1.250 +
   1.251 +				fsSession.Close();	
   1.252 +				
   1.253 +				CleanupStack::PopAndDestroy(hashImpl);
   1.254 +				}
   1.255 +			else
   1.256 +				{
   1.257 +				ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
   1.258 +				SetTestStepResult(EFail);	
   1.259 +				}
   1.260 +			}
   1.261 +			
   1.262 +		}
   1.263 +		
   1.264 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.265 +	return TestStepResult();
   1.266 +	}
   1.267 +
   1.268 +
   1.269 +TVerdict CHashIncrementalHashWithCopyStep::doTestStepPostambleL()
   1.270 +	{
   1.271 +	
   1.272 +	return TestStepResult();
   1.273 +	}