realtimenetprots/sipfw/Test/CapTests/ProfileAgent/Common/CSuite.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2004-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 // @file  TS_PARAM_SVR_SUITENAMEServer.cpp
       
    15 // for (WINS && !EKA2) versions will be xxxServer.Dll and require a thread to be started
       
    16 // in the process of the client. The client initialises the server by calling the
       
    17 // one and only ordinal.
       
    18 // 
       
    19 //
       
    20 
       
    21 // EPOC includes
       
    22 #include <e32base.h>
       
    23 #include <e32cmn.h>
       
    24 
       
    25 #if (!defined TS_PARAM_SVR_SUITENAME_SERVER_H_)
       
    26 	#include "CSuite.h"
       
    27 #endif
       
    28 
       
    29 
       
    30 //ADD TEST STEP HEADERS HERE
       
    31 PARAM_FOREACH_MESS_BEGIN
       
    32 #include "PARAM_MESS_NAME_CStep.h"
       
    33 PARAM_FOREACH_MESS_END
       
    34 
       
    35 
       
    36 _LIT(KSipProfileServerName,"SipProfileSrv");
       
    37 _LIT(KSipProfileServerFilename, "SipProfileSrv");
       
    38 _LIT(KSipProfileServerSemaphoreName, "SipProfileServerSemaphore");
       
    39 const TUid KServerUid3 = {0x101F413C};
       
    40 
       
    41 
       
    42 // __EDIT_ME__ - Substitute the name of your test server
       
    43 _LIT(KServerName,"Cap_PARAM_SVR_SUITENAME_sc");
       
    44 // __EDIT_ME__ - Use your own server class name
       
    45 
       
    46 CTestPARAM_SVR_SUITENAMEServer* CTestPARAM_SVR_SUITENAMEServer::NewL()
       
    47 /**
       
    48  * @return - Instance of the test server
       
    49  * Same code for Secure and non-secure variants
       
    50  * Called inside the MainL() function to create and start the
       
    51  * CTestServer derived server.
       
    52  */
       
    53 	{
       
    54 // __EDIT_ME__ - Use your own server class name
       
    55 	CTestPARAM_SVR_SUITENAMEServer * server = new (ELeave) CTestPARAM_SVR_SUITENAMEServer();
       
    56 	CleanupStack::PushL(server);
       
    57 	// CServer base class call
       
    58 	server->StartL(KServerName);
       
    59 	CleanupStack::Pop(server);
       
    60 	return server;
       
    61 	}
       
    62 
       
    63 CTestStep* CTestPARAM_SVR_SUITENAMEServer::CreateTestStep(const TDesC& aStepName)
       
    64 /**
       
    65  * @return - A CTestStep derived instance
       
    66  * Secure and non-secure variants
       
    67  * Implementation of CTestServer pure virtual
       
    68  */
       
    69 	{
       
    70 	 CTestStep* testStep = NULL;
       
    71 	// add test steps
       
    72 PARAM_FOREACH_MESS_BEGIN
       
    73 		if (aStepName == _L("CPARAM_MESS_NAMEStep"))
       
    74 			{
       
    75 			testStep =  new(ELeave) CPARAM_MESS_NAMEStep  ;
       
    76 			return testStep;
       
    77 			 }
       
    78 
       
    79 PARAM_FOREACH_MESS_END
       
    80 		return testStep;
       
    81 	 }
       
    82 
       
    83 
       
    84 
       
    85 // Secure variants much simpler
       
    86 // Just an E32Main and a MainL()
       
    87 LOCAL_C void MainL()
       
    88 /**
       
    89  * Secure variant
       
    90  * Much simpler, uses the new Rendezvous() call to sync with the client
       
    91  */
       
    92 	{
       
    93 #if (defined __DATA_CAGING__)
       
    94 	RProcess().DataCaging(RProcess::EDataCagingOn);
       
    95 	RProcess().DataCaging(RProcess::ESecureApiOn);
       
    96 #endif
       
    97 	CActiveScheduler* sched=NULL;
       
    98 	sched=new(ELeave) CActiveScheduler;
       
    99 	CActiveScheduler::Install(sched);
       
   100 // __EDIT_ME__ - Use your own server class name
       
   101 	CTestPARAM_SVR_SUITENAMEServer* server = NULL;
       
   102 	// Create the CTestServer derived server
       
   103 	TRAPD(err,server = CTestPARAM_SVR_SUITENAMEServer::NewL());
       
   104 	if(!err)
       
   105 		{
       
   106 		// Sync with the client and enter the active scheduler
       
   107 		RProcess::Rendezvous(KErrNone);
       
   108 		sched->Start();
       
   109 		}
       
   110 	delete server;
       
   111 	delete sched;
       
   112 	}
       
   113 
       
   114 
       
   115 GLDEF_C TInt E32Main()
       
   116 /**
       
   117  * @return - Standard Epoc error code on process exit
       
   118  * Secure variant only
       
   119  * Process entry point. Called by client using RProcess API
       
   120  */
       
   121 	{
       
   122 	__UHEAP_MARK;
       
   123 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   124 	if(cleanup == NULL)
       
   125 		{
       
   126 		return KErrNoMemory;
       
   127 		}
       
   128 	TRAPD(err,MainL());
       
   129 	delete cleanup;
       
   130 	__UHEAP_MARKEND;
       
   131 	return KErrNone;
       
   132     }
       
   133 
       
   134 
       
   135 TVerdict CCapabilityTestStep::doTestStepPreambleL( void )
       
   136 	{
       
   137 	//If Preamble is not required just pass a success value
       
   138 	TVerdict testResult = CTestStep::doTestStepPreambleL();
       
   139 
       
   140 
       
   141 	return TestStepResult();
       
   142 
       
   143 	}
       
   144 TVerdict CCapabilityTestStep::doTestStepPostambleL( void )
       
   145 	{
       
   146 	//If Postamble is not required just pass a success value
       
   147 	TVerdict testResult = CTestStep::doTestStepPostambleL();
       
   148 
       
   149 	return TestStepResult();
       
   150 
       
   151 	}
       
   152 
       
   153 
       
   154 // Moved from CStep.cpp
       
   155 
       
   156 enum TVerdict CCapabilityTestStep::doTestStepL()
       
   157 	{
       
   158 	//DEF! INFO_PRINTF2(_L("%S - Starting ..."), &iTestStepName);
       
   159 
       
   160 	//The MainThread()creates a separate thread that executes SendReceive
       
   161 	TVerdict vResult = MainThread();
       
   162 
       
   163 	SetTestStepResult(vResult);
       
   164 	return TestStepResult();
       
   165 	}
       
   166 
       
   167 /*
       
   168 ThreadStartFn:
       
   169 Called by:	The Child thread
       
   170 Function:	Calls the Exec_SendReceive
       
   171 */
       
   172 static TInt ThreadStartFn( TAny * ptr )
       
   173 	{
       
   174 	return(((CCapabilityTestStep *)ptr)->Exec_SendReceive());
       
   175 	}
       
   176 
       
   177 /*
       
   178 TVerdict GetVerdict(TInt aAPIretValue)
       
   179 
       
   180 Called by: "MainThread" for returning verdict
       
   181 
       
   182 Parameters(TInt aRetValue) :	0 if API call gets thru without any rejection
       
   183 								1 if API call is rejected for capability error
       
   184 */
       
   185 enum TVerdict CCapabilityTestStep::GetVerdict(TInt aAPIretValue)
       
   186 	{
       
   187 	TVerdict vVerdict[] = {EPass, EFail};
       
   188 
       
   189 	//please leave the following if/else block as the information printed by INFO_PRINTF1 is used bu CapTestSumm
       
   190 	if(iExpect_Rejection)//[Inverse Test] EPass for 1 while EFail for 0
       
   191 		{
       
   192 		INFO_PRINTF1(_L("Test Expected to Fail due to lack of capabilities"));
       
   193 		return vVerdict[(aAPIretValue)?0:1];
       
   194 
       
   195 		}
       
   196 	else //[Direct Test] EPass for 0 while EFail for 1
       
   197 		{
       
   198 		INFO_PRINTF1(_L("Test Expected to Pass with correct capabilities"));
       
   199 		return vVerdict[(aAPIretValue)?1:0];
       
   200 		}
       
   201 	}
       
   202 
       
   203 
       
   204 /*
       
   205 TVerdict MainThread()
       
   206 
       
   207 Called by: "doTestStepL"
       
   208 
       
   209 Purpose:	Creates the child thread(which calls the respective function with regard
       
   210 			to the server and also implements the Message Call). Then this fn.waits for the
       
   211 			completion of the childthread( doesnt matter how the thread did die!)
       
   212 
       
   213 Return Value(Verdict of the TestStep):
       
   214 
       
   215 			A.Reporting PASS/FAIL
       
   216 				Direct Test:
       
   217 						When a message call gets thru. Please note that in such cases
       
   218 						actually the implementation of the message has started. As we
       
   219 						are passing "0" Parameters, server may panic, though our botheration
       
   220 						stops once the call gets thru.
       
   221 						NOTE:	The style is the same when CONNECTION capabilities
       
   222 								are tested, the only diff is you dont have to expect a
       
   223 								panic from server
       
   224 				Inverse Test:
       
   225 						The call should be either failed or panicked with
       
   226 						"KErrPermissionDenied" flag.
       
   227 
       
   228 				General Case:
       
   229 						If a thread creation failed or if the server couldnt be connected
       
   230 						apart from the above two cases, then a FAIL is reported
       
   231 
       
   232 			B.Reporting INCONCLUSIVE
       
   233 						Any panic say from unexpected source (eg:KERN-EXEC) will be
       
   234 						reported INCONCLUSIVE
       
   235 */
       
   236 TVerdict CCapabilityTestStep::MainThread()
       
   237 	{
       
   238 
       
   239 	TBuf<100>	tExitCategory;
       
   240 	TInt		tExitReason = 0;
       
   241 	TBuf<100>	TestStyle;
       
   242 
       
   243 	iExpect_Rejection?TestStyle = _L("Inverse"):TestStyle = _L("Direct");
       
   244 	TCapabilitySet theCaps =  TSecurityInfo(RProcess()).iCaps ;
       
   245 	const TInt KMaxTestThreadHeapSize = 0x10000;
       
   246 
       
   247 	//Initialize return values
       
   248 	iResult_SR = iResult_Server = KErrNone;
       
   249 
       
   250 
       
   251 	// Create a child thread, with a new heap
       
   252 	TInt nRes_Thread =	tChildThread.Create(
       
   253 						ChildThread_SR,
       
   254 						ThreadStartFn,
       
   255 						KDefaultStackSize,
       
   256 						KMinHeapSize,
       
   257 						KMaxTestThreadHeapSize,
       
   258 						this,
       
   259 						EOwnerProcess);
       
   260 
       
   261 
       
   262 	if(nRes_Thread == KErrNone)//Thread Created
       
   263 		{
       
   264 
       
   265 		//Let me know when the thread is dead
       
   266 		TRequestStatus ThreadStatus;
       
   267 		tChildThread.Logon(ThreadStatus);
       
   268 		tChildThread.Resume();
       
   269 		//Is the thread dead?
       
   270 		User::WaitForRequest( ThreadStatus );
       
   271 
       
   272 		//yes, he is dead. RIP!  Now the Killer's profile
       
   273 		tExitCategory	=	tChildThread.ExitCategory();
       
   274 		tExitReason		=	tChildThread.ExitReason();
       
   275 
       
   276 
       
   277 		//Somebody Please say what are we testing!!
       
   278 		if(iSessionCreated && (SR_MESSAGE_ID >=0))//Flag set by Child thread when connected to Server
       
   279 		{
       
   280 			//DEF INFO_PRINTF5(_L("Connected to Server(%S) for %S Test [MessageID: %d,Req.Cap: 0x%x,Present.Cap: 0x%x]"),&SR_ServerName,&TestStyle,SR_MESSAGE_ID,iStepCap,TSecurityInfo(RProcess()));
       
   281 		}
       
   282 		else if(SR_MESSAGE_ID < 0)
       
   283 		{
       
   284 			//DEF INFO_PRINTF5(_L("Testing Connection capabilities[%S Test] for Server(%S)  [Req.Cap: 0x%x,Present.Cap: 0x%x]"),&TestStyle,
       
   285 			//&SR_ServerName,TSecurityInfo(RProcess()));
       
   286 		}
       
   287 		else if(!iSessionCreated)// NO Connection
       
   288 			{
       
   289 			INFO_PRINTF4(_L("Couldnt connect to the Server(%S) ErrorCode - ServerRet: %d C32ret: %d"),&SR_ServerName,iResult_Server,iResult_C32);
       
   290 			//INFO_PRINTF3(_L("Child Thread: ExitCategory : %S ExitReason : %d"),&tExitCategory,tExitReason);
       
   291   			return EFail;
       
   292  			}
       
   293 
       
   294 
       
   295 
       
   296 		switch(tChildThread.ExitType())
       
   297 			{
       
   298 			case EExitPanic:
       
   299 				//1.A Panic from the connected Server
       
   300 				//2.A CServer Panic normally for capability rejection
       
   301 				//3.A kernel Panic (consider yourself doomed!)
       
   302 				if((tExitReason == KErrPermissionDenied) ||
       
   303 					//DEF ? 	it's old version (tExitReason == CServer::EClientDoesntHaveRequiredCaps))//Rejected for Insufficient Cap.
       
   304 					// is it correct ?
       
   305 					(tExitReason == CServer2::EClientDoesntHaveRequiredCaps))//Rejected for Insufficient Cap.
       
   306 					{
       
   307 					INFO_PRINTF2(_L("Rejected for insufficient capabilities [Return Value : %d] "),tExitReason);
       
   308 					return(GetVerdict(API_RetValue_PermissionDenied));
       
   309 					}
       
   310 				else if(tExitCategory == iServer_Panic) //Panic from Server
       
   311 					{
       
   312 					INFO_PRINTF2(_L("Server(%S) Panic to child thread"),&tExitCategory);
       
   313 					INFO_PRINTF3(_L("Child Thread: ExitCategory : %S ExitReason : %d"),&tExitCategory,tExitReason);
       
   314 					return(GetVerdict(API_RetValue_ServerPanic));
       
   315 					}
       
   316 				else//A kernel Panic possibly
       
   317 					{
       
   318 					INFO_PRINTF3(_L("Child Thread: Panic from unexpected source (ExitCategory: %S ExitReason : %d)!"),&tExitCategory,tExitReason);
       
   319 					return EInconclusive;
       
   320 					}
       
   321 
       
   322 					break;
       
   323 			case EExitKill:
       
   324 				if(iResult_SR != KErrPermissionDenied)
       
   325 					{
       
   326 					INFO_PRINTF2(_L("A Successfull call (Return Value : %d)"),((SR_MESSAGE_ID >=0)?iResult_SR:iResult_Server));
       
   327 					return(GetVerdict(API_RetValue_NoCapError));
       
   328 					}
       
   329 				else
       
   330 					{
       
   331 					INFO_PRINTF2(_L("Rejected for insufficient capabilities [Return Value : %d] "),((SR_MESSAGE_ID >=0)?iResult_SR:iResult_Server));
       
   332 					return(GetVerdict(API_RetValue_PermissionDenied));
       
   333 					}
       
   334 
       
   335 					break;
       
   336 			default:
       
   337 					break;
       
   338 			}
       
   339 		}
       
   340 	else //Our thread couldnt start	:o(
       
   341 		{
       
   342 		INFO_PRINTF2(_L("ERROR: Failed to create Child thread,  ErrorCode:(%d)"),nRes_Thread);
       
   343 		return EFail;
       
   344 		}
       
   345 
       
   346 	return EInconclusive;
       
   347 	}
       
   348 
       
   349 /*
       
   350 TInt CCapabilityTestStep::StartServer()
       
   351 {
       
   352 	TInt err =  KErrNone ;
       
   353 	// EKA2 is simple No path required
       
   354 	TBuf<32> serverFile;
       
   355 	serverFile.Copy(_L("PARAM_SVR_SUITENAME"));
       
   356 	_LIT(KExe,".exe");
       
   357 	serverFile.Append(KExe);
       
   358 	RProcess server;
       
   359 	err = server.Create(serverFile,_L(""));
       
   360 	if(err != KErrNone)
       
   361 		return err;
       
   362 	// Synchronise with the server
       
   363 	TRequestStatus reqStatus;
       
   364 	server.Rendezvous(reqStatus);
       
   365 	server.Resume();
       
   366 	 //Server will call the reciprocal static synchronise call
       
   367 	User::WaitForRequest(reqStatus);
       
   368 	//server.Close();
       
   369 	if(reqStatus.Int() != KErrNone)
       
   370 		return reqStatus.Int();
       
   371 	return err;
       
   372 }
       
   373 */
       
   374 
       
   375 TInt CCapabilityTestStep::StartServer ()
       
   376 	{
       
   377     TFindServer findServer (KSipProfileServerName);
       
   378     TFullName name;
       
   379 	TInt err = findServer.Next (name);
       
   380 	if (err == KErrNone) return KErrNone; // Server already running
       
   381 
       
   382 	RSemaphore semaphore;
       
   383 
       
   384 	err = semaphore.CreateGlobal (KSipProfileServerSemaphoreName, 0);
       
   385     if (err != KErrNone) return err;
       
   386 
       
   387     err = CreateServerProcess ();
       
   388     if (err != KErrNone) return err;
       
   389 
       
   390 	semaphore.Wait ();
       
   391 	semaphore.Close ();
       
   392 
       
   393     return  KErrNone;
       
   394 	}
       
   395 
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // TSIPProfileServerStarter::CreateServerProcess
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 TInt CCapabilityTestStep::CreateServerProcess ()
       
   402 	{
       
   403     TInt err;
       
   404 	const TUidType serverUid (KNullUid, KNullUid, KServerUid3);
       
   405 
       
   406 #if ((defined (__WINS__) || defined(__WINSCW__)) && !defined(EKA2))
       
   407 
       
   408 	RLibrary lib;
       
   409     err = lib.Load (KSipProfileServerFilename, serverUid);
       
   410     if (err != KErrNone) return err;
       
   411 
       
   412     //  Get the WinsMain function
       
   413 	TLibraryFunction functionWinsMain = lib.Lookup (1);
       
   414 
       
   415     //  Call it and cast the result to a thread function
       
   416 	TThreadFunction serverThreadFunction =
       
   417         reinterpret_cast<TThreadFunction> (functionWinsMain());
       
   418 
       
   419 	TName threadName (KSipProfileServerName);
       
   420 
       
   421 	// Append a random number to make it unique
       
   422 	threadName.AppendNum (Math::Random(), EHex);
       
   423 
       
   424 	RThread server;
       
   425 
       
   426 	err = server.Create (threadName,   // create new server thread
       
   427 						 serverThreadFunction, // thread's main function
       
   428 						 KDefaultStackSize,
       
   429 						 NULL,
       
   430 						 &lib,
       
   431 						 NULL,
       
   432 						 KServerMinHeapSize,
       
   433 						 KServerMaxHeapSize,
       
   434 						 EOwnerProcess);
       
   435 
       
   436 	lib.Close ();	// if successful, server thread has handle to library now
       
   437     if (err != KErrNone) return err;
       
   438 
       
   439 	server.SetPriority (EPriorityMore);
       
   440 
       
   441 #else
       
   442 
       
   443 	RProcess server;
       
   444 	err = server.Create (KSipProfileServerName, _L(""), serverUid);
       
   445     if (err != KErrNone) return err;
       
   446 
       
   447 #endif
       
   448 
       
   449 	server.Resume ();
       
   450 	server.Close ();
       
   451 
       
   452     return  KErrNone;
       
   453 	}
       
   454 
       
   455  	TInt CCapabilityTestStep::TestDebugHeap(TInt* iDbgIPCNo)
       
   456  	{
       
   457 
       
   458  	//TDbgFns  {MarkHeapStart, MarkHeapEnd, CheckHeap, FailNext, ResetFailNext};
       
   459  	TInt aFnToTest= iDbgIPCNo[5];
       
   460 
       
   461 
       
   462  	TInt iResult_SR [6] ={0};
       
   463  	TInt i = 1;
       
   464  	TInt testedFn = 0;
       
   465 
       
   466  	TInt dbgTestSequence[5][6]	=	{	{MarkHeapStart	,2,0,1,-1,-1},
       
   467  										{MarkHeapEnd	,2,0,1,-1,-1},
       
   468  										{CheckHeap		,3,0,2, 1,-1},
       
   469  										{FailNext		,4,0,3, 4, 1},
       
   470  										{ResetFailNext	,4,0,3, 4, 1}
       
   471 
       
   472  									};
       
   473 
       
   474 
       
   475  	TInt aCount = dbgTestSequence[aFnToTest][i];
       
   476 
       
   477  	while(aCount--  )
       
   478  		{
       
   479  		testedFn =  dbgTestSequence[aFnToTest][(++i)];
       
   480 
       
   481 			iResult_SR[testedFn ]= SendReceive( iDbgIPCNo[testedFn],TIpcArgs(((iDbgIPCNo[testedFn]==3 )?4:0),0,0,0));
       
   482 
       
   483 
       
   484  		if( ((testedFn !=aFnToTest)?iResult_SR[testedFn]:KErrNone) == KErrPermissionDenied)
       
   485 
       
   486 			User::Panic(_L("Failed at Initialization"),iResult_SR[testedFn]);
       
   487 
       
   488 		}
       
   489 
       
   490 	return iResult_SR[aFnToTest];
       
   491 	}
       
   492