datacommsserver/esockserver/test/TE_SocketServer/TE_TestStepSocketServer.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 //
       
    15 
       
    16 #include<c32root.h>
       
    17 #include <e32base.h>
       
    18 #include <es_sock.h>
       
    19 #include <ss_std.h>
       
    20 #include <e32std.h>
       
    21 #include <test/es_dummy.h>
       
    22 
       
    23 #include "TE_TestStepSocketServer.h"
       
    24 #include "ES_DUMMY.H"
       
    25 
       
    26 //_LIT(KProtocolName, "Dummy Protocol 1");          // Name of test protocol to use in these tests
       
    27 //_LIT8(KSendStringTest, "Bad descriptor"); // Sample testdata to send in these tests
       
    28 _LIT(KOptimalDealerName, "!SocketServer-1*"); //The name of the optimal dealer
       
    29 
       
    30 //A class needed to get to the protected method CreateSession;
       
    31 class RIllegalConnect : public RSessionBase
       
    32 	{
       
    33 	public:
       
    34 		TInt Do(const RSocketServ& aSS); //Does the illegal Connect
       
    35 	};
       
    36 
       
    37 TInt RIllegalConnect::Do(const RSocketServ& aSS)
       
    38 	{
       
    39 	TFindServer fs(KOptimalDealerName);
       
    40 	TFullName fn;
       
    41 	TFullName newName;
       
    42 	
       
    43 	while (fs.Next(newName) == KErrNone)
       
    44 		{
       
    45 		fn = newName;
       
    46 		}
       
    47 	
       
    48 	return CreateSession(fn, aSS.Version(), KESockDefaultMessageSlots);
       
    49 	}
       
    50 
       
    51 // constructor
       
    52 CTestStepSocketServer::CTestStepSocketServer() : iErrText()
       
    53 	{
       
    54 	}
       
    55 
       
    56 // destructor
       
    57 CTestStepSocketServer::~CTestStepSocketServer()
       
    58 	{
       
    59 	}
       
    60 
       
    61 enum TVerdict CTestStepSocketServer::doTestStepPreambleL( void )
       
    62 	{
       
    63 /*
       
    64 	TInt ret;
       
    65 	ret = RProperty::Define(KDummyUid, KDummyTerminationProperty,0);
       
    66 	if (ret != KErrNone)
       
    67 		{
       
    68 		Logger().WriteFormat(_L("Error creating property"));
       
    69 		return EFail;
       
    70 		}
       
    71 	
       
    72 	ret = iHangingModuleFlag.Attach(KDummyUid, KDummyTerminationProperty);
       
    73 	if (ret != KErrNone)
       
    74 		{
       
    75 		Logger().WriteFormat(_L("Error attaching to property"));
       
    76 		return EFail;
       
    77 		}
       
    78 	ret = iHangingModuleFlag.Set(0);
       
    79 	if (ret != KErrNone)
       
    80 		{
       
    81 		Logger().WriteFormat(_L("Error setting property to 0"));
       
    82 		return EFail;
       
    83 		}
       
    84 */
       
    85 	return EPass;
       
    86 	}
       
    87 
       
    88 enum TVerdict CTestStepSocketServer::doTestStepL()
       
    89 	{
       
    90 	
       
    91 	TRAPD(trapRet,enum TVerdict stepRet = InternalDoTestStepL();\
       
    92 		if (stepRet !=EPass)\
       
    93 			{\
       
    94 			SetTestStepResult(stepRet);\
       
    95 			}\
       
    96 			)
       
    97 			
       
    98 			if (trapRet != KErrNone)
       
    99 				{
       
   100 				SetTestStepResult(EFail);
       
   101 				return EFail;
       
   102 				}
       
   103 			return EPass;
       
   104 	};
       
   105 
       
   106 //This method ensures that any
       
   107 enum TVerdict CTestStepSocketServer::doTestStepPostambleL( void )
       
   108 	{
       
   109 /*
       
   110 	TInt ret;
       
   111 	TInt hangingModules;
       
   112 	iHangingModuleFlag.Get(hangingModules);
       
   113 	if (hangingModules > 0)
       
   114 		{
       
   115 		//The code here is specifically designed to work on the function "void CDummyProvd::HangModule(void)"
       
   116 		//It wakes up any modules that have been hung during the course of testing and works in the following
       
   117 		//fashion:
       
   118 		//Each module when hung gets the value of the hanging modules property 
       
   119 		//The module increments the property by 1
       
   120 		//The module waits until the module retunrs to the previous value
       
   121 		//The module then decrements the decrements the hanging module count
       
   122 		//CDummyProvd::HangModule(void) returns
       
   123 		//So to start the shutdown process, decrement the hanging modules property by one and 
       
   124 		//wait for the property to reach -1
       
   125 		RProperty deathCountProp;
       
   126 		TInt requiredDeathCount;
       
   127 		TInt deathCount = 0;
       
   128 		TRequestStatus deathCountStat;
       
   129 		TRequestStatus hangingModulesStat;
       
   130 #ifdef SYMBIAN_NETWORKING_PLATSEC
       
   131 		ret = deathCountProp.Attach(KUidSystemCategory, KUidC32RootModuleDeathKey.iUid);
       
   132 #else
       
   133 		ret = deathCountProp.Attach(KUidCommsProcess, KC32RootModuleDeathKey);
       
   134 #endif
       
   135 		if (ret != KErrNone)
       
   136 			{
       
   137 			Logger().WriteFormat(_L("Attach returned with %d"),ret);
       
   138 			}
       
   139 		ret =deathCountProp.Get(requiredDeathCount);
       
   140 		if (ret != KErrNone)
       
   141 			{
       
   142 			Logger().WriteFormat(_L("Get returned with %d"),ret);
       
   143 			}
       
   144 		requiredDeathCount += hangingModules;
       
   145 		
       
   146 		iHangingModuleFlag.Subscribe(hangingModulesStat);
       
   147 		
       
   148 		--hangingModules;
       
   149 		Logger().WriteFormat(_L("Starting to unwind the hung modules"));
       
   150 		iHangingModuleFlag.Set(hangingModules); //Do decrement to begin process
       
   151 		iHangingModuleFlag.Get(hangingModules);
       
   152 		
       
   153 		Logger().WriteFormat(_L("Waiting for all the modules to be unwound"));
       
   154 		while(hangingModules > -1)
       
   155 			{
       
   156 			User::WaitForRequest(hangingModulesStat);
       
   157 			iHangingModuleFlag.Subscribe(hangingModulesStat);
       
   158 			iHangingModuleFlag.Get(hangingModules);
       
   159 			}
       
   160 		iHangingModuleFlag.Cancel();
       
   161 		Logger().WriteFormat(_L("All modulles now unwound - Waiting for thread to terminate"));
       
   162 		
       
   163 		//Now just make sure all the "stuck" prts have exited
       
   164 		
       
   165 		deathCountProp.Subscribe(deathCountStat);	
       
   166 		deathCountProp.Get(deathCount);
       
   167 		while (deathCount < requiredDeathCount)
       
   168 			{
       
   169 			User::WaitForRequest(deathCountStat);
       
   170 			deathCountProp.Subscribe(deathCountStat);
       
   171 			deathCountProp.Get(deathCount);
       
   172 			}
       
   173 		
       
   174 		deathCountProp.Cancel();
       
   175 		deathCountProp.Close();
       
   176 		Logger().WriteFormat(_L("Thread termination completed"));
       
   177 		}
       
   178 	iHangingModuleFlag.Close();	
       
   179 */
       
   180 	return EPass;
       
   181 
       
   182 	}
       
   183 
       
   184 
       
   185 // extra logging methods
       
   186 const TPtrC& CTestStepSocketServer::EpocErrorToText(const TInt aErrCode)
       
   187 /**
       
   188 Convert a Error code to text.
       
   189 
       
   190   @param aError error code to display as text.
       
   191   @return Text describing the error.
       
   192   */
       
   193 	{
       
   194 	//	TPtr errText;
       
   195 	switch (aErrCode)
       
   196 		{
       
   197 		case KErrNone:
       
   198 			iErrText.Set(_L("KErrNone"));
       
   199 			break;
       
   200 		case KErrNotFound:
       
   201 			iErrText.Set(_L("KErrNotFound"));
       
   202 			break;
       
   203 		case KErrGeneral:
       
   204 			iErrText.Set(_L("KErrGeneral"));
       
   205 			break;
       
   206 		case KErrCancel:
       
   207 			iErrText.Set(_L("KErrCancel"));
       
   208 			break;
       
   209 		case KErrNoMemory:
       
   210 			iErrText.Set(_L("KErrNoMemory"));
       
   211 			break;
       
   212 		case KErrNotSupported:
       
   213 			iErrText.Set(_L("KErrNotSupported"));
       
   214 			break;
       
   215 		case KErrArgument:
       
   216 			iErrText.Set(_L("KErrArgument"));
       
   217 			break;
       
   218 		case KErrTotalLossOfPrecision:
       
   219 			iErrText.Set(_L("KErrTotalLossOfPrecision"));
       
   220 			break;
       
   221 		case KErrBadHandle:
       
   222 			iErrText.Set(_L("KErrBadHandle"));
       
   223 			break;
       
   224 		case KErrOverflow:
       
   225 			iErrText.Set(_L("KErrOverflow"));
       
   226 			break;
       
   227 		case KErrUnderflow:
       
   228 			iErrText.Set(_L("KErrUnderflow"));
       
   229 			break;
       
   230 		case KErrAlreadyExists:
       
   231 			iErrText.Set(_L("KErrAlreadyExists"));
       
   232 			break;
       
   233 		case KErrPathNotFound:
       
   234 			iErrText.Set(_L("KErrPathNotFound"));
       
   235 			break;
       
   236 		case KErrDied:
       
   237 			iErrText.Set(_L("KErrDied"));
       
   238 			break;
       
   239 		case KErrInUse:
       
   240 			iErrText.Set(_L("KErrInUse"));
       
   241 			break;
       
   242 		case KErrServerTerminated:
       
   243 			iErrText.Set(_L("KErrServerTerminated"));
       
   244 			break;
       
   245 		case KErrServerBusy:
       
   246 			iErrText.Set(_L("KErrServerBusy"));
       
   247 			break;
       
   248 		case KErrCompletion:
       
   249 			iErrText.Set(_L("KErrCompletion"));
       
   250 			break;
       
   251 		case KErrNotReady:
       
   252 			iErrText.Set(_L("KErrNotReady"));
       
   253 			break;
       
   254 		case KErrUnknown:
       
   255 			iErrText.Set(_L("KErrUnknown"));
       
   256 			break;
       
   257 		case KErrCorrupt:
       
   258 			iErrText.Set(_L("KErrCorrupt"));
       
   259 			break;
       
   260 		case KErrAccessDenied:
       
   261 			iErrText.Set(_L("KErrAccessDenied"));
       
   262 			break;
       
   263 		case KErrLocked:
       
   264 			iErrText.Set(_L("KErrLocked"));
       
   265 			break;
       
   266 		case KErrWrite:
       
   267 			iErrText.Set(_L("KErrWrite"));
       
   268 			break;
       
   269 		case KErrDisMounted:
       
   270 			iErrText.Set(_L("KErrDisMounted"));
       
   271 			break;
       
   272 		case KErrEof:
       
   273 			iErrText.Set(_L("KErrEof"));
       
   274 			break;
       
   275 		case KErrDiskFull:
       
   276 			iErrText.Set(_L("KErrDiskFull"));
       
   277 			break;
       
   278 		case KErrBadDriver:
       
   279 			iErrText.Set(_L("KErrBadDriver"));
       
   280 			break;
       
   281 		case KErrBadName:
       
   282 			iErrText.Set(_L("KErrBadName"));
       
   283 			break;
       
   284 		case KErrCommsLineFail:
       
   285 			iErrText.Set(_L("KErrCommsLineFail"));
       
   286 			break;
       
   287 		case KErrCommsFrame:
       
   288 			iErrText.Set(_L("KErrCommsFrame"));
       
   289 			break;
       
   290 		case KErrCommsOverrun:
       
   291 			iErrText.Set(_L("KErrCommsOverrun"));
       
   292 			break;
       
   293 		case KErrCommsParity:
       
   294 			iErrText.Set(_L("KErrCommsParity"));
       
   295 			break;
       
   296 		case KErrTimedOut:
       
   297 			iErrText.Set(_L("KErrTimedOut"));
       
   298 			break;
       
   299 		case KErrCouldNotConnect:
       
   300 			iErrText.Set(_L("KErrCouldNotConnect"));
       
   301 			break;
       
   302 		case KErrCouldNotDisconnect:
       
   303 			iErrText.Set(_L("KErrCouldNotDisconnect"));
       
   304 			break;
       
   305 		case KErrDisconnected:
       
   306 			iErrText.Set(_L("KErrDisconnected"));
       
   307 			break;
       
   308 		case KErrBadLibraryEntryPoint:
       
   309 			iErrText.Set(_L("KErrBadLibraryEntryPoint"));
       
   310 			break;
       
   311 		case KErrBadDescriptor:
       
   312 			iErrText.Set(_L("KErrBadDescriptor"));
       
   313 			break;
       
   314 		case KErrAbort:
       
   315 			iErrText.Set(_L("KErrAbort"));
       
   316 			break;
       
   317 		case KErrTooBig:
       
   318 			iErrText.Set(_L("KErrTooBig"));
       
   319 			break;
       
   320 		case KRequestPending:
       
   321 			iErrText.Set(_L("KRequestPending"));
       
   322 			break;
       
   323 		default:
       
   324 			iErrBuff.Format(_L(" %d"),aErrCode);
       
   325 			iErrText.Set(iErrBuff.Ptr());
       
   326 			break;
       
   327 		}
       
   328 		return iErrText;
       
   329 	}	
       
   330 
       
   331 TInt CTestStepSocketServer::OptimalConnect(RSocketServ &aSrv)
       
   332 	{
       
   333     TSessionPref pref;
       
   334     pref.iAddrFamily = KDummyAddrFamily;
       
   335     pref.iProtocol = KDummyThree;
       
   336 	return aSrv.Connect(pref);
       
   337 	}
       
   338 
       
   339 TInt CTestStepSocketServer::IllegalOptimalConnect(RSocketServ &aSrv)
       
   340 	{
       
   341 	RIllegalConnect ill;
       
   342 	return ill.Do(aSrv); 
       
   343 	}
       
   344 
       
   345 
       
   346 TInt CTestStepSocketServer::PanicProtocolModule(RSocket &aSock)
       
   347 	{
       
   348 	TRequestStatus st;
       
   349 	TBool jit = User::JustInTime();
       
   350 	User::SetJustInTime(EFalse); //Don't stop for panic
       
   351 	aSock.Ioctl(KDummyIoctlPanicProtocolModule,st);
       
   352 	User::WaitForRequest(st);
       
   353 	User::SetJustInTime(jit);
       
   354 	return st.Int();
       
   355 	}
       
   356 
       
   357 
       
   358 void CTestStepSocketServer::NonCompletingIoclt(RSocket &aSock,TRequestStatus &st)
       
   359 	{
       
   360 	aSock.Ioctl(KDummyIocltNonCompleting,st);
       
   361 	}
       
   362 
       
   363 
       
   364 TInt CTestStepSocketServer::PanicDealer(RSocketServ &aSockServ)
       
   365 	{
       
   366 	/*This is a really crude way to cause the dealer to panic.
       
   367 	It works by marking the heap. Deliberately adding stuff to the heap
       
   368 	And then checking to see that nothing was allocated*/
       
   369 	TInt ret = aSockServ.__DbgMarkHeap();
       
   370 	if (ret != KErrNone)
       
   371 		{
       
   372 		return ret;
       
   373 		}
       
   374 	RSocket sock;
       
   375 	sock.Open(aSockServ);
       
   376 	TBool jit = User::JustInTime();
       
   377 	User::SetJustInTime(EFalse); //Don't stop for panic
       
   378 	ret = aSockServ.__DbgCheckHeap(0); //Heap should now be dead
       
   379 	User::SetJustInTime(jit);
       
   380 	return ret;
       
   381 	
       
   382 	}
       
   383 
       
   384 TInt CTestStepSocketServer::BlockHardOnClose(RSocket &aSock)
       
   385 	{
       
   386 	TRequestStatus st;
       
   387 	aSock.Ioctl(KDummyIoctBlockHardOnClose,st);
       
   388 	User::WaitForRequest(st);
       
   389 	return st.Int();
       
   390 	}
       
   391 
       
   392 void CTestStepSocketServer::SlowIoctlReturn(RSocket &aSock, TRequestStatus &aStat,
       
   393 											TBool aBlockPermanently)
       
   394 	{
       
   395 	aBlockPermanently ? aSock.Ioctl(KDummyIoctlHangIoctlReturn,aStat) : 
       
   396 aSock.Ioctl(KDummyIoctlSlowIoctlReturn,aStat);
       
   397 	}
       
   398 
       
   399 TInt CTestStepSocketServer::CheckForModuleL(const TDesC8 &aModuleName)
       
   400 	{
       
   401 	RRootServ rsrv;
       
   402 	TRSIter it;
       
   403 	TCFModuleName name;
       
   404 	int ret;
       
   405 	
       
   406 	ret = rsrv.Connect();
       
   407 	if (ret != KErrNone)
       
   408 		{
       
   409 		return ret;
       
   410 		}
       
   411 	
       
   412 	CleanupClosePushL(rsrv);
       
   413 	
       
   414 	TBool tst = false;
       
   415 	while (ret == KErrNone && !tst)
       
   416 		{
       
   417 		ret = rsrv.EnumerateModules(it,name);
       
   418 		if (ret != KErrNone && ret != KErrEof)
       
   419 			{
       
   420 			CleanupStack::PopAndDestroy();
       
   421 			return ret;
       
   422 			}
       
   423 		tst = (name == aModuleName);
       
   424 		}
       
   425 		
       
   426 	CleanupStack::PopAndDestroy(); //rsrv
       
   427 	
       
   428 	if (tst)
       
   429 		{
       
   430 		Logger().WriteFormat(_L8("Module was found in module list"));
       
   431 		}
       
   432 	else
       
   433 		{
       
   434 		Logger().WriteFormat(_L8("Module does not exist in module list"));
       
   435 		}	
       
   436 	TESTL(!tst);
       
   437 		
       
   438 	
       
   439 	return ret;
       
   440 	}
       
   441 
       
   442 void CTestStepSocketServer::OpenMultipleSocketsL(RSocketServ &aSS,RSocket *aSockets,TInt aNumSockets)
       
   443 	{
       
   444 	int ret;
       
   445 	int i;
       
   446 	for (i = 0; i < aNumSockets; i++)
       
   447 		{
       
   448 		Logger().WriteFormat(_L("Attempting to open socket %d"),i);
       
   449 		ret = aSockets[i].Open(aSS,_L("Dummy Protocol 1"));
       
   450 		Logger().WriteFormat(_L("Open returned %S"),&EpocErrorToText(ret));
       
   451 		TESTL(ret == KErrNone);
       
   452 		}
       
   453 	}
       
   454 
       
   455 TInt CTestStepSocketServer::OpenSocketProtocolStarted()
       
   456 	{
       
   457 	TRequestStatus protStatus;
       
   458 	TRequestStatus exclStatus;
       
   459 	TInt ret;
       
   460 	RSocketServ ss;
       
   461     
       
   462 	TInt err = ss.Connect();
       
   463 	if(err != KErrNone)
       
   464 		{
       
   465 		return err;
       
   466 		}			         
       
   467 	ss.StartProtocol( KAfInet,KSockDatagram,KProtocolInetUdp, protStatus );
       
   468 	User::WaitForRequest(protStatus);
       
   469 	if(protStatus.Int() != KErrNone)
       
   470 		{
       
   471 		return protStatus.Int();
       
   472 		}
       
   473 	ss.SetExclusiveMode(exclStatus);
       
   474 	User::WaitForRequest(exclStatus);
       
   475 	if(exclStatus.Int() != KErrNone)
       
   476 		{
       
   477 		return exclStatus.Int();
       
   478 		}
       
   479 	RSocket socketArray[2];
       
   480 	ret = socketArray[0].Open(ss);
       
   481 	if(ret != KErrNone)
       
   482 		{
       
   483 		return ret;
       
   484 		}
       
   485 	ret = socketArray[1].Open(ss);
       
   486 	if(ret != KErrNone)
       
   487 		{
       
   488 		return ret;
       
   489 		}
       
   490 	ss.ClearExclusiveMode();
       
   491 	ss.StopProtocol( KAfInet,KProtocolInetTcp, KSockStream, protStatus );
       
   492 	return ret;
       
   493 	}
       
   494