lowlevellibsandfws/pluginfw/Framework/MagicServerTest/MagicServerTest.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 // CEComServer and CEComSession OOM tests
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <e32math.h>
       
    20 #include <bautils.h>
       
    21 #include "EComServerStart.h"
       
    22 #include "EComServer.h"
       
    23 #include "EComServerSession.h"
       
    24 #include "EcomTestUtils.h"
       
    25 #include "EcomTestIniFileUtils.h"
       
    26 #include "ServerStartupManager.h"
       
    27 #include "Discoverer.h"
       
    28 #include "DiscovererObserver.h"
       
    29 
       
    30 static RTest TheTest(_L("ECOM Server - OOM test"));
       
    31 const TUid KCExampleInterfaceUid = {0x10009DC0};
       
    32 static RFs TheFs;
       
    33 
       
    34 static void KillEComServerL()
       
    35 	{
       
    36 	//Need to ensure that the EComServer process is killed before even starting this test by using
       
    37    	//the EComTestUtils library
       
    38    	_LIT(KEComServerProcessName,"ecomserver");
       
    39    	TRAPD(error, EComTestUtils::KillProcessL(KEComServerProcessName));
       
    40    	error=error;
       
    41 	}
       
    42 
       
    43 //
       
    44 //
       
    45 //Test macroes and functions
       
    46 //
       
    47 //
       
    48 
       
    49 static void Check(TInt aValue, TInt aLine)
       
    50 	{
       
    51 	if(!aValue)
       
    52 		{
       
    53 		TheTest(EFalse, aLine);
       
    54 		}
       
    55 	}
       
    56 static  void Check(TInt aValue, TInt aExpected, TInt aLine)
       
    57 	{
       
    58 	if(aValue != aExpected)
       
    59 		{
       
    60 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
       
    61 		TheTest(EFalse, aLine);
       
    62 		}
       
    63 	}
       
    64 #define TEST(arg) ::Check((arg), __LINE__)
       
    65 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
       
    66 
       
    67 //
       
    68 //
       
    69 
       
    70 void NewLC_OOMTestL()
       
    71 	{
       
    72 	TInt processHandlesS = 0;
       
    73 	TInt threadHandlesS = 0;
       
    74 	TInt processHandlesE = 0;
       
    75 	TInt threadHandlesE = 0;
       
    76 	RThread().HandleCount(processHandlesS, threadHandlesS);
       
    77 	for(TInt count=1;;count +=100)
       
    78 		{
       
    79 		// Setting Heap failure for OOM test
       
    80 		__UHEAP_SETFAIL(RHeap::EDeterministic, count);
       
    81 		__UHEAP_MARK;
       
    82 
       
    83 		CEComServer* ecomServer = NULL;
       
    84 		TRAPD(err, ecomServer = CEComServer::NewLC();
       
    85 				   CleanupStack::PopAndDestroy(ecomServer));
       
    86 		if(err == KErrNoMemory)
       
    87 			{
       
    88 			__UHEAP_MARKEND;
       
    89 			}
       
    90 		else if(err == KErrNone)
       
    91 			{
       
    92 			__UHEAP_MARKEND;
       
    93 			RDebug::Print(_L("The test succeeded at heap failure rate=%d.\n"), count);
       
    94 			break;
       
    95 			}
       
    96 		else
       
    97 			{
       
    98 			__UHEAP_MARKEND;
       
    99 			TEST2(err, KErrNone);
       
   100 			}
       
   101 		__UHEAP_RESET;
       
   102 		}
       
   103 	__UHEAP_RESET;
       
   104 	RThread().HandleCount(processHandlesE, threadHandlesE);
       
   105 	TEST(processHandlesS == processHandlesE);
       
   106 	TEST(threadHandlesS == threadHandlesE);
       
   107 	}
       
   108 
       
   109 void ListImplementations_OOMTestL()
       
   110 	{
       
   111 	TInt processHandlesS = 0;
       
   112 	TInt threadHandlesS = 0;
       
   113 	TInt processHandlesE = 0;
       
   114 	TInt threadHandlesE = 0;
       
   115 	RThread().HandleCount(processHandlesS, threadHandlesS);
       
   116 
       
   117 
       
   118 	for(TInt count=1;;++count)
       
   119 		{
       
   120 		CEComServer* ecomServer = CEComServer::NewLC();
       
   121 		// Setting Heap failure for OOM test
       
   122 		__UHEAP_SETFAIL(RHeap::EDeterministic, count);
       
   123 		__UHEAP_MARK;
       
   124 
       
   125 		TClientRequest clientReq;
       
   126 		RArray<TUid> extendedInterfaces;
       
   127 		CleanupClosePushL(extendedInterfaces);
       
   128 		RImplInfoArray* ifArray=NULL;
       
   129 		TRAPD(err, ifArray = ecomServer->ListImplementationsL(KCExampleInterfaceUid,extendedInterfaces,clientReq));
       
   130 		CleanupStack::PopAndDestroy(&extendedInterfaces);
       
   131 		if (ifArray!=NULL)
       
   132 			{
       
   133 			ifArray->Close();
       
   134 			delete ifArray;
       
   135 			}
       
   136 		if(err == KErrNoMemory)
       
   137 			{
       
   138 			CleanupStack::PopAndDestroy(ecomServer);
       
   139 			__UHEAP_MARKEND;
       
   140 			}
       
   141 		else if(err == KErrNone)
       
   142 			{
       
   143 			CleanupStack::PopAndDestroy(ecomServer);
       
   144 			__UHEAP_MARKEND;
       
   145 			//implInfoArray should not be deleted! The caller does not take the ownership.
       
   146 			RDebug::Print(_L("The test succeeded at heap failure rate=%d.\n"), count);
       
   147 			break;
       
   148 			}
       
   149 		else
       
   150 			{
       
   151 			CleanupStack::PopAndDestroy(ecomServer);
       
   152 			__UHEAP_MARKEND;
       
   153 			TEST2(err, KErrNone);
       
   154 			}
       
   155 		__UHEAP_RESET;
       
   156 		}
       
   157 	__UHEAP_RESET;
       
   158 	//CleanupStack::PopAndDestroy(ecomServer);
       
   159 	RThread().HandleCount(processHandlesE, threadHandlesE);
       
   160 	TEST(processHandlesS == processHandlesE);
       
   161 	TEST(threadHandlesS == threadHandlesE);
       
   162 	}
       
   163 
       
   164 /**
       
   165 @SYMTestCaseID			SYSLIB-ECOM-CT-3714
       
   166 @SYMTestCaseDesc 		Tests CEComServer::ListImplementationsL	with customer resolver.
       
   167 @SYMTestPriority 		High
       
   168 @SYMTestActions  		Calls ListImplementationsL(TUid,TUid,const RExtendedInterfacesArray&,const TClientRequest&)
       
   169 						with customer resolver.
       
   170 @SYMTestExpectedResults No OOM errors.
       
   171 @SYMDEF 				DEF111196
       
   172 */
       
   173 void ListImplementations_OOMTest1L()
       
   174 	{
       
   175 	TInt processHandlesS = 0;
       
   176 	TInt threadHandlesS = 0;
       
   177 	TInt processHandlesE = 0;
       
   178 	TInt threadHandlesE = 0;
       
   179 	RThread().HandleCount(processHandlesS, threadHandlesS);
       
   180 
       
   181 
       
   182 	for(TInt count=1;;++count)
       
   183 		{
       
   184 		CEComServer* ecomServer = CEComServer::NewLC();
       
   185 		// Setting Heap failure for OOM test
       
   186 		__UHEAP_SETFAIL(RHeap::EDeterministic, count);
       
   187 		__UHEAP_MARK;
       
   188 
       
   189 		TUid resolverUid = {0x10009DD0};
       
   190 
       
   191 		TClientRequest clientReq;
       
   192 		RArray<TUid> extendedInterfaces;
       
   193 		CleanupClosePushL(extendedInterfaces);
       
   194 		RImplInfoArray* ifArray=NULL;
       
   195 
       
   196 		TRAPD(err, ifArray = ecomServer->ListImplementationsL(KCExampleInterfaceUid,resolverUid,extendedInterfaces,clientReq));
       
   197 		CleanupStack::PopAndDestroy(&extendedInterfaces);
       
   198 		if (ifArray!=NULL)
       
   199 			{
       
   200 			ifArray->Close();
       
   201 			delete ifArray;
       
   202 			}
       
   203 		if(err == KErrNoMemory)
       
   204 			{
       
   205 			CleanupStack::PopAndDestroy(ecomServer);
       
   206 			__UHEAP_MARKEND;
       
   207 			}
       
   208 		else if(err == KErrNone)
       
   209 			{
       
   210 			CleanupStack::PopAndDestroy(ecomServer);
       
   211 			__UHEAP_MARKEND;
       
   212 			//implInfoArray should not be deleted! The caller does not take the ownership.
       
   213 			RDebug::Print(_L("The test succeeded at heap failure rate=%d.\n"), count);
       
   214 			break;
       
   215 			}
       
   216 		else
       
   217 			{
       
   218 			CleanupStack::PopAndDestroy(ecomServer);
       
   219 			__UHEAP_MARKEND;
       
   220 			TEST2(err, KErrNone);
       
   221 			}
       
   222 		__UHEAP_RESET;
       
   223 		}
       
   224 	__UHEAP_RESET;
       
   225 	//CleanupStack::PopAndDestroy(ecomServer);
       
   226 	RThread().HandleCount(processHandlesE, threadHandlesE);
       
   227 	TEST(processHandlesS == processHandlesE);
       
   228 	TEST(threadHandlesS == threadHandlesE);
       
   229 	}
       
   230 
       
   231 void GetResolvedDllInfo_OOMTestL()
       
   232 	{
       
   233 	TInt processHandlesS = 0;
       
   234 	TInt threadHandlesS = 0;
       
   235 	TInt processHandlesE = 0;
       
   236 	TInt threadHandlesE = 0;
       
   237 	RThread().HandleCount(processHandlesS, threadHandlesS);
       
   238 
       
   239 	CEComServer* ecomServer = CEComServer::NewLC();
       
   240 	TClientRequest clientReq;
       
   241 	RArray<TUid> extendedInterfaces;
       
   242 	CleanupClosePushL(extendedInterfaces);
       
   243 	RImplInfoArray* implInfoArray = ecomServer->ListImplementationsL(KCExampleInterfaceUid,extendedInterfaces,clientReq);
       
   244 	CleanupStack::PopAndDestroy(&extendedInterfaces);
       
   245 
       
   246 	TEST(implInfoArray->Count() > 0);
       
   247 
       
   248 	for(TInt count=1;;++count)
       
   249 		{
       
   250 		// Setting Heap failure for OOM test
       
   251 		__UHEAP_SETFAIL(RHeap::EDeterministic, count);
       
   252 		__UHEAP_MARK;
       
   253 
       
   254 		TEntry dllInfo;
       
   255 		TUid dtorIdKey;
       
   256 		TClientRequest clntRq;
       
   257 		CImplementationInformation* info = (*implInfoArray)[0];
       
   258 		TRAPD(err, ecomServer->GetResolvedDllInfoL(info->ImplementationUid(),
       
   259 												   dllInfo, dtorIdKey, clntRq));
       
   260 		if(err == KErrNoMemory)
       
   261 			{
       
   262 			__UHEAP_MARKEND;
       
   263 			}
       
   264 		else if(err == KErrNone)
       
   265 			{
       
   266 			__UHEAP_MARKEND;
       
   267 			RDebug::Print(_L("The test succeeded at heap failure rate=%d.\n"), count);
       
   268 			break;
       
   269 			}
       
   270 		else
       
   271 			{
       
   272 			__UHEAP_MARKEND;
       
   273 			TEST2(err, KErrNone);
       
   274 			}
       
   275 		__UHEAP_RESET;
       
   276 		}
       
   277 	__UHEAP_RESET;
       
   278 	//implInfoArray should be deleted! The caller takes the ownership.
       
   279 	if (implInfoArray!=NULL)
       
   280 		{
       
   281 		implInfoArray->Close();
       
   282 		delete implInfoArray;
       
   283 		}
       
   284 	CleanupStack::PopAndDestroy(ecomServer);
       
   285 	RThread().HandleCount(processHandlesE, threadHandlesE);
       
   286 	TEST(processHandlesS == processHandlesE);
       
   287 	TEST(threadHandlesS == threadHandlesE);
       
   288 	}
       
   289 
       
   290 /**
       
   291 @SYMTestCaseID		SYSLIB-ECOM-CT-0177
       
   292 @SYMTestCaseDesc 	Check that IsSSA works when ecomsrvr has different values.
       
   293 @SYMTestPriority 	High
       
   294 @SYMTestActions  	Check that IsSSA returns ETrue when ecomsrvr ini does not exist.
       
   295                     Check that IsSSA returns the value in the ecomsrvr ini file when
       
   296                     the file exists.
       
   297 @SYMTestExpectedResults The test must not fail.
       
   298 @SYMPREQ PREQ967
       
   299 */
       
   300 void IsSsa_TestL()
       
   301 	{
       
   302 	TInt processHandlesS = 0;
       
   303 	TInt threadHandlesS = 0;
       
   304 	TInt processHandlesE = 0;
       
   305 	TInt threadHandlesE = 0;
       
   306 	RThread().HandleCount(processHandlesS, threadHandlesS);
       
   307 
       
   308 	TBool res;
       
   309 
       
   310 	TestEnableDisableSsaL(TheTest, TheFs);
       
   311 
       
   312     /*****************************************************************/
       
   313 	//test that IsSSA() returns ETrue when ecomsrvr.ini file does
       
   314 	//not exist.
       
   315 
       
   316 	//enable ssa
       
   317 	EnableSsa(TheTest, TheFs);
       
   318 
       
   319 	//test that IsSSA() returns ETrue
       
   320 	CEComServer* server=CEComServer::NewLC();
       
   321 	res = server->IsSSA(TheFs);
       
   322 	CleanupStack::PopAndDestroy();
       
   323 	ResetSsa(TheTest, TheFs);
       
   324 	TEST(res);
       
   325 
       
   326     /*****************************************************************/
       
   327 	//test that IsSSA() returns EFalse when ecomsrvr.ini file exists.
       
   328 
       
   329 	//disable ssa
       
   330 	DisableSsa(TheTest, TheFs);
       
   331 
       
   332 	//test that IsSSA() returns EFalse
       
   333 	server=CEComServer::NewLC();
       
   334 	res = server->IsSSA(TheFs);
       
   335 	CleanupStack::PopAndDestroy();
       
   336 	ResetSsa(TheTest, TheFs);
       
   337 	TEST(!res);
       
   338 
       
   339 	RThread().HandleCount(processHandlesE, threadHandlesE);
       
   340 	TEST(processHandlesS == processHandlesE);
       
   341 	TEST(threadHandlesS == threadHandlesE);
       
   342 	}
       
   343 
       
   344 
       
   345 /**
       
   346 Thread entry point for test thread.
       
   347 Kills the existing ECOM server and launches a new instance
       
   348 
       
   349 */
       
   350 TInt ServerStartThreadEntryL(TAny* /*a*/)
       
   351 	{
       
   352 	__UHEAP_MARK;
       
   353 
       
   354 	//Threshold set so that a OOM failure occur whilst constructing CEComServer::iRegistryData
       
   355 	const TInt KFailThreshold = 500;
       
   356 
       
   357 	TInt dummy;
       
   358 	TAny* ptr  = NULL;
       
   359 
       
   360 	::KillEComServerL();
       
   361 
       
   362 	//Invoking OOM failure by allocating most of the heap before running the server
       
   363 	RHeap heap = User::Heap();
       
   364 	TInt avail = heap.Available(dummy);
       
   365     ptr = User::Alloc(avail-KFailThreshold);
       
   366 
       
   367 	TEST(ptr != NULL); //Check that the allocation was sucessful
       
   368 
       
   369 	TInt result = ServerStart();
       
   370 	delete ptr;
       
   371 
       
   372 	__UHEAP_MARKEND;
       
   373 
       
   374 	return result;
       
   375 	}
       
   376 
       
   377 /**
       
   378 @SYMTestCaseID		SYSLIB-ECOM-CT-3163
       
   379 @SYMTestCaseDesc 	Verify the startup behaviour of the ECOM server under OOM conditions
       
   380 @SYMTestPriority 	High
       
   381 @SYMTestActions  	Generate an OOM condition.
       
   382 					Create a new thread which will launch the ECOM server.
       
   383 					Wait for the thread to exit and check the thread exit type
       
   384 					and reason to verify behaviour.
       
   385 @SYMTestExpectedResults The test must not fail.
       
   386 @SYMDEF DEF094675
       
   387 */
       
   388 void StartServer_OOMTest()
       
   389 	{
       
   390 
       
   391 	_LIT(KStartThreadName,"Server Start Thread");
       
   392 
       
   393 	//Create a new thread to launch the ECOM server
       
   394 	RThread testThread;
       
   395 	testThread.Create(KStartThreadName, ServerStartThreadEntryL,
       
   396 				KDefaultStackSize,KMinHeapSize,KMinHeapSize,NULL);
       
   397 	TRequestStatus status;
       
   398 	testThread.Logon(status);
       
   399 	testThread.Resume();
       
   400 
       
   401 	//Wait for the thread to exit
       
   402 	User::WaitForRequest(status);
       
   403 
       
   404 	//Obtain exit type and reason for test thread
       
   405 	TExitType exitType = testThread.ExitType();
       
   406 	TInt exitReason = testThread.ExitReason();
       
   407 
       
   408 	//close the thread handle
       
   409 	testThread.Close();
       
   410 
       
   411 	//Verify the exit reason and exit code
       
   412 	//Exit type is TExitType::EExitKill when E32Main() exit normally
       
   413 	TEST(exitType == EExitKill);
       
   414 	TEST(exitReason == KErrNoMemory);
       
   415 
       
   416 	}
       
   417 
       
   418 void DoTestL()
       
   419 	{
       
   420 	TheTest.Start(_L("CEComServer::NewLC - OOM test"));
       
   421 	::NewLC_OOMTestL();
       
   422 
       
   423 	TheTest.Next(_L("CEComServer::ListImplementationsL - OOM test"));
       
   424 	::ListImplementations_OOMTestL();
       
   425 
       
   426 	TheTest.Next(_L("CEComServer::GetResolvedDllInfoL - OOM test"));
       
   427 	::GetResolvedDllInfo_OOMTestL();
       
   428 
       
   429 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0177 CEComServer::IsSsa test "));
       
   430 	::IsSsa_TestL();
       
   431 
       
   432 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-3163 Server Startup Panic test "));
       
   433     ::StartServer_OOMTest();
       
   434 
       
   435     TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-3714 CEComServer::ListImplementationsL with customer resolver - OOM test "));
       
   436 	::ListImplementations_OOMTest1L();
       
   437 
       
   438 	}
       
   439 
       
   440 TInt E32Main()
       
   441 	{
       
   442 	__UHEAP_MARK;
       
   443 
       
   444     ::KillEComServerL();
       
   445 
       
   446 	CTrapCleanup* tc = CTrapCleanup::New();
       
   447 	TEST(tc != NULL);
       
   448 
       
   449 	TEST2(TheFs.Connect(), KErrNone);
       
   450 
       
   451 	CActiveScheduler* activeSched = new CActiveScheduler;
       
   452 	TEST(activeSched != NULL);
       
   453 	CActiveScheduler::Install(activeSched);
       
   454 
       
   455 	TheTest.Title();
       
   456 	TRAPD(err, ::DoTestL());
       
   457 	TEST2(err, KErrNone);
       
   458 
       
   459 	ResetSsa(TheTest, TheFs);
       
   460 
       
   461 	TheTest.End();
       
   462 	TheTest.Close();
       
   463 
       
   464 	delete activeSched;
       
   465 
       
   466 	TheFs.Close();
       
   467 
       
   468 	delete tc;
       
   469 
       
   470 	__UHEAP_MARKEND;
       
   471 
       
   472 	User::Heap().Check();
       
   473 	return KErrNone;
       
   474 	}