1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/bsul/test/t_iniparser/T_IniParser16.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,822 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// @internalComponent
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32std.h>
1.22 +#include <e32base.h>
1.23 +#include <e32test.h>
1.24 +#include <f32file.h>
1.25 +#include <bsul/inifile.h> // CIniDocument8, CIniDocument16
1.26 +
1.27 +RTest test(_L("Ini File Parser and Generator"));
1.28 +
1.29 +RFs TheRFs;
1.30 +
1.31 +using namespace BSUL;
1.32 +#define UNUSED_VAR(a) a=a
1.33 +
1.34 +_LIT(KIniFile16,"z:\\resource\\testconfig16.ini") ;
1.35 +
1.36 +void LeaveIfNoMemory(TInt ret)
1.37 + {
1.38 + if (ret==KErrNoMemory)
1.39 + User::LeaveNoMemory();
1.40 + }
1.41 +
1.42 +void CheckResources(RFs& aFs, TInt aOriginalHandleCount)
1.43 + {
1.44 + UNUSED_VAR(aOriginalHandleCount);
1.45 + //check we haven't leaked any handles
1.46 + TInt processHandleCount = 0;
1.47 + TInt threadHandleCount = 0;
1.48 + RThread().HandleCount(processHandleCount, threadHandleCount);
1.49 + test(threadHandleCount == aOriginalHandleCount);
1.50 + //check we haven't leaked any files
1.51 + aFs.ResourceCountMarkEnd();
1.52 + }
1.53 +
1.54 +TBool CompareFilesL(RFs& aFs,const TDesC& aFileNameA,const TDesC& aFileNameB)
1.55 + {
1.56 + TBool filesAreSame=FALSE;
1.57 + RFile fileA;
1.58 + User::LeaveIfError(fileA.Open(aFs,aFileNameA,EFileShareReadersOrWriters ));
1.59 + CleanupClosePushL(fileA);
1.60 + RFile fileB;
1.61 + User::LeaveIfError(fileB.Open(aFs,aFileNameB,EFileShareReadersOrWriters ));
1.62 + CleanupClosePushL(fileB);
1.63 +
1.64 + TInt filesizeA=0;
1.65 + TInt filesizeB=0;
1.66 + fileA.Size(filesizeA);
1.67 + fileB.Size(filesizeB);
1.68 + if ( filesizeA == filesizeB)
1.69 + {
1.70 + HBufC8* aBufferPtrA=HBufC8::NewLC(filesizeA);
1.71 + HBufC8* aBufferPtrB=HBufC8::NewLC(filesizeB);
1.72 + TPtr8 asWriteableBufferA(aBufferPtrA->Des());
1.73 + User::LeaveIfError(fileA.Read(0,asWriteableBufferA,filesizeA));
1.74 + TPtr8 asWriteableBufferB(aBufferPtrB->Des());
1.75 + User::LeaveIfError(fileB.Read(0,asWriteableBufferB,filesizeB));
1.76 + if (asWriteableBufferA.Compare(asWriteableBufferB) == 0)
1.77 + {
1.78 + filesAreSame=TRUE;
1.79 + }
1.80 + CleanupStack::PopAndDestroy(2);
1.81 + }
1.82 + CleanupStack::PopAndDestroy(2);
1.83 + return filesAreSame;
1.84 + }
1.85 +
1.86 +class CIniParser16Test :public CBase
1.87 +{
1.88 +typedef void (CIniParser16Test::*ClassFuncPtr8L) (void);
1.89 +
1.90 +public:
1.91 + static CIniParser16Test* NewL();
1.92 + ~CIniParser16Test()
1.93 + {
1.94 + if (iIniDocument)
1.95 + {
1.96 + delete iIniDocument;
1.97 + iIniDocument=NULL;
1.98 + }
1.99 + }
1.100 +
1.101 + //heavy create and delete
1.102 + static void CreateDeleteTest1L();
1.103 + static void CreateDeleteOOMTestL();
1.104 +
1.105 + //light
1.106 + static void CreateDeleteTest2L();
1.107 + static void CreateDeleteOOMTest2L();
1.108 +
1.109 + //List of tests
1.110 + //heavy
1.111 + void DoTest1L();
1.112 + void DoTest2L();
1.113 + void DoTest3L();
1.114 + void DoTest4L();
1.115 + void DoTest5L();
1.116 + void DoTest6L();
1.117 + void DoTest7L();
1.118 + void DoTest8L();
1.119 + void DoTest9L();
1.120 + //light
1.121 + void DoTest10L();
1.122 + void DoTest11L();
1.123 +
1.124 + //OOM consistency test
1.125 + void DoTest12L();
1.126 +
1.127 + //class function utilities
1.128 + static void DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);
1.129 + static void DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc);
1.130 + static void DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc,const TBool aShouldBeSame);
1.131 +private:
1.132 + CIniParser16Test():iIniDocument(NULL){}
1.133 + CIniDocument16* iIniDocument;
1.134 +};
1.135 +
1.136 +CIniParser16Test* CIniParser16Test::NewL()
1.137 + {
1.138 + CIniParser16Test* self=new (ELeave)CIniParser16Test();
1.139 + CleanupStack::PushL(self);
1.140 + self->iIniDocument=CIniDocument16::NewL(TheRFs,KIniFile16);
1.141 + CleanupStack::Pop();
1.142 + return self;
1.143 + }
1.144 +
1.145 +void CIniParser16Test::DoBasicTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
1.146 + {
1.147 + test.Next(aTestDesc);
1.148 +
1.149 + __UHEAP_MARK;
1.150 + // find out the number of open handles
1.151 + TInt startProcessHandleCount;
1.152 + TInt startThreadHandleCount;
1.153 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.154 +
1.155 + CIniParser16Test* iniTest=CIniParser16Test::NewL();
1.156 +
1.157 + CleanupStack::PushL(iniTest);
1.158 +
1.159 + (iniTest->*testFuncL)();
1.160 +
1.161 + CleanupStack::PopAndDestroy();
1.162 +
1.163 + // check that no handles have leaked
1.164 + TInt endProcessHandleCount;
1.165 + TInt endThreadHandleCount;
1.166 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.167 +
1.168 + test(startProcessHandleCount == endProcessHandleCount);
1.169 + test(startThreadHandleCount == endThreadHandleCount);
1.170 +
1.171 + __UHEAP_MARKEND;
1.172 + }
1.173 +
1.174 +void CIniParser16Test::DoOOMTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc)
1.175 + {
1.176 + test.Next(aTestDesc);
1.177 +
1.178 + TInt err;
1.179 + TInt tryCount = 0;
1.180 + do
1.181 + {
1.182 + __UHEAP_MARK;
1.183 + // find out the number of open handles
1.184 + TInt startProcessHandleCount;
1.185 + TInt startThreadHandleCount;
1.186 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.187 +
1.188 + CIniParser16Test* iniTest=CIniParser16Test::NewL();
1.189 + CleanupStack::PushL(iniTest);
1.190 +
1.191 + __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
1.192 + TRAP(err, (iniTest->*testFuncL)());
1.193 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.194 +
1.195 + CleanupStack::PopAndDestroy(iniTest);
1.196 + iniTest=NULL;
1.197 + // check that no handles have leaked
1.198 + TInt endProcessHandleCount;
1.199 + TInt endThreadHandleCount;
1.200 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.201 +
1.202 + test(startProcessHandleCount == endProcessHandleCount);
1.203 + test(startThreadHandleCount == endThreadHandleCount);
1.204 +
1.205 + __UHEAP_MARKEND;
1.206 + } while(err == KErrNoMemory);
1.207 +
1.208 + test(err==KErrNone);
1.209 + test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
1.210 + }
1.211 +
1.212 +void CIniParser16Test::CreateDeleteOOMTestL()
1.213 + {
1.214 + TInt err;
1.215 + TInt tryCount = 0;
1.216 + do
1.217 + {
1.218 + __UHEAP_MARK;
1.219 +
1.220 + // find out the number of open handles
1.221 + TInt startProcessHandleCount;
1.222 + TInt startThreadHandleCount;
1.223 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.224 +
1.225 + __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
1.226 +
1.227 + CIniDocument16* ini=NULL;
1.228 + TRAP(err,ini=CIniDocument16::NewL(TheRFs,KIniFile16));
1.229 +
1.230 + delete ini;
1.231 +
1.232 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.233 +
1.234 + // check that no handles have leaked
1.235 + TInt endProcessHandleCount;
1.236 + TInt endThreadHandleCount;
1.237 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.238 + test(startProcessHandleCount == endProcessHandleCount);
1.239 + test(startThreadHandleCount == endThreadHandleCount);
1.240 +
1.241 + __UHEAP_MARKEND;
1.242 + } while(err == KErrNoMemory);
1.243 +
1.244 + test(err==KErrNone);
1.245 + test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
1.246 + }
1.247 +
1.248 +void CIniParser16Test::DoOOMWithConsistencyCheckTestL(ClassFuncPtr8L testFuncL, const TDesC& aTestDesc, const TBool aShouldBeSame)
1.249 + {
1.250 + test.Next(aTestDesc);
1.251 +
1.252 + // find out the number of open handles
1.253 + TInt startProcessHandleCount = 0;
1.254 + TInt startThreadHandleCount = 0;
1.255 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.256 +
1.257 + //Compare the results against this instance.
1.258 + TInt err;
1.259 + CIniDocument16* referenceDoc=NULL;
1.260 + TRAP(err,referenceDoc=CIniDocument16::NewL(TheRFs,KIniFile16));
1.261 + User::LeaveIfError(err);
1.262 + CleanupStack::PushL(referenceDoc);
1.263 +
1.264 + //Open a file and Externalise the reference oom to it.
1.265 + User::LeaveIfError(referenceDoc->Externalise(_L("c:\\initest\\oom_ref16.ini")));
1.266 + CIniParser16Test* iniTest=NULL;
1.267 +
1.268 + for (TInt i = 1;;i++)
1.269 + {
1.270 + __UHEAP_FAILNEXT(i);
1.271 + __UHEAP_MARK;
1.272 +
1.273 + TRAP(err, iniTest=CIniParser16Test::NewL());
1.274 + if (err != KErrNone)
1.275 + continue;
1.276 +
1.277 + CleanupStack::PushL(iniTest);
1.278 +
1.279 + TRAP(err, (iniTest->*testFuncL)());
1.280 + if (err != KErrNone)
1.281 + {
1.282 + test(iniTest->iIniDocument->CompareDocs(*referenceDoc));
1.283 + CleanupStack::PopAndDestroy(iniTest);
1.284 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.285 + __UHEAP_MARKEND;
1.286 + continue;
1.287 + }
1.288 + else
1.289 + {
1.290 + //Open a file and Externalise to it.
1.291 + TRAP(err, iniTest->iIniDocument->Externalise(_L("c:\\initest\\oom16.ini")));
1.292 + if (err != KErrNone)
1.293 + {
1.294 + CleanupStack::PopAndDestroy(iniTest);
1.295 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.296 + __UHEAP_MARKEND;
1.297 + continue;
1.298 + }
1.299 + else
1.300 + {
1.301 + TBool result=EFalse;
1.302 + TRAP(err, result=CompareFilesL(TheRFs,_L("c:\\initest\\oom_ref16.ini"), _L("c:\\initest\\oom16.ini")));
1.303 + if (err != KErrNone)
1.304 + {
1.305 + CleanupStack::PopAndDestroy(iniTest);
1.306 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.307 + __UHEAP_MARKEND;
1.308 + continue;
1.309 + }
1.310 + test(result == aShouldBeSame);
1.311 + }
1.312 + }
1.313 + CleanupStack::PopAndDestroy(iniTest);
1.314 + //check we haven't leaked any heap memory
1.315 + __UHEAP_MARKEND;
1.316 + CheckResources(TheRFs, startThreadHandleCount);
1.317 +
1.318 + if (err != KErrNoMemory)
1.319 + {
1.320 + test(err == KErrNone);
1.321 + break; // we reach here if we are unable to create the OOM condition.
1.322 + }
1.323 +
1.324 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.325 + }
1.326 + __UHEAP_RESET;
1.327 + CleanupStack::PopAndDestroy(referenceDoc);
1.328 +
1.329 + test.Printf(_L("Completed consistency check."));
1.330 + }
1.331 +
1.332 +void CIniParser16Test::CreateDeleteTest1L()
1.333 + {
1.334 + __UHEAP_MARK;
1.335 +
1.336 + CIniDocument16* ini=NULL;
1.337 + //note only support 16 bit Little Endian ini file
1.338 + ini=CIniDocument16::NewL(TheRFs,KIniFile16);
1.339 +
1.340 + delete ini;
1.341 +
1.342 + __UHEAP_MARKEND;
1.343 + }
1.344 +
1.345 +void CIniParser16Test::CreateDeleteOOMTest2L()
1.346 + {
1.347 + TInt err;
1.348 + TInt tryCount = 0;
1.349 + do
1.350 + {
1.351 + __UHEAP_MARK;
1.352 +
1.353 + // find out the number of open handles
1.354 + TInt startProcessHandleCount;
1.355 + TInt startThreadHandleCount;
1.356 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.357 +
1.358 + __UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
1.359 +
1.360 + CIniFile16* ini=NULL;
1.361 + TRAP(err,ini=CIniFile16::NewL(TheRFs,KIniFile16));
1.362 +
1.363 + delete ini;
1.364 +
1.365 + __UHEAP_SETFAIL(RHeap::ENone, 0);
1.366 +
1.367 + // check that no handles have leaked
1.368 + TInt endProcessHandleCount;
1.369 + TInt endThreadHandleCount;
1.370 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.371 + test(startProcessHandleCount == endProcessHandleCount);
1.372 + test(startThreadHandleCount == endThreadHandleCount);
1.373 +
1.374 + __UHEAP_MARKEND;
1.375 + } while(err == KErrNoMemory);
1.376 +
1.377 + test(err==KErrNone);
1.378 + test.Printf(_L("- succeeded at heap failure rate of %i\n"), tryCount);
1.379 + }
1.380 +
1.381 +
1.382 +void CIniParser16Test::CreateDeleteTest2L()
1.383 + {
1.384 + __UHEAP_MARK;
1.385 +
1.386 + CIniFile16* ini=NULL;
1.387 + //note only support 16 bit Little Endian ini file
1.388 + ini=CIniFile16::NewL(TheRFs,KIniFile16);
1.389 +
1.390 + delete ini;
1.391 +
1.392 + __UHEAP_MARKEND;
1.393 + }
1.394 +
1.395 +
1.396 +void CIniParser16Test::DoTest1L()
1.397 + {
1.398 + //Testing GetSectionList API
1.399 + RArray<TPtrC16> sectionNames;
1.400 + User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
1.401 + test(sectionNames.Count()==8);
1.402 +
1.403 + //Testing the sectionNames in name order
1.404 + test(sectionNames[0].Compare(_L("1"))==0);
1.405 + test(sectionNames[1].Compare(_L("MAPPINGS"))==0);
1.406 + test(sectionNames[2].Compare(_L("MEDIA"))==0);
1.407 + test(sectionNames[3].Compare(_L("OUTPUT_CHANNELS"))==0);
1.408 + test(sectionNames[4].Compare(_L("SERVERS"))==0);
1.409 + test(sectionNames[5].Compare(_L("SWTRACER"))==0);
1.410 + test(sectionNames[6].Compare(_L("test_section"))==0);
1.411 + test(sectionNames[7].Compare(_L("test_twosection"))==0);
1.412 + sectionNames.Reset();
1.413 + }
1.414 +
1.415 +void CIniParser16Test::DoTest2L()
1.416 + {
1.417 + //Test GetKeyValue API
1.418 + TPtrC16 value;
1.419 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("RDebug"),value));
1.420 + test(value.Compare(_L("SwtRDebugPlugin.dll"))==0);
1.421 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("OUTPUT_CHANNELS"),_L("1"),value));
1.422 + test(value.Compare(_L("RDebug"))==0);
1.423 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("1"),_L("new_setting"),value));
1.424 + test(value.Compare(_L("value \\n value1\\t value2"))==0);
1.425 +
1.426 + //unknown section
1.427 + TInt ret=KErrNone;
1.428 + ret=iIniDocument->GetKeyValue(_L("mySection"),_L("mykey"),value);
1.429 + LeaveIfNoMemory(ret);
1.430 + test(ret==KErrNotFound);
1.431 + //unknown key
1.432 + ret=iIniDocument->GetKeyValue(_L("MEDIA"),_L("mykey"),value);
1.433 + LeaveIfNoMemory(ret);
1.434 + test(ret==KErrNotFound);
1.435 + //empty value
1.436 + ret=iIniDocument->GetKeyValue(_L("SERVERS"),_L("SWTRACER"),value);
1.437 + LeaveIfNoMemory(ret);
1.438 + test(value.Length()==0);
1.439 + }
1.440 +
1.441 +void CIniParser16Test::DoTest3L()
1.442 + {
1.443 + //Test AddSection API
1.444 + RArray<TPtrC16> sectionNames;
1.445 + CleanupClosePushL(sectionNames);
1.446 + User::LeaveIfError(iIniDocument->AddSection(_L("NEW-SECTION")));
1.447 + User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
1.448 + test(sectionNames.Count()==9);
1.449 +
1.450 + //case sensitive
1.451 + User::LeaveIfError(iIniDocument->AddSection(_L("NeW-SECTION")));
1.452 + User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
1.453 + test(sectionNames.Count()==10);
1.454 + //adding existing section, no duplicate allowed
1.455 + TInt ret=iIniDocument->AddSection(_L("NEW-SECTION"));
1.456 + LeaveIfNoMemory(ret);
1.457 + test(ret==KErrAlreadyExists);
1.458 + CleanupStack::PopAndDestroy();
1.459 + }
1.460 +
1.461 +void CIniParser16Test::DoTest4L()
1.462 + {
1.463 + //Test RemoveSection API
1.464 + RArray<TPtrC16> sectionNames;
1.465 + CleanupClosePushL(sectionNames);
1.466 +
1.467 + //known section
1.468 + User::LeaveIfError(iIniDocument->RemoveSection(_L("SERVERS")));
1.469 + User::LeaveIfError(iIniDocument->GetSectionList(sectionNames));
1.470 + test(sectionNames.Count()==7);
1.471 +
1.472 + //check key inside section is also deleted
1.473 + TPtrC16 value;
1.474 + TInt ret=iIniDocument->GetKeyValue(_L("SERVERS"),_L("SWTRACER"),value);
1.475 + LeaveIfNoMemory(ret);
1.476 + test(ret==KErrNotFound);
1.477 +
1.478 + //unknown section
1.479 + ret=iIniDocument->RemoveSection(_L("AnySection"));
1.480 + LeaveIfNoMemory(ret);
1.481 + test(ret==KErrNotFound);
1.482 +
1.483 + CleanupStack::PopAndDestroy();
1.484 + }
1.485 +
1.486 +void CIniParser16Test::DoTest5L()
1.487 + {
1.488 + //Testing SetKey API
1.489 + TPtrC16 value;
1.490 + //Modifying existing value
1.491 + User::LeaveIfError(iIniDocument->SetKey(_L("MEDIA"),_L("RDebug"),_L("NewPlugin.dll")));
1.492 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("RDebug"),value));
1.493 + test(value.Compare(_L("NewPlugin.dll"))==0);
1.494 +
1.495 + //nonexist key should be created
1.496 + User::LeaveIfError(iIniDocument->SetKey(_L("MEDIA"),_L("newplug"),_L("")));
1.497 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("MEDIA"),_L("newplug"),value));
1.498 + test(value.Compare(_L(""))==0);
1.499 +
1.500 + //nonexist section should be created
1.501 + User::LeaveIfError(iIniDocument->SetKey(_L("unknown_section"),_L("unknown_key"),_L("unknown_value")));
1.502 + User::LeaveIfError(iIniDocument->GetKeyValue(_L("unknown_section"),_L("unknown_key"),value));
1.503 + test(value.Compare(_L("unknown_value"))==0);
1.504 +
1.505 + //Testing Externalise to a New file
1.506 + User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output16_1.ini")));
1.507 +
1.508 + //Try opening the written ini file now to ensure no corruption in writing
1.509 + CIniDocument16* iniReRead=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16_1.ini"));
1.510 + CleanupStack::PushL(iniReRead);
1.511 + User::LeaveIfError(iniReRead->GetKeyValue(_L("unknown_section"),_L("unknown_key"),value));
1.512 + test(value.Compare(_L("unknown_value"))==0);
1.513 + User::LeaveIfError(iniReRead->GetKeyValue(_L("MEDIA"),_L("newplug"),value));
1.514 + test(value.Compare(_L(""))==0);
1.515 +
1.516 + CleanupStack::PopAndDestroy(iniReRead);
1.517 + }
1.518 +
1.519 +void CIniParser16Test::DoTest6L()
1.520 + {
1.521 + //Testing RemoveKey API
1.522 + TPtrC16 value;
1.523 + //remove existing key
1.524 + User::LeaveIfError(iIniDocument->RemoveKey(_L("OUTPUT_CHANNELS"),_L("1")));
1.525 + TInt ret=iIniDocument->GetKeyValue(_L("OUTPUT_CHANNELS"),_L("1"),value);
1.526 + LeaveIfNoMemory(ret);
1.527 + test(ret==KErrNotFound);
1.528 +
1.529 + //remove non-exist key
1.530 + ret=iIniDocument->RemoveKey(_L("OUTPUT_CHANNELS"),_L("1"));
1.531 + LeaveIfNoMemory(ret);
1.532 + test(ret==KErrNotFound);
1.533 +
1.534 + //remove non-exist section
1.535 + ret=iIniDocument->RemoveKey(_L("Non-existSection"),_L("1"));
1.536 + LeaveIfNoMemory(ret);
1.537 + test(ret==KErrNotFound);
1.538 + }
1.539 +
1.540 +void CIniParser16Test::DoTest7L()
1.541 + {
1.542 + //Testing iterator class
1.543 + CIniSecIter16* iIniSecIter=NULL;
1.544 + TInt ret=KErrNone;
1.545 +
1.546 + //unknown section
1.547 + TRAP(ret,iIniSecIter=CIniSecIter16::NewL(_L("Unknown"),iIniDocument));
1.548 + LeaveIfNoMemory(ret);
1.549 + test(ret==KErrNotFound);
1.550 +
1.551 + //null document
1.552 + TRAP(ret,iIniSecIter=CIniSecIter16::NewL(_L("Unknown"),NULL));
1.553 + LeaveIfNoMemory(ret);
1.554 + test(ret==KErrArgument);
1.555 +
1.556 + //known section
1.557 + iIniSecIter=CIniSecIter16::NewL(_L("test_section"),iIniDocument);
1.558 + TPtrC16 key;
1.559 + TPtrC16 value;
1.560 + //test Next() and End();
1.561 + test(!iIniSecIter->End());
1.562 + test(iIniSecIter->Next(key,value));
1.563 + test(key.Compare(_L("key1"))==0);
1.564 + test(value.Compare(_L("value1"))==0);
1.565 + test(!iIniSecIter->End());
1.566 + test(iIniSecIter->Next(key,value));
1.567 + test(key.Compare(_L("key2"))==0);
1.568 + test(value.Compare(_L("value2"))==0);
1.569 + test(!iIniSecIter->End());
1.570 + test(iIniSecIter->Next(key,value));
1.571 + test(key.Compare(_L("key3"))==0);
1.572 + test(value.Compare(_L("value3"))==0);
1.573 + test(!iIniSecIter->End());
1.574 + test(iIniSecIter->Next(key,value));
1.575 + test(key.Compare(_L("key4"))==0);
1.576 + test(value.Compare(_L("value4"))==0);
1.577 + test(!iIniSecIter->End());
1.578 + test(iIniSecIter->Next(key,value));
1.579 + test(key.Compare(_L("key5"))==0);
1.580 + test(value.Compare(_L("value value value"))==0);
1.581 + test(iIniSecIter->End());
1.582 + test(iIniSecIter->Next(key,value)==EFalse);
1.583 +
1.584 + //test Reset()
1.585 + iIniSecIter->Reset();
1.586 + test(!iIniSecIter->End());
1.587 + test(iIniSecIter->Next(key,value));
1.588 + test(key.Compare(_L("key1"))==0);
1.589 + test(value.Compare(_L("value1"))==0);
1.590 +
1.591 + delete iIniSecIter;
1.592 + iIniSecIter=NULL;
1.593 + }
1.594 +
1.595 +void CIniParser16Test::DoTest8L()
1.596 + {
1.597 + //Testing Externalise to ROM drive
1.598 + TInt ret=iIniDocument->Externalise(_L("z:\\output16.ini"));
1.599 + LeaveIfNoMemory(ret);
1.600 + test(ret==KErrAccessDenied);
1.601 +
1.602 + //Testing Externalise to a New file
1.603 + User::LeaveIfError(iIniDocument->Externalise(_L("c:\\initest\\output16.ini")));
1.604 +
1.605 + //Try opening the written ini file now to ensure no corruption in writing
1.606 + CIniDocument16* output=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16.ini"));
1.607 + CleanupStack::PushL(output);
1.608 + User::LeaveIfError(output->SetKey(_L("Test"),_L("Test"),_L("Test")));
1.609 +
1.610 + //Testing Externaliseing to the already exist file
1.611 + User::LeaveIfError(output->Externalise(_L("c:\\initest\\output16_1.ini")));
1.612 + CleanupStack::PopAndDestroy();
1.613 +
1.614 + //Opening an empty file and Externaliseing an empty file
1.615 + output=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\unknown16.ini"));
1.616 + CleanupStack::PushL(output);
1.617 + User::LeaveIfError(output->Externalise(_L("c:\\initest\\unknown16_1.ini")));
1.618 + CleanupStack::PopAndDestroy();
1.619 +
1.620 + // Read it back in and find the additions put in above.
1.621 + CIniDocument16* iniReRead=NULL;
1.622 + iniReRead=CIniDocument16::NewL(TheRFs,_L("c:\\initest\\output16_1.ini"));
1.623 + CleanupStack::PushL(iniReRead);
1.624 +
1.625 + TPtrC16 value;
1.626 + User::LeaveIfError(iniReRead->GetKeyValue(_L("Test"),_L("Test"),value));
1.627 + test(value.Compare(_L("Test"))==0);
1.628 +
1.629 + CleanupStack::PopAndDestroy(iniReRead);
1.630 + }
1.631 +
1.632 +void CIniParser16Test::DoTest9L()
1.633 +{
1.634 + //Test for no leakage when handling corrupt file
1.635 + CIniDocument16* ini=NULL;
1.636 + TRAPD(err,ini=CIniDocument16::NewL(TheRFs,_L("z:\\resource\\corruptconfig16.ini")));
1.637 + LeaveIfNoMemory(err);
1.638 + test(err==KErrCorrupt);
1.639 + delete ini;
1.640 +}
1.641 +
1.642 +void CIniParser16Test::DoTest10L()
1.643 +{
1.644 + TPtrC16 value;
1.645 + //open existing ini file
1.646 + CIniFile16* ini=CIniFile16::NewL(TheRFs, _L16("z:\\resource\\testconfig16.ini"));
1.647 + CleanupStack::PushL(ini);
1.648 +
1.649 + //mid section
1.650 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key1"),value));
1.651 + test(value.Compare(_L16("value1"))==0);
1.652 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key2"),value));
1.653 + test(value.Compare(_L16("value2"))==0);
1.654 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key3"),value));
1.655 + test(value.Compare(_L16("value3"))==0);
1.656 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key4"),value));
1.657 + test(value.Compare(_L16("value4"))==0);
1.658 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key5"),value));
1.659 + test(value.Compare(_L16("value value value"))==0);
1.660 +
1.661 + //first section
1.662 + User::LeaveIfError(ini->FindVar(_L16("SERVERS"),_L16("SWTRACER"),value));
1.663 + test(value.Compare(_L16(""))==0);
1.664 +
1.665 + //last section
1.666 + User::LeaveIfError(ini->FindVar(_L16("1"),_L16("timestamps"),value));
1.667 + test(value.Compare(_L16("0"))==0);
1.668 + User::LeaveIfError(ini->FindVar(_L16("1"),_L16("setting"),value));
1.669 + test(value.Compare(_L16("value"))==0);
1.670 +
1.671 + CleanupStack::PopAndDestroy();
1.672 +
1.673 + //open a non existing file
1.674 + TInt ret=KErrNone;
1.675 + TRAP(ret,ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\nonexist.ini")));
1.676 + LeaveIfNoMemory(ret);
1.677 + test(ret==KErrNotFound);
1.678 +
1.679 + //open an empty ini file
1.680 + ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\empty16.ini"));
1.681 + CleanupStack::PushL(ini);
1.682 +
1.683 + ret=ini->FindVar(_L16("empty"),_L16("empty"),value);
1.684 + LeaveIfNoMemory(ret);
1.685 + test(ret==KErrNotFound);
1.686 +
1.687 + CleanupStack::PopAndDestroy();
1.688 +}
1.689 +
1.690 +void CIniParser16Test::DoTest11L()
1.691 + {
1.692 + TPtrC16 value;
1.693 + //open existing 8 bit ini file with 16 bit reader
1.694 + CIniFile16* ini;
1.695 + TInt err;
1.696 + TRAP(err,ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\testconfig8.ini")));
1.697 + test(err==KErrCorrupt || err==KErrNoMemory);
1.698 +
1.699 + //open existing 8 bit ini file, but allow conversion to 16 bits
1.700 + ini=CIniFile16::NewL(TheRFs,_L16("z:\\resource\\testconfig8.ini"),ETrue);
1.701 + CleanupStack::PushL(ini);
1.702 +
1.703 + //mid section
1.704 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key1"),value));
1.705 + test(value.Compare(_L16("value1"))==0);
1.706 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key2"),value));
1.707 + test(value.Compare(_L16("value2"))==0);
1.708 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key3"),value));
1.709 + test(value.Compare(_L16("value3"))==0);
1.710 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key4"),value));
1.711 + test(value.Compare(_L16("value4"))==0);
1.712 + User::LeaveIfError(ini->FindVar(_L16("test_section"),_L16("key5"),value));
1.713 + test(value.Compare(_L16("value value value"))==0);
1.714 +
1.715 + //first section
1.716 + User::LeaveIfError(ini->FindVar(_L16("SERVERS"),_L16("SWTRACER"),value));
1.717 + test(value.Compare(_L16(""))==0);
1.718 +
1.719 + //last section
1.720 + User::LeaveIfError(ini->FindVar(_L16("1"),_L16("timestamps"),value));
1.721 + test(value.Compare(_L16("0"))==0);
1.722 + User::LeaveIfError(ini->FindVar(_L16("1"),_L16("setting"),value));
1.723 + test(value.Compare(_L16("value"))==0);
1.724 +
1.725 + CleanupStack::PopAndDestroy();
1.726 + }
1.727 +
1.728 +void CIniParser16Test::DoTest12L()
1.729 + {
1.730 + TPtrC16 value;
1.731 + // We are trying to invoke an OOM condition for a single operation to test that the operation is atomic.
1.732 + // Under that condition the object should be rolled back to the original state. The resulting document should be the same
1.733 + // as before the condition was invoked. If the test succeeded, a new section should be created at end
1.734 + // (which is nice but not the real focus of the test).
1.735 + User::LeaveIfError(iIniDocument->SetKey(_L16("unknown_section3"),_L16("unknown_key3"),_L16("unknown_value3")));
1.736 + }
1.737 +
1.738 +static void DeleteFilesL()
1.739 + {
1.740 + CFileMan* fileman=CFileMan::NewL(TheRFs);
1.741 +
1.742 + fileman->Delete(_L("c:\\initest\\*"));
1.743 +
1.744 + delete fileman;
1.745 + }
1.746 +
1.747 +/**
1.748 +@SYMTestCaseID SYSLIB-BAFL-CT-1559
1.749 +@SYMTestCaseDesc Test CIniParser16Test
1.750 +@SYMTestPriority High
1.751 +@SYMTestActions Perform component tests on CIniParser16Test (includes OOM)
1.752 + Testing all the exported apis.
1.753 +@SYMTestExpectedResults The test must not fail.
1.754 +@SYMREQ PREQ505
1.755 +*/
1.756 +static void DoTestL()
1.757 + {
1.758 + TTime startTime(0), stopTime(0);
1.759 + TTimeIntervalMicroSeconds timeTaken;
1.760 +
1.761 + test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1559 "));
1.762 +
1.763 + DeleteFilesL();
1.764 + //16 bit basic testing
1.765 + CIniParser16Test::CreateDeleteTest1L();
1.766 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest1L,_L("GetSectionList16"));
1.767 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest2L,_L("GetKeyValue16"));
1.768 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest3L,_L("AddSection16"));
1.769 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest4L,_L("RemoveSection16"));
1.770 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest5L,_L("SetKeyValue16"));
1.771 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest6L,_L("RemoveKey16"));
1.772 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest7L,_L("IniSecIter16"));
1.773 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest8L,_L("Externalise16"));
1.774 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest9L,_L("Corrupt file16"));
1.775 + //16 bit OOM testing
1.776 + CIniParser16Test::CreateDeleteOOMTestL();
1.777 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest1L,_L("GetSectionList16-OOM"));
1.778 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest2L,_L("GetKeyValue16-OOM"));
1.779 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest3L,_L("AddSection16-OOM"));
1.780 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest4L,_L("RemoveSection16-OOM"));
1.781 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest5L,_L("SetKeyValue16-OOM"));
1.782 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest6L,_L("RemoveKey16-OOM"));
1.783 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest7L,_L("IniSecIter16-OOM"));
1.784 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest8L,_L("Externalise16-OOM"));
1.785 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest9L,_L("Corrupt file16-OOM"));
1.786 +
1.787 + //16 bit light basic testing
1.788 + CIniParser16Test::CreateDeleteTest2L();
1.789 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest10L,_L("Light FindVar16"));
1.790 + CIniParser16Test::DoBasicTestL(&CIniParser16Test::DoTest11L,_L("Load 16"));
1.791 + //16 bit light OOM testing
1.792 + CIniParser16Test::CreateDeleteOOMTest2L();
1.793 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest10L,_L("Light FindVar-OOM16"));
1.794 + CIniParser16Test::DoOOMTestL(&CIniParser16Test::DoTest11L,_L("Load 16-OOM"));
1.795 + stopTime.UniversalTime();
1.796 + timeTaken = stopTime.MicroSecondsFrom(startTime);
1.797 + test.Printf(_L("Time taken for Heavy= %d microseconds\n"), timeTaken.Int64() );
1.798 + startTime.UniversalTime();
1.799 + // Consistency checks
1.800 + CIniParser16Test::DoOOMWithConsistencyCheckTestL(&CIniParser16Test::DoTest12L,_L("Consistency16-OOMC"), FALSE);
1.801 +
1.802 + stopTime.UniversalTime();
1.803 + timeTaken = stopTime.MicroSecondsFrom(startTime);
1.804 + test.Printf(_L("Time taken for consistency checks= %d microseconds\n"), timeTaken.Int64() );
1.805 + }
1.806 +
1.807 +GLDEF_C TInt E32Main()
1.808 + {
1.809 + __UHEAP_MARK;
1.810 + CTrapCleanup* trapCleanup=CTrapCleanup::New();
1.811 + test(TheRFs.Connect()==KErrNone);
1.812 + test.Start(_L("Ini File Parser Test"));
1.813 +
1.814 + TRAPD(error, DoTestL());
1.815 + test(error == KErrNone);
1.816 +
1.817 +
1.818 + TheRFs.Close();
1.819 + test.End();
1.820 + test.Close();
1.821 + delete trapCleanup;
1.822 + __UHEAP_MARKEND;
1.823 + return error;
1.824 + }
1.825 +