os/ossrv/lowlevellibsandfws/apputils/bsul/test/t_iniparser/T_IniParser8.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // @internalComponent
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32base.h>
    20 #include <e32test.h>
    21 #include <f32file.h>
    22 #include <bsul/inifile.h>		// CIniDocument8, CIniDocument16
    23 
    24 RTest test(_L("Ini File Parser and Generator"));
    25 
    26 RFs TheRFs;
    27 
    28 using namespace BSUL;
    29 #define UNUSED_VAR(a) a=a
    30 
    31 _LIT(KIniFile8,"z:\\resource\\testconfig8.ini")	;
    32 
    33 void LeaveIfNoMemory(TInt ret)
    34 	{
    35 	if (ret==KErrNoMemory)
    36 		User::LeaveNoMemory();
    37 	}
    38 
    39 void CheckResources(RFs& aFs, TInt aOriginalHandleCount)
    40 	{
    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();	
    49 	}
    50 
    51 TBool CompareFilesL(RFs& aFs,const TDesC& aFileNameA,const TDesC& aFileNameB)
    52 	{
    53 	TBool filesAreSame=FALSE;
    54 	//open the file for reading
    55 	RFile fileA;
    56 	User::LeaveIfError(fileA.Open(aFs,aFileNameA,EFileShareReadersOrWriters ));
    57 	CleanupClosePushL(fileA);
    58 	RFile fileB;
    59 	User::LeaveIfError(fileB.Open(aFs,aFileNameB,EFileShareReadersOrWriters ));
    60 	CleanupClosePushL(fileB);
    61 	
    62 	//only process the file if it exists		
    63 	TInt filesizeA=0;
    64 	TInt filesizeB=0;
    65 	fileA.Size(filesizeA);
    66 	fileB.Size(filesizeB);
    67 	if (filesizeA == filesizeB)
    68 		{
    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)
    76 			{
    77 			filesAreSame=TRUE;
    78 			}
    79 		//pop the buffers
    80 		CleanupStack::PopAndDestroy(2);
    81 		}
    82 	//close the files
    83 	CleanupStack::PopAndDestroy(2);	
    84 	return filesAreSame;
    85 	}
    86 
    87 class CIniParser8Test :public CBase
    88 {
    89 typedef void (CIniParser8Test::*ClassFuncPtr8L) (void);
    90 
    91 public:
    92 	static CIniParser8Test* NewL();
    93 	~CIniParser8Test()
    94 	{
    95 	if (iIniDocument)
    96 		{
    97 		delete iIniDocument;
    98 		iIniDocument=NULL;
    99 		}
   100 	}
   101 
   102 	//simple create and delete
   103 	//heavy
   104 	static void CreateDeleteTest1L();
   105 	static void CreateDeleteOOMTestL();
   106 	//light
   107 	static void CreateDeleteTest2L();
   108 	static void CreateDeleteOOMTest2L();
   109 
   110 	//List of tests
   111 	//heavy weight
   112 	void DoTest1L();
   113 	void DoTest2L();
   114 	void DoTest3L();
   115 	void DoTest4L();
   116 	void DoTest5L();
   117 	void DoTest6L();
   118 	void DoTest7L();
   119 	void DoTest8L();
   120 	void DoTest9L();
   121 	//light weight
   122 	void DoTest10L();
   123 	
   124 	//Consistency check
   125 	void DoTest11L();
   126 	
   127 	//Defect 127618
   128 	void DoTest12L();
   129 			
   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);
   134 	
   135 private:
   136 	CIniParser8Test():iIniDocument(NULL){}
   137 	CIniDocument8* iIniDocument;
   138 };
   139 
   140 CIniParser8Test* CIniParser8Test::NewL()
   141 	{
   142 	CIniParser8Test* self=new (ELeave)CIniParser8Test();
   143 	CleanupStack::PushL(self);
   144 	self->iIniDocument=CIniDocument8::NewL(TheRFs,KIniFile8);
   145 	CleanupStack::Pop();
   146 	return self;
   147 	}
   148 
   149 void CIniParser8Test::DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
   150 	{
   151 	test.Next(aTestDesc);
   152 
   153 	__UHEAP_MARK;
   154   	// find out the number of open handles
   155 	TInt startProcessHandleCount;
   156 	TInt startThreadHandleCount;
   157 	RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   158 
   159 	CIniParser8Test* iniTest=CIniParser8Test::NewL();
   160 
   161 	CleanupStack::PushL(iniTest);
   162 
   163 	(iniTest->*testFuncL)();
   164 
   165 	CleanupStack::PopAndDestroy();
   166 
   167 	// check that no handles have leaked
   168 	TInt endProcessHandleCount;
   169 	TInt endThreadHandleCount;
   170 	RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   171 
   172 	test(startProcessHandleCount == endProcessHandleCount);
   173 	test(startThreadHandleCount  == endThreadHandleCount);
   174 
   175 	__UHEAP_MARKEND;
   176 	}
   177 
   178 void CIniParser8Test::DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
   179 	{
   180 	test.Next(aTestDesc);
   181 
   182 	TInt err;
   183 	TInt tryCount = 0;
   184 	do
   185 		{
   186 		__UHEAP_MARK;
   187   		// find out the number of open handles
   188 		TInt startProcessHandleCount;
   189 		TInt startThreadHandleCount;
   190 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   191 
   192 		CIniParser8Test* iniTest=CIniParser8Test::NewL();
   193 		CleanupStack::PushL(iniTest);
   194 
   195 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   196 		TRAP(err, (iniTest->*testFuncL)());
   197 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   198 
   199 		CleanupStack::PopAndDestroy(iniTest);
   200 		iniTest=NULL;
   201 		// check that no handles have leaked
   202 		TInt endProcessHandleCount;
   203 		TInt endThreadHandleCount;
   204 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   205 
   206 		test(startProcessHandleCount == endProcessHandleCount);
   207 		test(startThreadHandleCount  == endThreadHandleCount);
   208 
   209 		__UHEAP_MARKEND;
   210 		} while(err == KErrNoMemory);
   211 
   212  	test(err==KErrNone);
   213 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
   214 	}
   215 
   216 void CIniParser8Test::DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc, const TBool aShouldBeSame)
   217 	{
   218 	test.Next(aTestDesc);
   219 	
   220 	// find out the number of open handles
   221 	TInt startProcessHandleCount = 0;
   222 	TInt startThreadHandleCount = 0;
   223 	RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   224 	
   225 	//Compare the results against this instance.
   226 	TInt err;
   227 	CIniDocument8* referenceDoc=NULL;
   228 	TRAP(err,referenceDoc=CIniDocument8::NewL(TheRFs,KIniFile8));
   229 	User::LeaveIfError(err);
   230 	CleanupStack::PushL(referenceDoc);
   231 	
   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;
   235 	
   236 	for (TInt i = 1;;i++)
   237 		{
   238 		__UHEAP_MARK;
   239 		__UHEAP_FAILNEXT(i);
   240 		
   241 		TRAP(err, iniTest=CIniParser8Test::NewL());
   242 		if (err != KErrNone)
   243 			continue;
   244 		
   245 		CleanupStack::PushL(iniTest);
   246 		
   247 		TRAP(err, (iniTest->*testFuncL)());
   248 		if (err != KErrNone)
   249 			{
   250 			test(iniTest->iIniDocument->CompareDocs(*referenceDoc));
   251 			CleanupStack::PopAndDestroy(iniTest);
   252 			__UHEAP_SETFAIL(RHeap::ENone, 0);
   253 			__UHEAP_MARKEND;
   254 			continue;
   255 			}
   256 		else
   257 			{
   258 			//Open a file and Externalise to it.
   259 			TRAP(err, iniTest->iIniDocument->Externalise(_L("c:\\initest\\oom.ini")));
   260 			if (err != KErrNone)
   261 				{
   262 				CleanupStack::PopAndDestroy(iniTest);
   263 				__UHEAP_SETFAIL(RHeap::ENone, 0);
   264 				__UHEAP_MARKEND;
   265 				continue;
   266 				}
   267 			else
   268 				{
   269 				TBool result=EFalse;
   270 				TRAP(err, result=CompareFilesL(TheRFs,_L("c:\\initest\\oom_ref.ini"), _L("c:\\initest\\oom.ini")));
   271 				if (err != KErrNone)
   272 					{
   273 					CleanupStack::PopAndDestroy(iniTest);
   274 					__UHEAP_SETFAIL(RHeap::ENone, 0);
   275 					__UHEAP_MARKEND;
   276 					continue;
   277 					}
   278 				test(result == aShouldBeSame);
   279 				}
   280 			}
   281 		CleanupStack::PopAndDestroy(iniTest);
   282 		//check we haven't leaked any heap memory
   283 		__UHEAP_MARKEND;
   284 		CheckResources(TheRFs, startThreadHandleCount);
   285 		
   286 		if (err != KErrNoMemory)
   287 			{
   288 			test(err == KErrNone);
   289 			break;		// we reach here if we are unable to create the OOM condition.
   290 			}
   291 		 
   292 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   293 		} 
   294 		
   295 	__UHEAP_RESET;
   296 	CleanupStack::PopAndDestroy(referenceDoc);
   297 	
   298 	test.Printf(_L("Completed consistency check."));
   299 	}
   300 
   301 void CIniParser8Test::CreateDeleteOOMTestL()
   302 	{
   303 	TInt err;
   304 	TInt tryCount = 0;
   305 	do
   306 		{
   307 		__UHEAP_MARK;
   308 
   309 		// find out the number of open handles
   310 		TInt startProcessHandleCount;
   311 		TInt startThreadHandleCount;
   312 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   313 
   314 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   315 
   316 		CIniDocument8* ini=NULL;
   317 		TRAP(err,ini=CIniDocument8::NewL(TheRFs,KIniFile8));
   318 
   319 		delete ini;
   320 
   321 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   322 
   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);
   329 
   330 		__UHEAP_MARKEND;
   331 		} while(err == KErrNoMemory);
   332 
   333 	test(err==KErrNone);
   334 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
   335 	}
   336 
   337 
   338 void CIniParser8Test::CreateDeleteTest1L()
   339 	{
   340 	__UHEAP_MARK;
   341 
   342 	CIniDocument8* ini=NULL;
   343 	//note only support 16 bit Little Endian ini file
   344 	ini=CIniDocument8::NewL(TheRFs,KIniFile8);
   345 
   346 	delete ini;
   347 
   348 	__UHEAP_MARKEND;
   349 	}
   350 
   351 void CIniParser8Test::CreateDeleteOOMTest2L()
   352 	{
   353 	TInt err;
   354 	TInt tryCount = 0;
   355 	do
   356 		{
   357 		__UHEAP_MARK;
   358 
   359 		// find out the number of open handles
   360 		TInt startProcessHandleCount;
   361 		TInt startThreadHandleCount;
   362 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   363 
   364 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   365 
   366 		CIniFile8* ini=NULL;
   367 		TRAP(err,ini=CIniFile8::NewL(TheRFs,KIniFile8));
   368 
   369 		delete ini;
   370 
   371 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   372 
   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);
   379 
   380 		__UHEAP_MARKEND;
   381 		} while(err == KErrNoMemory);
   382 
   383 	test(err==KErrNone);
   384 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
   385 	}
   386 
   387 
   388 void CIniParser8Test::CreateDeleteTest2L()
   389 	{
   390 	__UHEAP_MARK;
   391 
   392 	CIniFile8* ini=NULL;
   393 	//note only support 16 bit Little Endian ini file
   394 	ini=CIniFile8::NewL(TheRFs,KIniFile8);
   395 
   396 	delete ini;
   397 
   398 	__UHEAP_MARKEND;
   399 	}
   400 
   401 void CIniParser8Test::DoTest1L()
   402 	{
   403 	//Testing GetSectionList API
   404 	RArray<TPtrC8> sectionNames;
   405 	User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
   406 	test(sectionNames.Count()==8);
   407 
   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();
   418 	}
   419 
   420 void CIniParser8Test::DoTest2L()
   421 	{
   422 	//Test GetKeyValue API
   423 	TPtrC8 value;
   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);
   430 
   431 	//unknown section
   432 	TInt ret=KErrNone;
   433 	ret=iIniDocument->GetKeyValue(_L8("mySection"),_L8("mykey"),value);
   434 	LeaveIfNoMemory(ret);
   435 	test(ret==KErrNotFound);
   436 	//unknown key
   437 	ret=iIniDocument->GetKeyValue(_L8("MEDIA"),_L8("mykey"),value);
   438 	LeaveIfNoMemory(ret);
   439 	test(ret==KErrNotFound);
   440 	//empty value
   441 	ret=iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
   442 	LeaveIfNoMemory(ret);
   443 	test(value.Length()==0);
   444 	}
   445 
   446 void CIniParser8Test::DoTest3L()
   447 	{
   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);
   454 
   455 	//case sensitive
   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();
   464 	}
   465 
   466 void CIniParser8Test::DoTest4L()
   467 	{
   468 	//Test RemoveSection API
   469 	RArray<TPtrC8> sectionNames;
   470 	CleanupClosePushL(sectionNames);
   471 
   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);
   476 
   477 	//check key inside section is also deleted
   478 	TPtrC8 value;
   479 	TInt ret=iIniDocument->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
   480 	LeaveIfNoMemory(ret);
   481 	//Any section	
   482 	test(ret==KErrNotFound);
   483 
   484 	//unknown section
   485 	ret=iIniDocument->RemoveSection(_L8("AnySection"));
   486 	LeaveIfNoMemory(ret);
   487 	test(ret==KErrNotFound);
   488 	
   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);
   493 	
   494 	//unknown section	
   495 	ret=iIniDocument->RemoveSection(_L8("unknown_section3"));
   496 	LeaveIfNoMemory(ret);
   497 	test(ret==KErrNone);
   498 	
   499 	//Open a file and Externalise to it.
   500 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_3.ini")));
   501 	CleanupStack::PopAndDestroy();		
   502 	
   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);	
   507 	
   508 	ret=iniReRead->GetKeyValue(_L8("unknown_section3"),_L8("unknown_key3"),value);
   509 	LeaveIfNoMemory(ret);
   510 	test(ret==KErrNotFound);
   511 	CleanupStack::PopAndDestroy(iniReRead);
   512 	}
   513 
   514 void CIniParser8Test::DoTest5L()
   515 	{
   516 	//Testing SetKey API
   517 	TPtrC8 value;
   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);
   522 
   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);
   527 
   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);
   532 	
   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);	
   537 	
   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);	
   542 	
   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);
   547 	
   548 	//Open a file and Externalise to it.
   549 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_1.ini")));
   550 	
   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);	
   555 	
   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);
   565 	
   566 	}
   567 
   568 void CIniParser8Test::DoTest6L()
   569 	{
   570 	//Testing RemoveKey API
   571 	TPtrC8 value;
   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);
   577 
   578 	//remove non-exist key 
   579 	ret=iIniDocument->RemoveKey(_L8("OUTPUT_CHANNELS"),_L8("1"));
   580 	LeaveIfNoMemory(ret);
   581 	test(ret==KErrNotFound);
   582 
   583 	//remove non-exist section
   584 	ret=iIniDocument->RemoveKey(_L8("Non-existSection"),_L8("1"));
   585 	LeaveIfNoMemory(ret);
   586 	test(ret==KErrNotFound);
   587 	
   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);
   593 	
   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);
   598 	
   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);
   604 	
   605 	//Open a file and Externalise to it.
   606 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\unknown8_2.ini")));
   607 	
   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);	
   612 	
   613 	ret=iniReRead->GetKeyValue(_L8("unknown_section"),_L8("unknown_key"),value);
   614 	LeaveIfNoMemory(ret);
   615 	test(ret==KErrNotFound);
   616 	
   617 	ret=iniReRead->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
   618 	LeaveIfNoMemory(ret);
   619 	test(ret==KErrNotFound);
   620 	CleanupStack::PopAndDestroy(iniReRead);
   621 	}
   622 
   623 void CIniParser8Test::DoTest7L()
   624 	{
   625 	//Testing iterator class
   626 	CIniSecIter8* iIniSecIter=NULL;
   627 	TInt ret=KErrNone;
   628 
   629 	//unknown section
   630 	TRAP(ret,iIniSecIter=CIniSecIter8::NewL(_L8("Unknown"),iIniDocument));
   631 	LeaveIfNoMemory(ret);
   632 	test(ret==KErrNotFound);
   633 
   634 	//null document
   635 	TRAP(ret,iIniSecIter=CIniSecIter8::NewL(_L8("Unknown"),NULL));
   636 	LeaveIfNoMemory(ret);
   637 	test(ret==KErrArgument);
   638 
   639 	//known section
   640 	iIniSecIter=CIniSecIter8::NewL(_L8("test_section"),iIniDocument);
   641 	TPtrC8 key;
   642 	TPtrC8 value;
   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);
   666 
   667 	//test Reset()
   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);
   673 
   674 	delete iIniSecIter;
   675 	iIniSecIter=NULL;
   676 	}
   677 
   678 void CIniParser8Test::DoTest8L()
   679 	{
   680 	//Testing Externalise to ROM drive
   681 	TInt ret=iIniDocument->Externalise(_L("z:\\output.ini"));
   682 	LeaveIfNoMemory(ret);
   683 	test(ret==KErrAccessDenied);
   684 
   685 	//Testing Externalise to a New file
   686 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output8.ini")));
   687 
   688 	test(CompareFilesL(TheRFs,_L("z:\\resource\\testconfig8.ini"), _L("c:\\initest\\output8.ini")));
   689 	
   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")));
   694 
   695 	//Testing Externaliseing to the already exist file
   696 	User::LeaveIfError(output->Externalise(_L("c:\\initest\\output8.ini")));
   697 	CleanupStack::PopAndDestroy();
   698 
   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();
   704 	}
   705 
   706 void CIniParser8Test::DoTest9L()
   707 {
   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);
   713 	delete ini;
   714 }
   715 
   716 void CIniParser8Test::DoTest10L()
   717 {
   718 	TPtrC8 value;
   719 	//open existing ini file
   720 	CIniFile8* ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\testconfig8.ini"));
   721 	CleanupStack::PushL(ini);
   722 
   723 	//mid section
   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);
   734 
   735 	//first section
   736 	User::LeaveIfError(ini->FindVar(_L8("SERVERS"),_L8("SWTRACER"),value));
   737 	test(value.Compare(_L8(""))==0);
   738 
   739 	//last section
   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);
   744 
   745 	CleanupStack::PopAndDestroy();
   746 
   747 	//open a non existing file
   748 	TInt ret=KErrNone;
   749 	TRAP(ret,ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\nonexist.ini")));
   750 	LeaveIfNoMemory(ret);
   751 	test(ret==KErrNotFound);
   752 
   753 	//open an empty ini file
   754 	ini=CIniFile8::NewL(TheRFs,_L("z:\\resource\\empty8.ini"));
   755 	CleanupStack::PushL(ini);
   756 
   757 	ret=ini->FindVar(_L8("empty"),_L8("empty"),value);
   758 	LeaveIfNoMemory(ret);
   759 	test(ret==KErrNotFound);
   760 
   761 	CleanupStack::PopAndDestroy();
   762 }
   763 
   764 void CIniParser8Test::DoTest11L()
   765 	{
   766 	TPtrC8 value;
   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")));
   772 	}
   773 	
   774 void CIniParser8Test::DoTest12L()
   775 {
   776 	__UHEAP_MARK;
   777 	
   778 	CIniDocument8* Ori_ini = NULL;
   779 	CIniDocument8* Com_ini = NULL;
   780 	CIniDocument8* Cre_ini = NULL;
   781 	
   782 	
   783 	RFs rfs;	
   784 	rfs.Connect();
   785 	
   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);
   790 	
   791 	Ori_ini->SetKey(_L8("DEFECT"), _L8("Number"), _L8("127618"));
   792 	Ori_ini->Externalise( _L("c:\\initest\\CreateConfig8.ini") );
   793 	
   794 	TRAPD( err3, Cre_ini = CIniDocument8::NewL(rfs, _L("c:\\initest\\CreateConfig8.ini")) );
   795 	test(err3 == KErrNone);
   796 	
   797 	bool result = Cre_ini->CompareDocs(*Com_ini);
   798 	test(result==true);
   799 	
   800 	rfs.Close();
   801 	delete Ori_ini;
   802 	delete Com_ini;
   803 	delete Cre_ini;
   804 	
   805 	__UHEAP_MARKEND;	
   806 }
   807 
   808 static void DeleteFilesL()
   809 	{
   810 	CFileMan* fileman=CFileMan::NewL(TheRFs);
   811 
   812 	fileman->Delete(_L("c:\\initest\\*"));
   813 
   814 	delete fileman;
   815 	}
   816 
   817 /**
   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.
   824 @SYMREQ PREQ505
   825 */
   826 static void DoTestL()
   827 	{
   828 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1558 "));
   829 	DeleteFilesL();
   830 
   831 	//8 bit basic testing
   832 
   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"));
   843 	
   844 	//Defect 127618
   845 	CIniParser8Test::DoBasicTestL(&CIniParser8Test::DoTest12L,_L("New Line Missing"));
   846 								
   847 
   848 	//8 bit OOM testing
   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"));
   859 
   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"));
   866 
   867 	//Light weight temporary testing
   868 	TInt ret=KErrNone;
   869 	TTime startTime(0), stopTime(0);
   870 
   871 	TPtrC8 value;
   872 	startTime.UniversalTime();
   873 for (TInt i=0;i<1000;i++)
   874 	{
   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);
   878 	test(ret==KErrNone);
   879 	ret=ini->FindVar(_L8("test_section"),_L8("key2"),value);
   880 	test(value.Compare(_L8("value2"))==0);
   881 	test(ret==KErrNone);
   882 	ret=ini->FindVar(_L8("test_section"),_L8("key3"),value);
   883 	test(value.Compare(_L8("value3"))==0);
   884 	test(ret==KErrNone);
   885 	ret=ini->FindVar(_L8("test_section"),_L8("key4"),value);
   886 	test(value.Compare(_L8("value4"))==0);
   887 	test(ret==KErrNone);
   888 	ret=ini->FindVar(_L8("test_section"),_L8("key5"),value);
   889 	test(value.Compare(_L8("value value value"))==0);
   890 	test(ret==KErrNone);
   891 	ret=ini->FindVar(_L8("SERVERS"),_L8("SWTRACER"),value);
   892 	test(value.Compare(_L8(""))==0);
   893 	test(ret==KErrNone);
   894 	ret=ini->FindVar(_L8("1"),_L8("timestamps"),value);
   895 	test(value.Compare(_L8("0"))==0);
   896 	test(ret==KErrNone);
   897 	ret=ini->FindVar(_L8("1"),_L8("setting"),value);
   898 	test(value.Compare(_L8("value"))==0);
   899 	test(ret==KErrNone);
   900 	delete ini;
   901 	}
   902 	stopTime.UniversalTime();
   903 	TTimeIntervalMicroSeconds timeTaken = stopTime.MicroSecondsFrom(startTime);
   904 	test.Printf(_L("Time taken for Light= %d microseconds\n"), timeTaken.Int64() );
   905 
   906 	//heavy weight
   907 
   908 	startTime.UniversalTime();
   909 for (TInt j=0;j<1000;j++)
   910 	{
   911 	CIniDocument8* dom=CIniDocument8::NewL(TheRFs,KIniFile8);
   912 	ret=dom->GetKeyValue(_L8("test_section"),_L8("key1"),value);
   913 	test(value.Compare(_L8("value1"))==0);
   914 	test(ret==KErrNone);
   915 	ret=dom->GetKeyValue(_L8("test_section"),_L8("key2"),value);
   916 	test(value.Compare(_L8("value2"))==0);
   917 	test(ret==KErrNone);
   918 	ret=dom->GetKeyValue(_L8("test_section"),_L8("key3"),value);
   919 	test(value.Compare(_L8("value3"))==0);
   920 	test(ret==KErrNone);
   921 	ret=dom->GetKeyValue(_L8("test_section"),_L8("key4"),value);
   922 	test(value.Compare(_L8("value4"))==0);
   923 	test(ret==KErrNone);
   924 	ret=dom->GetKeyValue(_L8("test_section"),_L8("key5"),value);
   925 	test(value.Compare(_L8("value value value"))==0);
   926 	test(ret==KErrNone);
   927 	ret=dom->GetKeyValue(_L8("SERVERS"),_L8("SWTRACER"),value);
   928 	test(value.Compare(_L8(""))==0);
   929 	test(ret==KErrNone);
   930 	ret=dom->GetKeyValue(_L8("1"),_L8("timestamps"),value);
   931 	test(value.Compare(_L8("0"))==0);
   932 	test(ret==KErrNone);
   933 	ret=dom->GetKeyValue(_L8("1"),_L8("setting"),value);
   934 	test(value.Compare(_L8("value"))==0);
   935 	test(ret==KErrNone);
   936 	delete dom;
   937 	}
   938 
   939 	stopTime.UniversalTime();
   940 	timeTaken = stopTime.MicroSecondsFrom(startTime);
   941 	test.Printf(_L("Time taken for Heavy= %d microseconds\n"), timeTaken.Int64() );	
   942 	
   943 	startTime.UniversalTime();	
   944 	// Consistency checks
   945 	CIniParser8Test::DoOOMWithConsistencyCheckTestL(&CIniParser8Test::DoTest11L,_L("Consistency8-OOMC"), FALSE);
   946 
   947 	stopTime.UniversalTime();
   948 	timeTaken = stopTime.MicroSecondsFrom(startTime);
   949 	test.Printf(_L("Time taken for consistency checks= %d microseconds\n"), timeTaken.Int64() );
   950 	DeleteFilesL();	
   951 	}
   952 
   953 GLDEF_C TInt E32Main()
   954 	{
   955 	__UHEAP_MARK;
   956 	CTrapCleanup* trapCleanup=CTrapCleanup::New();
   957 	test(TheRFs.Connect()==KErrNone);
   958 	test.Start(_L("MyTest"));
   959 
   960 	TRAPD(error, DoTestL());
   961 	test(error == KErrNone);
   962 
   963 
   964 	TheRFs.Close();
   965 	test.End();
   966 	test.Close();
   967 	delete trapCleanup;
   968 	__UHEAP_MARKEND;
   969 	return error;
   970 	}
   971