First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include "../../test/t_cenrep_helper.h"
21 #define CENREP_PC_TEST
25 #include <x86tool/centralrepository.h>
27 #include <centralrepository.h>
30 _LIT( KCentralRepositoryServerName, "Centralrepositorysrv");
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);
39 LOCAL_C void Check(TInt aValue, TInt aLine)
43 TheTest(EFalse, aLine);
47 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
49 if(aValue != aExpected)
51 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
52 TheTest(EFalse, aLine);
56 #define TEST(arg) ::Check((arg), __LINE__)
57 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
59 ///////////////////////////////////////////////////////////////////////////////////////
60 TInt CopyFile(const TDesC& aSource, const TDesC& aTarget)
63 TInt ret=file.Open(TheFs,aSource,EFileRead);
68 HBufC8* buf=HBufC8::New(fileSize);
74 TPtr8 mod(buf->Des());
77 ret=file.Replace(TheFs,aTarget,EFileWrite);
87 void OomTest(void (*testFuncL)(CRepository* aRepository),TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
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)
98 CRepository* repository=NULL;
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);
105 InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
108 // This is supported by symuser but still have problem, so skiped
109 // User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
112 // find out the number of open handles
113 // TOOLS2 somehow not supporting RThread().HandleCount()?
115 TInt startProcessHandleCount;
116 TInt startThreadHandleCount;
117 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
120 TRAP(error, (testFuncL)(repository));
122 // check that no handles have leaked
124 TInt endProcessHandleCount;
125 TInt endThreadHandleCount;
126 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
128 TEST2(endProcessHandleCount, startProcessHandleCount);
129 TEST2(endThreadHandleCount, startThreadHandleCount);
131 // User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
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));
142 void ObjectCreateDeleteOOM(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
148 SetupEnv(aInFilePath,aOutFilePath,aTestMode);
152 TInt startProcessHandleCount;
153 TInt startThreadHandleCount;
154 RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
156 // This is supported by symuser but still has problems, so skipped.
157 // User::__DbgSetAllocFail(RHeap::EUser, RHeap::EDeterministic, ++count);
159 CRepository* repository=NULL;
161 TRAP(error,InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);CleanupStack::Pop());
165 // User::__DbgSetAllocFail(RHeap::EUser, RHeap::ENone, 1);
168 // check that no handles have leaked
169 TInt endProcessHandleCount;
170 TInt endThreadHandleCount;
171 RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
173 TEST2(endProcessHandleCount, startProcessHandleCount);
174 TEST2(endThreadHandleCount, startThreadHandleCount);
178 } while(error == KErrNoMemory);
181 void GetFunctionL(CRepository* aRepository)
186 User::LeaveIfError(aRepository->Get(6,intVal));
188 User::LeaveIfError(aRepository->Get(8,realVal));
191 User::LeaveIfError(aRepository->Get(0x300,stringVal));
192 TEST(stringVal.Compare(_L("Hello World"))==0);
196 TInt ret=aRepository->Get(0x300,shortBuffer,actualLength);
197 TEST(ret==KErrOverflow);
198 TEST(actualLength==11);
199 TEST(shortBuffer.Compare(_L("Hello"))==0);
203 User::LeaveIfError(aRepository->GetMeta(2,metaValue));
204 TEST(metaValue==0xa);
206 User::LeaveIfError(aRepository->GetMeta(0x204,metaValue));
207 TEST(metaValue==0x20);
209 User::LeaveIfError(aRepository->GetMeta(0x10000,metaValue));
210 TEST(metaValue==0x10);
213 void FindFunctionL(CRepository* aRepository)
215 RArray<TUint32> keyList;
216 CleanupClosePushL(keyList);
218 TInt ret=aRepository->FindL(0xFFFFFFFF,0,keyList);
219 User::LeaveIfError(ret);
221 TEST(keyList.Count()==33);
223 //Find match EQ specific value
224 ret=aRepository->FindEqL(0xFFFFFFFF,0,10,keyList);
225 User::LeaveIfError(ret);
227 TEST(keyList.Count()==3);
229 //Find match NEQ specific value
230 ret=aRepository->FindNeqL(0xFFFFFFFF,0,10,keyList);
232 TEST(keyList.Count()==30);
234 //Find using string matching instead
235 _LIT(KString,"empty");
236 ret=aRepository->FindEqL(0xFFFFFFFF,0,KString(),keyList);
237 User::LeaveIfError(ret);
239 TEST(keyList.Count()==1);
240 CleanupStack::PopAndDestroy();
243 void SetFunctionL(CRepository* aRepository)
249 User::LeaveIfError(aRepository->Get(1,value));
251 User::LeaveIfError(aRepository->Set(1,100));
252 User::LeaveIfError(aRepository->Get(1,value));
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);
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")));
268 User::LeaveIfError(aRepository->Get(0x10000,stringValue));
269 TEST(stringValue.Compare(_L("full"))==0);
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));
276 User::LeaveIfError(aRepository->GetMeta(0x205,metaValue));
277 //as the key 0x205 is within range, expect use of range meta
278 TEST(metaValue==0x20);
281 void CreateFunctionL(CRepository* aRepository)
285 User::LeaveIfError(aRepository->Create(0x206,value));
286 User::LeaveIfError(aRepository->Get(0x206,value));
288 User::LeaveIfError(aRepository->GetMeta(0x206,metaValue));
290 TEST(metaValue==0x20);
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));
298 TEST(metaValue==0x10);
300 //create already existing setting
301 TInt ret=aRepository->Create(1,value);
302 TEST(ret==KErrAlreadyExists);
305 void DeleteFunctionL(CRepository* aRepository)
309 User::LeaveIfError(aRepository->Delete(6));
310 TInt ret=aRepository->Get(6,value);
311 TEST(ret==KErrNotFound);
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);
321 User::LeaveIfError(aRepository->Delete(0x03010000,0xFFFFF0FF,error));
322 //check that key no longer exist
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();
332 void MoveFunctionL(CRepository* aRepository)
334 RArray<TUint32> keyList;
335 CleanupClosePushL(keyList);
337 //check source key exists first
339 TInt ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
340 User::LeaveIfError(ret);
341 TEST(keyList.Count()==5);
343 User::LeaveIfError(aRepository->Move(0x02010000,0x06010000,0xFFFFF0FF,errorKey));
346 //check target now exists
347 ret=aRepository->FindL(0x06010000,0xFFFFF0FF,keyList);
348 User::LeaveIfError(ret);
350 TEST(keyList.Count()==5);
351 //check source now deleted
353 ret=aRepository->FindL(0x02010000,0xFFFFF0FF,keyList);
354 TEST(ret==KErrNotFound);
355 TEST2(keyList.Count(),0);
357 CleanupStack::PopAndDestroy();
362 void BasicFunctionL(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
365 SetupEnv(aInFilePath,aOutFilePath,aTestMode);
368 CRepository* repository=NULL;
370 InitialiseLC(repository,aUid,aInFilePath,aOutFilePath,aTestMode);
372 #ifdef CENREP_PC_TEST
373 //testing transaction
374 TInt r= repository->StartTransaction(CRepository::EConcurrentReadWriteTransaction);
376 repository->CleanupCancelTransactionPushL();
379 GetFunctionL(repository);
380 FindFunctionL(repository);
381 SetFunctionL(repository);
382 CreateFunctionL(repository);
383 DeleteFunctionL(repository);
384 MoveFunctionL(repository);
386 #ifdef CENREP_PC_TEST
387 CleanupStack::PopAndDestroy();
390 CleanupStack::PopAndDestroy();
395 void OomBasicFunction(TUid aUid,const TDesC& aInFilePath,const TDesC& aOutFilePath,TUint aTestMode)
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);
409 void DEF130394L(TUid aUid)
411 CRepository* repos=NULL;
413 repos = CRepository::NewL(aUid);
415 TInt err = repos->Create(1,1);
420 repos = CRepository::NewL(aUid);
422 err = repos->Create(1,1);
423 TEST(err==KErrAlreadyExists);
428 void DoFileCompL(const TDesC& aGenerated, const TDesC& aReference, TUint32& aCrcValue)
432 TInt err = genFile.Open(TheFs,aGenerated,EFileRead);
433 User::LeaveIfError(err);
434 err = refFile.Open(TheFs,aReference,EFileRead);
435 User::LeaveIfError(err);
438 genFile.Size(sizeOfGen);
440 refFile.Size(sizeOfRef);
441 TEST(sizeOfGen == sizeOfRef);
446 HBufC8* buf = HBufC8::New(sizeOfGen);
451 User::Leave(KErrNoMemory);
453 TPtr8 data(buf->Des());
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);
462 TEST(crcGen == crcRef);
473 void DoCrcCompL(const TUint32& aCrcValue, const TDesC& aCrcRecord, TBool aCreOrTxt)
476 TInt err = file.Open(TheFs, aCrcRecord, EFileRead);
477 User::LeaveIfError(err);
482 TBuf8<1> breaker(_L8("-"));
483 TInt pos = buf.Find(breaker);
485 TInt length = buf.Length();
487 const TPtrC8 crePtr = buf.Right(length - pos -1);
488 const TPtrC8 txtPtr = buf.Left(pos);
495 lex.Val(crc, EDecimal);
500 lex.Val(crc, EDecimal);
502 TEST2(crc, aCrcValue);