os/security/crypto/weakcryptospi/test/tcryptospi/src/hash_incremental_step.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/hash_incremental_step.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,236 @@
     1.4 +/*
     1.5 +* Copyright (c) 2008-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 "hash_incremental_step.h"
    1.28 +#include <cryptospi/cryptohashapi.h>
    1.29 +#include <cryptospi/plugincharacteristics.h>
    1.30 +
    1.31 +using namespace CryptoSpi;
    1.32 +
    1.33 +CHashIncrementalStep::~CHashIncrementalStep()
    1.34 +	{
    1.35 +	}
    1.36 +
    1.37 +
    1.38 +CHashIncrementalStep::CHashIncrementalStep()
    1.39 +	{
    1.40 +	SetTestStepName(KHashIncrementalStep);
    1.41 +	}
    1.42 +
    1.43 +
    1.44 +TVerdict CHashIncrementalStep::doTestStepPreambleL()
    1.45 +	{
    1.46 +	return EPass;
    1.47 +	}
    1.48 +
    1.49 +
    1.50 +TVerdict CHashIncrementalStep::doTestStepL()
    1.51 +	{
    1.52 +	//Assume faliure, unless all is successful
    1.53 +	SetTestStepResult(EFail);
    1.54 +
    1.55 +	INFO_PRINTF1(_L("*** Hash - Incremental ***"));
    1.56 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.57 +	
    1.58 +	TVariantPtrC algorithmUid;
    1.59 +	TPtrC sourcePath;
    1.60 +	TPtrC expectedHash;
    1.61 +	
    1.62 +	//Extract the Test Case ID parameter from the specified INI file
    1.63 +	if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.64 +		!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
    1.65 +		!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
    1.66 +		{
    1.67 +		ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
    1.68 +		SetTestStepResult(EFail);
    1.69 +		}
    1.70 +	else
    1.71 +		{
    1.72 +		//Create a pointer for the Hash Implementation Object
    1.73 +		CHash* hashImpl = NULL;
    1.74 +		
    1.75 +		//Retrieve a Hash Factory Object
    1.76 +		TRAPD(err,CHashFactory::CreateHashL(hashImpl,
    1.77 +											algorithmUid,
    1.78 +											NULL));  			
    1.79 +
    1.80 +		if(err==KErrNone)
    1.81 +			{
    1.82 +			
    1.83 +			//Push the Hash Implementation Object onto the Cleanup Stack
    1.84 +			CleanupStack::PushL(hashImpl);
    1.85 +			
    1.86 +			RFs fsSession;
    1.87 +			CleanupClosePushL(fsSession);
    1.88 +			
    1.89 +			//Create a connection to the file server	
    1.90 +			err = fsSession.Connect();
    1.91 +				
    1.92 +			if(err != KErrNone)
    1.93 +				{
    1.94 +				ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
    1.95 +				SetTestStepResult(EFail);
    1.96 +				}	
    1.97 +			else
    1.98 +				{
    1.99 +				RFile sourceFile;
   1.100 +				CleanupClosePushL(sourceFile);
   1.101 +    			
   1.102 +    			//Open the specified source file		
   1.103 +    			err = sourceFile.Open(fsSession,sourcePath, EFileRead);
   1.104 +    					
   1.105 +    			if(err != KErrNone)
   1.106 +					{
   1.107 +					ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
   1.108 +					SetTestStepResult(EFail);
   1.109 +					}
   1.110 +				else
   1.111 +					{
   1.112 +					
   1.113 +					TInt sourceLength = 0;
   1.114 +					TInt readPosition = 0;
   1.115 +					TInt readIncrement = 0;
   1.116 +					TBool hashComplete = EFalse;
   1.117 +					TPtrC8 hashStr;
   1.118 +					
   1.119 +					User::LeaveIfError(sourceFile.Size(sourceLength));
   1.120 +					
   1.121 +					//Divide the total size of the source file up into individual equal sized blocks to read
   1.122 +					//over several increments
   1.123 +					readIncrement = sourceLength/KDataReadBlocks;
   1.124 +					
   1.125 +					if (readIncrement == 0)
   1.126 +						{
   1.127 +						ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks);
   1.128 +						User::LeaveIfError(KErrNotSupported);
   1.129 +						}
   1.130 +					
   1.131 +					do 
   1.132 +						{
   1.133 +						//Create a heap based descriptor to store the data
   1.134 +						HBufC8* sourceData = HBufC8::NewL(readIncrement);
   1.135 +						CleanupStack::PushL(sourceData);
   1.136 +						TPtr8 sourcePtr = sourceData->Des();
   1.137 +						
   1.138 +						//Read in a block of data from the source file from the current position
   1.139 +						User::LeaveIfError(sourceFile.Read(readPosition,sourcePtr,readIncrement));
   1.140 +						
   1.141 +						//Update the read position by adding the number of bytes read
   1.142 +						readPosition += readIncrement;
   1.143 +						
   1.144 +						if(readPosition == readIncrement)
   1.145 +							{
   1.146 +							//Read in the first block from the data file into the Hash implementation object
   1.147 +							hashImpl->Hash(*sourceData);
   1.148 +							INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
   1.149 +							}
   1.150 +						else if(readPosition >= sourceLength)
   1.151 +							{
   1.152 +							//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
   1.153 +							hashStr.Set(hashImpl->Final(*sourceData));
   1.154 +							
   1.155 +							//Sets the Complete Flag to ETrue in order to drop out of the loop
   1.156 +							hashComplete = ETrue;
   1.157 +							
   1.158 +							TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
   1.159 +							INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
   1.160 +							}
   1.161 +						else
   1.162 +							{
   1.163 +							//Update the message data within the Hash object with the new block
   1.164 +							hashImpl->Update(*sourceData);
   1.165 +							INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
   1.166 +							}
   1.167 +						
   1.168 +						CleanupStack::PopAndDestroy(sourceData);
   1.169 +							
   1.170 +						}while(hashComplete == EFalse);
   1.171 +						
   1.172 +					//Create a NULL TCharacteristics pointer
   1.173 +					const TCharacteristics* charsPtr(NULL);
   1.174 +					
   1.175 +					//Retrieve the characteristics for the hash implementation object
   1.176 +					TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
   1.177 +					
   1.178 +					//Static cast the characteristics to type THashCharacteristics
   1.179 +					const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
   1.180 +					
   1.181 +					//The hash output size is returned in Bits, divide by 8 to get the Byte size
   1.182 +					TInt hashSize = hashCharsPtr->iOutputSize/8;
   1.183 +					
   1.184 +					//Retrieve the final 8bit hash value and convert to 16bit												
   1.185 +					HBufC* hashData = HBufC::NewLC(hashSize);
   1.186 +					TPtr hashPtr = hashData->Des();
   1.187 +					
   1.188 +					//Copy the hashed content into the heap based descriptor
   1.189 +					hashPtr.Copy(hashStr);
   1.190 +					
   1.191 +				 	//Take the 16bit descriptor and convert the string to hexadecimal
   1.192 +					TVariantPtrC convertHash;
   1.193 +					convertHash.Set(hashPtr);
   1.194 +					HBufC* hashResult = convertHash.HexStringLC();
   1.195 +					
   1.196 +					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
   1.197 +					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
   1.198 +					
   1.199 +					//If the returned hash value matches the expected hash, Pass the test									
   1.200 +					if(*hashResult == expectedHash)
   1.201 +						{
   1.202 +						INFO_PRINTF1(_L("*** Hash - Incremental Hash : PASS ***"));
   1.203 +						SetTestStepResult(EPass);	
   1.204 +						}
   1.205 +					else
   1.206 +						{
   1.207 +						ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
   1.208 +						SetTestStepResult(EFail);	
   1.209 +						}
   1.210 +					
   1.211 +					CleanupStack::PopAndDestroy(2, hashData);	// hashResult, hashData				
   1.212 +					}
   1.213 +				
   1.214 +				//Cleanup the Source RFile	
   1.215 +				CleanupStack::PopAndDestroy();	
   1.216 +				}
   1.217 +			
   1.218 +			CleanupStack::PopAndDestroy(&fsSession);
   1.219 +
   1.220 +			fsSession.Close();
   1.221 +			
   1.222 +			CleanupStack::PopAndDestroy(hashImpl);	
   1.223 +			}
   1.224 +		else
   1.225 +			{
   1.226 +			ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
   1.227 +			SetTestStepResult(EFail);	
   1.228 +			}
   1.229 +		}
   1.230 +		
   1.231 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.232 +	return TestStepResult();
   1.233 +	}
   1.234 +
   1.235 +
   1.236 +TVerdict CHashIncrementalStep::doTestStepPostambleL()
   1.237 +	{
   1.238 +	return TestStepResult();
   1.239 +	}