|
1 /* |
|
2 * Copyright (c) 2008-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 "hash_basic_data_step.h" |
|
25 #include <cryptospi/cryptohashapi.h> |
|
26 #include <cryptospi/plugincharacteristics.h> |
|
27 |
|
28 using namespace CryptoSpi; |
|
29 |
|
30 |
|
31 class CHashParam:public CBase |
|
32 { |
|
33 public: |
|
34 ~CHashParam(); |
|
35 TUid iAlgorithmUid; |
|
36 CHash* iHash; |
|
37 }; |
|
38 |
|
39 CHashParam::~CHashParam() |
|
40 { |
|
41 delete iHash; |
|
42 } |
|
43 |
|
44 static TInt CreatorThreadEntryPt(TAny* aParameters) |
|
45 { |
|
46 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
47 if (!cleanup) |
|
48 User::Exit(KErrNoMemory); |
|
49 |
|
50 ASSERT(aParameters); |
|
51 CHashParam* params=static_cast<CHashParam*>(aParameters); |
|
52 |
|
53 TRAPD(result,CHashFactory::CreateHashL(params->iHash, |
|
54 params->iAlgorithmUid, |
|
55 NULL)); |
|
56 delete cleanup; |
|
57 return result; |
|
58 } |
|
59 |
|
60 |
|
61 CHashBasicDataStep::~CHashBasicDataStep() |
|
62 { |
|
63 } |
|
64 |
|
65 |
|
66 CHashBasicDataStep::CHashBasicDataStep() |
|
67 { |
|
68 SetTestStepName(KHashBasicDataStep); |
|
69 } |
|
70 |
|
71 |
|
72 TVerdict CHashBasicDataStep::doTestStepPreambleL() |
|
73 { |
|
74 return EPass; |
|
75 } |
|
76 |
|
77 |
|
78 TVerdict CHashBasicDataStep::doTestStepL() |
|
79 { |
|
80 //Assume faliure, unless all is successful |
|
81 SetTestStepResult(EFail); |
|
82 |
|
83 INFO_PRINTF1(_L("*** Hash - Basic Data ***")); |
|
84 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
85 |
|
86 TVariantPtrC algorithmUid; |
|
87 TPtrC sourcePath; |
|
88 TPtrC expectedHash; |
|
89 |
|
90 //Extract the Test Case ID parameter from the specified INI file |
|
91 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) || |
|
92 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) || |
|
93 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash)) |
|
94 { |
|
95 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **")); |
|
96 User::Leave(KErrNotFound); |
|
97 } |
|
98 |
|
99 TPtrC threadName; |
|
100 CHash* hashImpl=NULL; |
|
101 TInt err=KErrNone; |
|
102 CHashParam* params = new (ELeave) CHashParam(); |
|
103 CleanupStack::PushL(params); |
|
104 |
|
105 if (GetStringFromConfig(ConfigSection(),KConfigThreadName,threadName)) |
|
106 { |
|
107 RThread creatorThread; |
|
108 |
|
109 CleanupClosePushL(creatorThread); |
|
110 params->iAlgorithmUid=algorithmUid; |
|
111 params->iHash=NULL; |
|
112 |
|
113 User::LeaveIfError(creatorThread.Create(threadName, CreatorThreadEntryPt, KDefaultStackSize, NULL, (TAny*)params)); |
|
114 TRequestStatus status=KRequestPending; |
|
115 creatorThread.Logon(status); |
|
116 creatorThread.Resume(); |
|
117 User::WaitForRequest(status); |
|
118 hashImpl=params->iHash; |
|
119 err=status.Int(); |
|
120 } |
|
121 else |
|
122 { |
|
123 //Retrieve a Hash Factory Object |
|
124 TRAP(err,CHashFactory::CreateHashL(hashImpl, |
|
125 algorithmUid, |
|
126 NULL)); |
|
127 } |
|
128 |
|
129 if(err == KErrNone) |
|
130 { |
|
131 RFs fsSession; |
|
132 |
|
133 //Create a connection to the file server |
|
134 User::LeaveIfError(fsSession.Connect()); |
|
135 |
|
136 CleanupClosePushL(fsSession); |
|
137 |
|
138 RFile sourceFile; |
|
139 |
|
140 //Open the specified source file |
|
141 User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead)); |
|
142 CleanupClosePushL(sourceFile); |
|
143 |
|
144 TInt sourceLength = 0; |
|
145 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
146 |
|
147 //Create a heap based descriptor to store the data |
|
148 HBufC8* sourceData = HBufC8::NewL(sourceLength); |
|
149 CleanupStack::PushL(sourceData); |
|
150 TPtr8 sourcePtr = sourceData->Des(); |
|
151 |
|
152 sourceFile.Read(sourcePtr); |
|
153 |
|
154 if(sourcePtr.Length() != sourceLength) |
|
155 { |
|
156 ERR_PRINTF1(_L("*** Error: Reading Source File ***")); |
|
157 SetTestStepResult(EFail); |
|
158 } |
|
159 else |
|
160 { |
|
161 //Create a NULL TCharacteristics pointer |
|
162 const TCharacteristics* charsPtr(NULL); |
|
163 |
|
164 //Retrieve the characteristics for the hash implementation object |
|
165 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr)); |
|
166 |
|
167 //Static cast the characteristics to type THashCharacteristics |
|
168 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr); |
|
169 |
|
170 //The hash output size is returned in Bits, divide by 8 to get the Byte size |
|
171 TInt hashSize = hashCharsPtr->iOutputSize/8; |
|
172 |
|
173 //Retrieve the final 8bit hash value and convert to 16bit |
|
174 HBufC* hashData = HBufC::NewLC(hashSize); |
|
175 TPtr hashPtr = hashData->Des(); |
|
176 |
|
177 // This is just a simple performance measurement on generating |
|
178 // the hash |
|
179 TTime startTime; |
|
180 startTime.UniversalTime(); |
|
181 |
|
182 //Copy the hashed content into the heap based descriptor |
|
183 hashPtr.Copy(hashImpl->Hash(*sourceData)); |
|
184 |
|
185 TTime endTime; |
|
186 endTime.UniversalTime(); |
|
187 TTimeIntervalMicroSeconds time = endTime.MicroSecondsFrom(startTime); |
|
188 |
|
189 //Take the 16bit descriptor and convert the string to hexadecimal |
|
190 TVariantPtrC convertHash; |
|
191 convertHash.Set(hashPtr); |
|
192 HBufC* hashResult = convertHash.HexStringLC(); |
|
193 |
|
194 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult); |
|
195 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); |
|
196 |
|
197 //If the returned hash value matches the expected hash, Pass the test |
|
198 if(*hashResult == expectedHash) |
|
199 { |
|
200 INFO_PRINTF2(_L("*** Hash - Basic Data generated after setup in %d micro sec.: PASS ***"), time.Int64()); |
|
201 SetTestStepResult(EPass); |
|
202 } |
|
203 else |
|
204 { |
|
205 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); |
|
206 SetTestStepResult(EFail); |
|
207 } |
|
208 |
|
209 CleanupStack::PopAndDestroy(3, sourceData); // hashResult, hashData, sourceData |
|
210 } |
|
211 |
|
212 CleanupStack::PopAndDestroy(2, &fsSession); // &fsSession, sourceFile |
|
213 |
|
214 if (threadName.Ptr() != NULL) |
|
215 { |
|
216 CleanupStack::PopAndDestroy(); // &creatorThread |
|
217 } |
|
218 } |
|
219 else |
|
220 { |
|
221 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err); |
|
222 SetTestStepResult(EFail); |
|
223 } |
|
224 |
|
225 CleanupStack::PopAndDestroy(); //params |
|
226 |
|
227 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
228 |
|
229 return TestStepResult(); |
|
230 } |
|
231 |
|
232 |
|
233 TVerdict CHashBasicDataStep::doTestStepPostambleL() |
|
234 { |
|
235 return TestStepResult(); |
|
236 } |