|
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 "hashincrementalhashwithreplicatestep.h" |
|
25 |
|
26 #include <cryptospi/cryptohashapi.h> |
|
27 #include <cryptospi/plugincharacteristics.h> |
|
28 |
|
29 using namespace CryptoSpi; |
|
30 |
|
31 CHashIncrementalHashWithReplicateStep::~CHashIncrementalHashWithReplicateStep() |
|
32 { |
|
33 } |
|
34 |
|
35 |
|
36 CHashIncrementalHashWithReplicateStep::CHashIncrementalHashWithReplicateStep() |
|
37 { |
|
38 SetTestStepName(KHashIncrementalHashWithReplicateStep); |
|
39 } |
|
40 |
|
41 |
|
42 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPreambleL() |
|
43 { |
|
44 SetTestStepResult(EPass); |
|
45 return TestStepResult(); |
|
46 } |
|
47 |
|
48 |
|
49 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepL() |
|
50 { |
|
51 if (TestStepResult()==EPass) |
|
52 { |
|
53 |
|
54 //Assume faliure, unless all is successful |
|
55 SetTestStepResult(EFail); |
|
56 |
|
57 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Replicate ***")); |
|
58 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
59 |
|
60 TVariantPtrC algorithmUid; |
|
61 TVariantPtrC operationModeUid; |
|
62 TPtrC sourcePath; |
|
63 TPtrC expectedHash; |
|
64 |
|
65 //Extract the Test Case ID parameter from the specified INI file |
|
66 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) || |
|
67 !GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) || |
|
68 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) || |
|
69 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash)) |
|
70 { |
|
71 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **")); |
|
72 SetTestStepResult(EFail); |
|
73 } |
|
74 else |
|
75 { |
|
76 //Create a pointer for the Hash Implementation Object |
|
77 CHash* hashImpl = NULL; |
|
78 |
|
79 //Retrieve a Hash Factory Object |
|
80 TRAPD(err,CHashFactory::CreateHashL(hashImpl, |
|
81 algorithmUid, |
|
82 operationModeUid, |
|
83 NULL, |
|
84 NULL)); |
|
85 |
|
86 if(hashImpl && (err == KErrNone)) |
|
87 { |
|
88 |
|
89 //Push the Hash Implementation Object onto the Cleanup Stack |
|
90 CleanupStack::PushL(hashImpl); |
|
91 |
|
92 RFs fsSession; |
|
93 |
|
94 //Create a connection to the file server |
|
95 err = fsSession.Connect(); |
|
96 |
|
97 if(err != KErrNone) |
|
98 { |
|
99 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err); |
|
100 SetTestStepResult(EFail); |
|
101 } |
|
102 else |
|
103 { |
|
104 RFile sourceFile; |
|
105 CleanupClosePushL(sourceFile); |
|
106 |
|
107 //Open the specified source file |
|
108 err = sourceFile.Open(fsSession,sourcePath, EFileRead); |
|
109 |
|
110 if(err != KErrNone) |
|
111 { |
|
112 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err); |
|
113 SetTestStepResult(EFail); |
|
114 } |
|
115 else |
|
116 { |
|
117 |
|
118 TInt sourceLength = 0; |
|
119 TInt readPosition = 0; |
|
120 TInt readIncrement = 0; |
|
121 TBool hashComplete = EFalse; |
|
122 TBool hashReplicated = EFalse; |
|
123 TPtrC8 hashStr; |
|
124 |
|
125 CHash* hashReplicateImpl = NULL; |
|
126 |
|
127 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
128 |
|
129 //Divide the total size of the source file up into individual equal sized blocks to read |
|
130 //over several increments |
|
131 readIncrement = sourceLength/KDataReadBlocks; |
|
132 |
|
133 do |
|
134 { |
|
135 //Create a heap based descriptor to store the data |
|
136 HBufC8* sourceData = HBufC8::NewL(readIncrement); |
|
137 CleanupStack::PushL(sourceData); |
|
138 TPtr8 sourcePtr = sourceData->Des(); |
|
139 |
|
140 //Read in a block of data from the source file from the current position |
|
141 err = sourceFile.Read(readPosition,sourcePtr,readIncrement); |
|
142 |
|
143 //Update the read position by adding the number of bytes read |
|
144 readPosition += readIncrement; |
|
145 |
|
146 if(readPosition == readIncrement) |
|
147 { |
|
148 //Read in the first block from the data file into the Hash implementation object |
|
149 if(hashReplicated == EFalse) |
|
150 { |
|
151 hashImpl->Hash(*sourceData); |
|
152 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition); |
|
153 } |
|
154 else |
|
155 { |
|
156 hashReplicateImpl->Hash(*sourceData); |
|
157 INFO_PRINTF2(_L("Intial Hash (Replicate) - Bytes Read: %d"), readPosition); |
|
158 } |
|
159 } |
|
160 else if(readPosition >= sourceLength) |
|
161 { |
|
162 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8 |
|
163 hashStr.Set(hashReplicateImpl->Final(*sourceData)); |
|
164 |
|
165 //Sets the Complete Flag to ETrue in order to drop out of the loop |
|
166 hashComplete = ETrue; |
|
167 |
|
168 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length(); |
|
169 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead); |
|
170 } |
|
171 //If the read position is half the source length and the implementation |
|
172 //object hasn't already been replicated |
|
173 else if((readPosition >= sourceLength/2) && (hashReplicated == EFalse)) |
|
174 { |
|
175 INFO_PRINTF1(_L("Replicating Hash Object...")); |
|
176 |
|
177 //Create a Copy of the existing Hash Object with NO internal message state |
|
178 hashReplicateImpl = hashImpl->ReplicateL(); |
|
179 |
|
180 hashReplicated = ETrue; |
|
181 |
|
182 //Sets the read position back to 0 inorder to restart the file read from the beginning |
|
183 readPosition =0; |
|
184 |
|
185 INFO_PRINTF2(_L("*** HASH REPLICATE - Bytes Read: %d ***"), readPosition); |
|
186 } |
|
187 else |
|
188 { |
|
189 //Update the message data within the Hash object with the new block |
|
190 if(hashReplicated == EFalse) |
|
191 { |
|
192 hashImpl->Update(*sourceData); |
|
193 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition); |
|
194 } |
|
195 else |
|
196 { |
|
197 hashReplicateImpl->Update(*sourceData); |
|
198 INFO_PRINTF2(_L("Hash Update (Replicate) - Bytes Read: %d"), readPosition); |
|
199 } |
|
200 } |
|
201 |
|
202 CleanupStack::PopAndDestroy(sourceData); |
|
203 |
|
204 }while(hashComplete == EFalse); |
|
205 |
|
206 //Create a NULL TCharacteristics pointer |
|
207 const TCharacteristics* charsPtr(NULL); |
|
208 |
|
209 //Retrieve the characteristics for the hash implementation object |
|
210 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr)); |
|
211 |
|
212 //Static cast the characteristics to type THashCharacteristics |
|
213 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr); |
|
214 |
|
215 //The hash output size is returned in Bits, divide by 8 to get the Byte size |
|
216 TInt hashSize = hashCharsPtr->iOutputSize/8; |
|
217 |
|
218 //Retrieve the final 8bit hash value and convert to 16bit |
|
219 HBufC* hashData = HBufC::NewLC(hashSize); |
|
220 TPtr hashPtr = hashData->Des(); |
|
221 |
|
222 //Copy the hashed content into the heap based descriptor |
|
223 hashPtr.Copy(hashStr); |
|
224 |
|
225 //Take the 16bit descriptor and convert the string to hexadecimal |
|
226 TVariantPtrC convertHash; |
|
227 convertHash.Set(hashPtr); |
|
228 HBufC* hashResult = convertHash.HexStringLC(); |
|
229 |
|
230 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult); |
|
231 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); |
|
232 |
|
233 //If the returned hash value matches the expected hash, Pass the test |
|
234 if(*hashResult == expectedHash) |
|
235 { |
|
236 INFO_PRINTF1(_L("*** Hash - Incremental Hash with Replicate : PASS ***")); |
|
237 SetTestStepResult(EPass); |
|
238 } |
|
239 else |
|
240 { |
|
241 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); |
|
242 SetTestStepResult(EFail); |
|
243 } |
|
244 |
|
245 CleanupStack::PopAndDestroy(hashResult); |
|
246 CleanupStack::PopAndDestroy(hashData); |
|
247 |
|
248 delete hashReplicateImpl; |
|
249 } |
|
250 |
|
251 //Cleanup the Source RFile |
|
252 CleanupStack::PopAndDestroy(); |
|
253 } |
|
254 |
|
255 fsSession.Close(); |
|
256 |
|
257 CleanupStack::PopAndDestroy(hashImpl); |
|
258 } |
|
259 else |
|
260 { |
|
261 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err); |
|
262 SetTestStepResult(EFail); |
|
263 } |
|
264 } |
|
265 } |
|
266 |
|
267 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
268 return TestStepResult(); |
|
269 } |
|
270 |
|
271 |
|
272 TVerdict CHashIncrementalHashWithReplicateStep::doTestStepPostambleL() |
|
273 { |
|
274 |
|
275 return TestStepResult(); |
|
276 } |