datacommsserver/esockserver/test/util/src/esocktestutils.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 "esocktestutils.h"
       
    17 #include <es_sock.h>
       
    18 #include <es_enum.h>
       
    19 #include <f32file.h>
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 EXPORT_C TInt ESockTestUtils::StopAllInterfaces(TDes& aErrDesc)
       
    27 /**
       
    28 Close all interfaces that are currently open by attaching to each in turn and then using the Stop() method.
       
    29 
       
    30 @param None
       
    31 */
       
    32 	{
       
    33 	// this code is based on the implementation of the same method in TS_RConnection
       
    34 	// there are no methods used here which are allowed to leave and so the cleanup stack is not used
       
    35 	TInt err = KErrNone;
       
    36 	
       
    37 	// all operations here require a socket server
       
    38 	RSocketServ ss;	
       
    39 	err = ss.Connect();
       
    40 
       
    41 	TDes16IgnoreOverflow overflow;
       
    42 	aErrDesc.Zero();
       
    43 	
       
    44 	// how many open interfaces are there?
       
    45 	RConnection enumerator;
       
    46 	if(err == KErrNone)
       
    47 		{
       
    48 		err = enumerator.Open(ss);
       
    49 		}
       
    50 	TUint noOfConnectionsBefore = 0;
       
    51 	TUint noOfConnectionsAfter = 0;	
       
    52 	if(err == KErrNone)
       
    53 		{
       
    54 		err = enumerator.EnumerateConnections(noOfConnectionsBefore);
       
    55 		}	
       
    56 	if (KErrNone != err)
       
    57 		{
       
    58 		aErrDesc.AppendFormat(_L("Up to initial EnumerateConnections failed"), &overflow);
       
    59 		return err;
       
    60 		}
       
    61 	aErrDesc.AppendFormat(_L("Started with %d connections\n"), &overflow, noOfConnectionsBefore);
       
    62 	// for each interface attach to it and then stop it.
       
    63 	TPckgBuf<TConnectionInfo> connectionInfo;
       
    64 	while (noOfConnectionsBefore && err == KErrNone)
       
    65 		{
       
    66 		err = enumerator.GetConnectionInfo(1, connectionInfo);
       
    67 		if (KErrNone != err)
       
    68 			{
       
    69 			aErrDesc.AppendFormat(_L("GetConnectionInfo(1) failed"), &overflow);
       
    70 			break;
       
    71 			}
       
    72 		aErrDesc.AppendFormat(_L("Conn#1 IAP=%d, NID=%d\n"), &overflow, connectionInfo().iIapId, connectionInfo().iNetId);
       
    73 		
       
    74 		RConnection real;
       
    75 		err = real.Open(ss);
       
    76 		if (KErrNone != err)
       
    77 			{
       
    78 			aErrDesc.AppendFormat(_L("RConnection::Open() failed"), &overflow);
       
    79 			break;
       
    80 			}
       
    81 		
       
    82 		//Attach can fail, and it may be acceptible for it to fail
       
    83 		//if the connection is no longer there.
       
    84 		err = real.Attach(connectionInfo, RConnection::EAttachTypeNormal);
       
    85 		if (KErrNone == err)
       
    86 			{
       
    87     		err = real.Stop();
       
    88 			if(KErrNone != err)
       
    89 				{
       
    90 				aErrDesc.Format(_L("RConnection::Stop failed"), &overflow);
       
    91 				}
       
    92 			}
       
    93 		aErrDesc.Format(_L("RConnection::Attach failed with %d - continuing"), &overflow, err);
       
    94   		real.Close();
       
    95   		
       
    96   		//Here we check the ultimate objective of the iteration, i.e.:
       
    97   		//at least one connection less. 
       
    98 		TInt errEnum = enumerator.EnumerateConnections(noOfConnectionsAfter);
       
    99 		if(errEnum != KErrNone)
       
   100 			{
       
   101 			aErrDesc.Format(_L("RConnection::Enumerate failed with %d - continuing"), &overflow, errEnum);
       
   102 			}
       
   103 		if (noOfConnectionsAfter < noOfConnectionsBefore)
       
   104     		{
       
   105     		//Iteration successful. If error attaching, clear it.
       
   106     		err = KErrNone;
       
   107     		noOfConnectionsBefore = noOfConnectionsAfter;
       
   108     		}
       
   109     	else if (err == KErrNone)
       
   110         	{
       
   111             //very strange - no error recorded, though no connection stopped.
       
   112             err = KErrGeneral;
       
   113         	}
       
   114 		}
       
   115 	
       
   116 	// clean up
       
   117 	enumerator.Close();
       
   118 	ss.Close();
       
   119 	return err;
       
   120 	}
       
   121 
       
   122 
       
   123 EXPORT_C void ESockTestUtils::DefaultToConfig1L()
       
   124 	{
       
   125 	// If no PDUMMY1 or PDUMMY2 config files are present then run "te_esock_test_copy_config_1.script". This allows elementary tests such as TE_SOCKET
       
   126 	// to "work out of the box" on a freshly booted ROM, so reducing support costs.
       
   127 	// This trivial thing is only implemented as a test step because our test scripting language has no conditional support.
       
   128 	_LIT(KConfiguratorPrivateDir, "\\private\\101F7988\\");
       
   129 	_LIT(KPDummyCmiMask, "pdummy*.cmi");
       
   130 	_LIT(KCodealerCmiMask, "esock_codealer*.cmi");
       
   131 
       
   132 	RFs fs;
       
   133 	User::LeaveIfError(fs.Connect());
       
   134 
       
   135 	TFindFile ff(fs);
       
   136 	CDir* dummyList = NULL;
       
   137 	TInt ret = ff.FindWildByDir(KPDummyCmiMask, KConfiguratorPrivateDir, dummyList);
       
   138 	if(ret != KErrNone)
       
   139 		{
       
   140 		delete dummyList;
       
   141 		ret = ff.FindWildByDir(KCodealerCmiMask, KConfiguratorPrivateDir, dummyList);
       
   142 		if(ret != KErrNone)
       
   143 			{
       
   144 			// Run the config script. Very horribly it seems to have to be like this with ScheduleTest, as running
       
   145 			// TestExecute from a TestExecute test step produces KErrAlreadyExists followed by a garbled log file for the
       
   146 			// parent. 
       
   147 			_LIT(KScheduleTestExe, "ScheduleTest");
       
   148 			_LIT(KCopyConfig1Script, "z:\\testdata\\scripts\\te_esock_test_copy_config_1.script");
       
   149 			RProcess testExec;
       
   150 			ret = testExec.Create(KScheduleTestExe, KCopyConfig1Script);
       
   151 			if(ret == KErrNone)
       
   152 				{
       
   153 				TRequestStatus completedStatus;
       
   154 				testExec.Logon(completedStatus);
       
   155 				testExec.Resume();
       
   156 				User::WaitForRequest(completedStatus);
       
   157 				}
       
   158 			testExec.Close();
       
   159 			}
       
   160 		}
       
   161 	delete dummyList;
       
   162 	}
       
   163 
       
   164 
       
   165 
       
   166 
       
   167 
       
   168 //from deprecated genconn library
       
   169 //modified to fit in with TE_ methodology
       
   170 EXPORT_C TBool ESockTestUtils::IsNetworkActiveL()
       
   171 {
       
   172 
       
   173 	TBool result = true;
       
   174 	TInt err = KErrNone;
       
   175 
       
   176 	RSocketServ ss;
       
   177 	err = ss.Connect();
       
   178 	User::LeaveIfError(err);
       
   179 
       
   180 	RConnection conn;
       
   181 	err = conn.Open(ss);
       
   182 	User::LeaveIfError(err);
       
   183 
       
   184 	TUint count(0);
       
   185 	err = conn.EnumerateConnections(count);
       
   186 	User::LeaveIfError(err);
       
   187 
       
   188 
       
   189 	conn.Close();
       
   190 	ss.Close();
       
   191 
       
   192 	//KErrNone - no connections, need to think about return status if there
       
   193 	//are connections taking place
       
   194 	if (count==0)
       
   195 		result= false;
       
   196 	else
       
   197 		result= true;
       
   198 
       
   199 	return result;
       
   200 }
       
   201 
       
   202 
       
   203 //from deprecated genconn library
       
   204 //modified to fit in with TE_ methodology as per esocktestsection9.cp-waitfor
       
   205 EXPORT_C TInt ESockTestUtils::Delay(TInt aGuardTimerValue)
       
   206 {
       
   207 	//Note this function step would have been called from the iTest scripts
       
   208 	//to delay for a certain amount of milliseconds before checking to see
       
   209 	//if the socket/network/connection has changed to the desired state.
       
   210 	//The delay tries to ensure the socket/network/connection
       
   211 	//status has change before we go to the next step where we would check 
       
   212 	//this status
       
   213 
       
   214 	// wait for either test thread or timer to end
       
   215 	TInt err = KErrNone;
       
   216 	RTimer GuardTimer;
       
   217 	GuardTimer.CreateLocal();			// create for this thread
       
   218 	TRequestStatus TimerStatus;
       
   219 
       
   220 	//aGuardTimerValue is in milliseconds
       
   221 	GuardTimer.After(TimerStatus,aGuardTimerValue * 1000); 
       
   222 
       
   223 
       
   224 	User::WaitForRequest(TimerStatus);
       
   225 	if( TimerStatus == KRequestPending )
       
   226 		{
       
   227 		// This is lifted from SockBench to solve some apparently
       
   228 		// strange behaviour with the timer.
       
   229 		
       
   230 		// Since we are still waiting for this timer, cancel it and
       
   231 		// wait for the request to be flagged.  I believe this is
       
   232 		// needed since the kernel may have setup a semaphore on the
       
   233 		// 'TimerStatus' Request Status.  The Cancel() will flag the
       
   234 		// status change (or maybe the timer will expire anyway) and
       
   235 		// if we don't clear it, we may crash later when we return
       
   236 		// to the Active Scheduler (since the TRequestStatus and
       
   237 		// RTimer won't exist).
       
   238 		//
       
   239 		GuardTimer.Cancel();
       
   240 		User::WaitForRequest( TimerStatus );
       
   241 		}
       
   242 	GuardTimer.Cancel();
       
   243 	if (TimerStatus == KErrNone)
       
   244 		err= KErrNone;
       
   245 	else
       
   246 		err= KErrGeneral;
       
   247 
       
   248 	return err;
       
   249 }
       
   250