networksecurity/tlsprovider/Test/src/TSecureConnection.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2006-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 <c32comm.h>
       
    17 #include <test/testexecutelog.h>
       
    18 #include "TSecureConnection.h"
       
    19 
       
    20 
       
    21 // PDD names for the physical device drivers that are loaded in wins or arm
       
    22 #if defined (__WINS__)
       
    23 #define PDD_NAME		_L("ECDRV")
       
    24 #else
       
    25 #define PDD_NAME		_L("EUART1")
       
    26 #define PDD2_NAME		_L("EUART2")
       
    27 #define PDD3_NAME		_L("EUART3")
       
    28 #define PDD4_NAME		_L("EUART4")
       
    29 #endif
       
    30 #define LDD_NAME		_L("ECOMM")
       
    31 
       
    32 _LIT8(KUserAgent, "SimpleHTTPClient (1.0)");
       
    33 _LIT8(KAccept, "*/*");
       
    34 
       
    35 CTSecureConnectionStep::CTSecureConnectionStep(const TDesC& aStepName)
       
    36 	{
       
    37 	SetTestStepName( aStepName );
       
    38 	}
       
    39 	
       
    40 CTSecureConnectionStep::~CTSecureConnectionStep()
       
    41 	{
       
    42 	}
       
    43 
       
    44 TVerdict CTSecureConnectionStep::doTestStepPreambleL()
       
    45 	{	
       
    46 	INFO_PRINTF1(_L("CTSecureConnectionStep Check Start"));
       
    47 	
       
    48 	InitCommsL();
       
    49 	User::LeaveIfError(iFs.Connect());
       
    50 	ConvertDlgConfigL();
       
    51 	
       
    52 	iScheduler=new(ELeave) CActiveScheduler;
       
    53 	CActiveScheduler::Install(iScheduler);
       
    54 
       
    55 	SetTestStepResult(EPass);
       
    56 	return TestStepResult();
       
    57 	}
       
    58 
       
    59 TVerdict CTSecureConnectionStep::doTestStepL()
       
    60 	{
       
    61 	TPtrC url;				
       
    62 	if(!GetStringFromConfig(ConfigSection(), KUrl, url))
       
    63 		{
       
    64 		ERR_PRINTF1(_L("Missing url"));
       
    65 		SetTestStepError(KErrBadName);
       
    66 		SetTestStepResult(EFail);
       
    67 		}
       
    68 		
       
    69 	iHttp=CSimpleHttpClient::NewL(this);
       
    70 	iHttp->StartClientL(url);
       
    71 	
       
    72 	return TestStepResult();
       
    73 	}
       
    74 
       
    75 TVerdict CTSecureConnectionStep::doTestStepPostambleL()
       
    76 	{
       
    77 	INFO_PRINTF1(_L("CTSecureConnectionStep Check Done"));
       
    78 	iFs.Close();
       
    79 	delete iScheduler;
       
    80 	return TestStepResult();
       
    81 	}
       
    82 
       
    83 void CTSecureConnectionStep::WriteDialogRecordL(RFileWriteStream& aStream, TSecurityDialogOperation aOp, const TDesC& aLabelSpec,
       
    84 											 const TDesC& aResponse1, const TDesC& aResponse2)
       
    85 	{
       
    86 	MStreamBuf* streamBuf = aStream.Sink();
       
    87 	streamBuf->SeekL(MStreamBuf::EWrite, EStreamEnd);
       
    88 	aStream.WriteInt32L(aOp);
       
    89 	aStream.WriteInt32L(aLabelSpec.Length());
       
    90 	aStream.WriteL(aLabelSpec);
       
    91 	aStream.WriteInt32L(aResponse1.Length());
       
    92 	aStream.WriteL(aResponse1);
       
    93 	aStream.WriteInt32L(aResponse2.Length());
       
    94 	aStream.WriteL(aResponse2);
       
    95 	}
       
    96 
       
    97 void CTSecureConnectionStep::InitCommsL()
       
    98 	{
       
    99 	// StartC32
       
   100 	TInt ret = User::LoadPhysicalDevice(PDD_NAME);
       
   101 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
   102 
       
   103 #ifndef __WINS__
       
   104 	ret = User::LoadPhysicalDevice(PDD2_NAME);
       
   105 	ret = User::LoadPhysicalDevice(PDD3_NAME);
       
   106 	ret = User::LoadPhysicalDevice(PDD4_NAME);
       
   107 #endif
       
   108 
       
   109 	ret = User::LoadLogicalDevice(LDD_NAME);
       
   110 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
   111 	ret = StartC32();
       
   112 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);		
       
   113 	}
       
   114 	
       
   115 void CTSecureConnectionStep::ConvertDlgConfigL()
       
   116 	{
       
   117 	//Clean the response files
       
   118 	CFileMan* fileMan = CFileMan::NewL(iFs);
       
   119 	CleanupStack::PushL(fileMan);
       
   120 	
       
   121 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
   122 	TDriveName sysDriveName (sysDrive.Name());
       
   123 	
       
   124 	TBuf<128> datFile (sysDriveName);
       
   125 	datFile.Append(KTSecDlgOutputFile);
       
   126 	TInt err = fileMan->Delete(datFile);
       
   127 	if (err != KErrNotFound)
       
   128 		User::LeaveIfError(err);
       
   129 	
       
   130 	datFile.Copy(sysDriveName);
       
   131 	datFile.Append(KTSecDlgInputFile);
       
   132 	
       
   133 	err = fileMan->Delete(datFile);
       
   134 	if (err != KErrNotFound)
       
   135 		User::LeaveIfError(err);
       
   136 	
       
   137 	CleanupStack::PopAndDestroy(fileMan);
       
   138 	
       
   139 	//Create the response input file
       
   140 	RFileWriteStream stream;
       
   141 	err = stream.Open(iFs, datFile, EFileWrite | EFileShareExclusive);
       
   142 	if (err == KErrNotFound)
       
   143 		{
       
   144 		err = stream.Create(iFs, datFile, EFileWrite | EFileShareExclusive);
       
   145 		}
       
   146 	User::LeaveIfError(err);
       
   147 	stream.PushL();
       
   148 	
       
   149 	// Read the dialog response data
       
   150 	TPtrC res1;
       
   151 	if (GetStringFromConfig(ConfigSection(), KServerAuthenticationResponse, res1))
       
   152 		{
       
   153 		WriteDialogRecordL(stream, EServerAuthenticationFailure, _L("Server Authentication Dialog"), res1, KNullDesC);			
       
   154 		}	
       
   155 
       
   156 	TPtrC res3, res4;
       
   157 	if (GetStringFromConfig(ConfigSection(), KClientAuthenticationResponse1, res3) && GetStringFromConfig(ConfigSection(), KClientAuthenticationResponse2, res4))
       
   158 		{
       
   159 		WriteDialogRecordL(stream, ESecureConnection, _L("Client Authentication Dialog"), res3, res4);
       
   160 		}		
       
   161 	
       
   162 	TPtrC res5;
       
   163 	if (!GetStringFromConfig(ConfigSection(), KKeyStorePassPhrase, res5))
       
   164 		{
       
   165 		res5.Set(_L("pinkcloud"));				
       
   166 		}
       
   167 	WriteDialogRecordL(stream, EEnterPIN, _L("Key store passphrase"), res5, KNullDesC);	
       
   168 
       
   169 	stream.CommitL();
       
   170 	CleanupStack::PopAndDestroy(); // stream		
       
   171 	}
       
   172 	
       
   173 void CTSecureConnectionStep::ReceiveTestResult(const TInt& aErrorCode, TVerdict aVerdict)
       
   174 	{
       
   175 	SetTestStepError(aErrorCode);
       
   176 	SetTestStepResult(aVerdict);	
       
   177 	}
       
   178 
       
   179 CSimpleHttpClient* CSimpleHttpClient::NewLC(CTSecureConnectionStep* aStep)
       
   180 	{
       
   181 	CSimpleHttpClient* me = new(ELeave) CSimpleHttpClient;
       
   182 	CleanupStack::PushL(me);
       
   183 	me->ConstructL(aStep);
       
   184 	return me;
       
   185 	}
       
   186 
       
   187 CSimpleHttpClient* CSimpleHttpClient::NewL(CTSecureConnectionStep* aStep)
       
   188 	{
       
   189 	CSimpleHttpClient* me = NewLC(aStep);
       
   190 	CleanupStack::Pop(me);
       
   191 	return me;
       
   192 	}
       
   193 	
       
   194 CSimpleHttpClient::~CSimpleHttpClient()
       
   195 	{
       
   196 	iSess.Close();
       
   197 	delete iTransObs;
       
   198 	}
       
   199 	
       
   200 void CSimpleHttpClient::ConstructL(CTSecureConnectionStep* aStep)
       
   201 	{
       
   202 	// Open the RHTTPSession
       
   203 	iSess.OpenL();	
       
   204 	iTransObs = CHttpEventHandler::NewL(aStep);
       
   205 	iTestStep=aStep;
       
   206 	}
       
   207 
       
   208 void CSimpleHttpClient::StartClientL(TDesC& aUrl)
       
   209 	{
       
   210 	RStringPool strP = iSess.StringPool();
       
   211 	RStringF method;
       
   212 	method = strP.StringF(HTTP::EGET,RHTTPSession::GetTable());
       
   213 	
       
   214 	// Start the method off
       
   215 	TBuf8<256> url8;
       
   216 	url8.Copy(aUrl);
       
   217 	InvokeHttpMethodL(url8, method);
       
   218 	}
       
   219 
       
   220 void CSimpleHttpClient::InvokeHttpMethodL(const TDesC8& aUri, RStringF aMethod)
       
   221 	{
       
   222 	TUriParser8 uri; 
       
   223 	uri.Parse(aUri);
       
   224 	iTrans = iSess.OpenTransactionL(uri, *iTransObs, aMethod);
       
   225 	RHTTPHeaders hdr = iTrans.Request().GetHeaderCollection();
       
   226 
       
   227 	SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
       
   228 	SetHeaderL(hdr, HTTP::EAccept, KAccept);
       
   229 	iTrans.SubmitL();
       
   230 
       
   231 	CActiveScheduler::Start();
       
   232 	}
       
   233 
       
   234 void CSimpleHttpClient::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
       
   235 	{
       
   236 	RStringF valStr = iSess.StringPool().OpenFStringL(aHdrValue);
       
   237 	THTTPHdrVal val(valStr);
       
   238 	aHeaders.SetFieldL(iSess.StringPool().StringF(aHdrField,RHTTPSession::GetTable()), val);
       
   239 	valStr.Close();
       
   240 	}
       
   241 
       
   242 CHttpEventHandler* CHttpEventHandler::NewLC(CTSecureConnectionStep* aStep)
       
   243 	{
       
   244 	CHttpEventHandler* me = new(ELeave)CHttpEventHandler();
       
   245 	CleanupStack::PushL(me);
       
   246 	me->ConstructL(aStep);
       
   247 	return me;
       
   248 	}
       
   249 
       
   250 CHttpEventHandler* CHttpEventHandler::NewL(CTSecureConnectionStep* aStep)
       
   251 	{
       
   252 	CHttpEventHandler* me = NewLC(aStep);
       
   253 	CleanupStack::Pop(me);
       
   254 	return me;
       
   255 	}
       
   256 void CHttpEventHandler::ConstructL(CTSecureConnectionStep* aStep)
       
   257 	{	
       
   258 	iTestStep=aStep;
       
   259 	}
       
   260 	
       
   261 void CHttpEventHandler::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
       
   262 	{
       
   263 	switch (aEvent.iStatus)
       
   264 		{
       
   265 		case THTTPEvent::EGotResponseHeaders:
       
   266 			{
       
   267 			} break;
       
   268 		case THTTPEvent::EGotResponseBodyData:
       
   269 			{
       
   270 			iRespBody = aTransaction.Response().Body();
       
   271 			iRespBody->ReleaseData();
       
   272 			} break;
       
   273 		case THTTPEvent::EResponseComplete:
       
   274 			{
       
   275 			} break;
       
   276 		case THTTPEvent::ESucceeded:
       
   277 			{
       
   278 			aTransaction.Close();
       
   279 			CActiveScheduler::Stop();
       
   280 			} break;
       
   281 		case THTTPEvent::EFailed:
       
   282 			{
       
   283 			aTransaction.Close();
       
   284 			CActiveScheduler::Stop();
       
   285 			} break;
       
   286 		case THTTPEvent::ERedirectedPermanently:
       
   287 			{
       
   288 			} break;
       
   289 		case THTTPEvent::ERedirectedTemporarily:
       
   290 			{
       
   291 			} break;
       
   292 		case THTTPEvent::ERedirectRequiresConfirmation:
       
   293  			{
       
   294  			aTransaction.Close();
       
   295  			CActiveScheduler::Stop();
       
   296  			} break;
       
   297 		default:
       
   298 			{
       
   299 			if (aEvent.iStatus < 0)
       
   300 				{
       
   301 				iTestStep->ReceiveTestResult(aEvent.iStatus, EFail);
       
   302 				aTransaction.Close();
       
   303 				CActiveScheduler::Stop();
       
   304 				}
       
   305 			} break;
       
   306 		}
       
   307 	}
       
   308 
       
   309 TInt CHttpEventHandler::MHFRunError(TInt /*aError*/, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/)
       
   310 	{
       
   311 	return KErrNone;
       
   312 	}
       
   313 
       
   314