applayerprotocols/httptransportfw/Test/Acceptance/Iter2/WspTestTransactions.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     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 // System includes
       
    17 #include <f32file.h>
       
    18 
       
    19 // User includes
       
    20 #include "WspTestTransactions.h"
       
    21 #include "csrvaddrval.h"
       
    22 
       
    23 // Proxy information properties
       
    24 _LIT(KItemWspProxyAddress,			"proxy_address");
       
    25 _LIT(KItemWspBearer,				"wsp_bearer");
       
    26 _LIT(KItemWspLocalPort,				"local_port");
       
    27 _LIT(KItemWspRemotePort,			"remote_port");
       
    28 _LIT(KItemWspServiceNumber,			"service_number");
       
    29 _LIT(KItemWspSessionType,			"session_type");
       
    30 _LIT(KItemWspSecurity,				"security");
       
    31 
       
    32 // Capability properties
       
    33 _LIT(KItemWspClientMsgSize,			"client_message_size");
       
    34 _LIT(KItemWspServerMsgSize,			"server_message_size");
       
    35 _LIT(KItemWspUseAcknowledgements,	"use_acknowledgements");
       
    36 _LIT(KItemWspSupportSuspendResume,	"support_suspend_resume");
       
    37 _LIT(KItemWspMaxOutstandingRequests,"max_outstanding_requests");
       
    38 _LIT(KItemExpectedError,			"test_validation_param_expectederror");
       
    39 
       
    40 // Security policy properties
       
    41 _LIT(KSecurityPolicy,				"securitypolicy");
       
    42 _LIT(KSecurityPolicyFileName,		"securitypolicy.ini");
       
    43 
       
    44 _LIT(KValidateUntrustedServerCert,	"ValidateUntrustedServerCert");
       
    45 _LIT(KWTLSCipherSuites,				"WTLSCipherSuites");
       
    46 _LIT(KWTLSKeyExchangeSuites,		"WTLSKeyExchangeSuites");
       
    47 _LIT(KTLSCipherSuites,				"TLSCipherSuites");
       
    48 _LIT(KPolicy,						"Policy");
       
    49 
       
    50 
       
    51 CWspTestTransactions* CWspTestTransactions::NewL(CScriptFile& aIniFile, CScriptFile* aIniSettingsFile, const TInt aSectionName)
       
    52 	{
       
    53 	CWspTestTransactions* self = new(ELeave) CWspTestTransactions(aIniSettingsFile);
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL(aIniFile, aIniSettingsFile, aSectionName);
       
    56 	CleanupStack::Pop(self);
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 
       
    61 CWspTestTransactions::CWspTestTransactions(CScriptFile* aIniSettingsFile) :
       
    62 	iIniSettingsFile(aIniSettingsFile)
       
    63 	{
       
    64 	}
       
    65 
       
    66 
       
    67 void CWspTestTransactions::ConstructL(CScriptFile& aIniFile, CScriptFile* aIniSettingsFile, const TInt aSectionName)
       
    68 	{
       
    69 	CHttpTestTransactions::ConstructL(aIniFile, aIniSettingsFile, aSectionName);
       
    70 	}
       
    71 
       
    72 
       
    73 CWspTestTransactions::~CWspTestTransactions()
       
    74 	{
       
    75 	}
       
    76 
       
    77 void CWspTestTransactions::DoRunL()
       
    78 	{
       
    79 	OpenTestSessionL(); // set up session and transaction here
       
    80 	if ((iSessionEvent.iStatus) == THTTPSessionEvent::EDisconnected || (iSessionEvent.iStatus) == THTTPSessionEvent::EConnectionTimedOut)	
       
    81 		{
       
    82 		DoCloseTestSession(); // close any strings, transactiona and session
       
    83 
       
    84 		// Check if the expected status is returned
       
    85 		if( (iSessionEvent.iStatus == THTTPSessionEvent::EConnectionTimedOut) && (iExpectedStatusCode == 504) )
       
    86 			{
       
    87 			// No fail...
       
    88 			iTestFail = 0;
       
    89 			}
       
    90 		else if( (iSessionEvent.iStatus == THTTPSessionEvent::EDisconnected) && (iExpectedError == THTTPSessionEvent::EAuthenticationFailure) && iAuthenticationFailure )
       
    91 			{
       
    92 			// No fail...
       
    93 			iTestFail = 0;
       
    94 			}
       
    95 		else
       
    96 			{
       
    97 			// Fail!
       
    98 			iTestFail = 1;
       
    99 			}
       
   100 
       
   101 		iEngine->Utils().LogIt(_L("Warning: Connection to proxy closed\n"));
       
   102 		}
       
   103 	else
       
   104 		{
       
   105 		CActiveScheduler::Start();
       
   106 		CloseTestSession(); // disconnect from gateway and then close any 
       
   107 							// strings, transactions and sessions
       
   108 		}
       
   109 	if (iTestFail==1)
       
   110 		{
       
   111 		User::Leave(-1);
       
   112 		}
       
   113 	}
       
   114 
       
   115 
       
   116 void CWspTestTransactions::OpenTestSessionL()
       
   117 	{
       
   118 	// Iterate through the TestCases.ini file, beginning at the first element 
       
   119 	// i.e. index 0, and write the security policy properties to the 
       
   120 	// securitypolicy.ini file.
       
   121 	SetWspSecurityPolicyPropertiesL(0);
       
   122 
       
   123 	iSession.OpenL(_L8("WSP/WSP"));
       
   124 
       
   125 	iMyStrP = iSession.StringPool();
       
   126 
       
   127 	// Set the session callback
       
   128 	iSession.SetSessionEventCallback(this);
       
   129 
       
   130 	SetWspSessionPropertiesL();
       
   131 
       
   132 	ConnectToGatewayL();
       
   133 	}
       
   134 
       
   135 
       
   136 void CWspTestTransactions::CloseTestSession()
       
   137 	{
       
   138 	LogCertificate();
       
   139 	// Disconnect from the gateway first
       
   140 	TRAP_IGNORE( DisconnectFromGatewayL() );
       
   141 	DoCloseTestSession();
       
   142 	}
       
   143 
       
   144 void CWspTestTransactions::LogCertificate()
       
   145 	{
       
   146 	if(iLogCert)
       
   147 		{
       
   148 		TRAPD(error, LogCertL());
       
   149 		if(error != KErrNone)
       
   150 			iEngine->Utils().LogIt(_L("Unable to log server certificate.  Error code=%d\n"), error);
       
   151 		}
       
   152 	}
       
   153 
       
   154 void CWspTestTransactions::LogCertL()
       
   155 	{
       
   156 	TCertInfo certInfo;
       
   157 	User::LeaveIfError(iSession.ServerCert(certInfo));
       
   158 
       
   159 	LogCertificateL(certInfo);
       
   160 	}
       
   161 
       
   162 void CWspTestTransactions::DoCloseTestSession()
       
   163 	{
       
   164 	CHttpTestTransactions::CloseTestSession();
       
   165 	iTransaction.Close();
       
   166 	iEngine->Utils().LogIt(_L("Transaction closed\n"));
       
   167 	iSession.Close();
       
   168 	iEngine->Utils().LogIt(_L("Session closed"));
       
   169 	}
       
   170 
       
   171 
       
   172 void CWspTestTransactions::ConnectToGatewayL()
       
   173 	{
       
   174 	iSession.ConnectL();
       
   175 	CActiveScheduler::Start();
       
   176 	}
       
   177 
       
   178 
       
   179 void CWspTestTransactions::DisconnectFromGatewayL()
       
   180 	{
       
   181 	iSession.DisconnectL();
       
   182 	CActiveScheduler::Start();
       
   183 	}
       
   184 
       
   185 
       
   186 void CWspTestTransactions::CreateSingleTransactionL()
       
   187 	{
       
   188 	// Iterate through the TestCases.ini file, beginning at the first element
       
   189 	// i.e. index 0, and set up the transaction properties
       
   190 	iTransaction = CreateTransactionL(0);
       
   191 	iTransaction.SubmitL();
       
   192 	}
       
   193 
       
   194 
       
   195 void CWspTestTransactions::MHFSessionRunL(const THTTPSessionEvent& aEvent)
       
   196 	{
       
   197 	iSessionEvent = aEvent;
       
   198 	switch(aEvent.iStatus)
       
   199 		{
       
   200 	case THTTPSessionEvent::EConnectedOK:
       
   201 		{
       
   202 		iEngine->Utils().LogIt(_L("WSP Session connected. No reduced capabilities\n"));
       
   203 		CreateSingleTransactionL();
       
   204 		CActiveScheduler::Stop();
       
   205 		} break;
       
   206 	case THTTPSessionEvent::EConnectedWithReducedCapabilities:
       
   207 		{
       
   208 		iEngine->Utils().LogIt(_L("WSP Session connected with reduced capabilities\n"));
       
   209 		CreateSingleTransactionL();
       
   210 		CActiveScheduler::Stop();
       
   211 		} break;
       
   212 	case THTTPSessionEvent::EConnectionTimedOut:
       
   213 		{
       
   214 		// Need to create the transaction even though its timed out to read
       
   215 		// the ini file for the time out tests in servertestcases.ini
       
   216 		iTransaction = CreateTransactionL(0);
       
   217 		iEngine->Utils().LogIt(_L("Proxy connection timed out\n"));
       
   218 		CActiveScheduler::Stop();
       
   219 		} break;
       
   220 	case THTTPSessionEvent::EDisconnected:
       
   221 		{
       
   222 		iEngine->Utils().LogIt(_L("WSP Session disconnected\n"));
       
   223 		CActiveScheduler::Stop();
       
   224 		} break;
       
   225 	case THTTPSessionEvent::ERedirected:
       
   226 		{
       
   227 		iEngine->Utils().LogIt(_L("Redirected\n"));
       
   228 		} break;
       
   229 	case THTTPSessionEvent::EAuthenticatedOK:
       
   230 		{
       
   231 		iEngine->Utils().LogIt(_L("Authenticated OK\n"));
       
   232 		} break;
       
   233 	case THTTPSessionEvent::EAuthenticationFailure:
       
   234 		{
       
   235 		iEngine->Utils().LogIt(_L("Warning: Authentication Failure\n"));
       
   236 		iAuthenticationFailure = ETrue;
       
   237 		} break;
       
   238 	case THTTPSessionEvent::ENotConnected:
       
   239 		{
       
   240 		iEngine->Utils().LogIt(_L("Warning: Not Connected\n"));
       
   241 		} break;
       
   242 	case THTTPSessionEvent::EExceptionInfo:
       
   243 		{
       
   244 		iEngine->Utils().LogIt(_L("Warning: Exception Info\n"));
       
   245 		} break;
       
   246 	case THTTPSessionEvent::EAlreadyConnecting:
       
   247 		{
       
   248 		iEngine->Utils().LogIt(_L("Warning: Already Connecting\n"));
       
   249 		} break;
       
   250 	case THTTPSessionEvent::EAlreadyConnected:
       
   251 		{
       
   252 		iEngine->Utils().LogIt(_L("Warning: Already Connected\n"));
       
   253 		} break;
       
   254 	case THTTPSessionEvent::EAlreadyDisconnecting:
       
   255 		{
       
   256 		iEngine->Utils().LogIt(_L("Warning: Already Disconnecting\n"));
       
   257 		} break;
       
   258 	case THTTPSessionEvent::EAlreadyDisconnected:
       
   259 		{
       
   260 		iEngine->Utils().LogIt(_L("Warning: Already Disconnected\n"));
       
   261 		} break;
       
   262 	default:
       
   263 		CActiveScheduler::Stop();
       
   264 		break;
       
   265 		}
       
   266 	}
       
   267 
       
   268 
       
   269 TInt CWspTestTransactions::MHFSessionRunError(TInt aError, const THTTPSessionEvent& /*aEvent*/)
       
   270 	{
       
   271 	return aError;
       
   272 	}
       
   273 
       
   274 
       
   275 void CWspTestTransactions::SetWspSessionPropertiesL()
       
   276 	{
       
   277 	// Iterate through the TestCases.ini file, beginning at the first element
       
   278 	// i.e. index 0, and set up the WSP proxy properties and the
       
   279 	// WSP capability properties
       
   280 	SetWspProxyPropertiesL(0);
       
   281 	SetWspCapabilityPropertiesL(0);
       
   282 	}
       
   283 
       
   284 
       
   285 void CWspTestTransactions::SetWspProxyPropertiesL(TInt aStartItem)
       
   286 	{
       
   287 	RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   288 	TInt endItem = FindEndOfTransaction(aStartItem);	
       
   289 	for (TInt ii = aStartItem; ii < endItem; ++ii)
       
   290 		{
       
   291 		TPtrC itemPtr(iIniFile->Section(iSectionNumber).Item(ii).Item());
       
   292 		HBufC8* itemBuff8 = HBufC8::NewLC(itemPtr.Length());
       
   293 		TPtr8  itemPtr8 = itemBuff8->Des();
       
   294 		itemPtr8.Copy(itemPtr);
       
   295 		RStringF item = iMyStrP.OpenFStringL(itemPtr8);
       
   296 		CleanupClosePushL(item);
       
   297 		
       
   298 
       
   299 		TPtrC valuePtr(iIniFile->Section(iSectionNumber).Item(ii).Value());
       
   300 
       
   301 		// Retrieve the actual value from the settings.ini file for
       
   302 		// KItemWspProxyAddress ("proxy_address").  Other values like
       
   303 		// uri value are retrieved in trans.cpp file.
       
   304 		
       
   305 		// Replace the host name in the URI
       
   306 		HBufC16* valueBuff16 = TSrvAddrVal::ReplaceHostNameL(valuePtr, iIniSettingsFile);
       
   307 		CleanupStack::PushL(valueBuff16);
       
   308 		TPtr16 valuePtr16 = valueBuff16->Des();
       
   309 
       
   310 		// Convert 16 bit to 8 bit.
       
   311 		HBufC8* valueBuff8 = HBufC8::NewLC(valuePtr16.Length());
       
   312 		TPtr8 valuePtr8 = valueBuff8->Des();
       
   313 		valuePtr8.Copy(valuePtr16);
       
   314 
       
   315 		RStringF value = iMyStrP.OpenFStringL(valuePtr8);
       
   316 		CleanupClosePushL(value);
       
   317 
       
   318 		if (itemPtr.CompareF(KItemWspProxyAddress)==0)
       
   319 			{
       
   320 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspProxyAddress, RHTTPSession::GetTable()), value);
       
   321 			}
       
   322 		if (itemPtr.Compare(KItemWspBearer)==0)
       
   323 			{
       
   324 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspBearer, RHTTPSession::GetTable()), value);
       
   325 			}
       
   326 		if (itemPtr.Compare(KItemWspLocalPort)==0)
       
   327 			{
       
   328 			TLex lex(valuePtr);
       
   329 			TInt localPort = 0;
       
   330 			lex.Val(localPort);
       
   331 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspLocalPort, RHTTPSession::GetTable()), localPort);
       
   332 			}
       
   333 		if (itemPtr.Compare(KItemWspRemotePort)==0)
       
   334 			{
       
   335 			TLex lex(valuePtr);
       
   336 			TInt remotePort = 0;
       
   337 			lex.Val(remotePort);
       
   338 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspRemotePort, RHTTPSession::GetTable()), remotePort);
       
   339 			}
       
   340 		if (itemPtr.Compare(KItemWspServiceNumber)==0)
       
   341 			{
       
   342 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspServiceNumber, RHTTPSession::GetTable()), value);
       
   343 			}
       
   344 		if (itemPtr.Compare(KItemWspSessionType)==0)
       
   345 			{
       
   346 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspSessionType, RHTTPSession::GetTable()), value);
       
   347 			}
       
   348 		if (itemPtr.Compare(KItemWspSecurity)==0)
       
   349 			{
       
   350 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspSecurity, RHTTPSession::GetTable()), value);
       
   351 			}
       
   352 
       
   353 		CleanupStack::PopAndDestroy(5, itemBuff8); // item, valueBuff8, value, valueBuff16
       
   354 		}
       
   355 	}
       
   356 
       
   357 void CWspTestTransactions::SetWspCapabilityPropertiesL(TInt aStartItem)
       
   358 	{
       
   359 	RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   360 	TInt endItem = FindEndOfTransaction(aStartItem);
       
   361 	for (TInt ii = aStartItem; ii < endItem; ++ii)
       
   362 		{
       
   363 		TPtrC itemPtr(iIniFile->Section(iSectionNumber).Item(ii).Item());
       
   364 		HBufC8* itemBuff8 = HBufC8::NewLC(itemPtr.Length());
       
   365 		TPtr8  itemPtr8= itemBuff8->Des();
       
   366 		itemPtr8.Copy(itemPtr);
       
   367 		RStringF item = iMyStrP.OpenFStringL(itemPtr8);
       
   368 		CleanupClosePushL(item);
       
   369 		
       
   370 
       
   371 		TPtrC valuePtr(iIniFile->Section(iSectionNumber).Item(ii).Value());
       
   372 		HBufC8* valueBuff8 = HBufC8::NewLC(valuePtr.Length());
       
   373 		TPtr8 valuePtr8= valueBuff8->Des();
       
   374 		valuePtr8.Copy(valuePtr);
       
   375 		RStringF value = iMyStrP.OpenFStringL(valuePtr8);
       
   376 		CleanupClosePushL(value);
       
   377 
       
   378 		if (itemPtr.CompareF(KItemWspClientMsgSize)==0)
       
   379 			{
       
   380 			TLex lex(valuePtr);
       
   381 			TInt cliMsgSize = 0;
       
   382 			lex.Val(cliMsgSize);
       
   383 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspCapClientMessageSize, RHTTPSession::GetTable()), cliMsgSize);
       
   384 			}
       
   385 		if (itemPtr.CompareF(KItemWspServerMsgSize)==0)
       
   386 			{
       
   387 			TLex lex(valuePtr);
       
   388 			TInt srvMsgSize = 0;
       
   389 			lex.Val(srvMsgSize);
       
   390 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspCapServerMessageSize, RHTTPSession::GetTable()), srvMsgSize);
       
   391 			}
       
   392 		if (itemPtr.CompareF(KItemWspUseAcknowledgements)==0)
       
   393 			{
       
   394 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspCapUseAcknowledgements, RHTTPSession::GetTable()), value);
       
   395 			}
       
   396 		if (itemPtr.CompareF(KItemWspSupportSuspendResume)==0)
       
   397 			{
       
   398 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspCapSupportSuspendResume, RHTTPSession::GetTable()), value);
       
   399 			}
       
   400 		if (itemPtr.CompareF(KItemWspMaxOutstandingRequests)==0)
       
   401 			{
       
   402 			TLex lex(valuePtr);
       
   403 			TInt maxOutReqs = 0;
       
   404 			lex.Val(maxOutReqs);
       
   405 			connInfo.SetPropertyL(iMyStrP.StringF(HTTP::EWspCapMaxOutstandingRequests, RHTTPSession::GetTable()), maxOutReqs);
       
   406 			}
       
   407 		if (itemPtr.CompareF(KItemExpectedError)==0)
       
   408 			{
       
   409 			//get the 'test_validation_param_expectederror' from the ini 
       
   410 			//file...
       
   411 			TPtrC expectedParamPtr = iIniFile->Section(iSectionNumber).ItemL(KItemExpectedError).Value();
       
   412 			TLex paramLex(expectedParamPtr);
       
   413 			TInt param;
       
   414 			paramLex.Val(param);
       
   415 			// ...and put it  into iExpectedError
       
   416 			iExpectedError = param;
       
   417 			}
       
   418 
       
   419 		CleanupStack::PopAndDestroy(4, itemBuff8); // itemBuff8, item, 
       
   420 												   // valueBuff8, value
       
   421 		}
       
   422 	}
       
   423 
       
   424 void CWspTestTransactions::SetWspSecurityPolicyPropertiesL(TInt aStartItem)
       
   425 	{
       
   426 	// Create the security policy plugin ini file
       
   427 	RFs fs;
       
   428 	CleanupClosePushL(fs);
       
   429 	User::LeaveIfError(fs.Connect());
       
   430 
       
   431 	TParse fileName;
       
   432 	User::LeaveIfError(iEngine->Utils().ResolveFile(KSecurityPolicy, KSecurityPolicyFileName, fileName));
       
   433 
       
   434 	RFile file;
       
   435 	CleanupClosePushL(file);
       
   436 	User::LeaveIfError(file.Replace(fs, fileName.FullName(), 
       
   437 		EFileWrite|EFileShareExclusive)); 
       
   438 
       
   439 	// Write the section name
       
   440 	file.Write(_L8("\r\n[Security Policy]\r\n"));
       
   441 
       
   442 	TInt endItem = FindEndOfTransaction(aStartItem);
       
   443 	for (TInt ii = aStartItem; ii < endItem; ++ii)
       
   444 		{
       
   445 		TPtrC itemPtr(iIniFile->Section(iSectionNumber).Item(ii).Item());
       
   446 		HBufC8* itemBuff8 = HBufC8::NewLC(itemPtr.Length());
       
   447 		TPtr8  itemPtr8 = itemBuff8->Des();
       
   448 		itemPtr8.Copy(itemPtr);
       
   449 		
       
   450 		TPtrC valuePtr(iIniFile->Section(iSectionNumber).Item(ii).Value());
       
   451 		HBufC8* valueBuff8 = HBufC8::NewLC(valuePtr.Length());
       
   452 		TPtr8 valuePtr8 = valueBuff8->Des();
       
   453 		valuePtr8.Copy(valuePtr);
       
   454 
       
   455 		if( itemPtr.CompareF(KValidateUntrustedServerCert) == 0 ||
       
   456 			itemPtr.CompareF(KWTLSCipherSuites) == 0||
       
   457 			itemPtr.CompareF(KWTLSKeyExchangeSuites) == 0 ||
       
   458 			itemPtr.CompareF(KTLSCipherSuites) == 0||
       
   459 			itemPtr.Find(KPolicy) == 0) // matches 'Policy1', 'Policy2', etc.
       
   460 			{
       
   461 			// Write out entry to the securitypolicy.ini file
       
   462 			file.Write(itemPtr8);
       
   463 			file.Write(_L8("= "));
       
   464 			file.Write(valuePtr8);
       
   465 			file.Write(_L8("\r\n"));
       
   466 			}
       
   467 
       
   468 		CleanupStack::PopAndDestroy(2, itemBuff8); // itemBuff8, valueBuff8
       
   469 		}
       
   470 	// Write the end of script marked in security ini
       
   471 	file.Write(_L8("\r\nendscript\r\n"));
       
   472 
       
   473 	// Cleanup
       
   474 	CleanupStack::PopAndDestroy(2, &fs); // fs, file
       
   475 	}