1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/centralrepository/pccenrep/test/common.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,505 @@
1.4 +// Copyright (c) 2008-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 +//
1.18 +
1.19 +#include <f32file.h>
1.20 +#include "../../test/t_cenrep_helper.h"
1.21 +#include <e32test.h>
1.22 +
1.23 +#ifdef __TOOLS2__
1.24 +#define CENREP_PC_TEST
1.25 +#endif
1.26 +
1.27 +#ifdef CENREP_PC_TEST
1.28 + #include <x86tool/centralrepository.h>
1.29 +#else
1.30 + #include <centralrepository.h>
1.31 +#endif
1.32 +
1.33 +_LIT( KCentralRepositoryServerName, "Centralrepositorysrv");
1.34 +
1.35 +
1.36 +//DEFINED IN THE TEST MAIN CPP
1.37 +extern void SetupEnv(const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode);
1.38 +extern void InitialiseLC(CRepository*& aRepository,TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode);
1.39 +extern RFs TheFs;
1.40 +extern RTest TheTest;
1.41 +
1.42 +LOCAL_C void Check(TInt aValue, TInt aLine)
1.43 + {
1.44 + if(!aValue)
1.45 + {
1.46 + TheTest(EFalse, aLine);
1.47 + }
1.48 + }
1.49 +
1.50 +LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
1.51 + {
1.52 + if(aValue != aExpected)
1.53 + {
1.54 + RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
1.55 + TheTest(EFalse, aLine);
1.56 + }
1.57 + }
1.58 +
1.59 +#define TEST(arg) ::Check((arg), __LINE__)
1.60 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
1.61 +
1.62 +///////////////////////////////////////////////////////////////////////////////////////
1.63 +TInt CopyFile(const TDesC& aSource, const TDesC& aTarget)
1.64 + {
1.65 + RFile file;
1.66 + TInt ret=file.Open(TheFs,aSource,EFileRead);
1.67 + if (ret!=KErrNone)
1.68 + return ret;
1.69 + TInt fileSize;
1.70 + file.Size(fileSize);
1.71 + HBufC8* buf=HBufC8::New(fileSize);
1.72 + if (!buf)
1.73 + {
1.74 + file.Close();
1.75 + return KErrNoMemory;
1.76 + }
1.77 + TPtr8 mod(buf->Des());
1.78 + file.Read(mod);
1.79 + file.Close();
1.80 + ret=file.Replace(TheFs,aTarget,EFileWrite);
1.81 + if (ret==KErrNone)
1.82 + {
1.83 + file.Write(*buf);
1.84 + }
1.85 + file.Close();
1.86 + delete buf;
1.87 + return ret;
1.88 + }
1.89 +
1.90 +void OomTest(void (*testFuncL)(CRepository* aRepository),TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
1.91 + {
1.92 + TInt error;
1.93 + TInt count = 0;
1.94 +
1.95 + do
1.96 + {
1.97 + SetupEnv(aInFilePath,aOutFilePath,aTestMode);
1.98 + //for CRE testing we need to ensure we have a fresh copy of CRE(sourced from the TXT template)
1.99 +
1.100 + __UHEAP_MARK;
1.101 + CRepository* repository=NULL;
1.102 +
1.103 +#ifndef CENREP_PC_TEST
1.104 + //for CS testing, we want to kill server to start with fresh repos, otherwise might still use
1.105 + //cache version from previous test
1.106 + KillProcess(KCentralRepositoryServerName);
1.107 +#endif
1.108 + InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
1.109 + if (repository)
1.110 + CleanupStack::Pop();
1.111 + // This is supported by symuser but still have problem, so skiped
1.112 +// User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
1.113 +
1.114 +
1.115 + // find out the number of open handles
1.116 + // TOOLS2 somehow not supporting RThread().HandleCount()?
1.117 +#ifndef __TOOLS2__
1.118 + TInt startProcessHandleCount;
1.119 + TInt startThreadHandleCount;
1.120 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.121 +#endif
1.122 +
1.123 + TRAP(error, (testFuncL)(repository));
1.124 +
1.125 + // check that no handles have leaked
1.126 +#ifndef __TOOLS2__
1.127 + TInt endProcessHandleCount;
1.128 + TInt endThreadHandleCount;
1.129 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.130 +
1.131 + TEST2(endProcessHandleCount, startProcessHandleCount);
1.132 + TEST2(endThreadHandleCount, startThreadHandleCount);
1.133 +#endif
1.134 +// User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
1.135 +
1.136 + delete repository;
1.137 +
1.138 + __UHEAP_MARKEND;
1.139 +
1.140 + } while(error == KErrNoMemory);
1.141 + _LIT(KTestFailed, "Out of memory test failure on iteration %d\n");
1.142 + __ASSERT_ALWAYS(error==KErrNone, TheTest.Panic(error, KTestFailed, count));
1.143 + }
1.144 +
1.145 +void ObjectCreateDeleteOOM(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
1.146 + {
1.147 +// TInt count=0;
1.148 + TInt error=KErrNone;
1.149 + do
1.150 + {
1.151 + SetupEnv(aInFilePath,aOutFilePath,aTestMode);
1.152 +
1.153 + __UHEAP_MARK;
1.154 +#ifndef __TOOLS2__
1.155 + TInt startProcessHandleCount;
1.156 + TInt startThreadHandleCount;
1.157 + RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
1.158 +#endif
1.159 + // This is supported by symuser but still has problems, so skipped.
1.160 +// User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
1.161 +
1.162 + CRepository* repository=NULL;
1.163 +
1.164 + TRAP(error,InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);CleanupStack::Pop());
1.165 +
1.166 + delete repository;
1.167 +
1.168 +// User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
1.169 +
1.170 +#ifndef __TOOLS2__
1.171 + // check that no handles have leaked
1.172 + TInt endProcessHandleCount;
1.173 + TInt endThreadHandleCount;
1.174 + RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
1.175 +
1.176 + TEST2(endProcessHandleCount, startProcessHandleCount);
1.177 + TEST2(endThreadHandleCount, startThreadHandleCount);
1.178 +#endif
1.179 +
1.180 + __UHEAP_MARKEND;
1.181 + } while(error == KErrNoMemory);
1.182 + }
1.183 +
1.184 +void GetFunctionL(CRepository* aRepository)
1.185 + {
1.186 + //[GET]
1.187 + TInt intVal;
1.188 + TReal realVal;
1.189 + User::LeaveIfError(aRepository->Get(6,intVal));
1.190 + TEST(intVal==12);
1.191 + User::LeaveIfError(aRepository->Get(8,realVal));
1.192 + TEST(realVal==1.5);
1.193 + TBuf<255> stringVal;
1.194 + User::LeaveIfError(aRepository->Get(0x300,stringVal));
1.195 + TEST(stringVal.Compare(_L("Hello World"))==0);
1.196 +
1.197 + TBuf<5> shortBuffer;
1.198 + TInt actualLength=0;
1.199 + TInt ret=aRepository->Get(0x300,shortBuffer,actualLength);
1.200 + TEST(ret==KErrOverflow);
1.201 + TEST(actualLength==11);
1.202 + TEST(shortBuffer.Compare(_L("Hello"))==0);
1.203 +
1.204 + //[GETMETA]
1.205 + TUint32 metaValue;
1.206 + User::LeaveIfError(aRepository->GetMeta(2,metaValue));
1.207 + TEST(metaValue==0xa);
1.208 + //range based
1.209 + User::LeaveIfError(aRepository->GetMeta(0x204,metaValue));
1.210 + TEST(metaValue==0x20);
1.211 + //default based
1.212 + User::LeaveIfError(aRepository->GetMeta(0x10000,metaValue));
1.213 + TEST(metaValue==0x10);
1.214 + }
1.215 +
1.216 +void FindFunctionL(CRepository* aRepository)
1.217 + {
1.218 + RArray<TUint32> keyList;
1.219 + CleanupClosePushL(keyList);
1.220 + //Find all settings
1.221 + TInt ret=aRepository->FindL(0xFFFFFFFF,0,keyList);
1.222 + User::LeaveIfError(ret);
1.223 + TEST(ret==KErrNone);
1.224 + TEST(keyList.Count()==33);
1.225 + keyList.Reset();
1.226 + //Find match EQ specific value
1.227 + ret=aRepository->FindEqL(0xFFFFFFFF,0,10,keyList);
1.228 + User::LeaveIfError(ret);
1.229 + TEST(ret==KErrNone);
1.230 + TEST(keyList.Count()==3);
1.231 + keyList.Reset();
1.232 + //Find match NEQ specific value
1.233 + ret=aRepository->FindNeqL(0xFFFFFFFF,0,10,keyList);
1.234 + TEST(ret==KErrNone);
1.235 + TEST(keyList.Count()==30);
1.236 + keyList.Reset();
1.237 + //Find using string matching instead
1.238 + _LIT(KString,"empty");
1.239 + ret=aRepository->FindEqL(0xFFFFFFFF,0,KString(),keyList);
1.240 + User::LeaveIfError(ret);
1.241 + TEST(ret==KErrNone);
1.242 + TEST(keyList.Count()==1);
1.243 + CleanupStack::PopAndDestroy();
1.244 + }
1.245 +
1.246 +void SetFunctionL(CRepository* aRepository)
1.247 + {
1.248 + //[SET]
1.249 + //int
1.250 + TUint32 metaValue;
1.251 + TInt value;
1.252 + User::LeaveIfError(aRepository->Get(1,value));
1.253 + TEST(value==1);
1.254 + User::LeaveIfError(aRepository->Set(1,100));
1.255 + User::LeaveIfError(aRepository->Get(1,value));
1.256 + TEST(value==100) ;
1.257 + //real
1.258 + TReal realValue;
1.259 + User::LeaveIfError(aRepository->Get(2,realValue));
1.260 + TEST(realValue==2.732);
1.261 + TReal newrealValue(5.464);
1.262 + User::LeaveIfError(aRepository->Set(2,newrealValue));
1.263 + User::LeaveIfError(aRepository->Get(2,realValue));
1.264 + TEST(realValue==5.464);
1.265 + //string
1.266 + TBuf<255> stringValue;
1.267 + User::LeaveIfError(aRepository->Get(0x10000,stringValue));
1.268 + TEST(stringValue.Compare(_L("empty"))==0);
1.269 + User::LeaveIfError(aRepository->Set(0x10000,_L("full")));
1.270 + stringValue.Zero();
1.271 + User::LeaveIfError(aRepository->Get(0x10000,stringValue));
1.272 + TEST(stringValue.Compare(_L("full"))==0);
1.273 +
1.274 + //use set to create new setting and also check the meta
1.275 + TInt newIntegerKeyValue=205;
1.276 + User::LeaveIfError(aRepository->Set(0x205,newIntegerKeyValue));
1.277 + User::LeaveIfError(aRepository->Get(0x205,value));
1.278 + TEST(value==205);
1.279 + User::LeaveIfError(aRepository->GetMeta(0x205,metaValue));
1.280 + //as the key 0x205 is within range, expect use of range meta
1.281 + TEST(metaValue==0x20);
1.282 + }
1.283 +
1.284 +void CreateFunctionL(CRepository* aRepository)
1.285 + {
1.286 + TUint32 metaValue;
1.287 + TInt value=206;
1.288 + User::LeaveIfError(aRepository->Create(0x206,value));
1.289 + User::LeaveIfError(aRepository->Get(0x206,value));
1.290 + TEST(value==206);
1.291 + User::LeaveIfError(aRepository->GetMeta(0x206,metaValue));
1.292 + //within meta range
1.293 + TEST(metaValue==0x20);
1.294 +
1.295 + TReal realValue=207.207;
1.296 + User::LeaveIfError(aRepository->Create(0x407,realValue));
1.297 + User::LeaveIfError(aRepository->Get(0x407,realValue));
1.298 + TEST(realValue==207.207);
1.299 + User::LeaveIfError(aRepository->GetMeta(0x407,metaValue));
1.300 + //default meta
1.301 + TEST(metaValue==0x10);
1.302 +
1.303 + //create already existing setting
1.304 + TInt ret=aRepository->Create(1,value);
1.305 + TEST(ret==KErrAlreadyExists);
1.306 + }
1.307 +
1.308 +void DeleteFunctionL(CRepository* aRepository)
1.309 + {
1.310 + //delete single
1.311 + TInt value;
1.312 + User::LeaveIfError(aRepository->Delete(6));
1.313 + TInt ret=aRepository->Get(6,value);
1.314 + TEST(ret==KErrNotFound);
1.315 +
1.316 + //delete range
1.317 + RArray<TUint32> keyList;
1.318 + CleanupClosePushL(keyList);
1.319 + //make sure that the list of keys are there first
1.320 + ret=aRepository->FindL(0x03010000,0xFFFFF0FF,keyList);
1.321 + User::LeaveIfError(ret);
1.322 + TEST(keyList.Count()==5);
1.323 + TUint32 error;
1.324 + User::LeaveIfError(aRepository->Delete(0x03010000,0xFFFFF0FF,error));
1.325 + //check that key no longer exist
1.326 + keyList.Reset();
1.327 + ret=aRepository->FindL(0x03010000,0xFFFFF0FF,keyList);
1.328 + if (ret!=KErrNotFound)
1.329 + User::LeaveIfError(ret);
1.330 + TEST(ret==KErrNotFound);
1.331 + TEST(keyList.Count()==0);
1.332 + CleanupStack::PopAndDestroy();
1.333 + }
1.334 +
1.335 +void MoveFunctionL(CRepository* aRepository)
1.336 + {
1.337 + RArray<TUint32> keyList;
1.338 + CleanupClosePushL(keyList);
1.339 + TUint32 errorKey;
1.340 + //check source key exists first
1.341 + keyList.Reset();
1.342 + TInt ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
1.343 + User::LeaveIfError(ret);
1.344 + TEST(keyList.Count()==5);
1.345 +
1.346 + User::LeaveIfError(aRepository->Move(0x02010000,0x06010000,0xFFFFF0FF,errorKey));
1.347 +
1.348 + keyList.Reset();
1.349 + //check target now exists
1.350 + ret=aRepository->FindL(0x06010000,0xFFFFF0FF,keyList);
1.351 + User::LeaveIfError(ret);
1.352 +
1.353 + TEST(keyList.Count()==5);
1.354 + //check source now deleted
1.355 + keyList.Reset();
1.356 + ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
1.357 + TEST(ret==KErrNotFound);
1.358 + TEST2(keyList.Count(),0);
1.359 +
1.360 + CleanupStack::PopAndDestroy();
1.361 + }
1.362 +
1.363 +/**
1.364 +*/
1.365 +void BasicFunctionL(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
1.366 + {
1.367 + //TEST SETUP
1.368 + SetupEnv(aInFilePath,aOutFilePath,aTestMode);
1.369 +
1.370 + __UHEAP_MARK;
1.371 + CRepository* repository=NULL;
1.372 +
1.373 + InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
1.374 +
1.375 +#ifdef CENREP_PC_TEST
1.376 + //testing transaction
1.377 + TInt r= repository->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
1.378 + TEST2(r, KErrNone);
1.379 + repository->CleanupCancelTransactionPushL();
1.380 +#endif
1.381 +
1.382 + GetFunctionL(repository);
1.383 + FindFunctionL(repository);
1.384 + SetFunctionL(repository);
1.385 + CreateFunctionL(repository);
1.386 + DeleteFunctionL(repository);
1.387 + MoveFunctionL(repository);
1.388 +
1.389 +#ifdef CENREP_PC_TEST
1.390 + CleanupStack::PopAndDestroy();
1.391 +#endif
1.392 +
1.393 + CleanupStack::PopAndDestroy();
1.394 +
1.395 + __UHEAP_MARKEND;
1.396 + }
1.397 +
1.398 +void OomBasicFunction(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
1.399 + {
1.400 + __UHEAP_MARK;
1.401 +
1.402 + OomTest(GetFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.403 + OomTest(FindFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.404 + OomTest(SetFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.405 + OomTest(CreateFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.406 + OomTest(DeleteFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.407 + OomTest(MoveFunctionL,aUid,aInFilePath,aOutFilePath,aTestMode);
1.408 +
1.409 + __UHEAP_MARKEND;
1.410 + }
1.411 +
1.412 +void DEF130394L(TUid aUid)
1.413 + {
1.414 + CRepository* repos=NULL;
1.415 +
1.416 + repos = CRepository::NewL(aUid);
1.417 +
1.418 + TInt err = repos->Create(1,1);
1.419 + TEST(err==KErrNone);
1.420 +
1.421 + delete repos;
1.422 +
1.423 + repos = CRepository::NewL(aUid);
1.424 +
1.425 + err = repos->Create(1,1);
1.426 + TEST(err==KErrAlreadyExists);
1.427 +
1.428 + delete repos;
1.429 + }
1.430 +
1.431 +void DoFileCompL(const TDesC& aGenerated, const TDesC& aReference, TUint32& aCrcValue)
1.432 + {
1.433 + RFile genFile;
1.434 + RFile refFile;
1.435 + TInt err = genFile.Open(TheFs,aGenerated,EFileRead);
1.436 + User::LeaveIfError(err);
1.437 + err = refFile.Open(TheFs,aReference,EFileRead);
1.438 + User::LeaveIfError(err);
1.439 +
1.440 + TInt sizeOfGen;
1.441 + genFile.Size(sizeOfGen);
1.442 + TInt sizeOfRef;
1.443 + refFile.Size(sizeOfRef);
1.444 + TEST(sizeOfGen == sizeOfRef);
1.445 +
1.446 + TUint32 crcGen = 0;
1.447 + TUint32 crcRef = 0;
1.448 +
1.449 + HBufC8* buf = HBufC8::New(sizeOfGen);
1.450 + if (!buf)
1.451 + {
1.452 + genFile.Close();
1.453 + refFile.Close();
1.454 + User::Leave(KErrNoMemory);
1.455 + }
1.456 + TPtr8 data(buf->Des());
1.457 +
1.458 + err = genFile.Read(data);
1.459 + User::LeaveIfError(err);
1.460 + Mem::Crc32(crcGen, buf, sizeOfGen);
1.461 + err = refFile.Read(data);
1.462 + User::LeaveIfError(err);
1.463 + Mem::Crc32(crcRef, buf, sizeOfRef);
1.464 +
1.465 + TEST(crcGen == crcRef);
1.466 +
1.467 + aCrcValue = crcGen;
1.468 +
1.469 + delete buf;
1.470 + genFile.Close();
1.471 + refFile.Close();
1.472 +
1.473 + return;
1.474 + }
1.475 +
1.476 +void DoCrcCompL(const TUint32& aCrcValue, const TDesC& aCrcRecord, TBool aCreOrTxt)
1.477 + {
1.478 + RFile file;
1.479 + TInt err = file.Open(TheFs, aCrcRecord, EFileRead);
1.480 + User::LeaveIfError(err);
1.481 +
1.482 + TBuf8<100> buf;
1.483 + file.Read(buf);
1.484 +
1.485 + TBuf8<1> breaker(_L8("-"));
1.486 + TInt pos = buf.Find(breaker);
1.487 +
1.488 + TInt length = buf.Length();
1.489 +
1.490 + const TPtrC8 crePtr = buf.Right(length - pos -1);
1.491 + const TPtrC8 txtPtr = buf.Left(pos);
1.492 +
1.493 + TUint32 crc = 0;
1.494 +
1.495 + if (aCreOrTxt)
1.496 + {
1.497 + TLex8 lex (crePtr);
1.498 + lex.Val(crc, EDecimal);
1.499 + }
1.500 + else
1.501 + {
1.502 + TLex8 lex (txtPtr);
1.503 + lex.Val(crc, EDecimal);
1.504 + }
1.505 + TEST2(crc, aCrcValue);
1.506 + file.Close();
1.507 + }
1.508 +