|
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_incremental_with_copy_step.h" |
|
25 #include <cryptospi/cryptohashapi.h> |
|
26 #include <cryptospi/plugincharacteristics.h> |
|
27 |
|
28 using namespace CryptoSpi; |
|
29 |
|
30 CHashIncrementalWithCopyStep::~CHashIncrementalWithCopyStep() |
|
31 { |
|
32 } |
|
33 |
|
34 |
|
35 CHashIncrementalWithCopyStep::CHashIncrementalWithCopyStep() |
|
36 { |
|
37 SetTestStepName(KHashIncrementalWithCopyStep); |
|
38 } |
|
39 |
|
40 |
|
41 TVerdict CHashIncrementalWithCopyStep::doTestStepPreambleL() |
|
42 { |
|
43 return EPass; |
|
44 } |
|
45 |
|
46 |
|
47 TVerdict CHashIncrementalWithCopyStep::doTestStepL() |
|
48 { |
|
49 //Assume faliure, unless all is successful |
|
50 SetTestStepResult(EFail); |
|
51 |
|
52 INFO_PRINTF1(_L("*** Hash - Incremental with Copy ***")); |
|
53 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
54 |
|
55 TVariantPtrC algorithmUid; |
|
56 TPtrC sourcePath; |
|
57 TPtrC expectedHash; |
|
58 |
|
59 //Extract the Test Case ID parameter from the specified INI file |
|
60 if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) || |
|
61 !GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) || |
|
62 !GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash)) |
|
63 { |
|
64 ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **")); |
|
65 SetTestStepResult(EFail); |
|
66 } |
|
67 else |
|
68 { |
|
69 //Create a pointer for the Hash Implementation Object |
|
70 CHash* hashImpl = NULL; |
|
71 |
|
72 //Retrieve a Hash Factory Object |
|
73 TRAPD(err,CHashFactory::CreateHashL(hashImpl, |
|
74 algorithmUid, |
|
75 NULL)); |
|
76 |
|
77 if(err == KErrNone) |
|
78 { |
|
79 //Push the Hash Implementation Object onto the Cleanup Stack |
|
80 CleanupStack::PushL(hashImpl); |
|
81 |
|
82 RFs fsSession; |
|
83 CleanupClosePushL(fsSession); |
|
84 |
|
85 //Create a connection to the file server |
|
86 err = fsSession.Connect(); |
|
87 |
|
88 if(err != KErrNone) |
|
89 { |
|
90 ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err); |
|
91 SetTestStepResult(EFail); |
|
92 } |
|
93 else |
|
94 { |
|
95 RFile sourceFile; |
|
96 CleanupClosePushL(sourceFile); |
|
97 |
|
98 //Open the specified source file |
|
99 err = sourceFile.Open(fsSession,sourcePath, EFileRead); |
|
100 |
|
101 if(err != KErrNone) |
|
102 { |
|
103 ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err); |
|
104 SetTestStepResult(EFail); |
|
105 } |
|
106 else |
|
107 { |
|
108 TInt sourceLength = 0; |
|
109 TInt readPosition = 0; |
|
110 TInt readIncrement = 0; |
|
111 TBool hashComplete = EFalse; |
|
112 TBool hashCopied = EFalse; |
|
113 TPtrC8 hashStr; |
|
114 |
|
115 CHash* hashCopyImpl = NULL; |
|
116 |
|
117 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
118 |
|
119 //Divide the total size of the source file up into individual equal sized blocks to read |
|
120 //over several increments |
|
121 readIncrement = sourceLength/KDataReadBlocks; |
|
122 |
|
123 if (readIncrement == 0) |
|
124 { |
|
125 ERR_PRINTF2(_L("*** Error: Source File must be larger than %d bytes ***"), KDataReadBlocks); |
|
126 User::LeaveIfError(KErrNotSupported); |
|
127 } |
|
128 |
|
129 do |
|
130 { |
|
131 //Create a heap based descriptor to store the data |
|
132 HBufC8* sourceData = HBufC8::NewL(readIncrement); |
|
133 CleanupStack::PushL(sourceData); |
|
134 TPtr8 sourcePtr = sourceData->Des(); |
|
135 |
|
136 //Read in a block of data from the source file from the current position |
|
137 err = sourceFile.Read(readPosition,sourcePtr,readIncrement); |
|
138 |
|
139 //Update the read position by adding the number of bytes read |
|
140 readPosition += readIncrement; |
|
141 |
|
142 if(readPosition == readIncrement) |
|
143 { |
|
144 //Read in the first block from the data file into the Hash implementation object |
|
145 hashImpl->Hash(*sourceData); |
|
146 CleanupStack::PopAndDestroy(sourceData); |
|
147 INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition); |
|
148 } |
|
149 else if(readPosition >= sourceLength) |
|
150 { |
|
151 //Reading in the final block, constructs the complete hash value and returns it within a TPtrC8 |
|
152 hashStr.Set(hashCopyImpl->Final(*sourceData)); |
|
153 CleanupStack::PopAndDestroy(sourceData); |
|
154 |
|
155 //Sets the Complete Flag to ETrue in order to drop out of the loop |
|
156 hashComplete = ETrue; |
|
157 |
|
158 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length(); |
|
159 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead); |
|
160 } |
|
161 //If the read position is half the source length and the implementation |
|
162 //object hasn't already been copied |
|
163 else if((readPosition >= sourceLength/2) && (hashCopied == EFalse)) |
|
164 { |
|
165 //Update the hash message before copying |
|
166 hashImpl->Update(*sourceData); |
|
167 CleanupStack::PopAndDestroy(sourceData); |
|
168 |
|
169 INFO_PRINTF1(_L("Copying Hash Object...")); |
|
170 |
|
171 //Create a Copy of the existing Hash Object and all internal state of the message digest |
|
172 hashCopyImpl = hashImpl->CopyL(); |
|
173 |
|
174 hashCopied = ETrue; |
|
175 CleanupStack::PushL(hashCopyImpl); |
|
176 INFO_PRINTF2(_L("*** HASH COPY - Bytes Read: %d ***"), readPosition); |
|
177 } |
|
178 else |
|
179 { |
|
180 //Update the message data within the Hash object with the new block |
|
181 if(hashCopied == EFalse) |
|
182 { |
|
183 hashImpl->Update(*sourceData); |
|
184 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition); |
|
185 } |
|
186 else |
|
187 { |
|
188 hashCopyImpl->Update(*sourceData); |
|
189 INFO_PRINTF2(_L("Hash Update (Copy) - Bytes Read: %d"), readPosition); |
|
190 } |
|
191 CleanupStack::PopAndDestroy(sourceData); |
|
192 } |
|
193 }while(hashComplete == EFalse); |
|
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(hashStr); |
|
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 - Incremental with Copy : 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(3, hashCopyImpl); // hashCopyImpl, hashResult, hashData |
|
235 } |
|
236 |
|
237 //Cleanup the Source RFile |
|
238 CleanupStack::PopAndDestroy(); |
|
239 } |
|
240 CleanupStack::PopAndDestroy(&fsSession); |
|
241 |
|
242 fsSession.Close(); |
|
243 |
|
244 CleanupStack::PopAndDestroy(hashImpl); |
|
245 } |
|
246 else |
|
247 { |
|
248 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err); |
|
249 SetTestStepResult(EFail); |
|
250 } |
|
251 } |
|
252 |
|
253 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
254 return TestStepResult(); |
|
255 } |
|
256 |
|
257 |
|
258 TVerdict CHashIncrementalWithCopyStep::doTestStepPostambleL() |
|
259 { |
|
260 return TestStepResult(); |
|
261 } |