|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "TestCompareCntFiles.h" |
|
17 |
|
18 // Class CTestCompareCntFiles - Opens import/export vcf and carries out the verification of the properties |
|
19 CTestCompareCntFiles::CTestCompareCntFiles() |
|
20 { |
|
21 SetTestStepName(KTestCompareCntFiles); |
|
22 } |
|
23 |
|
24 CTestCompareCntFiles::~CTestCompareCntFiles() |
|
25 { |
|
26 delete iExportObj; |
|
27 } |
|
28 |
|
29 |
|
30 TVerdict CTestCompareCntFiles::doTestStepPreambleL() |
|
31 /** |
|
32 * @return - TVerdict code |
|
33 * Override of base class virtual |
|
34 */ |
|
35 { |
|
36 INFO_PRINTF1(_L("Start verifcation of import/export vcf files")); |
|
37 |
|
38 SetTestStepResult(EPass); |
|
39 return TestStepResult(); |
|
40 } |
|
41 |
|
42 TVerdict CTestCompareCntFiles::doTestStepL() |
|
43 /** |
|
44 * @return - TVerdict code |
|
45 * Override of base class pure virtual |
|
46 */ |
|
47 { |
|
48 // connect to file system |
|
49 User::LeaveIfError(iFsSession.Connect()); |
|
50 |
|
51 TESTL(TestStepResult() == EPass); |
|
52 INFO_PRINTF1(_L("CTestCompareCntFiles::doTestStepL() start")); |
|
53 |
|
54 TESTL(GetStringFromConfig(ConfigSection(), KExportFile, iExportFilePath)); |
|
55 |
|
56 GetStringFromConfig(ConfigSection(), KExpectedExportFile, iExpectedExportFilePath); |
|
57 GetStringFromConfig(ConfigSection(), KFieldToVerify, iFieldToVerify); |
|
58 GetStringFromConfig(ConfigSection(), KNoField, iNoField); |
|
59 |
|
60 // Create Utility class object, to use TokenizeStringL |
|
61 CTestStep* self = static_cast<CTestStep*>(this); |
|
62 iExportObj = new(ELeave) CContactsPBAPExportUtilityClass(self); |
|
63 |
|
64 TokenizeAndCompareL(); |
|
65 TokenizeAndCheckNoFieldL(); |
|
66 |
|
67 // Comparing two files |
|
68 TBool compareFile = EFalse; |
|
69 GetBoolFromConfig(ConfigSection(), KCompareFile, compareFile); |
|
70 |
|
71 if(compareFile) |
|
72 { |
|
73 TBool fileCompare = EFalse; |
|
74 // Call CompareWholeFileL to use the normal comparison of two files, ignoring the REV property |
|
75 fileCompare = CompareWholeFileL(iExpectedExportFilePath, iExportFilePath); |
|
76 if(!fileCompare) |
|
77 { |
|
78 SetTestStepResult(EFail); |
|
79 } |
|
80 } |
|
81 |
|
82 iFsSession.Close(); |
|
83 |
|
84 INFO_PRINTF1(_L("CTestCompareCntFiles::doTestStepL() finish")); |
|
85 |
|
86 return TestStepResult(); |
|
87 |
|
88 } |
|
89 |
|
90 /** Compares 2 vcf fie |
|
91 @param aExpectedFile The expected vcf file |
|
92 @param aFile This is the vcf file produced, and will be compared against aExpectedFile |
|
93 @param aProperty This is the property we are interested in |
|
94 */ |
|
95 void CTestCompareCntFiles::CompareFileL(const TDesC& aExpectedFile, const TDesC& aFile, TDes8& aProperty) |
|
96 { |
|
97 RFile fileExpected; |
|
98 RFile fileOutput; |
|
99 CleanupClosePushL(fileExpected); |
|
100 CleanupClosePushL(fileOutput); |
|
101 |
|
102 TInt err = KErrNone; |
|
103 err = fileExpected.Open(iFsSession, aExpectedFile, EFileRead); |
|
104 |
|
105 if (err != KErrNone) |
|
106 { |
|
107 ERR_PRINTF2(_L("Unable to open file: %S"), &aExpectedFile); |
|
108 SetTestStepResult(EFail); |
|
109 SetTestStepError(err); |
|
110 } |
|
111 err = fileOutput.Open(iFsSession, aFile, EFileRead); |
|
112 if (err != KErrNone) |
|
113 { |
|
114 ERR_PRINTF2(_L("Unable to open file: %S"), &aFile); |
|
115 SetTestStepResult(EFail); |
|
116 SetTestStepError(err); |
|
117 } |
|
118 |
|
119 RFileReadStream streamExpected(fileExpected); |
|
120 RFileReadStream streamOutput(fileOutput); |
|
121 CleanupClosePushL(streamExpected); |
|
122 CleanupClosePushL(streamOutput); |
|
123 |
|
124 TBuf8<0x80> bufExpected,bufOutput; |
|
125 TBool isProperty = EFalse; |
|
126 TBool isPropertyExpected = EFalse; |
|
127 TBool foundBothProperty = EFalse; |
|
128 TBool flag = EFalse; |
|
129 |
|
130 TInt err1 = KErrNone; |
|
131 |
|
132 do // This do loop iterates through both files, until the end of file is found |
|
133 { |
|
134 do // This do loop iterates thorugh both files until both properties are found and compares them |
|
135 { |
|
136 foundBothProperty = EFalse; |
|
137 if (!isProperty) |
|
138 { |
|
139 TRAP(err, streamExpected.ReadL(bufExpected, KLinefeedChar)); |
|
140 } |
|
141 |
|
142 if (!isPropertyExpected) |
|
143 { |
|
144 TRAP(err1, streamOutput.ReadL(bufOutput, KLinefeedChar)); |
|
145 } |
|
146 |
|
147 if (err != KErrEof || err1 != KErrEof) |
|
148 { |
|
149 isProperty = IsPropertyPresent(bufExpected, aProperty); // checks if aProperty |
|
150 if(isProperty && err1 == KErrEof) |
|
151 { |
|
152 break; |
|
153 } |
|
154 isPropertyExpected = IsPropertyPresent(bufOutput, aProperty); // checks if aProperty |
|
155 } |
|
156 else |
|
157 { |
|
158 break; |
|
159 } |
|
160 if ((isProperty) && (isPropertyExpected)) |
|
161 { |
|
162 foundBothProperty = ETrue; |
|
163 flag = ETrue; |
|
164 } |
|
165 } while (!foundBothProperty); // exit 2nd do loop when both properties found |
|
166 |
|
167 // Exists 2nd do loop, so both properties are found |
|
168 if (err != KErrEof && err1 != KErrEof) |
|
169 { |
|
170 if(aProperty != KRevision) |
|
171 { |
|
172 CompareLinesL(bufExpected, bufOutput); // compares both properties |
|
173 } |
|
174 else |
|
175 { |
|
176 break; |
|
177 } |
|
178 } |
|
179 |
|
180 // After comparing, gets the next property parameters in vcs file, if we have not reached end of file |
|
181 TRAP(err, streamExpected.ReadL(bufExpected, KLinefeedChar)); |
|
182 TRAP(err1, streamOutput.ReadL(bufOutput, KLinefeedChar)); |
|
183 } while (err != KErrEof || err1 != KErrEof); // exits 2nd do loop when its reached the end of either file |
|
184 |
|
185 TBuf<KMaxLengthField> buf; |
|
186 buf.Copy(aProperty); |
|
187 TPtrC ptr(buf); |
|
188 |
|
189 if(flag) |
|
190 { |
|
191 INFO_PRINTF2(_L("Property: %S Exported properly"), &ptr); |
|
192 } |
|
193 else |
|
194 { |
|
195 INFO_PRINTF2(_L("Property: %S not exported"), &ptr); |
|
196 SetTestStepResult(EFail); |
|
197 } |
|
198 CleanupStack::PopAndDestroy(4, &fileExpected); |
|
199 |
|
200 } |
|
201 |
|
202 /** Checks if the line of data contains the property (aProperty) parameter |
|
203 @param aLine The line of data to check |
|
204 @return True or False indicating if property found in the line of data |
|
205 */ |
|
206 TBool CTestCompareCntFiles::IsPropertyPresent(const TDesC8& aLine, TDes8& aProperty) |
|
207 { |
|
208 TInt propertyLength = aProperty.Length(); |
|
209 TBuf8<KMaxLengthField> propertyFromLine(aLine.Left(propertyLength)); |
|
210 |
|
211 if (!propertyFromLine.Compare(aProperty)) |
|
212 { |
|
213 return ETrue; |
|
214 } |
|
215 else |
|
216 { |
|
217 return EFalse; |
|
218 } |
|
219 } |
|
220 |
|
221 |
|
222 /** Compares two lines of data |
|
223 @param aBufExpected The line of data to compare with aBufOutput |
|
224 @param aBufOutput The second line of data to compare to aBufExpected |
|
225 */ |
|
226 void CTestCompareCntFiles::CompareLinesL(const TDesC8& aBufExpected, const TDesC8& aBufOutput) |
|
227 { |
|
228 TESTL(!aBufExpected.Compare(aBufOutput)); |
|
229 } |
|
230 |
|
231 /** Checks for any field that should have not been exported |
|
232 @param aFile The vcs file to open |
|
233 */ |
|
234 TBool CTestCompareCntFiles::CheckForNoFieldL(const TDesC& aFile, const TDesC& aProperty) |
|
235 { |
|
236 RFile fileOutput; |
|
237 CleanupClosePushL(fileOutput); |
|
238 |
|
239 TInt err = fileOutput.Open(iFsSession, aFile, EFileRead); |
|
240 if (err != KErrNone) |
|
241 { |
|
242 ERR_PRINTF2(_L("Unable to open file: %S"), &aFile); |
|
243 SetTestStepResult(EFail); |
|
244 SetTestStepError(err); |
|
245 } |
|
246 |
|
247 RFileReadStream streamOutput(fileOutput); |
|
248 CleanupClosePushL(streamOutput); |
|
249 |
|
250 TBuf8<0x80> bufOutput; |
|
251 |
|
252 TBool result; |
|
253 TBuf8<KMaxLengthField> noField; |
|
254 noField.Copy(aProperty); |
|
255 |
|
256 do |
|
257 { |
|
258 TRAP(err, streamOutput.ReadL(bufOutput, TChar(KLinefeedChar))); |
|
259 |
|
260 result = IsPropertyPresent(bufOutput, noField); |
|
261 |
|
262 if(result) |
|
263 { |
|
264 break; |
|
265 } |
|
266 } while (err != KErrEof); |
|
267 |
|
268 CleanupStack::PopAndDestroy(2, &fileOutput); |
|
269 |
|
270 if(result) |
|
271 { |
|
272 return EFalse; |
|
273 } |
|
274 else |
|
275 { |
|
276 return ETrue; |
|
277 } |
|
278 } |
|
279 |
|
280 // Tokenize the property list taken from ini file and compare each of them with the expected one |
|
281 void CTestCompareCntFiles::TokenizeAndCompareL() |
|
282 { |
|
283 RArray<TPtrC> fieldList; |
|
284 CleanupClosePushL(fieldList); |
|
285 iExportObj->TokenizeStringL(iFieldToVerify, fieldList); |
|
286 |
|
287 TInt count = fieldList.Count(); |
|
288 TBuf8<KMaxLengthField> property; |
|
289 |
|
290 for(TInt i = 0;i < count;i++) |
|
291 { |
|
292 property.Copy(fieldList[i]); |
|
293 CompareFileL(iExpectedExportFilePath, iExportFilePath, property); |
|
294 } |
|
295 CleanupStack::PopAndDestroy(); |
|
296 fieldList.Close(); |
|
297 } |
|
298 |
|
299 // Tokenize the property list taken from ini file and check for non-existence of these properties in the exported file. |
|
300 void CTestCompareCntFiles::TokenizeAndCheckNoFieldL() |
|
301 { |
|
302 TBool result = EFalse; |
|
303 RArray<TPtrC> fieldList; |
|
304 CleanupClosePushL(fieldList); |
|
305 iExportObj->TokenizeStringL(iNoField, fieldList); |
|
306 TInt count = fieldList.Count(); |
|
307 |
|
308 for(TInt i = 0;i < count;i++) |
|
309 { |
|
310 result = CheckForNoFieldL(iExportFilePath, fieldList[i]); |
|
311 |
|
312 if(result) |
|
313 { |
|
314 INFO_PRINTF1(_L("Success: Property not present in vCard")); |
|
315 } |
|
316 else |
|
317 { |
|
318 INFO_PRINTF1(_L("Error: Property present in vCard")); |
|
319 SetTestStepResult(EFail); |
|
320 break; |
|
321 } |
|
322 } |
|
323 CleanupStack::PopAndDestroy(); |
|
324 fieldList.Close(); |
|
325 } |
|
326 |
|
327 /** Compares two files, excluding the REV property, as it doesn't remain the same always |
|
328 @param aExpectedFile The expected vcf file |
|
329 @param aFile This is the vcf file produced, and will be compared against aExpectedFile |
|
330 */ |
|
331 TBool CTestCompareCntFiles::CompareWholeFileL(const TDesC& aExpectedFile, const TDesC& aFile) |
|
332 { |
|
333 User::LeaveIfError(iFsSession.Connect()); |
|
334 CleanupClosePushL(iFsSession); |
|
335 |
|
336 RFile fileExpected; |
|
337 RFile fileOutput; |
|
338 CleanupClosePushL(fileExpected); |
|
339 CleanupClosePushL(fileOutput); |
|
340 |
|
341 TInt err; |
|
342 err = fileExpected.Open(iFsSession, aExpectedFile, EFileRead); |
|
343 |
|
344 if (err != KErrNone) |
|
345 { |
|
346 User::Leave(err); |
|
347 } |
|
348 |
|
349 err = fileOutput.Open(iFsSession, aFile, EFileWrite); |
|
350 if (err != KErrNone) |
|
351 { |
|
352 User::Leave(err); |
|
353 } |
|
354 |
|
355 RFileReadStream stream1(fileExpected); |
|
356 RFileReadStream stream2(fileOutput); |
|
357 CleanupClosePushL(stream1); |
|
358 CleanupClosePushL(stream2); |
|
359 TBuf8<0x80> bufO,bufC; |
|
360 TBool flag = ETrue; |
|
361 TInt line = 1; |
|
362 do |
|
363 { |
|
364 TRAP(err, stream1.ReadL(bufO, KLinefeedChar)); |
|
365 if (err == KErrNone || err == KErrEof) |
|
366 TRAP(err, stream2.ReadL(bufC, KLinefeedChar)); |
|
367 if (err != KErrNone && err != KErrEof) |
|
368 User::Leave(err); |
|
369 if (CompareLine(bufO, bufC) == EFalse) |
|
370 { |
|
371 flag = EFalse; |
|
372 break; |
|
373 } |
|
374 ++line; |
|
375 } |
|
376 while (err != KErrEof); |
|
377 |
|
378 CleanupStack::PopAndDestroy(4, &fileExpected); |
|
379 CleanupStack::PopAndDestroy(&iFsSession); |
|
380 iFsSession.Close(); |
|
381 |
|
382 if(flag) |
|
383 { |
|
384 return ETrue; |
|
385 } |
|
386 else |
|
387 { |
|
388 return EFalse; |
|
389 } |
|
390 } |
|
391 |
|
392 /* Compare the corresponding lines of two files |
|
393 @param aLine1 Complete line of a file to compare |
|
394 @param aLine2 Corresponding line to compare in the second file |
|
395 */ |
|
396 TBool CTestCompareCntFiles::CompareLine(const TDesC8& aLine1, const TDesC8& aLine2) |
|
397 { |
|
398 _LIT8(KREV,"REV:"); |
|
399 TBool ret = !aLine1.Compare(aLine2); |
|
400 if (!ret) |
|
401 { |
|
402 TBufC8<4> propertyRev(aLine1.Left(4)); |
|
403 if(!propertyRev.Compare(KREV())) |
|
404 { |
|
405 return ETrue; |
|
406 } |
|
407 } |
|
408 return ret; |
|
409 } |
|
410 |
|
411 |
|
412 TVerdict CTestCompareCntFiles::doTestStepPostambleL() |
|
413 /** |
|
414 * @return - TVerdict code |
|
415 * Override of base class virtual |
|
416 */ |
|
417 { |
|
418 INFO_PRINTF1(_L("Completed verifcation of import/export vcf files")); |
|
419 return TestStepResult(); |
|
420 } |
|
421 |