email/pop3andsmtpmtm/smtpservermtm/test/src/T_SMTSSECURESOCKETS.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2001-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 // Brief description of test harness:
       
    15 // Tests SSL/TLS functionality in SMTS
       
    16 // Input files required to run test harness:
       
    17 // The following files should be copied to <drive>:\msgtest\smts\scripts
       
    18 // t_smtsTlsNotSupported.scr
       
    19 // t_smtsTlsTempFail.scr
       
    20 // t_smtsTlsOk.scr
       
    21 // t_smtsTlsSyntaxError.scr
       
    22 // t_smtsTlsNoServices.scr
       
    23 // t_smtsTlsRefused.scr
       
    24 // t_smtsTlsStopResponding.scr
       
    25 // t_smtsTlsNewSession.scr
       
    26 // Output files produced by running test harness:
       
    27 // <DRIVE>:\msglogs\T_SMTSSECURESOCKETS.<PLATFORM>.<VARIANT>.LOG.txt
       
    28 // Description of how to build test harness:
       
    29 // cd \messaging\msg\smts\
       
    30 // bldmake bldfiles
       
    31 // abld test build t_SMTSSECURESOCKETS
       
    32 // Description of how to run test harness:
       
    33 // The following instructions are the same for all platforms:
       
    34 // 2. Build the test utilities:
       
    35 // cd \messaging\msg\testutils\group\
       
    36 // bldmake bldfiles
       
    37 // abld build
       
    38 // WINS running instructions:
       
    39 // 1. \epoc32\release\wins\<VARIANT>\T_SMTSSECURESOCKETS.exe can be used at the command prompt
       
    40 // or executable can be run from Windows Explorer.
       
    41 // All other platform running instructions:
       
    42 // 1. Copy \epoc32\release\<PLATFORM>\<VARIANT>\T_SMTSSECURESOCKETS.exe onto the other platform
       
    43 // 2. Copy \epoc32\release\<PLATFORM>\<VARIANT>\MSVTESTUTILS.DLL into 
       
    44 // <DRIVE>:\system\libs on the other platform
       
    45 // 3. Copy \epoc32\release\<PLATFORM>\<VARIANT>\EMAILTESTUTILS.DLL into 
       
    46 // <DRIVE>:\system\libs on the other platform
       
    47 // 4. Copy \epoc32\wins\c\msgtest\smts\scripts\*.scr to
       
    48 // <DRIVE>:\msgtest\smts\scripts\
       
    49 // Note that this drive should be a CF card as the test files are large
       
    50 // 5. Run T_SMTSSECURESOCKETS.exe on the other platform with the CF card
       
    51 // 
       
    52 //
       
    53 
       
    54 #include "emailtestutils.h"
       
    55 #include "scripttestutils.h"
       
    56 #include <cemailaccounts.h>
       
    57 
       
    58 _LIT(KSmtsTest, "Testing SMTS secure connection with script files");
       
    59 _LIT(KSmtsComponent,"SMTS");
       
    60 
       
    61 // Script Files for the Tests
       
    62 _LIT(KTest1ScriptFile,	"scripts\\t_smtsTlsNotSupported.scr");
       
    63 _LIT(KTest1Desc,		"Test 1 connects to a server which doesn't support SSLTLS.");
       
    64 _LIT(KTest1Result,		"Test 1 should finish with -246 (SMTP TLS Negotiate Failed).");
       
    65 
       
    66 _LIT(KTest2ScriptFile,	"scripts\\t_smtsTlsTempFail.scr");
       
    67 _LIT(KTest2Desc,		"Test 2 connects to a server which supports SSLTLS, but TLS is not available due to temporary reason.");
       
    68 _LIT(KTest2Result,		"Test 2 should finish with -185 (SSL TLS Negotiate Failed).");
       
    69 
       
    70 _LIT(KTest3ScriptFile,	"scripts\\t_smtsTlsOk.scr");
       
    71 _LIT(KTest3Desc,		"Test 3 connects to a server which supports SSLTLS.");
       
    72 _LIT(KTest3Result,		"Test 3 should complete with no error.");
       
    73 
       
    74 _LIT(KTest4ScriptFile,	"scripts\\t_smtsTlsSyntaxError.scr");
       
    75 _LIT(KTest4Desc,		"Test 4 connects to a server which supports SSLTLS, but returns an Error.");
       
    76 _LIT(KTest4Result,		"Test 4 should finish with -185 (SSL TLS Negotiate Failed).");
       
    77 
       
    78 _LIT(KTest5ScriptFile,	"scripts\\t_smtsTlsNoServices.scr");
       
    79 _LIT(KTest5Desc,		"Test 5 connects to a server which supports SSLTLS, starts TLS, but the server returns no supported services.");
       
    80 _LIT(KTest5Result,		"Test 5 should finish with -185 (SSL TLS Negotiate Failed).");
       
    81 
       
    82 _LIT(KTest6ScriptFile,	"scripts\\t_smtsTlsRefused.scr");
       
    83 _LIT(KTest6Desc,		"Test 6 connects to a server which supports SSLTLS, but then refuses connection, due to lack of security");
       
    84 _LIT(KTest6Result,		"Test 6 should finish with -244 (SMTP Login Refused).");
       
    85 
       
    86 _LIT(KTest8ScriptFile,	"scripts\\t_smtsTlsNewSession.scr");
       
    87 _LIT(KTest8Desc,		"Test 8 - After TLS handshake, the server tries to start new session, by returning STARTTLS");
       
    88 _LIT(KTest8Result,		"Test 8 should complete with no error, as MTM ignores this command.");
       
    89 
       
    90 RTest test(KSmtsTest);
       
    91 
       
    92 LOCAL_D CTrapCleanup* theCleanup;
       
    93 LOCAL_D CEmailTestUtils* testUtils; 
       
    94 LOCAL_D TMsvId smtpService;
       
    95 LOCAL_D CBaseServerMtm* serverMtm;
       
    96 LOCAL_D TInt testError;
       
    97 
       
    98 
       
    99 // Class to hold the data required for a Test
       
   100 class TTestData
       
   101 	{
       
   102 	public:
       
   103 		TInt		iTestNumber;
       
   104 		TBuf<50>	iTestFile;
       
   105 		TBuf<200>	iTestDescription;
       
   106 		TBuf<200>	iTestResult;
       
   107 	};
       
   108 
       
   109 
       
   110 LOCAL_C TInt TestSendingMessagesL()
       
   111 	{
       
   112 	testError = 0;
       
   113 
       
   114 	CTestActive* testActive = new (ELeave) CTestActive();
       
   115 	CleanupStack::PushL(testActive);
       
   116 
       
   117 	CMsvEntrySelection* sendSel = new(ELeave) CMsvEntrySelection();
       
   118 	CleanupStack::PushL(sendSel);
       
   119 
       
   120 	testUtils->iServerEntry->SetEntry(KMsvGlobalOutBoxIndexEntryId);
       
   121 	User::LeaveIfError(testUtils->iServerEntry->GetChildren(*sendSel));
       
   122 	TInt count=sendSel->Count();
       
   123 	if (!count)
       
   124 		{
       
   125 		testUtils->WriteComment(_L("\t No messages to send in the outbox"));
       
   126 		CleanupStack::PopAndDestroy(2); //testActive, sendsel
       
   127 		testError = KErrNotFound;
       
   128 		}
       
   129 	else
       
   130 		{
       
   131 
       
   132 		serverMtm->CopyFromLocalL(*sendSel,smtpService, testActive->iStatus);
       
   133 
       
   134 		testActive->StartL();
       
   135 		CActiveScheduler::Start();
       
   136  		TImSmtpProgress temp;	
       
   137 		TPckgC<TImSmtpProgress> paramPack(temp);
       
   138 
       
   139 		const TDesC8& progBuf =serverMtm->Progress();
       
   140 		paramPack.Set(progBuf);
       
   141 		TImSmtpProgress progress=paramPack();	
       
   142 		CleanupStack::PopAndDestroy(2); //testActive, sendsel
       
   143 		testError = progress.Error();
       
   144 		}
       
   145 
       
   146 	return testError;
       
   147 	}
       
   148 
       
   149 LOCAL_C void ClosedownL()
       
   150 	{
       
   151 	testUtils->TestHarnessCompleted();
       
   152 	CleanupStack::PopAndDestroy(2);  //testUtils, ischeduler
       
   153 	}
       
   154 
       
   155 LOCAL_C void ResetSettingsL(TBool aSecureSocket)
       
   156 	{
       
   157 	_LIT(KSmtpServer, "smtp.demon.co.uk");
       
   158 	_LIT(KSmtpEmailAlias, "SMTP STARTTLS");
       
   159 	_LIT(KSmtpEmailAddress, "<a@b>");
       
   160 
       
   161 	// Overwrite the setings with test code one.  Don't want the default settings.
       
   162 	CImSmtpSettings* settings = new(ELeave) CImSmtpSettings();
       
   163 	CleanupStack::PushL(settings);
       
   164 	
       
   165 	CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   166 	
       
   167 	TSmtpAccount smtpAccountId;
       
   168 	accounts->GetSmtpAccountL(smtpService,smtpAccountId); 
       
   169 	accounts->LoadSmtpSettingsL(smtpAccountId, *settings);
       
   170 
       
   171 	settings->SetServerAddressL(KSmtpServer);
       
   172 	settings->SetEmailAliasL(KSmtpEmailAlias);
       
   173 	settings->SetEmailAddressL(KSmtpEmailAddress);
       
   174 	settings->SetReplyToAddressL(KSmtpEmailAddress);
       
   175 	settings->SetBodyEncoding(EMsgOutboxMIME);
       
   176 	settings->SetReceiptAddressL(KSmtpEmailAddress);
       
   177 	settings->SetSecureSockets(aSecureSocket);
       
   178 	settings->SetPort(25);
       
   179 	
       
   180 	accounts->SaveSmtpSettingsL(smtpAccountId, *settings);
       
   181 
       
   182 	CleanupStack::PopAndDestroy(2, settings); //settings, store/accounts
       
   183 	}
       
   184 
       
   185 LOCAL_C void InitL()
       
   186 	{
       
   187 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
       
   188 	CleanupStack::PushL(scheduler);
       
   189 	CActiveScheduler::Install( scheduler );
       
   190 
       
   191 	testUtils = CEmailTestUtils::NewLC(test);
       
   192 	testUtils->WriteComment(KSmtsTest);
       
   193 
       
   194 	testUtils->CleanMessageFolderL();
       
   195 	testUtils->ClearEmailAccountsL();
       
   196 	
       
   197 	testUtils->GoClientSideL();
       
   198  	
       
   199 	smtpService = testUtils->CreateSmtpServiceL();
       
   200 	ResetSettingsL(ETrue);
       
   201 	CleanupStack::PopAndDestroy(testUtils);
       
   202 	testUtils = CEmailTestUtils::NewLC(test, ETuGoServerSide);
       
   203 	testUtils->InstantiateSmtpServerMtmL();
       
   204 	serverMtm=testUtils->iSmtpServerMtm;
       
   205 	testUtils->WriteComment(_L("Smtp server created"));
       
   206 	}
       
   207 
       
   208 LOCAL_C void CreateMsgToSendL()
       
   209 // Create a message to send & place it in the outbox
       
   210 	{
       
   211 	TParse scriptFile;
       
   212 	_LIT(KSmtsRfcFile, "RFC822\\rfc82201.txt");
       
   213 	testUtils->ResolveFile(KSmtsComponent, KSmtsRfcFile, scriptFile);
       
   214 	testUtils->CreateMessageL(scriptFile.FullName(), smtpService, KMsvGlobalOutBoxIndexEntryId);
       
   215 	testUtils->WriteComment(_L(" Message created in the outbox"));
       
   216 	testUtils->WriteComment(_L("\nPerforming Tests\n"));
       
   217 	}
       
   218 
       
   219 
       
   220 LOCAL_C void RunTestL(TTestData& aTestData)
       
   221 	{
       
   222 	// Add a blank line to the Log file before the new Test
       
   223 	_LIT(KBlankLine, "");
       
   224 	testUtils->WriteComment(KBlankLine);
       
   225 
       
   226 	TParse scriptFile;
       
   227 	testUtils->ResolveFile(KSmtsComponent, aTestData.iTestFile, scriptFile);
       
   228 	testUtils->CopyScriptFileL(scriptFile.FullName(), _L("25"));
       
   229 	testUtils->TestStart(aTestData.iTestNumber);
       
   230 	testUtils->WriteComment(aTestData.iTestDescription);
       
   231 	testUtils->WriteComment(aTestData.iTestResult);
       
   232 	
       
   233 	// Run the Test & check the error
       
   234 	TestSendingMessagesL();
       
   235 	}
       
   236 
       
   237 
       
   238 LOCAL_C void RunAllTestsL()
       
   239 	{
       
   240 	
       
   241 	//	Create a Message to Send
       
   242 	CreateMsgToSendL();
       
   243 
       
   244 	//-------------------------------
       
   245 	//	Test 1 - TLS Not Supported
       
   246 	//-------------------------------
       
   247 	TTestData testData;
       
   248 	testData.iTestNumber = 1;
       
   249 	testData.iTestFile = KTest1ScriptFile;
       
   250 	testData.iTestDescription = KTest1Desc;
       
   251 	testData.iTestResult = KTest1Result;
       
   252 	RunTestL(testData);
       
   253 	testError=(testError==-246)? 0 : testError;
       
   254 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   255 
       
   256 	//-----------------------------------------------------------------
       
   257 	//	Test 2 - TLS Not Available due to Temp Reason (ie 454 responce)
       
   258 	//-----------------------------------------------------------------
       
   259 	testData.iTestNumber = 2;
       
   260 	testData.iTestFile = KTest2ScriptFile;
       
   261 	testData.iTestDescription = KTest2Desc;
       
   262 	testData.iTestResult = KTest2Result;
       
   263 	RunTestL(testData);
       
   264 	testError=(testError==-185)? 0 : testError;
       
   265 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   266 
       
   267 	//------------------------------------------------------
       
   268 	//	Test 3 - TLS Supported & Msg Sent (ie 220 responce)
       
   269 	//------------------------------------------------------
       
   270 	testData.iTestNumber = 3;
       
   271 	testData.iTestFile = KTest3ScriptFile;
       
   272 	testData.iTestDescription = KTest3Desc;
       
   273 	testData.iTestResult = KTest3Result;
       
   274 	RunTestL(testData);
       
   275 	testError=(testError==0)? 0 : testError;
       
   276 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   277 
       
   278 	//	Create a Message to Send, as the previous one was sent by Test 3
       
   279 	CreateMsgToSendL();
       
   280 
       
   281 	//---------------------------------------------
       
   282 	//	Test 4 - TLS Syntax Error (ie 501 responce)
       
   283 	//---------------------------------------------
       
   284 	testData.iTestNumber = 4;
       
   285 	testData.iTestFile = KTest4ScriptFile;
       
   286 	testData.iTestDescription = KTest4Desc;
       
   287 	testData.iTestResult = KTest4Result;
       
   288 	RunTestL(testData);
       
   289 	testError=(testError==-185)? 0 : testError;
       
   290 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   291 
       
   292 	//--------------------------------------------------------------------------------------
       
   293 	//	Test 5 - No Supported Services (After Handshake, Server sends no Supported Services)
       
   294 	//--------------------------------------------------------------------------------------
       
   295 	testData.iTestNumber = 5;
       
   296 	testData.iTestFile = KTest5ScriptFile;
       
   297 	testData.iTestDescription = KTest5Desc;
       
   298 	testData.iTestResult = KTest5Result;
       
   299 	RunTestL(testData);
       
   300 	testError=(testError==-185)? 0 : testError;
       
   301 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   302 
       
   303 	//----------------------------------------
       
   304 	//	Test 6 - TLS Refusal (ie 554 responce)
       
   305 	//----------------------------------------
       
   306 	testData.iTestNumber = 6;
       
   307 	testData.iTestFile = KTest6ScriptFile;
       
   308 	testData.iTestDescription = KTest6Desc;
       
   309 	testData.iTestResult = KTest6Result;
       
   310 	RunTestL(testData);
       
   311 	testError=(testError==-244)? 0 : testError;
       
   312 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   313 
       
   314 	//----------------------------------------
       
   315 	//	Test 7 - Server Stops Responding
       
   316 	//----------------------------------------
       
   317 /*	testData.iTestNumber = 7;
       
   318 	testData.iTestFile = KTest7ScriptFile;
       
   319 	testData.iTestDescription = KTest7Desc;
       
   320 	testData.iTestResult = KTest7Result;
       
   321 	RunTestL(testData);*/
       
   322 
       
   323 	//--------------------------------------------------------------------------
       
   324 	//	Test 8 - Session Already Active (Server sends STARTTLS after handshake)
       
   325 	//--------------------------------------------------------------------------
       
   326 	testData.iTestNumber = 8;
       
   327 	testData.iTestFile = KTest8ScriptFile;
       
   328 	testData.iTestDescription = KTest8Desc;
       
   329 	testData.iTestResult = KTest8Result;
       
   330 	RunTestL(testData);
       
   331 	testError=(testError==0)? 0 : testError;
       
   332 	testUtils->TestFinish(testData.iTestNumber, testError);
       
   333 	}
       
   334 
       
   335 
       
   336 LOCAL_C void doMainL()
       
   337 	{
       
   338 	InitL();
       
   339 	testUtils->iServerEntry->SetEntry(smtpService);
       
   340 
       
   341 	// Set the Setting to use Secure Sockets & Run the Tests
       
   342 	_LIT(KSecurityOn, "------Set Security On------");
       
   343 	testUtils->WriteComment(KSecurityOn);
       
   344 	RunAllTestsL();
       
   345 
       
   346 	// End of the Tests
       
   347 	ClosedownL();
       
   348 	}
       
   349 
       
   350 
       
   351 GLDEF_C TInt E32Main()
       
   352 	{	
       
   353 	__UHEAP_MARK;
       
   354 	test.Start(_L("T_SMTSSECURESOCKETS Test SMTP server MTM secure connection"));
       
   355 	theCleanup=CTrapCleanup::New();
       
   356 	TRAPD(ret,doMainL());		
       
   357 	test(ret==KErrNone);
       
   358 	delete theCleanup;	
       
   359 	test.End();
       
   360 	test.Close();
       
   361 	__UHEAP_MARKEND;
       
   362 	User::Heap().Check();
       
   363 	return(KErrNone);
       
   364 	}