os/persistentdata/persistentstorage/centralrepository/test/t_cenrep_cache.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/centralrepository/test/t_cenrep_cache.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1403 @@
     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 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include <f32file.h>
    1.21 +#include <badesca.h>
    1.22 +
    1.23 +#include "srvrepos_noc.h"
    1.24 +#include "srvres.h"
    1.25 +#include "cachemgr.h"
    1.26 +#include "sessnotf.h"
    1.27 +
    1.28 +RTest TheTest(_L("Central Repository Coarse-Grained Cache Tests"));
    1.29 +
    1.30 +enum TBorderTestStage
    1.31 +	{
    1.32 +	ENominalNoFile = -1,
    1.33 +	ENominal = 0,
    1.34 +	ESizeMin,
    1.35 +	ESizeMinplus,
    1.36 +	ESizeMaxminus,
    1.37 +	ESizeMax,
    1.38 +	ESizeMaxplus,
    1.39 +	ETimeoutMin,
    1.40 +	ETimeoutMinplus,
    1.41 +	ETimeoutMaxminus,
    1.42 +	EReallyWorstCase,
    1.43 +	ELastStage
    1.44 +	};
    1.45 +
    1.46 +const TUid KUidCacheTestRepositorySm	= { 0x00000002 };
    1.47 +const TUid KUidCacheTestRepositorySm2	= { 0x00000011 };
    1.48 +const TUid KUidCacheTestRepositoryMed   = { 0x11111111 };
    1.49 +const TUid KUidCacheTestRepositoryMed2  = { 0x00057778 };
    1.50 +const TUid KUidCacheTestRepositoryLrg	= { 0xCCCCCC01 };
    1.51 +
    1.52 +const TInt KTimerDelay = 1000000;
    1.53 +const TInt KMemoryFiller = 3762;
    1.54 +
    1.55 +_LIT(KCacheMgrIniFileFolder, "\\private\\10202BE9\\");
    1.56 +_LIT(KCacheMgrIniSrcFile, "centrepcache.ini");
    1.57 +
    1.58 +TTime starttime;
    1.59 +
    1.60 +class TRepositoryCacheManagerTester
    1.61 +	{
    1.62 +	public:
    1.63 +	TRepositoryCacheManagerTester(): iOOMTest(EFalse), iStage(ENominalNoFile), iTestStepStage(0)
    1.64 +		{
    1.65 +		iFs.Connect();
    1.66 +		}
    1.67 +	~TRepositoryCacheManagerTester()
    1.68 +		{
    1.69 +		iFs.Close();
    1.70 +		}
    1.71 +	TInt CacheRepositoryL(TUid aRepUid, TBool& aTrap);
    1.72 +	TInt CacheRepositoryL(TUid aRepUid);
    1.73 +	TBool FindRepository(TUid aRepUid);
    1.74 +	void FuncTestsL();
    1.75 +	void SizeTestsL();
    1.76 +	void TimingTestsL();
    1.77 +	void DefectTestsL();
    1.78 +	void DEF093491L();
    1.79 +	void INC105967();
    1.80 +	void DeleteFilesL();
    1.81 +	void DEF111734L();
    1.82 +	void DEF124147();
    1.83 +	void InstallIniFileL( TBorderTestStage aFileSet );
    1.84 +	void AdvanceToStageL(TBorderTestStage aStage);
    1.85 +	void NextTest(TPtrC aMsg);
    1.86 +	static TInt Callback(TAny* aParent);
    1.87 +	static void CleanUp(TAny*);
    1.88 +public:
    1.89 +	TBool iOOMTest;
    1.90 +	RFs iFs;
    1.91 +	TBorderTestStage iStage;
    1.92 +	TInt iTestStepStage;
    1.93 +	};
    1.94 +
    1.95 +LOCAL_C TRepositoryCacheManagerTester Tester;
    1.96 +
    1.97 +LOCAL_C TCleanupItem gCleanup(TRepositoryCacheManagerTester::CleanUp);
    1.98 +///////////////////////////////////////////////////////////////////////////////////////
    1.99 +///////////////////////////////////////////////////////////////////////////////////////
   1.100 +//Test macroses and functions
   1.101 +
   1.102 +void TRepositoryCacheManagerTester::DeleteFilesL()
   1.103 +	{
   1.104 +	_LIT( KOldInstallFiles, "c:\\private\\10202BE9\\*.ini" );
   1.105 +
   1.106 +	CFileMan* fm = CFileMan::NewL( iFs );
   1.107 +	CleanupStack::PushL( fm );
   1.108 +
   1.109 +	TInt r = fm->Delete( KOldInstallFiles );
   1.110 +	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
   1.111 +		User::Leave(r);
   1.112 +
   1.113 +	CleanupStack::PopAndDestroy( fm );
   1.114 +	}
   1.115 +
   1.116 +LOCAL_C void Check( TInt aValue, TInt aLine )
   1.117 +	{
   1.118 +	if ( !aValue )
   1.119 +		{
   1.120 +		TheTest( EFalse, aLine );
   1.121 +		}
   1.122 +	}
   1.123 +
   1.124 +LOCAL_C void Check( TInt aValue, TInt aExpected, TInt aLine )
   1.125 +	{
   1.126 +	if ( aValue != aExpected )
   1.127 +		{
   1.128 +		RDebug::Print( _L( "*** Expected error: %d, got: %d\r\n"), aExpected, aValue );
   1.129 +		TheTest( EFalse, aLine );
   1.130 +		}
   1.131 +	}
   1.132 +
   1.133 +#define TEST(arg) ::Check((arg), __LINE__)
   1.134 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   1.135 +
   1.136 +///////////////////////////////////////////////////////////////////////////////////////
   1.137 +///////////////////////////////////////////////////////////////////////////////////////
   1.138 +
   1.139 +void TRepositoryCacheManagerTester::NextTest(TPtrC aMsg)
   1.140 +	{
   1.141 +	if (!iOOMTest)
   1.142 +		{
   1.143 +		TheTest.Next( aMsg );
   1.144 +		}
   1.145 +	}
   1.146 +
   1.147 +void TRepositoryCacheManagerTester::AdvanceToStageL(TBorderTestStage aStage)
   1.148 +	{
   1.149 +	iStage = aStage;
   1.150 +	if (aStage != ENominalNoFile)
   1.151 +		{
   1.152 +		InstallIniFileL( aStage );
   1.153 +		}
   1.154 +	else
   1.155 +		{
   1.156 +		DeleteFilesL();
   1.157 +		}
   1.158 +	delete TServerResources::iCacheManager;
   1.159 +	TServerResources::iCacheManager = NULL;
   1.160 +	TServerResources::iCacheManager = CRepositoryCacheManager::NewLC(iFs);
   1.161 +	CleanupStack::Pop(TServerResources::iCacheManager);
   1.162 +	}
   1.163 +
   1.164 +void TRepositoryCacheManagerTester::InstallIniFileL( TBorderTestStage aFileSet )
   1.165 +	{
   1.166 +	_LIT( KDriveC, "c:" );
   1.167 +	_LIT( KDriveZ, "z:" );
   1.168 +
   1.169 +	TBuf<KMaxFileName> src1;
   1.170 +	TBuf<KMaxFileName> dest1;
   1.171 +	TInt r;
   1.172 +
   1.173 +	DeleteFilesL();
   1.174 +
   1.175 +	CFileMan* fm = CFileMan::NewL( iFs );
   1.176 +	CleanupStack::PushL( fm );
   1.177 +
   1.178 +	if ((aFileSet>=ELastStage)||(aFileSet<0))
   1.179 +		{
   1.180 +		RDebug::Print( _L( "Illegal parameter to function: %d\r\n" ), aFileSet );
   1.181 +		TheTest( EFalse, __LINE__ );
   1.182 +		}
   1.183 +	else
   1.184 +		{
   1.185 +		dest1.Copy( KDriveC );
   1.186 +		dest1.Append( KCacheMgrIniFileFolder );
   1.187 +		dest1.Append( KCacheMgrIniFile );
   1.188 +		r = fm->Delete( dest1 );
   1.189 +		if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
   1.190 +			User::Leave( r );
   1.191 +		r = iFs.MkDirAll( dest1 );
   1.192 +		if ( r != KErrNone && r != KErrAlreadyExists )
   1.193 +			User::Leave( r );
   1.194 +		src1.Copy( KDriveZ );
   1.195 +		src1.Append( KCacheMgrIniFileFolder );
   1.196 +		src1.Append( KCacheMgrIniSrcFile );
   1.197 +		TBuf<2> testNo;
   1.198 +		testNo.Num(aFileSet);
   1.199 +		src1.Append( testNo );
   1.200 +		User::LeaveIfError( fm->Copy( src1, dest1 ) );
   1.201 +		r = fm->Attribs( dest1, KEntryAttArchive, KEntryAttReadOnly, TTime( 0 ), CFileMan::ERecurse );
   1.202 +		TEST2( r, KErrNone );
   1.203 +		}
   1.204 +
   1.205 +	CleanupStack::PopAndDestroy( fm );
   1.206 +	}
   1.207 +
   1.208 +TInt TRepositoryCacheManagerTester::CacheRepositoryL(TUid aRepUid)
   1.209 +	{
   1.210 +	TBool trap = EFalse;
   1.211 +	return CacheRepositoryL(aRepUid, trap);
   1.212 +	}
   1.213 +
   1.214 +void TRepositoryCacheManagerTester::CleanUp(TAny*)
   1.215 +	{
   1.216 +	// If cache manager is initialized and used before, we flush it
   1.217 +	if (TServerResources::iCacheManager)
   1.218 +		{
   1.219 +		TServerResources::iCacheManager->FlushCache();
   1.220 +		}
   1.221 +	// To get rid of the iOpenRepositories array leaking problem during OOM testing.
   1.222 +	TServerResources::iObserver->CloseiOpenRepositories();
   1.223 +	TServerResources::iObserver->Reset();
   1.224 +	// To get rid of the array leaking problems during OOM testing.
   1.225 +	TServerResources::iOwnerIdLookUpTable.Reset();
   1.226 +	}
   1.227 +
   1.228 +// Opens and Closes a repository to place it in the cache
   1.229 +TInt TRepositoryCacheManagerTester::CacheRepositoryL(TUid aRepUid, TBool& aTrap)
   1.230 +	{
   1.231 +	TInt repsize = 0;
   1.232 +	// Notifier needed to open repositories.
   1.233 +	CSessionNotifier* notifier;
   1.234 +	notifier = new(ELeave)CSessionNotifier;
   1.235 +	CleanupStack::PushL(notifier);
   1.236 +
   1.237 +    CServerRepository* repository = new(ELeave) CServerRepository();
   1.238 +    CleanupStack::PushL(repository);
   1.239 +    TServerResources::iObserver->iTrapOOMOnOpen = aTrap;
   1.240 +    aTrap = EFalse; // This means that this function didn't leave until attempting to open the repository
   1.241 +    repository->OpenL(aRepUid,*notifier);
   1.242 +    repsize = repository->SizeiRepository();
   1.243 +	repository->Close();
   1.244 +	TServerResources::iObserver->iTrapOOMOnOpen = EFalse;
   1.245 +
   1.246 +   	CleanupStack::PopAndDestroy(repository);
   1.247 +   	CleanupStack::PopAndDestroy(notifier);
   1.248 +
   1.249 +   	return repsize;
   1.250 +	}
   1.251 +
   1.252 +// Checks if a repository is in the cache
   1.253 +TBool TRepositoryCacheManagerTester::FindRepository(TUid aRepUid)
   1.254 +	{
   1.255 +	for(TInt i=TServerResources::iCacheManager->iIdleRepositories.Count()-1; i>=0; i--)
   1.256 +		{
   1.257 +		if(TServerResources::iCacheManager->iIdleRepositories[i].iSharedRepository->Uid()==aRepUid)
   1.258 +			{
   1.259 +			return ETrue;
   1.260 +			}
   1.261 +		}
   1.262 +	return EFalse;
   1.263 +	}
   1.264 +
   1.265 +TInt TRepositoryCacheManagerTester::Callback(TAny* aParent)
   1.266 +	{
   1.267 +	TTime now;
   1.268 +	now.UniversalTime();
   1.269 +	TheTest.Printf(_L("Timer Expired at %Ld\n"), now.MicroSecondsFrom(starttime).Int64());
   1.270 +	((CPeriodic*)aParent)->Cancel();;
   1.271 +	CActiveScheduler::Stop();
   1.272 +
   1.273 +	return KErrNone;
   1.274 +	}
   1.275 +
   1.276 +/**
   1.277 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-CT-1419
   1.278 +@SYMTestCaseDesc Test Coarse-Grained Caching functionality.
   1.279 +@SYMTestPriority High
   1.280 +@SYMTestActions  Check correct initialization of the cache manager, expected caching behaviour after
   1.281 + repository open/close, correct setting of cache size, correct functioning of the cache flush
   1.282 + functionality, OOM recovery by cache flushing feature and cache Disable/Enable functionality
   1.283 +@SYMTestExpectedResults The test must not fail.
   1.284 +@SYMPREQ PREQ1192
   1.285 +@SYMTestStatus Defined
   1.286 +@SYMDevelopedForRelease Symbian OS v9.3
   1.287 +@SYMAuthor Aleks Pamir
   1.288 +*/
   1.289 +void TRepositoryCacheManagerTester::FuncTestsL()
   1.290 +	{
   1.291 +	NextTest(_L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-1419 Initialize Cache Manager " ));
   1.292 +
   1.293 +	iTestStepStage = 0;
   1.294 +	TEST( TServerResources::iCacheManager != 0 );
   1.295 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.296 +	TServerResources::iCacheManager->EnableCache();
   1.297 +
   1.298 +	NextTest( _L( "Cache Repository (Open/Close)" ) );
   1.299 +
   1.300 +	CacheRepositoryL(KUidCacheTestRepositorySm);
   1.301 +
   1.302 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.303 +		{
   1.304 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.305 +		}
   1.306 +	else
   1.307 +		{
   1.308 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.309 +		}
   1.310 +
   1.311 +	NextTest( _L( "Check Cache Size" ) );
   1.312 +
   1.313 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.314 +		{
   1.315 +		TEST( TServerResources::iCacheManager->iTotalCacheUsage == 0 );
   1.316 +		}
   1.317 +	else
   1.318 +		{
   1.319 +		TEST( TServerResources::iCacheManager->iTotalCacheUsage > 0 );
   1.320 +		}
   1.321 +
   1.322 +	NextTest( _L( "Flush Cache" ) );
   1.323 +
   1.324 +	TServerResources::iCacheManager->FlushCache();
   1.325 +
   1.326 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.327 +	TEST( TServerResources::iCacheManager->iTotalCacheUsage == 0 );
   1.328 +
   1.329 +
   1.330 +	if (!iOOMTest)
   1.331 +		{
   1.332 +		// This test is excluded from generic OOM testing because
   1.333 +		// 1. It is trying to simulate OOM condition in a different way (fillingup the memory with
   1.334 +		// allocs) which makes it very tricky to work under normal OOM testing method
   1.335 +		// 2. Loading Lrg repository within OOM takes forever because there're many allocations, and
   1.336 +		// loading other repositories are already tested in other tests
   1.337 +
   1.338 +		NextTest( _L( "Cache OOM Handling" ) );
   1.339 +
   1.340 +		// Fill cache with reps
   1.341 +		TInt totalsize;
   1.342 +		TInt cachedRepositoryCount = 0;
   1.343 +
   1.344 +		totalsize = CacheRepositoryL(KUidCacheTestRepositorySm);
   1.345 +		if (TServerResources::iCacheManager->iCacheSize >= totalsize)
   1.346 +			{
   1.347 +			cachedRepositoryCount++;
   1.348 +			}
   1.349 +		totalsize += CacheRepositoryL(KUidCacheTestRepositorySm2);
   1.350 +		if (TServerResources::iCacheManager->iCacheSize >= totalsize)
   1.351 +			{
   1.352 +			cachedRepositoryCount++;
   1.353 +			}
   1.354 +		totalsize += CacheRepositoryL(KUidCacheTestRepositoryLrg);
   1.355 +		if (TServerResources::iCacheManager->iCacheSize >= totalsize)
   1.356 +			{
   1.357 +			cachedRepositoryCount++;
   1.358 +			}
   1.359 +		totalsize += CacheRepositoryL(KUidCacheTestRepositoryMed2);
   1.360 +		if (TServerResources::iCacheManager->iCacheSize >= totalsize)
   1.361 +			{
   1.362 +			cachedRepositoryCount++;
   1.363 +			}
   1.364 +
   1.365 +		TInt res = KErrNone;
   1.366 +		//	Fill Memory
   1.367 +		TInt popCount=0;
   1.368 +		RHeap& myHeap = User::Heap();
   1.369 +		TInt firstSize = myHeap.Size();
   1.370 +		TInt biggestBlock;
   1.371 +		TInt firstAvail = myHeap.Available(biggestBlock);
   1.372 +
   1.373 +		while(ETrue)
   1.374 +			{
   1.375 +			// We need to really fill up the memory, because we want it to be really freed when we explictly
   1.376 +			// free it, so that when we alloc again there will be some free memory. Using debug allocfail
   1.377 +			// tools would have been too artifical for such a test
   1.378 +			TAny* dummy = User::Alloc(KMemoryFiller);
   1.379 +			if (dummy)
   1.380 +				{
   1.381 +				popCount++;
   1.382 +				TRAP( res, CleanupStack::PushL(dummy); CleanupStack::Pop(dummy););
   1.383 +				if (res == KErrNoMemory)
   1.384 +					{
   1.385 +					// If we cannot allocate enough memory for the cleanupstack frame, we also deallocate
   1.386 +					// the last memory block. This is mandatory for the correct functioning of the
   1.387 +					// following test cases
   1.388 +					// Note when an object that is attempted to be PushL to the cleanupstack and it fails
   1.389 + 					// the object will be freed automatically.
   1.390 +					popCount--;
   1.391 +					break;
   1.392 +					}
   1.393 +				else
   1.394 +					{
   1.395 +					CleanupStack::PushL(dummy);
   1.396 +					}
   1.397 +				}
   1.398 +			else
   1.399 +				{
   1.400 +				firstSize = myHeap.Size();
   1.401 +				firstAvail = myHeap.Available(biggestBlock);
   1.402 +				break;
   1.403 +				}
   1.404 +			};
   1.405 +
   1.406 +		// Test if cache is as full as it should be
   1.407 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == cachedRepositoryCount );
   1.408 +
   1.409 +		// Try loading Med rep. Should Fail
   1.410 +		TRAP( res, CacheRepositoryL(KUidCacheTestRepositoryMed));
   1.411 +
   1.412 +		TEST( res == KErrNoMemory );
   1.413 +
   1.414 +		// Cache is still as it was before attempting load
   1.415 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == cachedRepositoryCount );
   1.416 +
   1.417 +		// Try loading Med rep with OOM trapping.
   1.418 +		TBool oomtrap = ETrue;
   1.419 +		TRAP( res, CacheRepositoryL(KUidCacheTestRepositoryMed, oomtrap));
   1.420 +
   1.421 +		if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.422 +			{
   1.423 +			// Should still fail because the cache was empty to begin with so there's nothing
   1.424 +			// to evict and no memory to gain
   1.425 +			TEST( res == KErrNoMemory );
   1.426 +			TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.427 +			}
   1.428 +		else if (iStage == ESizeMinplus)
   1.429 +			{
   1.430 +			// Should still fail because although the cache was full, the amount of memory freed
   1.431 +			// will not be enough for the medium repository
   1.432 +			TEST( res == KErrNoMemory );
   1.433 +			if (oomtrap)
   1.434 +				{
   1.435 +				// Memory alloc failed even before open was attempted, so no cache flush happened
   1.436 +				TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == cachedRepositoryCount );
   1.437 +				}
   1.438 +			else
   1.439 +				{
   1.440 +				TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.441 +				}
   1.442 +			}
   1.443 +		else
   1.444 +			{
   1.445 +			// Should Pass and evict repositories
   1.446 +			TEST( res == KErrNone );
   1.447 +			TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.448 +			}
   1.449 +
   1.450 +		CleanupStack::PopAndDestroy(popCount);
   1.451 +		}
   1.452 +
   1.453 +	NextTest( _L( "Cache Disable/Enable" ) );
   1.454 +
   1.455 +	TServerResources::iCacheManager->FlushCache();
   1.456 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.457 +
   1.458 +	TServerResources::iCacheManager->EnableCache();
   1.459 +
   1.460 +	CacheRepositoryL(KUidCacheTestRepositorySm);
   1.461 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.462 +		{
   1.463 +		// No cache
   1.464 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.465 +		}
   1.466 +	else
   1.467 +		{
   1.468 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.469 +		}
   1.470 +
   1.471 +	TServerResources::iCacheManager->DisableCache();
   1.472 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.473 +	CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.474 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.475 +	TServerResources::iCacheManager->EnableCache();
   1.476 +
   1.477 +	NextTest( _L( "Multi Client Test" ) );
   1.478 +
   1.479 +	TServerResources::iCacheManager->FlushCache();
   1.480 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.481 +
   1.482 +	// Creating two sets of server objects
   1.483 +	CSessionNotifier* notifier = new(ELeave)CSessionNotifier;
   1.484 +	CleanupStack::PushL(notifier);
   1.485 +
   1.486 +    CServerRepository* repository = new(ELeave) CServerRepository();
   1.487 +    CleanupStack::PushL(repository);
   1.488 +
   1.489 +	CSessionNotifier* notifier2 = new(ELeave)CSessionNotifier;
   1.490 +	CleanupStack::PushL(notifier2);
   1.491 +
   1.492 +    CServerRepository* repository2 = new(ELeave) CServerRepository();
   1.493 +    CleanupStack::PushL(repository2);
   1.494 +
   1.495 +    // Open a rep with 1st client
   1.496 +    repository->OpenL(KUidCacheTestRepositorySm,*notifier);
   1.497 +    // Open same rep with 2nd client
   1.498 +    repository2->OpenL(KUidCacheTestRepositorySm,*notifier2);
   1.499 +
   1.500 +    // One repository is in the cache, because now(PREQ 1228) all repositories
   1.501 +    // are moved to cache just after they are opened, not after they are closed.
   1.502 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.503 +		{
   1.504 +		// No cache
   1.505 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.506 +		}
   1.507 +	else
   1.508 +		{
   1.509 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.510 +		}
   1.511 +	// First client closed
   1.512 +	repository->Close();
   1.513 +	// Repository still in the cache
   1.514 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.515 +		{
   1.516 +		// No cache
   1.517 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.518 +		}
   1.519 +	else
   1.520 +		{
   1.521 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.522 +		}
   1.523 +	// Second client closed
   1.524 +	repository2->Close();
   1.525 +
   1.526 +	// One rep must still be in the cache now
   1.527 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.528 +		{
   1.529 +		// No cache
   1.530 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.531 +		}
   1.532 +	else
   1.533 +		{
   1.534 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.535 +		}
   1.536 +
   1.537 +	NextTest( _L( "Notify-Only Client Cache Repository (Open Only)" ) );
   1.538 +
   1.539 +	TServerResources::iCacheManager->FlushCache();
   1.540 +	TServerResources::iObserver->CloseiOpenRepositories();
   1.541 +
   1.542 +    // Open a rep with 1st client
   1.543 +    repository->OpenL(KUidCacheTestRepositorySm,*notifier);
   1.544 +
   1.545 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.546 +		{
   1.547 +		// No memory, so no cache
   1.548 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.549 +		// But it's open
   1.550 +		TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.551 +		}
   1.552 +	else
   1.553 +		{
   1.554 +		// Repository should have been cached after open
   1.555 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.556 +		// And it's open
   1.557 +		TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.558 +		}
   1.559 +
   1.560 +	// First client closed
   1.561 +	repository->Close();
   1.562 +
   1.563 +	if ((iStage == ESizeMin)||(iStage == EReallyWorstCase))
   1.564 +		{
   1.565 +		// No memory, so no cache
   1.566 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.567 +		// Now it's closed
   1.568 +		TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) == KErrNotFound );
   1.569 +		}
   1.570 +	else
   1.571 +		{
   1.572 +		// Still in cache because timeout hasn't occured
   1.573 +		TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.574 +		// And still open
   1.575 +		TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.576 +		}
   1.577 +
   1.578 +   	CleanupStack::PopAndDestroy(repository2);
   1.579 +   	CleanupStack::PopAndDestroy(notifier2);
   1.580 +   	CleanupStack::PopAndDestroy(repository);
   1.581 +   	CleanupStack::PopAndDestroy(notifier);
   1.582 +
   1.583 +	TServerResources::iCacheManager->FlushCache();
   1.584 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.585 +
   1.586 +	}
   1.587 +
   1.588 +/**
   1.589 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-CT-1420
   1.590 +@SYMTestCaseDesc Test Coarse-Grained Caching forced eviction rules.
   1.591 +@SYMTestPriority High
   1.592 +@SYMTestActions  Check correct initialization of the cache manager, disabling/enabling
   1.593 + cache with different cache size, filling, eventual forced eviction of repository under
   1.594 + small and large memory cases
   1.595 +@SYMTestExpectedResults The test must not fail.
   1.596 +@SYMPREQ PREQ1192
   1.597 +@SYMTestStatus Defined
   1.598 +@SYMDevelopedForRelease Symbian OS v9.3
   1.599 +@SYMAuthor Aleks Pamir
   1.600 +*/
   1.601 +void TRepositoryCacheManagerTester::SizeTestsL()
   1.602 +	{
   1.603 +	NextTest( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-1420 Forced Eviction-Small cache " ) );
   1.604 +
   1.605 +	iTestStepStage = 1;
   1.606 +	TEST( TServerResources::iCacheManager != 0 );
   1.607 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.608 +
   1.609 +	// Flush and disable the cache
   1.610 +	TServerResources::iCacheManager->DisableCache(ETrue);
   1.611 +
   1.612 +	// Calculate cache size for this test. It's important to calculate this at runtime,
   1.613 +	// because this size is approximate and it changes from emulator to hardware
   1.614 +
   1.615 +	TServerResources::iCacheManager->EnableCache(KInvalidEvictionTimeout, KDefaultCacheSize);
   1.616 +
   1.617 +	TInt smallCacheSize = CacheRepositoryL(KUidCacheTestRepositorySm);
   1.618 +	smallCacheSize+= CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.619 +	smallCacheSize+= (CacheRepositoryL(KUidCacheTestRepositorySm2)/2);
   1.620 +
   1.621 +	// smallCacheSize is KUidCacheTestRepositorySm+KUidCacheTestRepositoryMed+ half of KUidCacheTestRepositorySm2
   1.622 +	// the reason is we don't want KUidCacheTestRepositorySm2 to fit
   1.623 +
   1.624 +	TServerResources::iCacheManager->DisableCache(ETrue);
   1.625 +	//Change cache size
   1.626 +	TServerResources::iCacheManager->EnableCache(KInvalidEvictionTimeout, smallCacheSize);
   1.627 +
   1.628 +	// Fill cache
   1.629 +	CacheRepositoryL(KUidCacheTestRepositorySm);
   1.630 +	CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.631 +	User::After(KTimerDelay);
   1.632 +	//This one will not fit, and the Small rep will be evicted because it's small
   1.633 +	CacheRepositoryL(KUidCacheTestRepositorySm2);
   1.634 +
   1.635 +	TEST( !FindRepository(KUidCacheTestRepositorySm) );
   1.636 +	TEST( FindRepository(KUidCacheTestRepositorySm2) );
   1.637 +
   1.638 +	TInt delay = TServerResources::iCacheManager->iDefaultTimeout.Int()-KTimerDelay;
   1.639 +	// If timeout is smaller then KTimerDelay, don't bother to wait
   1.640 +	if (delay>0)
   1.641 +		{
   1.642 +		User::After(delay);
   1.643 +		}
   1.644 +	//Medium will be evicted this time, because it's older and size difference doesn't make up for the time difference
   1.645 +	CacheRepositoryL(KUidCacheTestRepositorySm);
   1.646 +
   1.647 +	TEST( !FindRepository(KUidCacheTestRepositoryMed) );
   1.648 +	TEST( FindRepository(KUidCacheTestRepositorySm) );
   1.649 +
   1.650 +	if (!iOOMTest)
   1.651 +		{
   1.652 +		// This test is excluded from generic OOM testing because
   1.653 +		// 1. Loading Lrg repository within OOM takes forever because there're many allocations, and
   1.654 +		// the test is very similar to previous test
   1.655 +
   1.656 +		NextTest( _L( "Forced Eviction-Large cache" ) );
   1.657 +
   1.658 +		TServerResources::iCacheManager->DisableCache(ETrue);
   1.659 +
   1.660 +		TServerResources::iCacheManager->EnableCache(KInvalidEvictionTimeout, KDefaultCacheSize);
   1.661 +
   1.662 +		TInt largeCacheSize = CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.663 +		largeCacheSize+= CacheRepositoryL(KUidCacheTestRepositoryLrg);
   1.664 +		largeCacheSize+= (CacheRepositoryL(KUidCacheTestRepositoryMed2)/2);
   1.665 +
   1.666 +		// smallCacheSize is KUidCacheTestRepositorySm+KUidCacheTestRepositoryMed+ half of KUidCacheTestRepositorySm2
   1.667 +		// the reason is we don't want KUidCacheTestRepositorySm2 to fit
   1.668 +
   1.669 +		TServerResources::iCacheManager->DisableCache(ETrue);
   1.670 +
   1.671 +		//Change Parameters
   1.672 +		TServerResources::iCacheManager->EnableCache(KInvalidEvictionTimeout, largeCacheSize);
   1.673 +		// Fill cache
   1.674 +		CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.675 +		CacheRepositoryL(KUidCacheTestRepositoryLrg);
   1.676 +		User::After(KTimerDelay);
   1.677 +		//This one will not fit, and the Med rep will be evicted because it's small
   1.678 +		CacheRepositoryL(KUidCacheTestRepositoryMed2);
   1.679 +
   1.680 +		TEST( !FindRepository(KUidCacheTestRepositoryMed) );
   1.681 +		TEST( FindRepository(KUidCacheTestRepositoryLrg) );
   1.682 +
   1.683 +		delay = TServerResources::iCacheManager->iDefaultTimeout.Int()-KTimerDelay;
   1.684 +		// If timeout is smaller then KTimerDelay, don't bother to wait
   1.685 +		if (delay>0)
   1.686 +			{
   1.687 +			User::After(delay);
   1.688 +			}
   1.689 +
   1.690 +		//Medium2 will be evicted, because even if it's new, this time size difference makes up for the
   1.691 +		//time difference and the Large repository stays even if it's older
   1.692 +		CacheRepositoryL(KUidCacheTestRepositoryMed);
   1.693 +
   1.694 +		TEST( !FindRepository(KUidCacheTestRepositoryMed2) );
   1.695 +		TEST( FindRepository(KUidCacheTestRepositoryLrg) );
   1.696 +		}
   1.697 +	}
   1.698 +
   1.699 +/**
   1.700 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-CT-1421
   1.701 +@SYMTestCaseDesc Test Coarse-Grained Caching normal eviction rules.
   1.702 +@SYMTestPriority High
   1.703 +@SYMTestActions  Check correct initialization of the cache manager, adding repository into
   1.704 + cache and active object driven eviction at correct times.
   1.705 +@SYMTestExpectedResults The test must not fail.
   1.706 +@SYMPREQ PREQ1192
   1.707 +@SYMTestStatus Defined
   1.708 +@SYMDevelopedForRelease Symbian OS v9.3
   1.709 +@SYMAuthor Aleks Pamir
   1.710 +*/
   1.711 +void TRepositoryCacheManagerTester::TimingTestsL()
   1.712 +	{
   1.713 +
   1.714 +	iTestStepStage = 2;
   1.715 +
   1.716 +	TEST( TServerResources::iCacheManager != 0 );
   1.717 +	TServerResources::iCacheManager->FlushCache();
   1.718 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.719 +
   1.720 +	CPeriodic* timer = CPeriodic::NewL(EPriorityLow);
   1.721 +	CleanupStack::PushL(timer);
   1.722 +
   1.723 +	NextTest( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-1421 Cache Timing(Component Test) " ) );
   1.724 +
   1.725 +	starttime.UniversalTime();
   1.726 +	// Add one repository in the cache
   1.727 +	CacheRepositoryL(KUidCacheTestRepositorySm);
   1.728 +	TTime now;
   1.729 +	now.UniversalTime();
   1.730 +	TheTest.Printf(_L("KUidCacheTestRepositorySm Added at %Ld\n"), now.MicroSecondsFrom(starttime).Int64());
   1.731 +
   1.732 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.733 +
   1.734 +	// Wait until the repository is evicted normally
   1.735 +	// We can't use User::After because we don't want to suspend the thread
   1.736 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()-KTimerDelay), 0, TCallBack(Callback, timer));
   1.737 +
   1.738 +	CActiveScheduler::Start();
   1.739 +
   1.740 +	// Callback should have been called before Cache timeout had occured
   1.741 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.742 +
   1.743 +	TServerResources::iCacheManager->FlushCache();
   1.744 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.745 +
   1.746 +	starttime.UniversalTime();
   1.747 +	// Add another repository in the cache
   1.748 +	CacheRepositoryL(KUidCacheTestRepositorySm2);
   1.749 +	now.UniversalTime();
   1.750 +	TheTest.Printf(_L("KUidCacheTestRepositorySm2 added at %Ld\n"), now.MicroSecondsFrom(starttime).Int64());
   1.751 +
   1.752 +	// wait more than default timeout.
   1.753 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()+KTimerDelay), 0, TCallBack(Callback, timer));
   1.754 +
   1.755 +	CActiveScheduler::Start();
   1.756 +
   1.757 +	// Callback should have been called by now
   1.758 +	TInt count = TServerResources::iCacheManager->iIdleRepositories.Count();
   1.759 +	if (count > 0)
   1.760 +		{
   1.761 +		// Do not fail test on emulator. CTimer::AtUTC is often
   1.762 +		// late by 1 to a few seconds.
   1.763 +		#if defined(__WINSCW__) || defined(__WINS__)
   1.764 +		TheTest.Printf(_L("*** Line %d check fail. Ignored on winscw."), __LINE__);
   1.765 +		TServerResources::iCacheManager->FlushCache();
   1.766 +		#endif
   1.767 +		}
   1.768 +
   1.769 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.770 +
   1.771 +	TServerResources::iCacheManager->FlushCache();
   1.772 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.773 +
   1.774 +	NextTest( _L( "Notify-Only Client Timing Test" ) );
   1.775 +
   1.776 +	// Add one repository in the cache
   1.777 +	CSessionNotifier* notifier = new(ELeave)CSessionNotifier;
   1.778 +	CleanupStack::PushL(notifier);
   1.779 +
   1.780 +    CServerRepository* repository = new(ELeave) CServerRepository();
   1.781 +    CleanupStack::PushL(repository);
   1.782 +
   1.783 +	starttime.UniversalTime();
   1.784 +
   1.785 +    // Open a rep
   1.786 +    repository->OpenL(KUidCacheTestRepositorySm,*notifier);
   1.787 +
   1.788 +	now.UniversalTime();
   1.789 +	TheTest.Printf(_L("KUidCacheTestRepositorySm Added at %Ld\n"), now.MicroSecondsFrom(starttime).Int64());
   1.790 +
   1.791 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.792 +
   1.793 +	// Wait until the repository is evicted normally
   1.794 +	// We can't use User::After because we don't want to suspend the thread
   1.795 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()-KTimerDelay), 0, TCallBack(Callback, timer));
   1.796 +
   1.797 +	CActiveScheduler::Start();
   1.798 +
   1.799 +	// Callback should have been called before Cache timeout had occured
   1.800 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.801 +
   1.802 +	TServerResources::iCacheManager->FlushCache();
   1.803 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.804 +
   1.805 +	// Add another repository in the cache
   1.806 +	// Add one repository in the cache
   1.807 +	CSessionNotifier* notifier2 = new(ELeave)CSessionNotifier;
   1.808 +	CleanupStack::PushL(notifier2);
   1.809 +
   1.810 +    CServerRepository* repository2 = new(ELeave) CServerRepository();
   1.811 +    CleanupStack::PushL(repository2);
   1.812 +
   1.813 +	starttime.UniversalTime();
   1.814 +
   1.815 +    // Open another rep
   1.816 +    repository2->OpenL(KUidCacheTestRepositorySm2,*notifier2);
   1.817 +
   1.818 +	now.UniversalTime();
   1.819 +	TheTest.Printf(_L("KUidCacheTestRepositorySm2 added at %Ld\n"), now.MicroSecondsFrom(starttime).Int64());
   1.820 +
   1.821 +	// wait more than default timeout.
   1.822 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()+KTimerDelay), 0, TCallBack(Callback, timer));
   1.823 +
   1.824 +	CActiveScheduler::Start();
   1.825 +
   1.826 +	// Callback should have been called by now
   1.827 +	count = TServerResources::iCacheManager->iIdleRepositories.Count();
   1.828 +	if (count > 0)
   1.829 +		{
   1.830 +		// Do not fail test on emulator. CTimer::AtUTC is often
   1.831 +		// late by 1 to a few seconds.
   1.832 +		#if defined(__WINSCW__) || defined(__WINS__)
   1.833 +		TheTest.Printf(_L("*** Line %d check fail. Ignored on winscw."), __LINE__);
   1.834 +		TServerResources::iCacheManager->FlushCache();
   1.835 +		#endif
   1.836 +		}
   1.837 +
   1.838 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.839 +
   1.840 +	repository2->Close();
   1.841 +	repository->Close();
   1.842 +
   1.843 +   	CleanupStack::PopAndDestroy(repository2);
   1.844 +   	CleanupStack::PopAndDestroy(notifier2);
   1.845 +   	CleanupStack::PopAndDestroy(repository);
   1.846 +   	CleanupStack::PopAndDestroy(notifier);
   1.847 +
   1.848 +	CleanupStack::PopAndDestroy(timer);
   1.849 +	}
   1.850 +
   1.851 +void TRepositoryCacheManagerTester::DefectTestsL()
   1.852 +	{
   1.853 +	iTestStepStage = 3;	// Don't run OOM tests on defects
   1.854 +
   1.855 +	NextTest(_L( "DEF093491: [AQP]Centrep server flushes repository cache when temporarily disabled" ));
   1.856 +   	DEF093491L();
   1.857 +
   1.858 +	NextTest(_L( "INC105967: CenRep crashes when no ROM directory" ));
   1.859 +   	INC105967();
   1.860 +   	
   1.861 +	}
   1.862 +
   1.863 +/**
   1.864 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-CT-1883
   1.865 +@SYMTestCaseDesc [AQP]Centrep server flushes repository cache when temporarily disabled
   1.866 +@SYMTestPriority High
   1.867 +@SYMTestActions  Open repository, disable cache, check if the rep is still in memory
   1.868 +@SYMTestExpectedResults The test must not fail or panic .
   1.869 +@SYMDEF DEF093491
   1.870 +*/
   1.871 +void TRepositoryCacheManagerTester::DEF093491L()
   1.872 +	{
   1.873 +	NextTest( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-1883 " ) );
   1.874 +	TServerResources::iCacheManager->FlushCache();
   1.875 +
   1.876 +	// Notifier needed to open repositories.
   1.877 +	CSessionNotifier* notifier;
   1.878 +	notifier = new(ELeave)CSessionNotifier;
   1.879 +	CleanupStack::PushL(notifier);
   1.880 +
   1.881 +    CServerRepository* repository = new(ELeave) CServerRepository();
   1.882 +    CleanupStack::PushL(repository);
   1.883 +    // Open repository
   1.884 +    repository->OpenL(KUidCacheTestRepositorySm,*notifier);
   1.885 +
   1.886 +	// Check it's in cache and memory
   1.887 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.888 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.889 +    // disable cache
   1.890 +	TServerResources::iCacheManager->DisableCache();
   1.891 +
   1.892 +	// Check it's in memory but not in cache
   1.893 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.894 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.895 +
   1.896 +	repository->Close();
   1.897 +	// the repository should now be unloaded from memory
   1.898 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) == KErrNotFound );
   1.899 +	// enable cache again
   1.900 +	TServerResources::iCacheManager->EnableCache();
   1.901 +
   1.902 +    // Open repository again
   1.903 +    repository->OpenL(KUidCacheTestRepositorySm,*notifier);
   1.904 +
   1.905 +	// Check it's in cache and memory
   1.906 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 1 );
   1.907 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound );
   1.908 +
   1.909 +	repository->Close();
   1.910 +
   1.911 +    // disable cache
   1.912 +	TServerResources::iCacheManager->DisableCache(ETrue);
   1.913 +
   1.914 +	// now it should be flushed out of both memory and cache
   1.915 +	TEST( TServerResources::iCacheManager->iIdleRepositories.Count() == 0 );
   1.916 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) == KErrNotFound );
   1.917 +
   1.918 +   	CleanupStack::PopAndDestroy(repository);
   1.919 +   	CleanupStack::PopAndDestroy(notifier);
   1.920 +	}
   1.921 +
   1.922 +// Helper function for INC105967L
   1.923 +LOCAL_C TInt TestThread(TAny*)
   1.924 +	{
   1.925 +	CTrapCleanup* cleanup = CTrapCleanup::New();
   1.926 +	if(!cleanup)
   1.927 +		return KErrNoMemory;
   1.928 +
   1.929 +	HBufC* tempFileName;
   1.930 +	// this call shouldn't cause a panic, but leave with KErrNotFound
   1.931 +	TRAPD(err,
   1.932 +		{
   1.933 +		TServerResources::CreateRepositoryFileNameLC(tempFileName, KUidCacheTestRepositorySm, ERom, ECre);
   1.934 +		CleanupStack::PopAndDestroy(tempFileName);
   1.935 +		});
   1.936 +	// test if the function leaves with KErrNot Found
   1.937 +	TEST2(err, KErrNotFound);
   1.938 +
   1.939 +	delete cleanup;
   1.940 +	return KErrNone;
   1.941 +	}
   1.942 +
   1.943 +/**
   1.944 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-UT-3474
   1.945 +@SYMTestCaseDesc CenRep crashes when no ROM directory
   1.946 +@SYMTestPriority High
   1.947 +@SYMTestActions Save RomDirectory, temporarily NULL the pointer, call panicking function in a seperate thread
   1.948 + (to survive even if it panics and continue other tests), check that it leaves with KErrNotFound, restore RomDirectory
   1.949 +@SYMTestExpectedResults The test must not panic.
   1.950 +@SYMDEF INC105967
   1.951 +*/
   1.952 +void TRepositoryCacheManagerTester::INC105967()
   1.953 +	{
   1.954 +	NextTest( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-UT-3474 " ) );
   1.955 +	//store RomDirectory
   1.956 +	HBufC* tempRomDirectory = TServerResources::iRomDirectory;
   1.957 +
   1.958 +	// temporarily delete it to simulate the behaviour in TServerResources::InitialiseL() when:
   1.959 +	// 	if(iFs.Entry(*iRomDirectory, fsEntry)!=KErrNone || !fsEntry.IsDir())
   1.960 +	TServerResources::iRomDirectory = NULL;
   1.961 +
   1.962 +	RThread testThread;
   1.963 +	_LIT(KThreadName, "TestThread");
   1.964 +
   1.965 +	testThread.Create(KThreadName, TestThread, KDefaultStackSize, KMinHeapSize, 0x100000, NULL);
   1.966 +
   1.967 +	TRequestStatus requestStatus;
   1.968 +	// Request notification when the thread terminates
   1.969 +	testThread.Logon(requestStatus);
   1.970 +	// Let the thread execute
   1.971 +	testThread.Resume();
   1.972 +
   1.973 +	// Wait for termination
   1.974 +	User::WaitForRequest(requestStatus);
   1.975 +	// test if the thread terminated normally (the leave has been trapped) not panicked
   1.976 +	TEST2(requestStatus.Int(), KErrNone);
   1.977 +
   1.978 +	//restore RomDirectory
   1.979 +	TServerResources::iRomDirectory = tempRomDirectory;
   1.980 +	}
   1.981 +
   1.982 +/**
   1.983 +@SYMTestCaseID PDS-CENTRALREPOSITORY-UT-4077
   1.984 +@SYMTestCaseDesc Central Repository cache manager incorrectly uses CTimer::AtUTC() 
   1.985 +@SYMTestPriority High
   1.986 +@SYMTestActions Call CRepositoryCacheManager::RescheduleTimer passing in various
   1.987 +				timer values to test conversion of 64 bit UTC time value into
   1.988 +				32 bit microsecond value.
   1.989 +@SYMTestExpectedResults The timer should be active after each call to RescheduleTimer
   1.990 +						The test must not panic.
   1.991 +@SYMDEF DEF124147
   1.992 +*/
   1.993 +void TRepositoryCacheManagerTester::DEF124147()
   1.994 +	{
   1.995 +	NextTest( _L( " @SYMTestCaseID:PDS-CENTRALREPOSITORY-UT-4077 " ) );
   1.996 +	
   1.997 +	//Cancel any pending timer
   1.998 +	TServerResources::iCacheManager->Cancel();
   1.999 +	
  1.1000 +	TTime now;
  1.1001 +	now.UniversalTime();
  1.1002 +	
  1.1003 +	TTimeIntervalMinutes oneMinute(1);
  1.1004 +	TTimeIntervalHours oneHour(1);
  1.1005 +	
  1.1006 +	//Rechedule timer now
  1.1007 +	TServerResources::iCacheManager->RescheduleTimer(now);
  1.1008 +	TEST(TServerResources::iCacheManager->IsActive());
  1.1009 +	
  1.1010 +	//Cancel any pending timer
  1.1011 +	TServerResources::iCacheManager->Cancel();
  1.1012 +	
  1.1013 +	
  1.1014 +	//Rechedule timer in the past
  1.1015 +	TServerResources::iCacheManager->RescheduleTimer(now - oneMinute);
  1.1016 +	TEST(TServerResources::iCacheManager->IsActive());
  1.1017 +	
  1.1018 +	//Cancel any pending timer
  1.1019 +	TServerResources::iCacheManager->Cancel();
  1.1020 +	
  1.1021 +	//Rechedule timer in the future
  1.1022 +	TServerResources::iCacheManager->RescheduleTimer(now + oneMinute);
  1.1023 +	TEST(TServerResources::iCacheManager->IsActive());
  1.1024 +	
  1.1025 +	//Cancel any pending timer
  1.1026 +	TServerResources::iCacheManager->Cancel();
  1.1027 +	
  1.1028 +	//Rechedule timer an hour in the past
  1.1029 +	TServerResources::iCacheManager->RescheduleTimer(now - oneHour);
  1.1030 +	TEST(TServerResources::iCacheManager->IsActive());
  1.1031 +	
  1.1032 +	//Cancel any pending timer
  1.1033 +	TServerResources::iCacheManager->Cancel();
  1.1034 +	
  1.1035 +	//Rechedule timer an hour in the future
  1.1036 +	TServerResources::iCacheManager->RescheduleTimer(now + oneHour);
  1.1037 +	TEST(TServerResources::iCacheManager->IsActive());
  1.1038 +	
  1.1039 +	//Cancel any pending timer
  1.1040 +	TServerResources::iCacheManager->Cancel();
  1.1041 +	}
  1.1042 +
  1.1043 +// ---------------------------------------------------
  1.1044 +// OomTest
  1.1045 +//
  1.1046 +// Function to convert a test into an OOM test
  1.1047 +
  1.1048 +/**
  1.1049 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-CT-1422
  1.1050 +@SYMTestCaseDesc Test functionality under OOM.
  1.1051 +@SYMTestPriority High
  1.1052 +@SYMTestActions  Test functionality under OOM.
  1.1053 +@SYMTestExpectedResults The test must not fail.
  1.1054 +@SYMPREQ PREQ1192
  1.1055 +@SYMTestStatus Defined
  1.1056 +@SYMDevelopedForRelease Symbian OS v9.3
  1.1057 +@SYMAuthor Aleks Pamir
  1.1058 +*/
  1.1059 +LOCAL_C void OomTest( void (*testFuncL)() )
  1.1060 +	{
  1.1061 +	TheTest.Next( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-CT-1422 " ) );
  1.1062 +	TInt error;
  1.1063 +	TInt count = 0;
  1.1064 +
  1.1065 +	do
  1.1066 +		{
  1.1067 +		User::__DbgMarkStart( RHeap::EUser );
  1.1068 +
  1.1069 +		// find out the number of open handles
  1.1070 +		TInt startProcessHandleCount;
  1.1071 +		TInt startThreadHandleCount;
  1.1072 +		RThread().HandleCount(startProcessHandleCount, startThreadHandleCount);
  1.1073 +
  1.1074 +		User::__DbgSetAllocFail( RHeap::EUser, RHeap::EFailNext, ++count );
  1.1075 +
  1.1076 +		TRAP( error, (testFuncL)() );
  1.1077 +
  1.1078 +		User::__DbgSetAllocFail( RHeap::EUser, RHeap::ENone, 1 );
  1.1079 +
  1.1080 +		// check that no handles have leaked
  1.1081 +		TInt endProcessHandleCount;
  1.1082 +		TInt endThreadHandleCount;
  1.1083 +		RThread().HandleCount(endProcessHandleCount, endThreadHandleCount);
  1.1084 +
  1.1085 +		TEST(startProcessHandleCount == endProcessHandleCount);
  1.1086 +		TEST(startThreadHandleCount  == endThreadHandleCount);
  1.1087 +
  1.1088 +		User::__DbgMarkEnd( RHeap::EUser, 0 );
  1.1089 +		} while( error == KErrNoMemory );
  1.1090 +
  1.1091 +	_LIT( KTestFailed, "Out of memory test failure on iteration %d\n" );
  1.1092 +	__ASSERT_ALWAYS( error == KErrNone, TheTest.Panic( error, KTestFailed, count ) );
  1.1093 +
  1.1094 +	TheTest.Printf( _L( "Out of memory tests succeeded at heap failure rate of %i\n" ), count );
  1.1095 +	TheTest.Printf( _L( "Process handle count preserved\n" ) );
  1.1096 +	TheTest.Printf( _L( "Thread handle count preserved\n" ) );
  1.1097 +	}
  1.1098 +
  1.1099 +LOCAL_C void DoOOMTestsL()
  1.1100 +	{
  1.1101 +	// To clean up the static array under OOM
  1.1102 +	CleanupStack::PushL(gCleanup);
  1.1103 +
  1.1104 +	// To be able to run tests faster, especially the last 2 tests with delays,
  1.1105 +	// a flow-through to the next case statement is intended here. If the Tester
  1.1106 +	// function completes successfully, meaning that it has passed all memory
  1.1107 +	// allocations, then the execution continues from the next step and the iTestStepStage
  1.1108 +	// variable is set to the current test step stage. Otherwise the function will Leave
  1.1109 +	// with KErrNoMemory, will be trapped in OomTest and DoOOMTestsL will be recalled.
  1.1110 +	// Once a test step stage is passed, Tester.iTestStepStage will ensure that the same
  1.1111 +	// function will no be called over and over again for every memory allocation failure,
  1.1112 +	// thereby reducing the running time of the test considerably.
  1.1113 +
  1.1114 +	switch(Tester.iTestStepStage)
  1.1115 +		{
  1.1116 +		case 0:
  1.1117 +			{
  1.1118 +			Tester.FuncTestsL();
  1.1119 +			}
  1.1120 +		case 1:
  1.1121 +			{
  1.1122 +			Tester.SizeTestsL();
  1.1123 +			}
  1.1124 +		case 2:
  1.1125 +			{
  1.1126 +			Tester.TimingTestsL();
  1.1127 +			}
  1.1128 +		default:
  1.1129 +			break;
  1.1130 +		}
  1.1131 +	// To clean up the memory left in static variables
  1.1132 +	Tester.CleanUp(NULL);
  1.1133 +
  1.1134 +	CleanupStack::Pop();
  1.1135 +	}
  1.1136 +
  1.1137 +LOCAL_C void DoTestsL()
  1.1138 +	{
  1.1139 +	switch(Tester.iStage)
  1.1140 +		{
  1.1141 +		case ENominalNoFile:
  1.1142 +		default:
  1.1143 +			{
  1.1144 +			TheTest.Start( _L( "Cache functionality tests with default params(no .ini)" ) );
  1.1145 +			Tester.FuncTestsL();
  1.1146 +			Tester.SizeTestsL();
  1.1147 +			Tester.TimingTestsL();
  1.1148 +			TheTest.End();
  1.1149 +			break;
  1.1150 +			}
  1.1151 +		case ENominal:
  1.1152 +			{
  1.1153 +			TheTest.Start( _L( "Cache functionality tests with default params(default values read from .ini)" ) );
  1.1154 +			Tester.FuncTestsL();
  1.1155 +			Tester.SizeTestsL();
  1.1156 +			Tester.TimingTestsL();
  1.1157 +			// Only test defects once here unless a defect occurs in different memory conditions
  1.1158 +			Tester.DefectTestsL();
  1.1159 +			TheTest.End();
  1.1160 +			break;
  1.1161 +			}
  1.1162 +		case ESizeMin:
  1.1163 +			{
  1.1164 +			TheTest.Start( _L( "Cache functionality tests with no space for cache (size=0)" ) );
  1.1165 +			Tester.FuncTestsL();
  1.1166 +			// No size tests because size is controlled in this test
  1.1167 +			// No Timing tests because nothing will be cached(cache size 0) so nothing to time
  1.1168 +			TheTest.End();
  1.1169 +			break;
  1.1170 +			}
  1.1171 +		case ESizeMinplus:
  1.1172 +			{
  1.1173 +			TheTest.Start( _L( "Cache functionality tests with small cache size" ) );
  1.1174 +			Tester.FuncTestsL();
  1.1175 +			// No size tests because size is controlled in this test
  1.1176 +			Tester.TimingTestsL();
  1.1177 +			TheTest.End();
  1.1178 +			break;
  1.1179 +			}
  1.1180 +		case ESizeMaxminus:
  1.1181 +			{
  1.1182 +			TheTest.Start( _L( "Cache functionality tests with large cache size" ) );
  1.1183 +			Tester.FuncTestsL();
  1.1184 +			// No size tests because size is controlled in this test
  1.1185 +			Tester.TimingTestsL();
  1.1186 +			TheTest.End();
  1.1187 +			break;
  1.1188 +			}
  1.1189 +		case ESizeMax:
  1.1190 +			{
  1.1191 +			TheTest.Start( _L( "Cache functionality tests with max cache size (=heap max 2MB)" ) );
  1.1192 +			Tester.FuncTestsL();
  1.1193 +			// No size tests because size is controlled in this test
  1.1194 +			Tester.TimingTestsL();
  1.1195 +			TheTest.End();
  1.1196 +			break;
  1.1197 +			}
  1.1198 +		case ESizeMaxplus:
  1.1199 +			{
  1.1200 +			TheTest.Start( _L( "Cache functionality Robustness test" ) );
  1.1201 +			Tester.FuncTestsL();
  1.1202 +			// No size tests because size is controlled in this test
  1.1203 +			Tester.TimingTestsL();
  1.1204 +			TheTest.End();
  1.1205 +			break;
  1.1206 +			}
  1.1207 +		case ETimeoutMin:
  1.1208 +			{
  1.1209 +			TheTest.Start( _L( "Cache functionality tests with no timeout for cache (timeout=0)" ) );
  1.1210 +			Tester.FuncTestsL();
  1.1211 +			Tester.SizeTestsL();
  1.1212 +			// No timing tests because timeout is controlled in this test
  1.1213 +			TheTest.End();
  1.1214 +			break;
  1.1215 +			}
  1.1216 +		case ETimeoutMinplus:
  1.1217 +			{
  1.1218 +			TheTest.Start( _L( "Cache functionality tests with short timeout" ) );
  1.1219 +			Tester.FuncTestsL();
  1.1220 +			Tester.SizeTestsL();
  1.1221 +			// No timing tests because timeout is controlled in this test
  1.1222 +			TheTest.End();
  1.1223 +			break;
  1.1224 +			}
  1.1225 +		case ETimeoutMaxminus:
  1.1226 +			{
  1.1227 +			TheTest.Start( _L( "Cache functionality tests with large timeout" ) );
  1.1228 +			Tester.FuncTestsL();
  1.1229 +			Tester.SizeTestsL();
  1.1230 +			// No timing tests because timeout is controlled in this test
  1.1231 +			TheTest.End();
  1.1232 +			break;
  1.1233 +			}
  1.1234 +		case EReallyWorstCase:
  1.1235 +			{
  1.1236 +			TheTest.Start( _L( "Cache functionality Worst Case test with no timeout and no size for cache" ) );
  1.1237 +			Tester.FuncTestsL();
  1.1238 +			Tester.SizeTestsL();
  1.1239 +			// No Timing tests because nothing will be cached(cache size 0) so nothing to time
  1.1240 +			TheTest.End();
  1.1241 +			break;
  1.1242 +			}
  1.1243 +		};
  1.1244 +	}
  1.1245 +
  1.1246 +
  1.1247 +/**
  1.1248 +@SYMTestCaseID SYSLIB-CENTRALREPOSITORY-UT-4014
  1.1249 +@SYMTestCaseDesc Cenrep cache manager should not evict sub-sessions in active transactions.
  1.1250 +@SYMTestPriority High
  1.1251 +@SYMTestActions  Open 2 sub-sessions to a repository, start a transaction, let the cache manager run.
  1.1252 +@SYMTestExpectedResults The test must not fail or panic .
  1.1253 +@SYMDEF DEF111734
  1.1254 +*/
  1.1255 +void TRepositoryCacheManagerTester::DEF111734L()
  1.1256 +	{
  1.1257 +	NextTest( _L( " @SYMTestCaseID:SYSLIB-CENTRALREPOSITORY-UT-4014 DEF111734: Cache Manager and Open Transactions "));
  1.1258 +
  1.1259 +	TServerResources::iCacheManager->FlushCache();
  1.1260 +
  1.1261 +	// Notifier needed to open repositories.
  1.1262 +	// notifier 1
  1.1263 +	CSessionNotifier* notifier;
  1.1264 +	notifier = new(ELeave)CSessionNotifier;
  1.1265 +	CleanupStack::PushL(notifier);
  1.1266 +
  1.1267 +	// notifier 2
  1.1268 +	CSessionNotifier* notifier2;
  1.1269 +	notifier2 = new(ELeave)CSessionNotifier;
  1.1270 +	CleanupStack::PushL(notifier2);
  1.1271 +
  1.1272 +	// connection 1
  1.1273 + 	CServerRepository* repository = new(ELeave) CServerRepository();
  1.1274 +	CleanupStack::PushL(repository);
  1.1275 +
  1.1276 +	// connection 2
  1.1277 +	CServerRepository* repository2 = new(ELeave) CServerRepository();
  1.1278 + 	CleanupStack::PushL(repository2);
  1.1279 +
  1.1280 +	// Open repository
  1.1281 +	repository->OpenL(KUidCacheTestRepositorySm,*notifier);
  1.1282 +
  1.1283 +	// open second connection to the repository
  1.1284 +	repository2->OpenL(KUidCacheTestRepositorySm,*notifier2);
  1.1285 +
  1.1286 +	// we have to observers to the same repository - so we should have only 1 entry in
  1.1287 +	// the idle repositories for the cache manager
  1.1288 +
  1.1289 +	TEST(TServerResources::iCacheManager->iIdleRepositories.Count() == 1);
  1.1290 +
  1.1291 +	// check CR's global memory for an instance for this repository.
  1.1292 +	TEST(TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound);
  1.1293 +
  1.1294 +	// start a transaction on connection 2, this adds this sub-session to the
  1.1295 +	// transaction queue for the repository.
  1.1296 +
  1.1297 +	TInt error = repository2->StartTransaction(3);
  1.1298 +	TEST(error == KErrNone);
  1.1299 +
  1.1300 +	// This will wipe out the memory for the open repository unless we modify the code in some fashion.
  1.1301 +	TServerResources::iCacheManager->FlushCache(EFalse);
  1.1302 +
  1.1303 +	// since we can't use the user::after method since we need to this thread to suspend
  1.1304 +	// start a timer (stolen from above).
  1.1305 +	CPeriodic* timer = CPeriodic::NewL(EPriorityLow);
  1.1306 +	CleanupStack::PushL(timer);
  1.1307 +
  1.1308 +
  1.1309 +	// since repositories that involved in active transactions no longer have their memory
  1.1310 +	// removed by cache manager, give this time enough time to go through a few cycles of
  1.1311 +	// the normal eviction process.
  1.1312 +
  1.1313 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()*3), 0, TCallBack(Callback, timer));
  1.1314 +
  1.1315 +	// will cause the cache manager to run
  1.1316 +	CActiveScheduler::Start();
  1.1317 +
  1.1318 +	// If it isn't in cache and memory than the cache manager must have collected it
  1.1319 +	// and the code for this defect is not in the build.
  1.1320 +	TEST(TServerResources::iCacheManager->iIdleRepositories.Count() == 0);
  1.1321 +	TEST(TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) != KErrNotFound);
  1.1322 +
  1.1323 +	// close this sub-session.  should cancel the active transaction. allowing cache manager to clean
  1.1324 +	// up the memory.
  1.1325 +	repository2->Close();
  1.1326 +
  1.1327 +	TInt i = 0;
  1.1328 +	const TUint32 KInt1 = 0x1;
  1.1329 +	repository->Get(KInt1, i);
  1.1330 +
  1.1331 +	// close the last observer for this repository.  this places the memory for this repository
  1.1332 +	// back onto the idle list for cache manager.
  1.1333 +	repository->Close();
  1.1334 +
  1.1335 +	// Wait until the repository is evicted normally
  1.1336 +
  1.1337 +	timer->Start(TTimeIntervalMicroSeconds32(TServerResources::iCacheManager->iDefaultTimeout.Int()*2), 0, TCallBack(Callback, timer));
  1.1338 +
  1.1339 +	CActiveScheduler::Start();
  1.1340 +
  1.1341 +	// the repository should now be unloaded from memory
  1.1342 +	TEST( TServerResources::iObserver->FindOpenRepository(KUidCacheTestRepositorySm) == KErrNotFound );
  1.1343 +
  1.1344 +   	CleanupStack::PopAndDestroy(5);
  1.1345 +	}
  1.1346 +
  1.1347 +LOCAL_C void MainL()
  1.1348 +	{
  1.1349 +	__UHEAP_MARK;
  1.1350 +	TheTest.Start( _L( "Cache Tests" ) );
  1.1351 +
  1.1352 +	// create and install the active scheduler we need
  1.1353 +	CActiveScheduler* s = new(ELeave) CActiveScheduler;
  1.1354 +	CleanupStack::PushL( s );
  1.1355 +	CActiveScheduler::Install( s );
  1.1356 +
  1.1357 +	Tester.DeleteFilesL();
  1.1358 +
  1.1359 +	TServerResources::InitialiseL();
  1.1360 +
  1.1361 +	
  1.1362 +	Tester.DEF111734L();
  1.1363 +	Tester.DEF124147();
  1.1364 +	
  1.1365 +
  1.1366 +	for(TInt i=ENominalNoFile; i<ELastStage; i++)
  1.1367 +		{
  1.1368 +		Tester.AdvanceToStageL( static_cast<TBorderTestStage>(i) );
  1.1369 +		DoTestsL();
  1.1370 +		}
  1.1371 +
  1.1372 +	TheTest.Next( _L(  "Out of memory tests" ) );
  1.1373 +	Tester.iOOMTest = ETrue;
  1.1374 +
  1.1375 +	Tester.AdvanceToStageL( ENominal );
  1.1376 +	OomTest( DoOOMTestsL );
  1.1377 +
  1.1378 +	TServerResources::Close();
  1.1379 +
  1.1380 +	// Cleanup the scheduler
  1.1381 +	CleanupStack::PopAndDestroy( s );
  1.1382 +
  1.1383 +	TheTest.End();
  1.1384 +	TheTest.Close();
  1.1385 +	__UHEAP_MARKEND;
  1.1386 +	}
  1.1387 +
  1.1388 +TInt E32Main()
  1.1389 +	{
  1.1390 +#ifdef __SECURE_DATA__
  1.1391 +	__UHEAP_MARK;
  1.1392 +	CTrapCleanup* cleanup = CTrapCleanup::New();
  1.1393 +	if ( !cleanup )
  1.1394 +		return KErrNoMemory;
  1.1395 +
  1.1396 +	TRAPD( err, MainL()  );
  1.1397 +	if ( err != KErrNone )
  1.1398 +		User::Panic( _L( "Testing failed: " ), err );
  1.1399 +
  1.1400 +	delete cleanup;
  1.1401 +	__UHEAP_MARKEND;
  1.1402 +#endif
  1.1403 +
  1.1404 +	return 0;
  1.1405 +	}
  1.1406 +