1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hashincrementalhashstep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,238 @@
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 "hashincrementalhashstep.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 +CHashIncrementalHashStep::~CHashIncrementalHashStep()
1.35 + {
1.36 + }
1.37 +
1.38 +
1.39 +CHashIncrementalHashStep::CHashIncrementalHashStep()
1.40 + {
1.41 + SetTestStepName(KHashIncrementalHashStep);
1.42 + }
1.43 +
1.44 +
1.45 +TVerdict CHashIncrementalHashStep::doTestStepPreambleL()
1.46 + {
1.47 + SetTestStepResult(EPass);
1.48 + return TestStepResult();
1.49 + }
1.50 +
1.51 +
1.52 +TVerdict CHashIncrementalHashStep::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 ***"));
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 + TPtrC8 hashStr;
1.126 +
1.127 + User::LeaveIfError(sourceFile.Size(sourceLength));
1.128 +
1.129 + //Divide the total size of the source file up into individual equal sized blocks to read
1.130 + //over several increments
1.131 + readIncrement = sourceLength/KDataReadBlocks;
1.132 +
1.133 + do
1.134 + {
1.135 + //Create a heap based descriptor to store the data
1.136 + HBufC8* sourceData = HBufC8::NewL(readIncrement);
1.137 + CleanupStack::PushL(sourceData);
1.138 + TPtr8 sourcePtr = sourceData->Des();
1.139 +
1.140 + //Read in a block of data from the source file from the current position
1.141 + err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
1.142 +
1.143 + //Update the read position by adding the number of bytes read
1.144 + readPosition += readIncrement;
1.145 +
1.146 + if(readPosition == readIncrement)
1.147 + {
1.148 + //Read in the first block from the data file into the Hash implementation object
1.149 + hashImpl->Hash(*sourceData);
1.150 + INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
1.151 + }
1.152 + else if(readPosition >= sourceLength)
1.153 + {
1.154 + //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
1.155 + hashStr.Set(hashImpl->Final(*sourceData));
1.156 +
1.157 + //Sets the Complete Flag to ETrue in order to drop out of the loop
1.158 + hashComplete = ETrue;
1.159 +
1.160 + TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
1.161 + INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
1.162 + }
1.163 + else
1.164 + {
1.165 + //Update the message data within the Hash object with the new block
1.166 + hashImpl->Update(*sourceData);
1.167 + INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);
1.168 + }
1.169 +
1.170 + CleanupStack::PopAndDestroy(sourceData);
1.171 +
1.172 + }while(hashComplete == EFalse);
1.173 +
1.174 + //Create a NULL TCharacteristics pointer
1.175 + const TCharacteristics* charsPtr(NULL);
1.176 +
1.177 + //Retrieve the characteristics for the hash implementation object
1.178 + TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
1.179 +
1.180 + //Static cast the characteristics to type THashCharacteristics
1.181 + const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
1.182 +
1.183 + //The hash output size is returned in Bits, divide by 8 to get the Byte size
1.184 + TInt hashSize = hashCharsPtr->iOutputSize/8;
1.185 +
1.186 + //Retrieve the final 8bit hash value and convert to 16bit
1.187 + HBufC* hashData = HBufC::NewLC(hashSize);
1.188 + TPtr hashPtr = hashData->Des();
1.189 +
1.190 + //Copy the hashed content into the heap based descriptor
1.191 + hashPtr.Copy(hashStr);
1.192 +
1.193 + //Take the 16bit descriptor and convert the string to hexadecimal
1.194 + TVariantPtrC convertHash;
1.195 + convertHash.Set(hashPtr);
1.196 + HBufC* hashResult = convertHash.HexStringLC();
1.197 +
1.198 + INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
1.199 + INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
1.200 +
1.201 + //If the returned hash value matches the expected hash, Pass the test
1.202 + if(*hashResult == expectedHash)
1.203 + {
1.204 + INFO_PRINTF1(_L("*** Hash - Incremental Hash : PASS ***"));
1.205 + SetTestStepResult(EPass);
1.206 + }
1.207 + else
1.208 + {
1.209 + ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
1.210 + SetTestStepResult(EFail);
1.211 + }
1.212 +
1.213 + CleanupStack::PopAndDestroy(hashResult);
1.214 + CleanupStack::PopAndDestroy(hashData);
1.215 + }
1.216 +
1.217 + //Cleanup the Source RFile
1.218 + CleanupStack::PopAndDestroy();
1.219 + }
1.220 +
1.221 + fsSession.Close();
1.222 +
1.223 + CleanupStack::PopAndDestroy(hashImpl);
1.224 + }
1.225 + else
1.226 + {
1.227 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
1.228 + SetTestStepResult(EFail);
1.229 + }
1.230 + }
1.231 + }
1.232 +
1.233 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.234 + return TestStepResult();
1.235 + }
1.236 +
1.237 +
1.238 +TVerdict CHashIncrementalHashStep::doTestStepPostambleL()
1.239 + {
1.240 + return TestStepResult();
1.241 + }