First public contribution.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 #include <bsul/inifile.h> // CIniDocument8, CIniDocument16
24 RTest test(_L("Ini File Parser and Generator"));
29 #define UNUSED_VAR(a) a=a
31 _LIT(KIniFile8,"z:\\resource\\testconfig8.ini") ;
33 void LeaveIfNoMemory(TInt ret)
35 if (ret==KErrNoMemory)
36 User::LeaveNoMemory();
39 void CheckResources(RFs& aFs, TInt aOriginalHandleCount)
41 UNUSED_VAR(aOriginalHandleCount);
42 //check we haven't leaked any handles
43 TInt processHandleCount = 0;
44 TInt threadHandleCount = 0;
45 RThread().HandleCount(processHandleCount, threadHandleCount);
46 test(threadHandleCount == aOriginalHandleCount);
47 //check we haven't leaked any files
48 aFs.ResourceCountMarkEnd();
51 TBool CompareFilesL(RFs& aFs,const TDesC& aFileNameA,const TDesC& aFileNameB)
53 TBool filesAreSame=FALSE;
54 //open the file for reading
56 User::LeaveIfError(fileA.Open(aFs,aFileNameA,EFileShareReadersOrWriters ));
57 CleanupClosePushL(fileA);
59 User::LeaveIfError(fileB.Open(aFs,aFileNameB,EFileShareReadersOrWriters ));
60 CleanupClosePushL(fileB);
62 //only process the file if it exists
65 fileA.Size(filesizeA);
66 fileB.Size(filesizeB);
67 if (filesizeA == filesizeB)
69 HBufC8* aBufferPtrA=HBufC8::NewLC(filesizeA);
70 HBufC8* aBufferPtrB=HBufC8::NewLC(filesizeB);
71 TPtr8 asWriteableBufferA(aBufferPtrA->Des());
72 User::LeaveIfError(fileA.Read(0,asWriteableBufferA,filesizeA));
73 TPtr8 asWriteableBufferB(aBufferPtrB->Des());
74 User::LeaveIfError(fileB.Read(0,asWriteableBufferB,filesizeB));
75 if (asWriteableBufferA.Compare(asWriteableBufferB) == 0)
80 CleanupStack::PopAndDestroy(2);
83 CleanupStack::PopAndDestroy(2);
87 class CIniParser8Test :public CBase
89 typedef void (CIniParser8Test::*ClassFuncPtr8L) (void);
92 static CIniParser8Test* NewL();
102 //simple create and delete
104 static void CreateDeleteTest1L();
105 static void CreateDeleteOOMTestL();
107 static void CreateDeleteTest2L();
108 static void CreateDeleteOOMTest2L();
130 //class function utilities
131 static void DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);
132 static void DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);
133 static void DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc, const TBool aShouldBeDifferent);
136 CIniParser8Test():iIniDocument(NULL){}
137 CIniDocument8* iIniDocument;
140 CIniParser8Test* CIniParser8Test::NewL()
142 CIniParser8Test* self=new (ELeave)CIniParser8Test();
143 CleanupStack::PushL(self);
144 self->iIniDocument=CIniDocument8::NewL(TheRFs,KIniFile8);
149 void CIniParser8Test::DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
151 test.Next(aTestDesc);
154 // find out the number of open handles
155 TInt startProcessHandleCount;
156 TInt startThreadHandleCount;
157 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
159 CIniParser8Test* iniTest=CIniParser8Test::NewL();
161 CleanupStack::PushL(iniTest);
163 (iniTest->*testFuncL)();
165 CleanupStack::PopAndDestroy();
167 // check that no handles have leaked
168 TInt endProcessHandleCount;
169 TInt endThreadHandleCount;
170 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
172 test(startProcessHandleCount == endProcessHandleCount);
173 test(startThreadHandleCount == endThreadHandleCount);
178 void CIniParser8Test::DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
180 test.Next(aTestDesc);
187 // find out the number of open handles
188 TInt startProcessHandleCount;
189 TInt startThreadHandleCount;
190 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
192 CIniParser8Test* iniTest=CIniParser8Test::NewL();
193 CleanupStack::PushL(iniTest);
195 __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
196 TRAP(err, (iniTest->*testFuncL)());
197 __UHEAP_SETFAIL(RHeap::ENone, 0);
199 CleanupStack::PopAndDestroy(iniTest);
201 // check that no handles have leaked
202 TInt endProcessHandleCount;
203 TInt endThreadHandleCount;
204 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
206 test(startProcessHandleCount == endProcessHandleCount);
207 test(startThreadHandleCount == endThreadHandleCount);
210 } while(err == KErrNoMemory);
213 test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
216 void CIniParser8Test::DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc, const TBool aShouldBeSame)
218 test.Next(aTestDesc);
220 // find out the number of open handles
221 TInt startProcessHandleCount = 0;
222 TInt startThreadHandleCount = 0;
223 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
225 //Compare the results against this instance.
227 CIniDocument8* referenceDoc=NULL;
228 TRAP(err,referenceDoc=CIniDocument8::NewL(TheRFs,KIniFile8));
229 User::LeaveIfError(err);
230 CleanupStack::PushL(referenceDoc);
232 //Open a file and Externalise the reference oom to it.
233 User::LeaveIfError(referenceDoc->Externalise(_L("c:\\initest\\oom_ref.ini")));
234 CIniParser8Test* iniTest=NULL;
236 for (TInt i = 1;;i++)
241 TRAP(err, iniTest=CIniParser8Test::NewL());
245 CleanupStack::PushL(iniTest);
247 TRAP(err, (iniTest->*testFuncL)());
250 test(iniTest->iIniDocument->CompareDocs(*referenceDoc));
251 CleanupStack::PopAndDestroy(iniTest);
252 __UHEAP_SETFAIL(RHeap::ENone, 0);
258 //Open a file and Externalise to it.
259 TRAP(err, iniTest->iIniDocument->Externalise(_L("c:\\initest\\oom.ini")));
262 CleanupStack::PopAndDestroy(iniTest);
263 __UHEAP_SETFAIL(RHeap::ENone, 0);
270 TRAP(err, result=CompareFilesL(TheRFs,_L("c:\\initest\\oom_ref.ini"), _L("c:\\initest\\oom.ini")));
273 CleanupStack::PopAndDestroy(iniTest);
274 __UHEAP_SETFAIL(RHeap::ENone, 0);
278 test(result == aShouldBeSame);
281 CleanupStack::PopAndDestroy(iniTest);
282 //check we haven't leaked any heap memory
284 CheckResources(TheRFs, startThreadHandleCount);
286 if (err != KErrNoMemory)
288 test(err == KErrNone);
289 break; // we reach here if we are unable to create the OOM condition.
292 __UHEAP_SETFAIL(RHeap::ENone, 0);
296 CleanupStack::PopAndDestroy(referenceDoc);
298 test.Printf(_L("Completed consistency check."));
301 void CIniParser8Test::CreateDeleteOOMTestL()
309 // find out the number of open handles
310 TInt startProcessHandleCount;
311 TInt startThreadHandleCount;
312 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
314 __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
316 CIniDocument8* ini=NULL;
317 TRAP(err,ini=CIniDocument8::NewL(TheRFs,KIniFile8));
321 __UHEAP_SETFAIL(RHeap::ENone, 0);
323 // check that no handles have leaked
324 TInt endProcessHandleCount;
325 TInt endThreadHandleCount;
326 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
327 test(startProcessHandleCount == endProcessHandleCount);
328 test(startThreadHandleCount == endThreadHandleCount);
331 } while(err == KErrNoMemory);
334 test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
338 void CIniParser8Test::CreateDeleteTest1L()
342 CIniDocument8* ini=NULL;
343 //note only support 16 bit Little Endian ini file
344 ini=CIniDocument8::NewL(TheRFs,KIniFile8);
351 void CIniParser8Test::CreateDeleteOOMTest2L()
359 // find out the number of open handles
360 TInt startProcessHandleCount;
361 TInt startThreadHandleCount;
362 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
364 __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
367 TRAP(err,ini=CIniFile8::NewL(TheRFs,KIniFile8));
371 __UHEAP_SETFAIL(RHeap::ENone, 0);
373 // check that no handles have leaked
374 TInt endProcessHandleCount;
375 TInt endThreadHandleCount;
376 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
377 test(startProcessHandleCount == endProcessHandleCount);
378 test(startThreadHandleCount == endThreadHandleCount);
381 } while(err == KErrNoMemory);
384 test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
388 void CIniParser8Test::CreateDeleteTest2L()
393 //note only support 16 bit Little Endian ini file
394 ini=CIniFile8::NewL(TheRFs,KIniFile8);
401 void CIniParser8Test::DoTest1L()
403 //Testing GetSectionList API
404 RArray<TPtrC8> sectionNames;
405 User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
406 test(sectionNames.Count()==8);
408 //Testing the sectionNames in name order
409 test(sectionNames[0].Compare(_L8("1"))==0);
410 test(sectionNames[1].Compare(_L8("MAPPINGS"))==0);
411 test(sectionNames[2].Compare(_L8("MEDIA"))==0);
412 test(sectionNames[3].Compare(_L8("OUTPUT_CHANNELS"))==0);
413 test(sectionNames[4].Compare(_L8("SERVERS"))==0);
414 test(sectionNames[5].Compare(_L8("SWTRACER"))==0);
415 test(sectionNames[6].Compare(_L8("test_section"))==0);
416 test(sectionNames[7].Compare(_L8("test_twosection"))==0);
417 sectionNames.Reset();
420 void CIniParser8Test::DoTest2L()
422 //Test GetKeyValue API
424 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("MEDIA"),_L8("RDebug"),value));
425 test(value.Compare(_L8("SwtRDebugPlugin.dll"))==0);
426 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("OUTPUT_CHANNELS"),_L8("1"),value));
427 test(value.Compare(_L8("RDebug"))==0);
428 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("1"),_L8("new_setting"),value));
429 test(value.Compare(_L8("value \\n value1\\t value2"))==0);
433 ret=iIniDocument->GetKeyValue(_L8("mySection"),_L8("mykey"),value);
434 LeaveIfNoMemory(ret);
435 test(ret==KErrNotFound);
437 ret=iIniDocument->GetKeyValue(_L8("MEDIA"),_L8("mykey"),value);
438 LeaveIfNoMemory(ret);
439 test(ret==KErrNotFound);
441 ret=iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
442 LeaveIfNoMemory(ret);
443 test(value.Length()==0);
446 void CIniParser8Test::DoTest3L()
448 //Test AddSection API
449 RArray<TPtrC8> sectionNames;
450 CleanupClosePushL(sectionNames);
451 User::LeaveIfError(iIniDocument->AddSection(_L8("NEW-SECTION")));
452 User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
453 test(sectionNames.Count()==9);
456 User::LeaveIfError(iIniDocument->AddSection(_L8("NeW-SECTION")));
457 User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
458 test(sectionNames.Count()==10);
459 //adding existing section, no duplicate allowed
460 TInt ret=iIniDocument->AddSection(_L8("NEW-SECTION"));
461 LeaveIfNoMemory(ret);
462 test(ret==KErrAlreadyExists);
463 CleanupStack::PopAndDestroy();
466 void CIniParser8Test::DoTest4L()
468 //Test RemoveSection API
469 RArray<TPtrC8> sectionNames;
470 CleanupClosePushL(sectionNames);
472 //known section at start of file.
473 User::LeaveIfError(iIniDocument->RemoveSection(_L8("SERVERS")));
474 User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
475 test(sectionNames.Count()==7);
477 //check key inside section is also deleted
479 TInt ret=iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
480 LeaveIfNoMemory(ret);
482 test(ret==KErrNotFound);
485 ret=iIniDocument->RemoveSection(_L8("AnySection"));
486 LeaveIfNoMemory(ret);
487 test(ret==KErrNotFound);
489 //nonexist section should be created at end
490 User::LeaveIfError(iIniDocument->SetKey(_L8("unknown_section3"),_L8("unknown_key3"),_L8("unknown_value3")));
491 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("unknown_section3"),_L8("unknown_key3"),value));
492 test(value.Compare(_L8("unknown_value3"))==0);
495 ret=iIniDocument->RemoveSection(_L8("unknown_section3"));
496 LeaveIfNoMemory(ret);
499 //Open a file and Externalise to it.
500 User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_3.ini")));
501 CleanupStack::PopAndDestroy();
503 // Read it back in and find the additions put in above.
504 CIniDocument8* iniReRead=NULL;
505 iniReRead=CIniDocument8::NewL(TheRFs,_L("c:\\initest\\unknown8_3.ini"));
506 CleanupStack::PushL(iniReRead);
508 ret=iniReRead->GetKeyValue(_L8("unknown_section3"),_L8("unknown_key3"),value);
509 LeaveIfNoMemory(ret);
510 test(ret==KErrNotFound);
511 CleanupStack::PopAndDestroy(iniReRead);
514 void CIniParser8Test::DoTest5L()
518 //Modifying existing value
519 User::LeaveIfError(iIniDocument->SetKey(_L8("MEDIA"),_L8("RDebug"),_L8("NewPlugin.dll")));
520 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("MEDIA"),_L8("RDebug"),value));
521 test(value.Compare(_L8("NewPlugin.dll"))==0);
523 //nonexist key should be created
524 User::LeaveIfError(iIniDocument->SetKey(_L8("MEDIA"),_L8("newplug"),_L8("")));
525 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("MEDIA"),_L8("newplug"),value));
526 test(value.Compare(_L8(""))==0);
528 //nonexist section should be created
529 User::LeaveIfError(iIniDocument->SetKey(_L8("unknown_section"),_L8("unknown_key"),_L8("unknown_value")));
530 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value));
531 test(value.Compare(_L8("unknown_value"))==0);
533 //nonexist key should be created
534 User::LeaveIfError(iIniDocument->SetKey(_L8("SERVERS"),_L8("host"),_L8("newhost")));
535 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("host"),value));
536 test(value.Compare(_L8("newhost"))==0);
538 //create a second key in first section
539 User::LeaveIfError(iIniDocument->SetKey(_L8("SERVERS"),_L8("host2"),_L8("newhost2")));
540 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("host2"),value));
541 test(value.Compare(_L8("newhost2"))==0);
543 //alter existing key value again
544 User::LeaveIfError(iIniDocument->SetKey(_L8("unknown_section"),_L8("unknown_key"),_L8("unknown_value2")));
545 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value));
546 test(value.Compare(_L8("unknown_value2"))==0);
548 //Open a file and Externalise to it.
549 User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_1.ini")));
551 // Read it back in and find the additions put in above.
552 CIniDocument8* iniReRead=NULL;
553 iniReRead=CIniDocument8::NewL(TheRFs,_L("c:\\initest\\unknown8_1.ini"));
554 CleanupStack::PushL(iniReRead);
556 User::LeaveIfError(iniReRead->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value));
557 test(value.Compare(_L8("unknown_value2"))==0);
558 User::LeaveIfError(iniReRead->GetKeyValue(_L8("SERVERS"),_L8("host2"),value));
559 test(value.Compare(_L8("newhost2"))==0);
560 User::LeaveIfError(iniReRead->GetKeyValue(_L8("MEDIA"),_L8("RDebug"),value));
561 test(value.Compare(_L8("NewPlugin.dll"))==0);
562 User::LeaveIfError(iniReRead->GetKeyValue(_L8("MEDIA"),_L8("newplug"),value));
563 test(value.Compare(_L8(""))==0);
564 CleanupStack::PopAndDestroy(iniReRead);
568 void CIniParser8Test::DoTest6L()
570 //Testing RemoveKey API
572 //remove existing key in middle of file.
573 User::LeaveIfError(iIniDocument->RemoveKey(_L8("OUTPUT_CHANNELS"),_L8("1")));
574 TInt ret=iIniDocument->GetKeyValue(_L8("OUTPUT_CHANNELS"),_L8("1"),value);
575 LeaveIfNoMemory(ret);
576 test(ret==KErrNotFound);
578 //remove non-exist key
579 ret=iIniDocument->RemoveKey(_L8("OUTPUT_CHANNELS"),_L8("1"));
580 LeaveIfNoMemory(ret);
581 test(ret==KErrNotFound);
583 //remove non-exist section
584 ret=iIniDocument->RemoveKey(_L8("Non-existSection"),_L8("1"));
585 LeaveIfNoMemory(ret);
586 test(ret==KErrNotFound);
588 //remove existing key at start of file
589 User::LeaveIfError(iIniDocument->RemoveKey(_L8("SERVERS"),_L8("SWTRACER")));
590 ret=iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
591 LeaveIfNoMemory(ret);
592 test(ret==KErrNotFound);
594 //nonexist section should be created at end
595 User::LeaveIfError(iIniDocument->SetKey(_L8("unknown_section"),_L8("unknown_key"),_L8("unknown_value")));
596 User::LeaveIfError(iIniDocument->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value));
597 test(value.Compare(_L8("unknown_value"))==0);
599 //remove existing key at end of section
600 User::LeaveIfError(iIniDocument->RemoveKey(_L8("unknown_section"),_L8("unknown_key")));
601 ret=iIniDocument->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value);
602 LeaveIfNoMemory(ret);
603 test(ret==KErrNotFound);
605 //Open a file and Externalise to it.
606 User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_2.ini")));
608 // Read it back in and find the additions put in above.
609 CIniDocument8* iniReRead=NULL;
610 iniReRead=CIniDocument8::NewL(TheRFs,_L("c:\\initest\\unknown8_2.ini"));
611 CleanupStack::PushL(iniReRead);
613 ret=iniReRead->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value);
614 LeaveIfNoMemory(ret);
615 test(ret==KErrNotFound);
617 ret=iniReRead->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
618 LeaveIfNoMemory(ret);
619 test(ret==KErrNotFound);
620 CleanupStack::PopAndDestroy(iniReRead);
623 void CIniParser8Test::DoTest7L()
625 //Testing iterator class
626 CIniSecIter8* iIniSecIter=NULL;
630 TRAP(ret,iIniSecIter=CIniSecIter8::NewL(_L8("Unknown"),iIniDocument));
631 LeaveIfNoMemory(ret);
632 test(ret==KErrNotFound);
635 TRAP(ret,iIniSecIter=CIniSecIter8::NewL(_L8("Unknown"),NULL));
636 LeaveIfNoMemory(ret);
637 test(ret==KErrArgument);
640 iIniSecIter=CIniSecIter8::NewL(_L8("test_section"),iIniDocument);
643 //test Next() and End();
644 test(!iIniSecIter->End());
645 test(iIniSecIter->Next(key,value));
646 test(key.Compare(_L8("key1"))==0);
647 test(value.Compare(_L8("value1"))==0);
648 test(!iIniSecIter->End());
649 test(iIniSecIter->Next(key,value));
650 test(key.Compare(_L8("key2"))==0);
651 test(value.Compare(_L8("value2"))==0);
652 test(!iIniSecIter->End());
653 test(iIniSecIter->Next(key,value));
654 test(key.Compare(_L8("key3"))==0);
655 test(value.Compare(_L8("value3"))==0);
656 test(!iIniSecIter->End());
657 test(iIniSecIter->Next(key,value));
658 test(key.Compare(_L8("key4"))==0);
659 test(value.Compare(_L8("value4"))==0);
660 test(!iIniSecIter->End());
661 test(iIniSecIter->Next(key,value));
662 test(key.Compare(_L8("key5"))==0);
663 test(value.Compare(_L8("value value value"))==0);
664 test(iIniSecIter->End());
665 test(iIniSecIter->Next(key,value)==EFalse);
668 iIniSecIter->Reset();
669 test(!iIniSecIter->End());
670 test(iIniSecIter->Next(key,value));
671 test(key.Compare(_L8("key1"))==0);
672 test(value.Compare(_L8("value1"))==0);
678 void CIniParser8Test::DoTest8L()
680 //Testing Externalise to ROM drive
681 TInt ret=iIniDocument->Externalise(_L("z:\\output.ini"));
682 LeaveIfNoMemory(ret);
683 test(ret==KErrAccessDenied);
685 //Testing Externalise to a New file
686 User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output8.ini")));
688 test(CompareFilesL(TheRFs,_L("z:\\resource\\testconfig8.ini"), _L("c:\\initest\\output8.ini")));
690 //Try opening the written ini file now to ensure no corruption in writing
691 CIniDocument8* output=CIniDocument8::NewL(TheRFs,_L("c:\\initest\\output8.ini"));
692 CleanupStack::PushL(output);
693 User::LeaveIfError(output->SetKey(_L8("Test"),_L8("Test"),_L8("Test")));
695 //Testing Externaliseing to the already exist file
696 User::LeaveIfError(output->Externalise(_L("c:\\initest\\output8.ini")));
697 CleanupStack::PopAndDestroy();
699 //Opening an empty file and Externaliseing an empty file
700 output=CIniDocument8::NewL(TheRFs,_L("c:\\initest\\unknown8.ini"));
701 CleanupStack::PushL(output);
702 User::LeaveIfError(output->Externalise(_L("c:\\initest\\unknown8.ini")));
703 CleanupStack::PopAndDestroy();
706 void CIniParser8Test::DoTest9L()
708 //Test for no leakage when handling corrupt file
709 CIniDocument8* ini=NULL;
710 TRAPD(err,ini=CIniDocument8::NewL(TheRFs,_L("z:\\resource\\corruptconfig8.ini")));
711 LeaveIfNoMemory(err);
712 test(err==KErrCorrupt);
716 void CIniParser8Test::DoTest10L()
719 //open existing ini file
720 CIniFile8* ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\testconfig8.ini"));
721 CleanupStack::PushL(ini);
724 User::LeaveIfError(ini->FindVar(_L8("test_section"),_L8("key1"),value));
725 test(value.Compare(_L8("value1"))==0);
726 User::LeaveIfError(ini->FindVar(_L8("test_section"),_L8("key2"),value));
727 test(value.Compare(_L8("value2"))==0);
728 User::LeaveIfError(ini->FindVar(_L8("test_section"),_L8("key3"),value));
729 test(value.Compare(_L8("value3"))==0);
730 User::LeaveIfError(ini->FindVar(_L8("test_section"),_L8("key4"),value));
731 test(value.Compare(_L8("value4"))==0);
732 User::LeaveIfError(ini->FindVar(_L8("test_section"),_L8("key5"),value));
733 test(value.Compare(_L8("value value value"))==0);
736 User::LeaveIfError(ini->FindVar(_L8("SERVERS"),_L8("SWTRACER"),value));
737 test(value.Compare(_L8(""))==0);
740 User::LeaveIfError(ini->FindVar(_L8("1"),_L8("timestamps"),value));
741 test(value.Compare(_L8("0"))==0);
742 User::LeaveIfError(ini->FindVar(_L8("1"),_L8("setting"),value));
743 test(value.Compare(_L8("value"))==0);
745 CleanupStack::PopAndDestroy();
747 //open a non existing file
749 TRAP(ret,ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\nonexist.ini")));
750 LeaveIfNoMemory(ret);
751 test(ret==KErrNotFound);
753 //open an empty ini file
754 ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\empty8.ini"));
755 CleanupStack::PushL(ini);
757 ret=ini->FindVar(_L8("empty"),_L8("empty"),value);
758 LeaveIfNoMemory(ret);
759 test(ret==KErrNotFound);
761 CleanupStack::PopAndDestroy();
764 void CIniParser8Test::DoTest11L()
767 // We are trying to invoke an OOM condition for a single operation to test that the operation is atomic.
768 // Under that condition the object should be rolled back to the original state. The resulting document should be the same
769 // as before the condition was invoked. If the test succeeded, a new section should be created at end
770 // (which is nice but not the real focus of the test).
771 User::LeaveIfError(iIniDocument->SetKey(_L8("unknown_section3"),_L8("unknown_key3"),_L8("unknown_value3")));
774 void CIniParser8Test::DoTest12L()
778 CIniDocument8* Ori_ini = NULL;
779 CIniDocument8* Com_ini = NULL;
780 CIniDocument8* Cre_ini = NULL;
786 TRAPD( err1, Ori_ini = CIniDocument8::NewL(rfs, _L("z:\\resource\\OriConfig8.ini")) );
787 test(err1 == KErrNone);
788 TRAPD( err2, Com_ini = CIniDocument8::NewL(rfs, _L("z:\\resource\\ComConfig8.ini")) );
789 test(err2 == KErrNone);
791 Ori_ini->SetKey(_L8("DEFECT"), _L8("Number"), _L8("127618"));
792 Ori_ini->Externalise( _L("c:\\initest\\CreateConfig8.ini") );
794 TRAPD( err3, Cre_ini = CIniDocument8::NewL(rfs, _L("c:\\initest\\CreateConfig8.ini")) );
795 test(err3 == KErrNone);
797 bool result = Cre_ini->CompareDocs(*Com_ini);
808 static void DeleteFilesL()
810 CFileMan* fileman=CFileMan::NewL(TheRFs);
812 fileman->Delete(_L("c:\\initest\\*"));
818 @SYMTestCaseID SYSLIB-BAFL-CT-1558
819 @SYMTestCaseDesc Test CIniParser8Test
820 @SYMTestPriority High
821 @SYMTestActions Perform various component tests on CIniParser8Test (includes OOM)
822 Testing all the exported APis
823 @SYMTestExpectedResults The test must not fail.
826 static void DoTestL()
828 test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1558 "));
831 //8 bit basic testing
833 CIniParser8Test::CreateDeleteTest1L();
834 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest1L,_L("GetSectionList8"));
835 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest2L,_L("GetKeyValue8"));
836 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest3L,_L("AddSection8"));
837 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest4L,_L("RemoveSection8"));
838 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest5L,_L("SetKeyValue8"));
839 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest6L,_L("RemoveKey8"));
840 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest7L,_L("IniSecIter8"));
841 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest8L,_L("Externalise"));
842 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest9L,_L("Corrupt file"));
845 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest12L,_L("New Line Missing"));
849 CIniParser8Test::CreateDeleteOOMTestL();
850 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest1L,_L("GetSectionList8-OOM"));
851 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest2L,_L("GetKeyValue8-OOM"));
852 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest3L,_L("AddSection8-OOM"));
853 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest4L,_L("RemoveSection8-OOM"));
854 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest5L,_L("SetKeyValue8-OOM"));
855 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest6L,_L("RemoveKey8-OOM"));
856 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest7L,_L("IniSecIter8-OOM"));
857 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest8L,_L("Externalise-OOM"));
858 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest9L,_L("Corrupt file-OOM"));
860 //8 bit light basic testing
861 CIniParser8Test::CreateDeleteTest2L();
862 CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest10L,_L("Light FindVar"));
863 //8 bit light OOM testing
864 CIniParser8Test::CreateDeleteOOMTest2L();
865 CIniParser8Test::DoOOMTestL(&CIniParser8Test::DoTest10L,_L("Light FindVar-OOM"));
867 //Light weight temporary testing
869 TTime startTime(0), stopTime(0);
872 startTime.UniversalTime();
873 for (TInt i=0;i<1000;i++)
875 CIniFile8* ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\testconfig8.ini"));
876 ret=ini->FindVar(_L8("test_section"),_L8("key1"),value);
877 test(value.Compare(_L8("value1"))==0);
879 ret=ini->FindVar(_L8("test_section"),_L8("key2"),value);
880 test(value.Compare(_L8("value2"))==0);
882 ret=ini->FindVar(_L8("test_section"),_L8("key3"),value);
883 test(value.Compare(_L8("value3"))==0);
885 ret=ini->FindVar(_L8("test_section"),_L8("key4"),value);
886 test(value.Compare(_L8("value4"))==0);
888 ret=ini->FindVar(_L8("test_section"),_L8("key5"),value);
889 test(value.Compare(_L8("value value value"))==0);
891 ret=ini->FindVar(_L8("SERVERS"),_L8("SWTRACER"),value);
892 test(value.Compare(_L8(""))==0);
894 ret=ini->FindVar(_L8("1"),_L8("timestamps"),value);
895 test(value.Compare(_L8("0"))==0);
897 ret=ini->FindVar(_L8("1"),_L8("setting"),value);
898 test(value.Compare(_L8("value"))==0);
902 stopTime.UniversalTime();
903 TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
904 test.Printf(_L("Time taken for Light= %d microseconds\n"), timeTaken.Int64() );
908 startTime.UniversalTime();
909 for (TInt j=0;j<1000;j++)
911 CIniDocument8* dom=CIniDocument8::NewL(TheRFs,KIniFile8);
912 ret=dom->GetKeyValue(_L8("test_section"),_L8("key1"),value);
913 test(value.Compare(_L8("value1"))==0);
915 ret=dom->GetKeyValue(_L8("test_section"),_L8("key2"),value);
916 test(value.Compare(_L8("value2"))==0);
918 ret=dom->GetKeyValue(_L8("test_section"),_L8("key3"),value);
919 test(value.Compare(_L8("value3"))==0);
921 ret=dom->GetKeyValue(_L8("test_section"),_L8("key4"),value);
922 test(value.Compare(_L8("value4"))==0);
924 ret=dom->GetKeyValue(_L8("test_section"),_L8("key5"),value);
925 test(value.Compare(_L8("value value value"))==0);
927 ret=dom->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
928 test(value.Compare(_L8(""))==0);
930 ret=dom->GetKeyValue(_L8("1"),_L8("timestamps"),value);
931 test(value.Compare(_L8("0"))==0);
933 ret=dom->GetKeyValue(_L8("1"),_L8("setting"),value);
934 test(value.Compare(_L8("value"))==0);
939 stopTime.UniversalTime();
940 timeTaken = stopTime.MicroSecondsFrom(startTime);
941 test.Printf(_L("Time taken for Heavy= %d microseconds\n"), timeTaken.Int64() );
943 startTime.UniversalTime();
944 // Consistency checks
945 CIniParser8Test::DoOOMWithConsistencyCheckTestL(&CIniParser8Test::DoTest11L,_L("Consistency8-OOMC"), FALSE);
947 stopTime.UniversalTime();
948 timeTaken = stopTime.MicroSecondsFrom(startTime);
949 test.Printf(_L("Time taken for consistency checks= %d microseconds\n"), timeTaken.Int64() );
953 GLDEF_C TInt E32Main()
956 CTrapCleanup* trapCleanup=CTrapCleanup::New();
957 test(TheRFs.Connect()==KErrNone);
958 test.Start(_L("MyTest"));
960 TRAPD(error, DoTestL());
961 test(error == KErrNone);