os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithreplicatestep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashwithreplicatestep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,276 @@
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 "hashincrementalhashwithreplicatestep.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 +CHashIncrementalHashWithReplicateStep::~CHashIncrementalHashWithReplicateStep()
1.35 + {
1.36 + }
1.37 +
1.38 +
1.39 +CHashIncrementalHashWithReplicateStep::CHashIncrementalHashWithReplicateStep()
1.40 + {
1.41 + SetTestStepName(KHashIncrementalHashWithReplicateStep);
1.42 + }
1.43 +
1.44 +
1.45 +TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPreambleL()
1.46 + {
1.47 + SetTestStepResult(EPass);
1.48 + return TestStepResult();
1.49 + }
1.50 +
1.51 +
1.52 +TVerdict CHashIncrementalHashWithReplicateStep::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 Replicate ***"));
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 hashReplicated = EFalse;
1.126 + TPtrC8 hashStr;
1.127 +
1.128 + CHash* hashReplicateImpl = 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 + if(hashReplicated == EFalse)
1.153 + {
1.154 + hashImpl->Hash(*sourceData);
1.155 + INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
1.156 + }
1.157 + else
1.158 + {
1.159 + hashReplicateImpl->Hash(*sourceData);
1.160 + INFO_PRINTF2(_L("Intial Hash (Replicate) - Bytes Read: %d"), readPosition);
1.161 + }
1.162 + }
1.163 + else if(readPosition >= sourceLength)
1.164 + {
1.165 + //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
1.166 + hashStr.Set(hashReplicateImpl->Final(*sourceData));
1.167 +
1.168 + //Sets the Complete Flag to ETrue in order to drop out of the loop
1.169 + hashComplete = ETrue;
1.170 +
1.171 + TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
1.172 + INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
1.173 + }
1.174 + //If the read position is half the source length and the implementation
1.175 + //object hasn't already been replicated
1.176 + else if((readPosition >= sourceLength/2) && (hashReplicated == EFalse))
1.177 + {
1.178 + INFO_PRINTF1(_L("Replicating Hash Object..."));
1.179 +
1.180 + //Create a Copy of the existing Hash Object with NO internal message state
1.181 + hashReplicateImpl = hashImpl->ReplicateL();
1.182 +
1.183 + hashReplicated = ETrue;
1.184 +
1.185 + //Sets the read position back to 0 inorder to restart the file read from the beginning
1.186 + readPosition =0;
1.187 +
1.188 + INFO_PRINTF2(_L("*** HASH REPLICATE - Bytes Read: %d ***"), readPosition);
1.189 + }
1.190 + else
1.191 + {
1.192 + //Update the message data within the Hash object with the new block
1.193 + if(hashReplicated == EFalse)
1.194 + {
1.195 + hashImpl->Update(*sourceData);
1.196 + INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
1.197 + }
1.198 + else
1.199 + {
1.200 + hashReplicateImpl->Update(*sourceData);
1.201 + INFO_PRINTF2(_L("Hash Update (Replicate) - Bytes Read: %d"), readPosition);
1.202 + }
1.203 + }
1.204 +
1.205 + CleanupStack::PopAndDestroy(sourceData);
1.206 +
1.207 + }while(hashComplete == EFalse);
1.208 +
1.209 + //Create a NULL TCharacteristics pointer
1.210 + const TCharacteristics* charsPtr(NULL);
1.211 +
1.212 + //Retrieve the characteristics for the hash implementation object
1.213 + TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
1.214 +
1.215 + //Static cast the characteristics to type THashCharacteristics
1.216 + const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
1.217 +
1.218 + //The hash output size is returned in Bits, divide by 8 to get the Byte size
1.219 + TInt hashSize = hashCharsPtr->iOutputSize/8;
1.220 +
1.221 + //Retrieve the final 8bit hash value and convert to 16bit
1.222 + HBufC* hashData = HBufC::NewLC(hashSize);
1.223 + TPtr hashPtr = hashData->Des();
1.224 +
1.225 + //Copy the hashed content into the heap based descriptor
1.226 + hashPtr.Copy(hashStr);
1.227 +
1.228 + //Take the 16bit descriptor and convert the string to hexadecimal
1.229 + TVariantPtrC convertHash;
1.230 + convertHash.Set(hashPtr);
1.231 + HBufC* hashResult = convertHash.HexStringLC();
1.232 +
1.233 + INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
1.234 + INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
1.235 +
1.236 + //If the returned hash value matches the expected hash, Pass the test
1.237 + if(*hashResult == expectedHash)
1.238 + {
1.239 + INFO_PRINTF1(_L("*** Hash - Incremental Hash with Replicate : PASS ***"));
1.240 + SetTestStepResult(EPass);
1.241 + }
1.242 + else
1.243 + {
1.244 + ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
1.245 + SetTestStepResult(EFail);
1.246 + }
1.247 +
1.248 + CleanupStack::PopAndDestroy(hashResult);
1.249 + CleanupStack::PopAndDestroy(hashData);
1.250 +
1.251 + delete hashReplicateImpl;
1.252 + }
1.253 +
1.254 + //Cleanup the Source RFile
1.255 + CleanupStack::PopAndDestroy();
1.256 + }
1.257 +
1.258 + fsSession.Close();
1.259 +
1.260 + CleanupStack::PopAndDestroy(hashImpl);
1.261 + }
1.262 + else
1.263 + {
1.264 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
1.265 + SetTestStepResult(EFail);
1.266 + }
1.267 + }
1.268 + }
1.269 +
1.270 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.271 + return TestStepResult();
1.272 + }
1.273 +
1.274 +
1.275 +TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPostambleL()
1.276 + {
1.277 +
1.278 + return TestStepResult();
1.279 + }