os/persistentdata/persistentstorage/centralrepository/pccenrep/test/common.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <f32file.h>
    17 #include "../../test/t_cenrep_helper.h"
    18 #include <e32test.h>
    19 
    20 #ifdef __TOOLS2__
    21 #define CENREP_PC_TEST
    22 #endif
    23 
    24 #ifdef CENREP_PC_TEST
    25 	#include <x86tool/centralrepository.h>
    26 #else
    27 	#include <centralrepository.h>
    28 #endif
    29 
    30 _LIT( KCentralRepositoryServerName, "Centralrepositorysrv");
    31 
    32 
    33 //DEFINED IN THE TEST MAIN CPP
    34 extern void SetupEnv(const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode);
    35 extern void InitialiseLC(CRepository*& aRepository,TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode);
    36 extern RFs TheFs;
    37 extern RTest TheTest;
    38 
    39 LOCAL_C void Check(TInt aValue, TInt aLine)
    40 	{
    41 	if(!aValue)
    42 		{
    43 		TheTest(EFalse, aLine);
    44 		}
    45 	}
    46 
    47 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
    48 	{
    49 	if(aValue != aExpected)
    50 		{
    51 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    52 		TheTest(EFalse, aLine);
    53 		}
    54 	}
    55 
    56 #define TEST(arg) ::Check((arg), __LINE__)
    57 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    58 
    59 ///////////////////////////////////////////////////////////////////////////////////////
    60 TInt CopyFile(const TDesC& aSource, const TDesC& aTarget)
    61 	{
    62 	RFile file;
    63 	TInt ret=file.Open(TheFs,aSource,EFileRead);
    64 	if (ret!=KErrNone)
    65 		return ret;
    66 	TInt fileSize;
    67 	file.Size(fileSize);
    68 	HBufC8* buf=HBufC8::New(fileSize);
    69 	if (!buf)
    70 		{
    71 		file.Close();
    72 		return KErrNoMemory;
    73 		}
    74 	TPtr8 mod(buf->Des());
    75 	file.Read(mod);
    76 	file.Close();
    77 	ret=file.Replace(TheFs,aTarget,EFileWrite);
    78 	if (ret==KErrNone)
    79 		{
    80 		file.Write(*buf);
    81 		}
    82 	file.Close();
    83 	delete buf;
    84 	return ret;
    85 	}
    86 
    87 void OomTest(void (*testFuncL)(CRepository* aRepository),TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
    88 	{	
    89 	TInt error;
    90 	TInt count = 0;
    91 
    92 	do
    93 		{
    94 		SetupEnv(aInFilePath,aOutFilePath,aTestMode);
    95 		//for CRE testing we need to ensure we have a fresh copy of CRE(sourced from the TXT template)
    96 
    97 		__UHEAP_MARK;
    98 		CRepository* repository=NULL;
    99 		
   100 #ifndef CENREP_PC_TEST
   101 		//for CS testing, we want to kill server to start with fresh repos, otherwise might still use
   102 		//cache version from previous test		
   103 		KillProcess(KCentralRepositoryServerName);		
   104 #endif		
   105 		InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
   106 		if (repository)
   107 			CleanupStack::Pop();		
   108 		// This is supported by symuser but still have problem, so skiped
   109 //		User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
   110 
   111 		
   112   		// find out the number of open handles
   113   		// TOOLS2 somehow not supporting RThread().HandleCount()?
   114 #ifndef __TOOLS2__  		
   115 		TInt startProcessHandleCount;
   116 		TInt startThreadHandleCount;
   117 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   118 #endif
   119 		
   120 		TRAP(error, (testFuncL)(repository));
   121 		
   122 		// check that no handles have leaked
   123 #ifndef __TOOLS2__		
   124 		TInt endProcessHandleCount;
   125 		TInt endThreadHandleCount;
   126 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   127 		
   128 		TEST2(endProcessHandleCount, startProcessHandleCount);
   129 		TEST2(endThreadHandleCount, startThreadHandleCount);
   130 #endif
   131 //		User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
   132 		
   133 		delete repository;
   134 		
   135 		__UHEAP_MARKEND;
   136 		
   137 		} while(error == KErrNoMemory);
   138 	_LIT(KTestFailed, "Out of memory test failure on iteration %d\n");
   139 	__ASSERT_ALWAYS(error==KErrNone, TheTest.Panic(error, KTestFailed, count));
   140  	}
   141 
   142 void ObjectCreateDeleteOOM(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
   143 	{
   144 //	TInt count=0;
   145 	TInt error=KErrNone;
   146 	do
   147 		{
   148 		SetupEnv(aInFilePath,aOutFilePath,aTestMode);
   149 		
   150 		__UHEAP_MARK;
   151 #ifndef __TOOLS2__		
   152 		TInt startProcessHandleCount;
   153 		TInt startThreadHandleCount;
   154 		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
   155 #endif		
   156 		// This is supported by symuser but still has problems, so skipped.
   157 //		User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
   158 		
   159 		CRepository* repository=NULL;
   160 		
   161 		TRAP(error,InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);CleanupStack::Pop());
   162 		
   163 		delete repository;
   164 
   165 //		User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
   166 
   167 #ifndef __TOOLS2__		
   168 		// check that no handles have leaked
   169 		TInt endProcessHandleCount;
   170 		TInt endThreadHandleCount;
   171 		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
   172 		
   173 		TEST2(endProcessHandleCount, startProcessHandleCount);
   174 		TEST2(endThreadHandleCount, startThreadHandleCount);
   175 #endif		
   176 		
   177 		__UHEAP_MARKEND;
   178 		} while(error == KErrNoMemory);
   179 	}
   180 
   181 void GetFunctionL(CRepository* aRepository)
   182 	{
   183 	//[GET]
   184 	TInt intVal;
   185 	TReal realVal;
   186 	User::LeaveIfError(aRepository->Get(6,intVal));
   187 	TEST(intVal==12);
   188 	User::LeaveIfError(aRepository->Get(8,realVal));
   189 	TEST(realVal==1.5);
   190 	TBuf<255> stringVal;
   191 	User::LeaveIfError(aRepository->Get(0x300,stringVal));
   192 	TEST(stringVal.Compare(_L("Hello World"))==0);	
   193 
   194 	TBuf<5> shortBuffer;
   195 	TInt actualLength=0;
   196 	TInt ret=aRepository->Get(0x300,shortBuffer,actualLength);
   197 	TEST(ret==KErrOverflow);
   198 	TEST(actualLength==11);
   199 	TEST(shortBuffer.Compare(_L("Hello"))==0);
   200 		
   201 	//[GETMETA]
   202 	TUint32 metaValue;
   203 	User::LeaveIfError(aRepository->GetMeta(2,metaValue));
   204 	TEST(metaValue==0xa);
   205 	//range based
   206 	User::LeaveIfError(aRepository->GetMeta(0x204,metaValue));
   207 	TEST(metaValue==0x20);
   208 	//default based
   209 	User::LeaveIfError(aRepository->GetMeta(0x10000,metaValue));
   210 	TEST(metaValue==0x10);
   211 	}
   212 
   213 void FindFunctionL(CRepository* aRepository)
   214 	{
   215 	RArray<TUint32> keyList;
   216 	CleanupClosePushL(keyList);
   217 	//Find all settings
   218 	TInt ret=aRepository->FindL(0xFFFFFFFF,0,keyList);
   219 	User::LeaveIfError(ret);	
   220 	TEST(ret==KErrNone);
   221 	TEST(keyList.Count()==33);
   222 	keyList.Reset();
   223 	//Find match EQ specific value
   224 	ret=aRepository->FindEqL(0xFFFFFFFF,0,10,keyList);
   225 	User::LeaveIfError(ret);	
   226 	TEST(ret==KErrNone);	
   227 	TEST(keyList.Count()==3);
   228 	keyList.Reset();
   229 	//Find match NEQ specific value
   230 	ret=aRepository->FindNeqL(0xFFFFFFFF,0,10,keyList);
   231 	TEST(ret==KErrNone);
   232 	TEST(keyList.Count()==30);
   233 	keyList.Reset();		
   234 	//Find using string matching instead
   235 	_LIT(KString,"empty");
   236 	ret=aRepository->FindEqL(0xFFFFFFFF,0,KString(),keyList);
   237 	User::LeaveIfError(ret);
   238 	TEST(ret==KErrNone);
   239 	TEST(keyList.Count()==1);	
   240 	CleanupStack::PopAndDestroy();
   241 	}
   242 
   243 void SetFunctionL(CRepository* aRepository)
   244 	{
   245 	//[SET]
   246 	//int
   247 	TUint32 metaValue;
   248 	TInt value;
   249 	User::LeaveIfError(aRepository->Get(1,value));
   250 	TEST(value==1);
   251 	User::LeaveIfError(aRepository->Set(1,100));
   252 	User::LeaveIfError(aRepository->Get(1,value));
   253 	TEST(value==100)	;
   254 	//real
   255 	TReal realValue;
   256 	User::LeaveIfError(aRepository->Get(2,realValue));
   257 	TEST(realValue==2.732);
   258 	TReal newrealValue(5.464);
   259 	User::LeaveIfError(aRepository->Set(2,newrealValue));
   260 	User::LeaveIfError(aRepository->Get(2,realValue));
   261 	TEST(realValue==5.464);
   262 	//string
   263 	TBuf<255> stringValue;
   264 	User::LeaveIfError(aRepository->Get(0x10000,stringValue));
   265 	TEST(stringValue.Compare(_L("empty"))==0);
   266 	User::LeaveIfError(aRepository->Set(0x10000,_L("full")));
   267 	stringValue.Zero();
   268 	User::LeaveIfError(aRepository->Get(0x10000,stringValue));
   269 	TEST(stringValue.Compare(_L("full"))==0);		
   270 	
   271 	//use set to create new setting and also check the meta
   272 	TInt newIntegerKeyValue=205;
   273 	User::LeaveIfError(aRepository->Set(0x205,newIntegerKeyValue));
   274 	User::LeaveIfError(aRepository->Get(0x205,value));
   275 	TEST(value==205);
   276 	User::LeaveIfError(aRepository->GetMeta(0x205,metaValue));
   277 	//as the key 0x205 is within range, expect use of range meta
   278 	TEST(metaValue==0x20);	
   279 	}
   280 
   281 void CreateFunctionL(CRepository* aRepository)
   282 	{
   283 	TUint32 metaValue;
   284 	TInt value=206;
   285 	User::LeaveIfError(aRepository->Create(0x206,value));
   286 	User::LeaveIfError(aRepository->Get(0x206,value));
   287 	TEST(value==206);
   288 	User::LeaveIfError(aRepository->GetMeta(0x206,metaValue));
   289 	//within meta range
   290 	TEST(metaValue==0x20);			
   291 	
   292 	TReal realValue=207.207;
   293 	User::LeaveIfError(aRepository->Create(0x407,realValue));
   294 	User::LeaveIfError(aRepository->Get(0x407,realValue));
   295 	TEST(realValue==207.207);
   296 	User::LeaveIfError(aRepository->GetMeta(0x407,metaValue));
   297 	//default meta
   298 	TEST(metaValue==0x10);
   299 	
   300 	//create already existing setting
   301 	TInt ret=aRepository->Create(1,value);
   302 	TEST(ret==KErrAlreadyExists);	
   303 	}
   304 
   305 void DeleteFunctionL(CRepository* aRepository)
   306 	{
   307 	//delete single
   308 	TInt value;
   309 	User::LeaveIfError(aRepository->Delete(6));
   310 	TInt ret=aRepository->Get(6,value);
   311 	TEST(ret==KErrNotFound);
   312 	
   313 	//delete range
   314 	RArray<TUint32> keyList;
   315 	CleanupClosePushL(keyList);
   316 	//make sure that the list of keys are there first
   317 	ret=aRepository->FindL(0x03010000,0xFFFFF0FF,keyList);
   318 	User::LeaveIfError(ret);
   319 	TEST(keyList.Count()==5);
   320 	TUint32 error;
   321 	User::LeaveIfError(aRepository->Delete(0x03010000,0xFFFFF0FF,error));
   322 	//check that key no longer exist
   323 	keyList.Reset();
   324 	ret=aRepository->FindL(0x03010000,0xFFFFF0FF,keyList);
   325 	if (ret!=KErrNotFound)
   326 		User::LeaveIfError(ret);
   327 	TEST(ret==KErrNotFound);
   328 	TEST(keyList.Count()==0);
   329 	CleanupStack::PopAndDestroy();	
   330 	}
   331 
   332 void MoveFunctionL(CRepository* aRepository)
   333 	{
   334 	RArray<TUint32> keyList;
   335 	CleanupClosePushL(keyList);
   336 	TUint32 errorKey;
   337 	//check source key exists first
   338 	keyList.Reset();
   339 	TInt ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
   340 	User::LeaveIfError(ret);
   341 	TEST(keyList.Count()==5);
   342 	
   343 	User::LeaveIfError(aRepository->Move(0x02010000,0x06010000,0xFFFFF0FF,errorKey));
   344 	
   345 	keyList.Reset();
   346 	//check target now exists
   347 	ret=aRepository->FindL(0x06010000,0xFFFFF0FF,keyList);
   348 	User::LeaveIfError(ret);
   349 
   350 	TEST(keyList.Count()==5);
   351 	//check source now deleted
   352 	keyList.Reset();
   353 	ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
   354 	TEST(ret==KErrNotFound);
   355 	TEST2(keyList.Count(),0);
   356 	
   357 	CleanupStack::PopAndDestroy();	
   358 	}
   359 
   360 /**
   361 */
   362 void BasicFunctionL(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
   363 	{
   364 	//TEST SETUP
   365 	SetupEnv(aInFilePath,aOutFilePath,aTestMode);
   366 	
   367 	__UHEAP_MARK;
   368 	CRepository* repository=NULL;
   369 	
   370 	InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
   371 
   372 #ifdef CENREP_PC_TEST
   373 	//testing transaction
   374 	TInt r= repository->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
   375 	TEST2(r, KErrNone);
   376 	repository->CleanupCancelTransactionPushL();
   377 #endif
   378 
   379 	GetFunctionL(repository);
   380 	FindFunctionL(repository);
   381 	SetFunctionL(repository);
   382 	CreateFunctionL(repository);
   383 	DeleteFunctionL(repository);
   384 	MoveFunctionL(repository);
   385 
   386 #ifdef CENREP_PC_TEST
   387 	CleanupStack::PopAndDestroy();
   388 #endif
   389 
   390 	CleanupStack::PopAndDestroy();	
   391 	
   392 	__UHEAP_MARKEND;
   393 	}
   394 
   395 void OomBasicFunction(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
   396 	{
   397 	__UHEAP_MARK;
   398 
   399 	OomTest(GetFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   400 	OomTest(FindFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   401 	OomTest(SetFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   402 	OomTest(CreateFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   403 	OomTest(DeleteFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   404 	OomTest(MoveFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
   405 	
   406 	__UHEAP_MARKEND;
   407 	}
   408 
   409 void DEF130394L(TUid aUid)
   410 	{
   411 	CRepository* repos=NULL;
   412 	
   413 	repos = CRepository::NewL(aUid);
   414 	
   415 	TInt err = repos->Create(1,1);
   416 	TEST(err==KErrNone);
   417 	
   418 	delete repos;
   419 	
   420 	repos = CRepository::NewL(aUid);
   421 	
   422 	err = repos->Create(1,1);
   423 	TEST(err==KErrAlreadyExists);
   424 	
   425 	delete repos;
   426 	}
   427 
   428 void DoFileCompL(const TDesC& aGenerated, const TDesC& aReference, TUint32& aCrcValue)
   429 	{
   430 	RFile genFile;
   431 	RFile refFile;
   432 	TInt err = genFile.Open(TheFs,aGenerated,EFileRead);
   433 	User::LeaveIfError(err);
   434 	err = refFile.Open(TheFs,aReference,EFileRead);
   435 	User::LeaveIfError(err);
   436 	
   437 	TInt sizeOfGen;
   438 	genFile.Size(sizeOfGen);
   439 	TInt sizeOfRef;
   440 	refFile.Size(sizeOfRef);
   441 	TEST(sizeOfGen == sizeOfRef);
   442 	
   443 	TUint32 crcGen = 0;
   444 	TUint32 crcRef = 0;
   445 
   446 	HBufC8* buf = HBufC8::New(sizeOfGen);
   447 	if (!buf)
   448 		{
   449 		genFile.Close();
   450 		refFile.Close();
   451 		User::Leave(KErrNoMemory);
   452 		}
   453 	TPtr8 data(buf->Des());
   454 	
   455 	err = genFile.Read(data);
   456 	User::LeaveIfError(err);
   457 	Mem::Crc32(crcGen, buf, sizeOfGen);
   458 	err = refFile.Read(data);
   459 	User::LeaveIfError(err);
   460 	Mem::Crc32(crcRef, buf, sizeOfRef);
   461 
   462 	TEST(crcGen == crcRef);
   463 	
   464 	aCrcValue = crcGen;
   465 	
   466 	delete buf;
   467 	genFile.Close();
   468 	refFile.Close();
   469 	
   470 	return;
   471 	}
   472 
   473 void DoCrcCompL(const TUint32& aCrcValue, const TDesC& aCrcRecord, TBool aCreOrTxt)
   474 	{
   475 	RFile file;
   476 	TInt err = file.Open(TheFs, aCrcRecord, EFileRead);
   477 	User::LeaveIfError(err);
   478 	
   479 	TBuf8<100> buf;
   480 	file.Read(buf);
   481 	
   482 	TBuf8<1> breaker(_L8("-"));
   483 	TInt pos = buf.Find(breaker);
   484 	
   485 	TInt length = buf.Length();
   486 	
   487 	const TPtrC8 crePtr = buf.Right(length - pos -1);
   488 	const TPtrC8 txtPtr = buf.Left(pos);
   489 	
   490 	TUint32 crc = 0;
   491 	
   492 	if (aCreOrTxt)
   493 		{
   494 		TLex8 lex (crePtr);
   495 		lex.Val(crc, EDecimal);
   496 		}
   497 	else
   498 		{
   499 		TLex8 lex (txtPtr);
   500 		lex.Val(crc, EDecimal);
   501 		}
   502 	TEST2(crc, aCrcValue);
   503 	file.Close();
   504 	}
   505