os/ossrv/lowlevellibsandfws/apputils/bsul/test/t_iniparser/T_IniParser16.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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(KIniFile16,"z:\\resource\\testconfig16.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 	RFile fileA;
    55 	User::LeaveIfError(fileA.Open(aFs,aFileNameA,EFileShareReadersOrWriters ));
    56 	CleanupClosePushL(fileA);
    57 	RFile fileB;
    58 	User::LeaveIfError(fileB.Open(aFs,aFileNameB,EFileShareReadersOrWriters ));
    59 	CleanupClosePushL(fileB);
    60 			
    61 	TInt filesizeA=0;
    62 	TInt filesizeB=0;
    63 	fileA.Size(filesizeA);
    64 	fileB.Size(filesizeB);
    65 	if ( filesizeA == filesizeB)
    66 		{
    67 		HBufC8* aBufferPtrA=HBufC8::NewLC(filesizeA);
    68 		HBufC8* aBufferPtrB=HBufC8::NewLC(filesizeB);
    69 		TPtr8 asWriteableBufferA(aBufferPtrA->Des());
    70 		User::LeaveIfError(fileA.Read(0,asWriteableBufferA,filesizeA));
    71 		TPtr8 asWriteableBufferB(aBufferPtrB->Des());
    72 		User::LeaveIfError(fileB.Read(0,asWriteableBufferB,filesizeB));
    73 		if (asWriteableBufferA.Compare(asWriteableBufferB) == 0)
    74 			{
    75 			filesAreSame=TRUE;
    76 			}
    77 		CleanupStack::PopAndDestroy(2);
    78 		}
    79 	CleanupStack::PopAndDestroy(2);	
    80 	return filesAreSame;
    81 	}
    82 
    83 class CIniParser16Test :public CBase
    84 {
    85 typedef void (CIniParser16Test::*ClassFuncPtr8L) (void);
    86 
    87 public:
    88 	static CIniParser16Test* NewL();
    89 	~CIniParser16Test()
    90 	{
    91 	if (iIniDocument)
    92 		{
    93 		delete iIniDocument;
    94 		iIniDocument=NULL;
    95 		}
    96 	}
    97 	
    98 	//heavy create and delete
    99 	static void CreateDeleteTest1L();
   100 	static void CreateDeleteOOMTestL();	
   101 	
   102 	//light
   103 	static void CreateDeleteTest2L();
   104 	static void CreateDeleteOOMTest2L();	
   105 	
   106 	//List of tests
   107 	//heavy
   108 	void DoTest1L();
   109 	void DoTest2L();
   110 	void DoTest3L();
   111 	void DoTest4L();
   112 	void DoTest5L();
   113 	void DoTest6L();
   114 	void DoTest7L();
   115 	void DoTest8L();
   116 	void DoTest9L();
   117 	//light
   118 	void DoTest10L();
   119 	void DoTest11L();
   120 	
   121 	//OOM consistency test
   122 	void DoTest12L();
   123 			
   124 	//class function utilities
   125 	static void DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);	
   126 	static void DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);
   127 	static void DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc,const TBool aShouldBeSame);
   128 private:
   129 	CIniParser16Test():iIniDocument(NULL){}
   130 	CIniDocument16* iIniDocument;
   131 };
   132 
   133 CIniParser16Test* CIniParser16Test::NewL()
   134 	{
   135 	CIniParser16Test* self=new (ELeave)CIniParser16Test();
   136 	CleanupStack::PushL(self);
   137 	self->iIniDocument=CIniDocument16::NewL(TheRFs,KIniFile16);		
   138 	CleanupStack::Pop();
   139 	return self;
   140 	}
   141 
   142 void CIniParser16Test::DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
   143 	{
   144 	test.Next(aTestDesc);
   145 
   146 	__UHEAP_MARK;
   147   	// find out the number of open handles
   148 	TInt startProcessHandleCount;
   149 	TInt startThreadHandleCount;
   150 	RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   151 	
   152 	CIniParser16Test* iniTest=CIniParser16Test::NewL();
   153 
   154 	CleanupStack::PushL(iniTest);
   155 
   156 	(iniTest->*testFuncL)();
   157 
   158 	CleanupStack::PopAndDestroy();
   159 
   160 	// check that no handles have leaked
   161 	TInt endProcessHandleCount;
   162 	TInt endThreadHandleCount;
   163 	RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   164 
   165 	test(startProcessHandleCount == endProcessHandleCount);
   166 	test(startThreadHandleCount  == endThreadHandleCount);
   167 
   168 	__UHEAP_MARKEND;
   169 	}
   170 
   171 void CIniParser16Test::DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
   172 	{
   173 	test.Next(aTestDesc);
   174 
   175 	TInt err;
   176 	TInt tryCount = 0;
   177 	do
   178 		{
   179 		__UHEAP_MARK;
   180   		// find out the number of open handles
   181 		TInt startProcessHandleCount;
   182 		TInt startThreadHandleCount;
   183 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   184 
   185 		CIniParser16Test* iniTest=CIniParser16Test::NewL();
   186 		CleanupStack::PushL(iniTest);
   187 
   188 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   189 		TRAP(err, (iniTest->*testFuncL)());
   190 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   191 
   192 		CleanupStack::PopAndDestroy(iniTest);
   193 		iniTest=NULL;
   194 		// check that no handles have leaked
   195 		TInt endProcessHandleCount;
   196 		TInt endThreadHandleCount;
   197 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   198 
   199 		test(startProcessHandleCount == endProcessHandleCount);
   200 		test(startThreadHandleCount  == endThreadHandleCount);
   201 
   202 		__UHEAP_MARKEND;
   203 		} while(err == KErrNoMemory);
   204 
   205  	test(err==KErrNone);
   206 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
   207 	}
   208 
   209 void CIniParser16Test::CreateDeleteOOMTestL()
   210 	{
   211 	TInt err;
   212 	TInt tryCount = 0;
   213 	do
   214 		{
   215 		__UHEAP_MARK;
   216 
   217 		// find out the number of open handles
   218 		TInt startProcessHandleCount;
   219 		TInt startThreadHandleCount;
   220 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   221 						
   222 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   223 
   224 		CIniDocument16* ini=NULL;
   225 		TRAP(err,ini=CIniDocument16::NewL(TheRFs,KIniFile16));
   226 		
   227 		delete ini;
   228 		
   229 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   230 		
   231 		// check that no handles have leaked
   232 		TInt endProcessHandleCount;
   233 		TInt endThreadHandleCount;
   234 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   235 		test(startProcessHandleCount == endProcessHandleCount);
   236 		test(startThreadHandleCount  == endThreadHandleCount);
   237 		
   238 		__UHEAP_MARKEND;
   239 		} while(err == KErrNoMemory);
   240 
   241 	test(err==KErrNone);
   242 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);	
   243 	}
   244 
   245 void CIniParser16Test::DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc, const TBool aShouldBeSame)
   246 	{
   247 	test.Next(aTestDesc);
   248 	
   249 	// find out the number of open handles
   250 	TInt startProcessHandleCount = 0;
   251 	TInt startThreadHandleCount = 0;
   252 	RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   253 	
   254 	//Compare the results against this instance.
   255 	TInt err;
   256 	CIniDocument16* referenceDoc=NULL;
   257 	TRAP(err,referenceDoc=CIniDocument16::NewL(TheRFs,KIniFile16));
   258 	User::LeaveIfError(err);
   259 	CleanupStack::PushL(referenceDoc);
   260 	
   261 	//Open a file and Externalise the reference oom to it.
   262 	User::LeaveIfError(referenceDoc->Externalise(_L("c:\\initest\\oom_ref16.ini")));
   263 	CIniParser16Test* iniTest=NULL;
   264 	
   265 	for (TInt i = 1;;i++)
   266 		{
   267 		__UHEAP_FAILNEXT(i);
   268 		__UHEAP_MARK;
   269 		
   270 		TRAP(err, iniTest=CIniParser16Test::NewL());
   271 		if (err != KErrNone)
   272 			continue;
   273 		
   274 		CleanupStack::PushL(iniTest);
   275 		
   276 		TRAP(err, (iniTest->*testFuncL)());
   277 		if (err != KErrNone)
   278 			{
   279 			test(iniTest->iIniDocument->CompareDocs(*referenceDoc));
   280 			CleanupStack::PopAndDestroy(iniTest);
   281 			__UHEAP_SETFAIL(RHeap::ENone, 0);
   282 			__UHEAP_MARKEND;
   283 			continue;
   284 			}
   285 		else
   286 			{
   287 			//Open a file and Externalise to it.
   288 			TRAP(err, iniTest->iIniDocument->Externalise(_L("c:\\initest\\oom16.ini")));
   289 			if (err != KErrNone)
   290 				{
   291 				CleanupStack::PopAndDestroy(iniTest);
   292 				__UHEAP_SETFAIL(RHeap::ENone, 0);
   293 				__UHEAP_MARKEND;
   294 				continue;
   295 				}
   296 			else
   297 				{
   298 				TBool result=EFalse;
   299 				TRAP(err, result=CompareFilesL(TheRFs,_L("c:\\initest\\oom_ref16.ini"), _L("c:\\initest\\oom16.ini")));
   300 				if (err != KErrNone)
   301 					{
   302 					CleanupStack::PopAndDestroy(iniTest);
   303 					__UHEAP_SETFAIL(RHeap::ENone, 0);
   304 					__UHEAP_MARKEND;
   305 					continue;
   306 					}
   307 				test(result == aShouldBeSame);
   308 				}
   309 			}
   310 		CleanupStack::PopAndDestroy(iniTest);
   311 		//check we haven't leaked any heap memory
   312 		__UHEAP_MARKEND;
   313 		CheckResources(TheRFs, startThreadHandleCount);
   314 		
   315 		if (err != KErrNoMemory)
   316 			{
   317 			test(err == KErrNone);
   318 			break;		// we reach here if we are unable to create the OOM condition.
   319 			}
   320 			
   321 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   322 		}
   323 	__UHEAP_RESET;
   324 	CleanupStack::PopAndDestroy(referenceDoc);
   325 	
   326 	test.Printf(_L("Completed consistency check."));
   327 	}
   328 
   329 void CIniParser16Test::CreateDeleteTest1L()
   330 	{
   331 	__UHEAP_MARK;
   332 
   333 	CIniDocument16* ini=NULL;
   334 	//note only support 16 bit Little Endian ini file
   335 	ini=CIniDocument16::NewL(TheRFs,KIniFile16);
   336 	
   337 	delete ini;
   338 	
   339 	__UHEAP_MARKEND;
   340 	}
   341 
   342 void CIniParser16Test::CreateDeleteOOMTest2L()
   343 	{
   344 	TInt err;
   345 	TInt tryCount = 0;
   346 	do
   347 		{
   348 		__UHEAP_MARK;
   349 
   350 		// find out the number of open handles
   351 		TInt startProcessHandleCount;
   352 		TInt startThreadHandleCount;
   353 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   354 						
   355 		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   356 
   357 		CIniFile16* ini=NULL;
   358 		TRAP(err,ini=CIniFile16::NewL(TheRFs,KIniFile16));
   359 		
   360 		delete ini;
   361 		
   362 		__UHEAP_SETFAIL(RHeap::ENone, 0);
   363 		
   364 		// check that no handles have leaked
   365 		TInt endProcessHandleCount;
   366 		TInt endThreadHandleCount;
   367 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   368 		test(startProcessHandleCount == endProcessHandleCount);
   369 		test(startThreadHandleCount  == endThreadHandleCount);
   370 		
   371 		__UHEAP_MARKEND;
   372 		} while(err == KErrNoMemory);
   373 
   374 	test(err==KErrNone);
   375 	test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);	
   376 	}
   377 
   378 
   379 void CIniParser16Test::CreateDeleteTest2L()
   380 	{
   381 	__UHEAP_MARK;
   382 
   383 	CIniFile16* ini=NULL;
   384 	//note only support 16 bit Little Endian ini file
   385 	ini=CIniFile16::NewL(TheRFs,KIniFile16);
   386 	
   387 	delete ini;
   388 	
   389 	__UHEAP_MARKEND;
   390 	}	
   391 
   392 
   393 void CIniParser16Test::DoTest1L()
   394 	{
   395 	//Testing GetSectionList API
   396 	RArray<TPtrC16> sectionNames;
   397 	User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
   398 	test(sectionNames.Count()==8);	
   399 	
   400 	//Testing the sectionNames in name order
   401 	test(sectionNames[0].Compare(_L("1"))==0);
   402 	test(sectionNames[1].Compare(_L("MAPPINGS"))==0);
   403 	test(sectionNames[2].Compare(_L("MEDIA"))==0);
   404 	test(sectionNames[3].Compare(_L("OUTPUT_CHANNELS"))==0);
   405 	test(sectionNames[4].Compare(_L("SERVERS"))==0);
   406 	test(sectionNames[5].Compare(_L("SWTRACER"))==0);
   407 	test(sectionNames[6].Compare(_L("test_section"))==0);
   408 	test(sectionNames[7].Compare(_L("test_twosection"))==0);	
   409 	sectionNames.Reset();
   410 	}
   411 
   412 void CIniParser16Test::DoTest2L()
   413 	{
   414 	//Test GetKeyValue API
   415 	TPtrC16 value;
   416 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("RDebug"),value));
   417 	test(value.Compare(_L("SwtRDebugPlugin.dll"))==0);
   418 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("OUTPUT_CHANNELS"),_L("1"),value));
   419 	test(value.Compare(_L("RDebug"))==0);
   420 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("1"),_L("new_setting"),value));
   421 	test(value.Compare(_L("value \\n value1\\t value2"))==0);
   422 	
   423 	//unknown section
   424 	TInt ret=KErrNone;
   425 	ret=iIniDocument->GetKeyValue(_L("mySection"),_L("mykey"),value);
   426 	LeaveIfNoMemory(ret);
   427 	test(ret==KErrNotFound);
   428 	//unknown key
   429 	ret=iIniDocument->GetKeyValue(_L("MEDIA"),_L("mykey"),value);
   430 	LeaveIfNoMemory(ret);
   431 	test(ret==KErrNotFound);	
   432 	//empty value
   433 	ret=iIniDocument->GetKeyValue(_L("SERVERS"),_L("SWTRACER"),value);
   434 	LeaveIfNoMemory(ret);
   435 	test(value.Length()==0);	
   436 	}
   437 
   438 void CIniParser16Test::DoTest3L()
   439 	{
   440 	//Test AddSection API
   441 	RArray<TPtrC16> sectionNames;
   442 	CleanupClosePushL(sectionNames);
   443 	User::LeaveIfError(iIniDocument->AddSection(_L("NEW-SECTION")));
   444 	User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
   445 	test(sectionNames.Count()==9);
   446 
   447 	//case sensitive
   448 	User::LeaveIfError(iIniDocument->AddSection(_L("NeW-SECTION")));	
   449 	User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
   450 	test(sectionNames.Count()==10);	
   451 	//adding existing section, no duplicate allowed
   452 	TInt ret=iIniDocument->AddSection(_L("NEW-SECTION"));
   453 	LeaveIfNoMemory(ret);
   454 	test(ret==KErrAlreadyExists);
   455 	CleanupStack::PopAndDestroy();	
   456 	}
   457 
   458 void CIniParser16Test::DoTest4L()
   459 	{
   460 	//Test RemoveSection API
   461 	RArray<TPtrC16> sectionNames;
   462 	CleanupClosePushL(sectionNames);	
   463 	
   464 	//known section
   465 	User::LeaveIfError(iIniDocument->RemoveSection(_L("SERVERS")));
   466 	User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
   467 	test(sectionNames.Count()==7);
   468 	
   469 	//check key inside section is also deleted
   470 	TPtrC16 value;
   471 	TInt ret=iIniDocument->GetKeyValue(_L("SERVERS"),_L("SWTRACER"),value);
   472 	LeaveIfNoMemory(ret);
   473 	test(ret==KErrNotFound);	
   474 	
   475 	//unknown section	
   476 	ret=iIniDocument->RemoveSection(_L("AnySection"));
   477 	LeaveIfNoMemory(ret);
   478 	test(ret==KErrNotFound);
   479 	
   480 	CleanupStack::PopAndDestroy();		
   481 	}
   482 	
   483 void CIniParser16Test::DoTest5L()
   484 	{
   485 	//Testing SetKey API
   486 	TPtrC16 value;
   487 	//Modifying existing value
   488 	User::LeaveIfError(iIniDocument->SetKey(_L("MEDIA"),_L("RDebug"),_L("NewPlugin.dll")));
   489 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("RDebug"),value));
   490 	test(value.Compare(_L("NewPlugin.dll"))==0);
   491 	
   492 	//nonexist key should be created
   493 	User::LeaveIfError(iIniDocument->SetKey(_L("MEDIA"),_L("newplug"),_L("")));
   494 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("newplug"),value));		
   495 	test(value.Compare(_L(""))==0);	
   496 
   497 	//nonexist section should be created
   498 	User::LeaveIfError(iIniDocument->SetKey(_L("unknown_section"),_L("unknown_key"),_L("unknown_value")));
   499 	User::LeaveIfError(iIniDocument->GetKeyValue(_L("unknown_section"),_L("unknown_key"),value));
   500 	test(value.Compare(_L("unknown_value"))==0);	
   501 	
   502 	//Testing Externalise to a New file
   503 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output16_1.ini")));
   504 
   505 	//Try opening the written ini file now to ensure no corruption in writing
   506 	CIniDocument16* iniReRead=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16_1.ini"));
   507 	CleanupStack::PushL(iniReRead);
   508 	User::LeaveIfError(iniReRead->GetKeyValue(_L("unknown_section"),_L("unknown_key"),value));
   509 	test(value.Compare(_L("unknown_value"))==0);	
   510 	User::LeaveIfError(iniReRead->GetKeyValue(_L("MEDIA"),_L("newplug"),value));		
   511 	test(value.Compare(_L(""))==0);	
   512 	
   513 	CleanupStack::PopAndDestroy(iniReRead);
   514 	}
   515 	
   516 void CIniParser16Test::DoTest6L()
   517 	{
   518 	//Testing RemoveKey API
   519 	TPtrC16 value;
   520 	//remove existing key
   521 	User::LeaveIfError(iIniDocument->RemoveKey(_L("OUTPUT_CHANNELS"),_L("1")));
   522 	TInt ret=iIniDocument->GetKeyValue(_L("OUTPUT_CHANNELS"),_L("1"),value);
   523 	LeaveIfNoMemory(ret);
   524 	test(ret==KErrNotFound);	
   525 	
   526 	//remove non-exist key
   527 	ret=iIniDocument->RemoveKey(_L("OUTPUT_CHANNELS"),_L("1"));
   528 	LeaveIfNoMemory(ret);
   529 	test(ret==KErrNotFound);	
   530 	
   531 	//remove non-exist section
   532 	ret=iIniDocument->RemoveKey(_L("Non-existSection"),_L("1"));
   533 	LeaveIfNoMemory(ret);
   534 	test(ret==KErrNotFound);	
   535 	}
   536 
   537 void CIniParser16Test::DoTest7L()
   538 	{
   539 	//Testing iterator class
   540 	CIniSecIter16* iIniSecIter=NULL;
   541 	TInt ret=KErrNone;
   542 	
   543 	//unknown section
   544 	TRAP(ret,iIniSecIter=CIniSecIter16::NewL(_L("Unknown"),iIniDocument));
   545 	LeaveIfNoMemory(ret);
   546 	test(ret==KErrNotFound);
   547 	
   548 	//null document
   549 	TRAP(ret,iIniSecIter=CIniSecIter16::NewL(_L("Unknown"),NULL));
   550 	LeaveIfNoMemory(ret);
   551 	test(ret==KErrArgument);				
   552 	
   553 	//known section
   554 	iIniSecIter=CIniSecIter16::NewL(_L("test_section"),iIniDocument);
   555 	TPtrC16 key;
   556 	TPtrC16 value;
   557 	//test Next() and End();
   558 	test(!iIniSecIter->End());
   559 	test(iIniSecIter->Next(key,value));
   560 	test(key.Compare(_L("key1"))==0);
   561 	test(value.Compare(_L("value1"))==0);	
   562 	test(!iIniSecIter->End());	
   563 	test(iIniSecIter->Next(key,value));
   564 	test(key.Compare(_L("key2"))==0);
   565 	test(value.Compare(_L("value2"))==0);
   566 	test(!iIniSecIter->End());	
   567 	test(iIniSecIter->Next(key,value));
   568 	test(key.Compare(_L("key3"))==0);
   569 	test(value.Compare(_L("value3"))==0);
   570 	test(!iIniSecIter->End());	
   571 	test(iIniSecIter->Next(key,value));
   572 	test(key.Compare(_L("key4"))==0);
   573 	test(value.Compare(_L("value4"))==0);
   574 	test(!iIniSecIter->End());	
   575 	test(iIniSecIter->Next(key,value));
   576 	test(key.Compare(_L("key5"))==0);
   577 	test(value.Compare(_L("value value value"))==0);			
   578 	test(iIniSecIter->End());	
   579 	test(iIniSecIter->Next(key,value)==EFalse);
   580 		
   581 	//test Reset()
   582 	iIniSecIter->Reset();
   583 	test(!iIniSecIter->End());	
   584 	test(iIniSecIter->Next(key,value));
   585 	test(key.Compare(_L("key1"))==0);
   586 	test(value.Compare(_L("value1"))==0);
   587 
   588 	delete iIniSecIter;
   589 	iIniSecIter=NULL;	
   590 	}
   591 	
   592 void CIniParser16Test::DoTest8L()
   593 	{
   594 	//Testing Externalise to ROM drive
   595 	TInt ret=iIniDocument->Externalise(_L("z:\\output16.ini"));
   596 	LeaveIfNoMemory(ret);
   597 	test(ret==KErrAccessDenied);
   598 	
   599 	//Testing Externalise to a New file
   600 	User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output16.ini")));
   601 
   602 	//Try opening the written ini file now to ensure no corruption in writing
   603 	CIniDocument16* output=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16.ini"));
   604 	CleanupStack::PushL(output);
   605 	User::LeaveIfError(output->SetKey(_L("Test"),_L("Test"),_L("Test")));
   606 
   607 	//Testing Externaliseing to the already exist file
   608 	User::LeaveIfError(output->Externalise(_L("c:\\initest\\output16_1.ini")));	
   609 	CleanupStack::PopAndDestroy();	
   610 	
   611 	//Opening an empty file and Externaliseing an empty file
   612 	output=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\unknown16.ini"));
   613 	CleanupStack::PushL(output);
   614 	User::LeaveIfError(output->Externalise(_L("c:\\initest\\unknown16_1.ini")));
   615 	CleanupStack::PopAndDestroy();	
   616 	
   617 	// Read  it back in and find the additions put in above.
   618 	CIniDocument16* iniReRead=NULL;
   619 	iniReRead=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16_1.ini"));
   620 	CleanupStack::PushL(iniReRead);	
   621 	
   622 	TPtrC16 value;
   623 	User::LeaveIfError(iniReRead->GetKeyValue(_L("Test"),_L("Test"),value));
   624 	test(value.Compare(_L("Test"))==0);
   625 	
   626 	CleanupStack::PopAndDestroy(iniReRead);
   627 	}
   628 
   629 void CIniParser16Test::DoTest9L()
   630 {
   631 	//Test for no leakage when handling corrupt file
   632 	CIniDocument16* ini=NULL;
   633 	TRAPD(err,ini=CIniDocument16::NewL(TheRFs,_L("z:\\resource\\corruptconfig16.ini")));
   634 	LeaveIfNoMemory(err);
   635 	test(err==KErrCorrupt);
   636 	delete ini;
   637 }
   638 
   639 void CIniParser16Test::DoTest10L()
   640 {
   641 	TPtrC16 value;
   642 	//open existing ini file
   643 	CIniFile16* ini=CIniFile16::NewL(TheRFs, _L16("z:\\resource\\testconfig16.ini"));
   644 	CleanupStack::PushL(ini);
   645 	
   646 	//mid section
   647 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key1"),value));
   648 	test(value.Compare(_L16("value1"))==0);
   649 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key2"),value));
   650 	test(value.Compare(_L16("value2"))==0);
   651 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key3"),value));
   652 	test(value.Compare(_L16("value3"))==0);
   653 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key4"),value));
   654 	test(value.Compare(_L16("value4"))==0);
   655 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key5"),value));
   656 	test(value.Compare(_L16("value value value"))==0);
   657 
   658 	//first section
   659 	User::LeaveIfError(ini->FindVar(_L16("SERVERS"),_L16("SWTRACER"),value));
   660 	test(value.Compare(_L16(""))==0);
   661 
   662 	//last section
   663 	User::LeaveIfError(ini->FindVar(_L16("1"),_L16("timestamps"),value));
   664 	test(value.Compare(_L16("0"))==0);
   665 	User::LeaveIfError(ini->FindVar(_L16("1"),_L16("setting"),value));
   666 	test(value.Compare(_L16("value"))==0);
   667 
   668 	CleanupStack::PopAndDestroy();
   669 	
   670 	//open a non existing file
   671 	TInt ret=KErrNone;
   672 	TRAP(ret,ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\nonexist.ini")));	
   673 	LeaveIfNoMemory(ret);
   674 	test(ret==KErrNotFound);
   675 	
   676 	//open an empty ini file
   677 	ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\empty16.ini"));
   678 	CleanupStack::PushL(ini);	
   679 	
   680 	ret=ini->FindVar(_L16("empty"),_L16("empty"),value);
   681 	LeaveIfNoMemory(ret);
   682 	test(ret==KErrNotFound);	
   683 
   684 	CleanupStack::PopAndDestroy();	
   685 }
   686 
   687 void CIniParser16Test::DoTest11L()
   688 	{
   689 	TPtrC16 value;
   690 	//open existing 8 bit ini file with 16 bit reader
   691 	CIniFile16* ini;
   692 	TInt err;
   693 	TRAP(err,ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\testconfig8.ini")));
   694 	test(err==KErrCorrupt || err==KErrNoMemory);
   695 	
   696 	//open existing 8 bit ini file, but allow conversion to 16 bits
   697 	ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\testconfig8.ini"),ETrue);
   698 	CleanupStack::PushL(ini);
   699 	
   700 	//mid section
   701 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key1"),value));
   702 	test(value.Compare(_L16("value1"))==0);
   703 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key2"),value));
   704 	test(value.Compare(_L16("value2"))==0);
   705 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key3"),value));
   706 	test(value.Compare(_L16("value3"))==0);
   707 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key4"),value));
   708 	test(value.Compare(_L16("value4"))==0);
   709 	User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key5"),value));
   710 	test(value.Compare(_L16("value value value"))==0);
   711 
   712 	//first section
   713 	User::LeaveIfError(ini->FindVar(_L16("SERVERS"),_L16("SWTRACER"),value));
   714 	test(value.Compare(_L16(""))==0);
   715 
   716 	//last section
   717 	User::LeaveIfError(ini->FindVar(_L16("1"),_L16("timestamps"),value));
   718 	test(value.Compare(_L16("0"))==0);
   719 	User::LeaveIfError(ini->FindVar(_L16("1"),_L16("setting"),value));
   720 	test(value.Compare(_L16("value"))==0);	
   721 
   722 	CleanupStack::PopAndDestroy();	
   723 	}
   724 
   725 void CIniParser16Test::DoTest12L()
   726 	{
   727 	TPtrC16 value;
   728 	// We are trying to invoke an OOM condition for a single operation to test that the operation is atomic. 
   729 	// Under that condition the object should be rolled back to the original state. The resulting document should be the same 
   730 	// as before the condition was invoked. If the test succeeded, a new section should be created at end 
   731 	// (which is nice but not the real focus of the test).
   732 	User::LeaveIfError(iIniDocument->SetKey(_L16("unknown_section3"),_L16("unknown_key3"),_L16("unknown_value3")));
   733 	}
   734 
   735 static void DeleteFilesL()
   736 	{
   737 	CFileMan* fileman=CFileMan::NewL(TheRFs);
   738 
   739 	fileman->Delete(_L("c:\\initest\\*"));
   740 
   741 	delete fileman;
   742 	}
   743 
   744 /**
   745 @SYMTestCaseID		SYSLIB-BAFL-CT-1559
   746 @SYMTestCaseDesc 	Test CIniParser16Test
   747 @SYMTestPriority 	High
   748 @SYMTestActions  	Perform component tests on CIniParser16Test (includes OOM)
   749 			Testing all the exported apis.
   750 @SYMTestExpectedResults The test must not fail.
   751 @SYMREQ PREQ505
   752 */		
   753 static void DoTestL()
   754 	{
   755 	TTime startTime(0), stopTime(0);
   756 	TTimeIntervalMicroSeconds timeTaken;
   757 	
   758 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1559 "));
   759 	
   760 	DeleteFilesL();
   761 	//16 bit basic testing
   762 	CIniParser16Test::CreateDeleteTest1L();
   763 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest1L,_L("GetSectionList16"));
   764 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest2L,_L("GetKeyValue16"));
   765 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest3L,_L("AddSection16"));
   766 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest4L,_L("RemoveSection16"));
   767 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest5L,_L("SetKeyValue16"));
   768 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest6L,_L("RemoveKey16"));
   769 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest7L,_L("IniSecIter16"));
   770 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest8L,_L("Externalise16"));
   771 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest9L,_L("Corrupt file16"));							
   772 	//16 bit OOM testing
   773 	CIniParser16Test::CreateDeleteOOMTestL();	
   774 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest1L,_L("GetSectionList16-OOM"));
   775 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest2L,_L("GetKeyValue16-OOM"));
   776 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest3L,_L("AddSection16-OOM"));	
   777 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest4L,_L("RemoveSection16-OOM"));
   778 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest5L,_L("SetKeyValue16-OOM"));				
   779 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest6L,_L("RemoveKey16-OOM"));
   780 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest7L,_L("IniSecIter16-OOM"));	
   781 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest8L,_L("Externalise16-OOM"));	
   782 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest9L,_L("Corrupt file16-OOM"));
   783 	
   784 	//16 bit light basic testing
   785 	CIniParser16Test::CreateDeleteTest2L();
   786 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest10L,_L("Light FindVar16"));
   787 	CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest11L,_L("Load 16"));
   788 	//16 bit light OOM testing	
   789 	CIniParser16Test::CreateDeleteOOMTest2L();
   790 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest10L,_L("Light FindVar-OOM16"));
   791 	CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest11L,_L("Load 16-OOM"));
   792 	stopTime.UniversalTime();
   793 	timeTaken = stopTime.MicroSecondsFrom(startTime);
   794 	test.Printf(_L("Time taken for Heavy= %d microseconds\n"), timeTaken.Int64() );	
   795 	startTime.UniversalTime();	
   796 	// Consistency checks
   797 	CIniParser16Test::DoOOMWithConsistencyCheckTestL(&CIniParser16Test::DoTest12L,_L("Consistency16-OOMC"), FALSE);
   798 
   799 	stopTime.UniversalTime();
   800 	timeTaken = stopTime.MicroSecondsFrom(startTime);
   801 	test.Printf(_L("Time taken for consistency checks= %d microseconds\n"), timeTaken.Int64() );	
   802 	}
   803 
   804 GLDEF_C TInt E32Main()
   805 	{
   806 	__UHEAP_MARK;
   807 	CTrapCleanup* trapCleanup=CTrapCleanup::New();
   808 	test(TheRFs.Connect()==KErrNone);
   809 	test.Start(_L("Ini File Parser Test"));
   810 
   811 	TRAPD(error, DoTestL());
   812 	test(error == KErrNone);
   813 	
   814 	
   815 	TheRFs.Close();
   816 	test.End();
   817 	test.Close();
   818 	delete trapCleanup;
   819 	__UHEAP_MARKEND;
   820 	return error;
   821 	}
   822