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