1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/hashbasichashofdatastep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,268 @@
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 "hashbasichashofdatastep.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 +
1.35 +class THashParameters
1.36 + {
1.37 +public:
1.38 + ~THashParameters();
1.39 + TUid iAlgorithmUid;
1.40 + TUid iOperationUid;
1.41 + CHash* iHash;
1.42 + };
1.43 +
1.44 +THashParameters::~THashParameters()
1.45 + {
1.46 + delete iHash;
1.47 + }
1.48 +
1.49 +TInt CreatorThreadEntryPoint(TAny* aParameters)
1.50 + {
1.51 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.52 + if (!cleanup)
1.53 + User::Exit(KErrNoMemory);
1.54 +
1.55 + ASSERT(aParameters);
1.56 + THashParameters* params=static_cast<THashParameters*>(aParameters);
1.57 +
1.58 + TRAPD(result,CHashFactory::CreateHashL(params->iHash,
1.59 + params->iAlgorithmUid,
1.60 + params->iOperationUid,
1.61 + NULL,
1.62 + NULL));
1.63 + delete cleanup;
1.64 + User::Exit(result);
1.65 + return KErrNone;
1.66 + }
1.67 +
1.68 +
1.69 +CHashBasicHashOfDataStep::~CHashBasicHashOfDataStep()
1.70 + {
1.71 + }
1.72 +
1.73 +
1.74 +CHashBasicHashOfDataStep::CHashBasicHashOfDataStep()
1.75 + {
1.76 + SetTestStepName(KHashBasicHashOfDataStep);
1.77 + }
1.78 +
1.79 +
1.80 +TVerdict CHashBasicHashOfDataStep::doTestStepPreambleL()
1.81 + {
1.82 + SetTestStepResult(EPass);
1.83 + return TestStepResult();
1.84 + }
1.85 +
1.86 +
1.87 +TVerdict CHashBasicHashOfDataStep::doTestStepL()
1.88 + {
1.89 + if (TestStepResult()==EPass)
1.90 + {
1.91 +
1.92 + //Assume faliure, unless all is successful
1.93 + SetTestStepResult(EFail);
1.94 +
1.95 + INFO_PRINTF1(_L("*** Hash - Basic Hash of Data ***"));
1.96 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.97 +
1.98 + TVariantPtrC algorithmUid;
1.99 + TVariantPtrC operationModeUid;
1.100 + TPtrC sourcePath;
1.101 + TPtrC expectedHash;
1.102 +
1.103 + //Extract the Test Case ID parameter from the specified INI file
1.104 + if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
1.105 + !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
1.106 + !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
1.107 + !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
1.108 + {
1.109 + ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
1.110 + SetTestStepResult(EFail);
1.111 + }
1.112 + else
1.113 + {
1.114 + TPtrC threadName;
1.115 + CHash* hashImpl=NULL;
1.116 + TInt err=KErrNone;
1.117 + THashParameters params;
1.118 + RThread creatorThread;
1.119 +
1.120 + CleanupClosePushL(creatorThread);
1.121 +
1.122 + if (GetStringFromConfig(ConfigSection(),KConfigThreadName,threadName))
1.123 + {
1.124 +
1.125 + params.iAlgorithmUid=algorithmUid;
1.126 + params.iOperationUid=operationModeUid;
1.127 + params.iHash=NULL;
1.128 +
1.129 +
1.130 + User::LeaveIfError(creatorThread.Create(threadName, CreatorThreadEntryPoint, KDefaultStackSize, NULL, (TAny*)¶ms));
1.131 + TRequestStatus status=KRequestPending;
1.132 + creatorThread.Logon(status);
1.133 + creatorThread.Resume();
1.134 + User::WaitForRequest(status);
1.135 + //creatorThread.Close();
1.136 + hashImpl=params.iHash;
1.137 + err=status.Int();
1.138 + }
1.139 + else
1.140 + {
1.141 + //Retrieve a Hash Factory Object
1.142 + TRAP(err,CHashFactory::CreateHashL(hashImpl,
1.143 + algorithmUid,
1.144 + operationModeUid,
1.145 + NULL,
1.146 + NULL));
1.147 + params.iHash=hashImpl;
1.148 + }
1.149 +
1.150 + if(hashImpl && (err == KErrNone))
1.151 + {
1.152 +
1.153 + //Push the Hash Implementation Object onto the Cleanup Stack
1.154 + //CleanupStack::PushL(hashImpl);
1.155 +
1.156 + RFs fsSession;
1.157 +
1.158 + //Create a connection to the file server
1.159 + err = fsSession.Connect();
1.160 +
1.161 + if(err != KErrNone)
1.162 + {
1.163 + ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
1.164 + SetTestStepResult(EFail);
1.165 + }
1.166 + else
1.167 + {
1.168 + RFile sourceFile;
1.169 + CleanupClosePushL(sourceFile);
1.170 +
1.171 + //Open the specified source file
1.172 + err = sourceFile.Open(fsSession,sourcePath, EFileRead);
1.173 +
1.174 + if(err != KErrNone)
1.175 + {
1.176 + ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
1.177 + SetTestStepResult(EFail);
1.178 + }
1.179 + else
1.180 + {
1.181 + TInt sourceLength = 0;
1.182 + User::LeaveIfError(sourceFile.Size(sourceLength));
1.183 +
1.184 + //Create a heap based descriptor to store the data
1.185 + HBufC8* sourceData = HBufC8::NewL(sourceLength);
1.186 + CleanupStack::PushL(sourceData);
1.187 + TPtr8 sourcePtr = sourceData->Des();
1.188 +
1.189 + sourceFile.Read(sourcePtr);
1.190 +
1.191 + if(sourcePtr.Length() != sourceLength)
1.192 + {
1.193 + ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
1.194 + SetTestStepResult(EFail);
1.195 + }
1.196 + else
1.197 + {
1.198 + //Create a NULL TCharacteristics pointer
1.199 + const TCharacteristics* charsPtr(NULL);
1.200 +
1.201 + //Retrieve the characteristics for the hash implementation object
1.202 + TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
1.203 +
1.204 + //Static cast the characteristics to type THashCharacteristics
1.205 + const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
1.206 +
1.207 + //The hash output size is returned in Bits, divide by 8 to get the Byte size
1.208 + TInt hashSize = hashCharsPtr->iOutputSize/8;
1.209 +
1.210 + //Retrieve the final 8bit hash value and convert to 16bit
1.211 + HBufC* hashData = HBufC::NewLC(hashSize);
1.212 + TPtr hashPtr = hashData->Des();
1.213 +
1.214 + //Copy the hashed content into the heap based descriptor
1.215 + hashPtr.Copy(hashImpl->Hash(*sourceData));
1.216 +
1.217 + //Take the 16bit descriptor and convert the string to hexadecimal
1.218 + TVariantPtrC convertHash;
1.219 + convertHash.Set(hashPtr);
1.220 + HBufC* hashResult = convertHash.HexStringLC();
1.221 +
1.222 + INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
1.223 + INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
1.224 +
1.225 + //If the returned hash value matches the expected hash, Pass the test
1.226 + if(*hashResult == expectedHash)
1.227 + {
1.228 + INFO_PRINTF1(_L("*** Hash - Basic Hash of Data : PASS ***"));
1.229 + SetTestStepResult(EPass);
1.230 + }
1.231 + else
1.232 + {
1.233 + ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
1.234 + SetTestStepResult(EFail);
1.235 + }
1.236 +
1.237 + CleanupStack::PopAndDestroy(hashResult);
1.238 + CleanupStack::PopAndDestroy(hashData);
1.239 + CleanupStack::PopAndDestroy(sourceData);
1.240 + }
1.241 + }
1.242 +
1.243 + //Cleanup the Source RFile
1.244 + CleanupStack::PopAndDestroy();
1.245 + }
1.246 +
1.247 + fsSession.Close();
1.248 +
1.249 + //CleanupStack::PopAndDestroy(hashImpl);
1.250 + }
1.251 + else
1.252 + {
1.253 + ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
1.254 + SetTestStepResult(EFail);
1.255 + }
1.256 +
1.257 + CleanupStack::PopAndDestroy(&creatorThread);
1.258 + }
1.259 + }
1.260 +
1.261 + INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
1.262 +
1.263 + return TestStepResult();
1.264 + }
1.265 +
1.266 +
1.267 +TVerdict CHashBasicHashOfDataStep::doTestStepPostambleL()
1.268 + {
1.269 +
1.270 + return TestStepResult();
1.271 + }