sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* Example CTestStep derived implementation
|
sl@0
|
16 |
*
|
sl@0
|
17 |
*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
/**
|
sl@0
|
21 |
@file
|
sl@0
|
22 |
@internalTechnology
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
#include "hashbasichashofdatastep.h"
|
sl@0
|
25 |
|
sl@0
|
26 |
#include <cryptospi/cryptohashapi.h>
|
sl@0
|
27 |
#include <cryptospi/plugincharacteristics.h>
|
sl@0
|
28 |
|
sl@0
|
29 |
using namespace CryptoSpi;
|
sl@0
|
30 |
|
sl@0
|
31 |
|
sl@0
|
32 |
class THashParameters
|
sl@0
|
33 |
{
|
sl@0
|
34 |
public:
|
sl@0
|
35 |
~THashParameters();
|
sl@0
|
36 |
TUid iAlgorithmUid;
|
sl@0
|
37 |
TUid iOperationUid;
|
sl@0
|
38 |
CHash* iHash;
|
sl@0
|
39 |
};
|
sl@0
|
40 |
|
sl@0
|
41 |
THashParameters::~THashParameters()
|
sl@0
|
42 |
{
|
sl@0
|
43 |
delete iHash;
|
sl@0
|
44 |
}
|
sl@0
|
45 |
|
sl@0
|
46 |
TInt CreatorThreadEntryPoint(TAny* aParameters)
|
sl@0
|
47 |
{
|
sl@0
|
48 |
CTrapCleanup* cleanup = CTrapCleanup::New();
|
sl@0
|
49 |
if (!cleanup)
|
sl@0
|
50 |
User::Exit(KErrNoMemory);
|
sl@0
|
51 |
|
sl@0
|
52 |
ASSERT(aParameters);
|
sl@0
|
53 |
THashParameters* params=static_cast<THashParameters*>(aParameters);
|
sl@0
|
54 |
|
sl@0
|
55 |
TRAPD(result,CHashFactory::CreateHashL(params->iHash,
|
sl@0
|
56 |
params->iAlgorithmUid,
|
sl@0
|
57 |
params->iOperationUid,
|
sl@0
|
58 |
NULL,
|
sl@0
|
59 |
NULL));
|
sl@0
|
60 |
delete cleanup;
|
sl@0
|
61 |
User::Exit(result);
|
sl@0
|
62 |
return KErrNone;
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
|
sl@0
|
66 |
CHashBasicHashOfDataStep::~CHashBasicHashOfDataStep()
|
sl@0
|
67 |
{
|
sl@0
|
68 |
}
|
sl@0
|
69 |
|
sl@0
|
70 |
|
sl@0
|
71 |
CHashBasicHashOfDataStep::CHashBasicHashOfDataStep()
|
sl@0
|
72 |
{
|
sl@0
|
73 |
SetTestStepName(KHashBasicHashOfDataStep);
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
|
sl@0
|
77 |
TVerdict CHashBasicHashOfDataStep::doTestStepPreambleL()
|
sl@0
|
78 |
{
|
sl@0
|
79 |
SetTestStepResult(EPass);
|
sl@0
|
80 |
return TestStepResult();
|
sl@0
|
81 |
}
|
sl@0
|
82 |
|
sl@0
|
83 |
|
sl@0
|
84 |
TVerdict CHashBasicHashOfDataStep::doTestStepL()
|
sl@0
|
85 |
{
|
sl@0
|
86 |
if (TestStepResult()==EPass)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
|
sl@0
|
89 |
//Assume faliure, unless all is successful
|
sl@0
|
90 |
SetTestStepResult(EFail);
|
sl@0
|
91 |
|
sl@0
|
92 |
INFO_PRINTF1(_L("*** Hash - Basic Hash of Data ***"));
|
sl@0
|
93 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
94 |
|
sl@0
|
95 |
TVariantPtrC algorithmUid;
|
sl@0
|
96 |
TVariantPtrC operationModeUid;
|
sl@0
|
97 |
TPtrC sourcePath;
|
sl@0
|
98 |
TPtrC expectedHash;
|
sl@0
|
99 |
|
sl@0
|
100 |
//Extract the Test Case ID parameter from the specified INI file
|
sl@0
|
101 |
if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
|
sl@0
|
102 |
!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
|
sl@0
|
103 |
!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
|
sl@0
|
104 |
!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
|
sl@0
|
105 |
{
|
sl@0
|
106 |
ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
|
sl@0
|
107 |
SetTestStepResult(EFail);
|
sl@0
|
108 |
}
|
sl@0
|
109 |
else
|
sl@0
|
110 |
{
|
sl@0
|
111 |
TPtrC threadName;
|
sl@0
|
112 |
CHash* hashImpl=NULL;
|
sl@0
|
113 |
TInt err=KErrNone;
|
sl@0
|
114 |
THashParameters params;
|
sl@0
|
115 |
RThread creatorThread;
|
sl@0
|
116 |
|
sl@0
|
117 |
CleanupClosePushL(creatorThread);
|
sl@0
|
118 |
|
sl@0
|
119 |
if (GetStringFromConfig(ConfigSection(),KConfigThreadName,threadName))
|
sl@0
|
120 |
{
|
sl@0
|
121 |
|
sl@0
|
122 |
params.iAlgorithmUid=algorithmUid;
|
sl@0
|
123 |
params.iOperationUid=operationModeUid;
|
sl@0
|
124 |
params.iHash=NULL;
|
sl@0
|
125 |
|
sl@0
|
126 |
|
sl@0
|
127 |
User::LeaveIfError(creatorThread.Create(threadName, CreatorThreadEntryPoint, KDefaultStackSize, NULL, (TAny*)¶ms));
|
sl@0
|
128 |
TRequestStatus status=KRequestPending;
|
sl@0
|
129 |
creatorThread.Logon(status);
|
sl@0
|
130 |
creatorThread.Resume();
|
sl@0
|
131 |
User::WaitForRequest(status);
|
sl@0
|
132 |
//creatorThread.Close();
|
sl@0
|
133 |
hashImpl=params.iHash;
|
sl@0
|
134 |
err=status.Int();
|
sl@0
|
135 |
}
|
sl@0
|
136 |
else
|
sl@0
|
137 |
{
|
sl@0
|
138 |
//Retrieve a Hash Factory Object
|
sl@0
|
139 |
TRAP(err,CHashFactory::CreateHashL(hashImpl,
|
sl@0
|
140 |
algorithmUid,
|
sl@0
|
141 |
operationModeUid,
|
sl@0
|
142 |
NULL,
|
sl@0
|
143 |
NULL));
|
sl@0
|
144 |
params.iHash=hashImpl;
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
if(hashImpl && (err == KErrNone))
|
sl@0
|
148 |
{
|
sl@0
|
149 |
|
sl@0
|
150 |
//Push the Hash Implementation Object onto the Cleanup Stack
|
sl@0
|
151 |
//CleanupStack::PushL(hashImpl);
|
sl@0
|
152 |
|
sl@0
|
153 |
RFs fsSession;
|
sl@0
|
154 |
|
sl@0
|
155 |
//Create a connection to the file server
|
sl@0
|
156 |
err = fsSession.Connect();
|
sl@0
|
157 |
|
sl@0
|
158 |
if(err != KErrNone)
|
sl@0
|
159 |
{
|
sl@0
|
160 |
ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
|
sl@0
|
161 |
SetTestStepResult(EFail);
|
sl@0
|
162 |
}
|
sl@0
|
163 |
else
|
sl@0
|
164 |
{
|
sl@0
|
165 |
RFile sourceFile;
|
sl@0
|
166 |
CleanupClosePushL(sourceFile);
|
sl@0
|
167 |
|
sl@0
|
168 |
//Open the specified source file
|
sl@0
|
169 |
err = sourceFile.Open(fsSession,sourcePath, EFileRead);
|
sl@0
|
170 |
|
sl@0
|
171 |
if(err != KErrNone)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
|
sl@0
|
174 |
SetTestStepResult(EFail);
|
sl@0
|
175 |
}
|
sl@0
|
176 |
else
|
sl@0
|
177 |
{
|
sl@0
|
178 |
TInt sourceLength = 0;
|
sl@0
|
179 |
User::LeaveIfError(sourceFile.Size(sourceLength));
|
sl@0
|
180 |
|
sl@0
|
181 |
//Create a heap based descriptor to store the data
|
sl@0
|
182 |
HBufC8* sourceData = HBufC8::NewL(sourceLength);
|
sl@0
|
183 |
CleanupStack::PushL(sourceData);
|
sl@0
|
184 |
TPtr8 sourcePtr = sourceData->Des();
|
sl@0
|
185 |
|
sl@0
|
186 |
sourceFile.Read(sourcePtr);
|
sl@0
|
187 |
|
sl@0
|
188 |
if(sourcePtr.Length() != sourceLength)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
|
sl@0
|
191 |
SetTestStepResult(EFail);
|
sl@0
|
192 |
}
|
sl@0
|
193 |
else
|
sl@0
|
194 |
{
|
sl@0
|
195 |
//Create a NULL TCharacteristics pointer
|
sl@0
|
196 |
const TCharacteristics* charsPtr(NULL);
|
sl@0
|
197 |
|
sl@0
|
198 |
//Retrieve the characteristics for the hash implementation object
|
sl@0
|
199 |
TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
|
sl@0
|
200 |
|
sl@0
|
201 |
//Static cast the characteristics to type THashCharacteristics
|
sl@0
|
202 |
const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
|
sl@0
|
203 |
|
sl@0
|
204 |
//The hash output size is returned in Bits, divide by 8 to get the Byte size
|
sl@0
|
205 |
TInt hashSize = hashCharsPtr->iOutputSize/8;
|
sl@0
|
206 |
|
sl@0
|
207 |
//Retrieve the final 8bit hash value and convert to 16bit
|
sl@0
|
208 |
HBufC* hashData = HBufC::NewLC(hashSize);
|
sl@0
|
209 |
TPtr hashPtr = hashData->Des();
|
sl@0
|
210 |
|
sl@0
|
211 |
//Copy the hashed content into the heap based descriptor
|
sl@0
|
212 |
hashPtr.Copy(hashImpl->Hash(*sourceData));
|
sl@0
|
213 |
|
sl@0
|
214 |
//Take the 16bit descriptor and convert the string to hexadecimal
|
sl@0
|
215 |
TVariantPtrC convertHash;
|
sl@0
|
216 |
convertHash.Set(hashPtr);
|
sl@0
|
217 |
HBufC* hashResult = convertHash.HexStringLC();
|
sl@0
|
218 |
|
sl@0
|
219 |
INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
|
sl@0
|
220 |
INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
|
sl@0
|
221 |
|
sl@0
|
222 |
//If the returned hash value matches the expected hash, Pass the test
|
sl@0
|
223 |
if(*hashResult == expectedHash)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
INFO_PRINTF1(_L("*** Hash - Basic Hash of Data : PASS ***"));
|
sl@0
|
226 |
SetTestStepResult(EPass);
|
sl@0
|
227 |
}
|
sl@0
|
228 |
else
|
sl@0
|
229 |
{
|
sl@0
|
230 |
ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err);
|
sl@0
|
231 |
SetTestStepResult(EFail);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|
sl@0
|
234 |
CleanupStack::PopAndDestroy(hashResult);
|
sl@0
|
235 |
CleanupStack::PopAndDestroy(hashData);
|
sl@0
|
236 |
CleanupStack::PopAndDestroy(sourceData);
|
sl@0
|
237 |
}
|
sl@0
|
238 |
}
|
sl@0
|
239 |
|
sl@0
|
240 |
//Cleanup the Source RFile
|
sl@0
|
241 |
CleanupStack::PopAndDestroy();
|
sl@0
|
242 |
}
|
sl@0
|
243 |
|
sl@0
|
244 |
fsSession.Close();
|
sl@0
|
245 |
|
sl@0
|
246 |
//CleanupStack::PopAndDestroy(hashImpl);
|
sl@0
|
247 |
}
|
sl@0
|
248 |
else
|
sl@0
|
249 |
{
|
sl@0
|
250 |
ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
|
sl@0
|
251 |
SetTestStepResult(EFail);
|
sl@0
|
252 |
}
|
sl@0
|
253 |
|
sl@0
|
254 |
CleanupStack::PopAndDestroy(&creatorThread);
|
sl@0
|
255 |
}
|
sl@0
|
256 |
}
|
sl@0
|
257 |
|
sl@0
|
258 |
INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
|
sl@0
|
259 |
|
sl@0
|
260 |
return TestStepResult();
|
sl@0
|
261 |
}
|
sl@0
|
262 |
|
sl@0
|
263 |
|
sl@0
|
264 |
TVerdict CHashBasicHashOfDataStep::doTestStepPostambleL()
|
sl@0
|
265 |
{
|
sl@0
|
266 |
|
sl@0
|
267 |
return TestStepResult();
|
sl@0
|
268 |
}
|