|
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 "hashincrementalhashstep.h" |
|
25 |
|
26 #include <cryptospi/cryptohashapi.h> |
|
27 #include <cryptospi/plugincharacteristics.h> |
|
28 |
|
29 using namespace CryptoSpi; |
|
30 |
|
31 CHashIncrementalHashStep::~CHashIncrementalHashStep() |
|
32 { |
|
33 } |
|
34 |
|
35 |
|
36 CHashIncrementalHashStep::CHashIncrementalHashStep() |
|
37 { |
|
38 SetTestStepName(KHashIncrementalHashStep); |
|
39 } |
|
40 |
|
41 |
|
42 TVerdict CHashIncrementalHashStep::doTestStepPreambleL() |
|
43 { |
|
44 SetTestStepResult(EPass); |
|
45 return TestStepResult(); |
|
46 } |
|
47 |
|
48 |
|
49 TVerdict CHashIncrementalHashStep::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 ***")); |
|
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 TPtrC8 hashStr; |
|
123 |
|
124 User::LeaveIfError(sourceFile.Size(sourceLength)); |
|
125 |
|
126 //Divide the total size of the source file up into individual equal sized blocks to read |
|
127 //over several increments |
|
128 readIncrement = sourceLength/KDataReadBlocks; |
|
129 |
|
130 do |
|
131 { |
|
132 //Create a heap based descriptor to store the data |
|
133 HBufC8* sourceData = HBufC8::NewL(readIncrement); |
|
134 CleanupStack::PushL(sourceData); |
|
135 TPtr8 sourcePtr = sourceData->Des(); |
|
136 |
|
137 //Read in a block of data from the source file from the current position |
|
138 err = sourceFile.Read(readPosition,sourcePtr,readIncrement); |
|
139 |
|
140 //Update the read position by adding the number of bytes read |
|
141 readPosition += readIncrement; |
|
142 |
|
143 if(readPosition == readIncrement) |
|
144 { |
|
145 //Read in the first block from the data file into the Hash implementation object |
|
146 hashImpl->Hash(*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(hashImpl->Final(*sourceData)); |
|
153 |
|
154 //Sets the Complete Flag to ETrue in order to drop out of the loop |
|
155 hashComplete = ETrue; |
|
156 |
|
157 TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length(); |
|
158 INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead); |
|
159 } |
|
160 else |
|
161 { |
|
162 //Update the message data within the Hash object with the new block |
|
163 hashImpl->Update(*sourceData); |
|
164 INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition); |
|
165 } |
|
166 |
|
167 CleanupStack::PopAndDestroy(sourceData); |
|
168 |
|
169 }while(hashComplete == EFalse); |
|
170 |
|
171 //Create a NULL TCharacteristics pointer |
|
172 const TCharacteristics* charsPtr(NULL); |
|
173 |
|
174 //Retrieve the characteristics for the hash implementation object |
|
175 TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr)); |
|
176 |
|
177 //Static cast the characteristics to type THashCharacteristics |
|
178 const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr); |
|
179 |
|
180 //The hash output size is returned in Bits, divide by 8 to get the Byte size |
|
181 TInt hashSize = hashCharsPtr->iOutputSize/8; |
|
182 |
|
183 //Retrieve the final 8bit hash value and convert to 16bit |
|
184 HBufC* hashData = HBufC::NewLC(hashSize); |
|
185 TPtr hashPtr = hashData->Des(); |
|
186 |
|
187 //Copy the hashed content into the heap based descriptor |
|
188 hashPtr.Copy(hashStr); |
|
189 |
|
190 //Take the 16bit descriptor and convert the string to hexadecimal |
|
191 TVariantPtrC convertHash; |
|
192 convertHash.Set(hashPtr); |
|
193 HBufC* hashResult = convertHash.HexStringLC(); |
|
194 |
|
195 INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult); |
|
196 INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash); |
|
197 |
|
198 //If the returned hash value matches the expected hash, Pass the test |
|
199 if(*hashResult == expectedHash) |
|
200 { |
|
201 INFO_PRINTF1(_L("*** Hash - Incremental Hash : PASS ***")); |
|
202 SetTestStepResult(EPass); |
|
203 } |
|
204 else |
|
205 { |
|
206 ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch ***"), err); |
|
207 SetTestStepResult(EFail); |
|
208 } |
|
209 |
|
210 CleanupStack::PopAndDestroy(hashResult); |
|
211 CleanupStack::PopAndDestroy(hashData); |
|
212 } |
|
213 |
|
214 //Cleanup the Source RFile |
|
215 CleanupStack::PopAndDestroy(); |
|
216 } |
|
217 |
|
218 fsSession.Close(); |
|
219 |
|
220 CleanupStack::PopAndDestroy(hashImpl); |
|
221 } |
|
222 else |
|
223 { |
|
224 ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err); |
|
225 SetTestStepResult(EFail); |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); |
|
231 return TestStepResult(); |
|
232 } |
|
233 |
|
234 |
|
235 TVerdict CHashIncrementalHashStep::doTestStepPostambleL() |
|
236 { |
|
237 return TestStepResult(); |
|
238 } |