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