email/pop3andsmtpmtm/imapservermtm/test/src/T_impssecuresocket.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     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 IMPS
       
    16 // Input files required to run test harness:
       
    17 // None
       
    18 // Output files produced by running test harness:
       
    19 // <DRIVE>:\msglogs\T_IMPSSECURESOCKET.<PLATFORM>.<VARIANT>.LOG.txt
       
    20 // Description of how to build test harness:
       
    21 // cd \messaging\msg\imps\
       
    22 // bldmake bldfiles
       
    23 // abld test build T_IMPSSECURESOCKET
       
    24 // Description of how to run test harness:
       
    25 // The following instructions are the same for all platforms:
       
    26 // 2. Build the test utilities:
       
    27 // cd \messaging\msg\testutils\group\
       
    28 // bldmake bldfiles
       
    29 // abld build
       
    30 // WINS running instructions:
       
    31 // 1. \epoc32\release\wins\<VARIANT>\T_IMPSSECURESOCKET.exe can be used at the command prompt
       
    32 // or executable can be run from Windows Explorer.
       
    33 // All other platform running instructions:
       
    34 // 1. Copy \epoc32\release\<PLATFORM>\<VARIANT>\T_IMPSSECURESOCKET.exe onto the other platform
       
    35 // 2. Copy \epoc32\release\<PLATFORM>\<VARIANT>\MSVTESTUTILS.DLL into 
       
    36 // <DRIVE>:\system\libs on the other platform
       
    37 // 3. Copy \epoc32\release\<PLATFORM>\<VARIANT>\EMAILTESTUTILS.DLL into 
       
    38 // <DRIVE>:\system\libs on the other platform
       
    39 // 5. Run T_IMPSSECURESOCKET.exe on the other platform with the CF card
       
    40 // 
       
    41 //
       
    42 
       
    43 #include "EmailTestUtils.h"
       
    44 #include <miutmsg.h>
       
    45 #include "impsmtm.h"
       
    46 #include <securesocket.h>
       
    47 #include <bacline.h>
       
    48 #include <cemailaccounts.h>
       
    49 
       
    50 // For loading the serial comms device drivers 
       
    51 #if defined (__WINS__)
       
    52 	#define PDD_NAME		_L("ECDRV")
       
    53 	#define LDD_NAME		_L("ECOMM")
       
    54 #else
       
    55 	#define PDD_NAME		_L("EUART1")
       
    56 	#define LDD_NAME		_L("ECOMM")
       
    57 #endif
       
    58 
       
    59 
       
    60 LOCAL_D CTrapCleanup* theCleanup;
       
    61 LOCAL_D CEmailTestUtils* testUtils; 
       
    62 LOCAL_D CActiveScheduler* scheduler;
       
    63 LOCAL_D RFs theFs;	
       
    64 LOCAL_D TMsvId	imapService;
       
    65 
       
    66 
       
    67 _LIT(KImpsTest, "T_IMPSSECURESOCKET - IMPS SecureSockets Functionality Testing");
       
    68 
       
    69 RTest test(KImpsTest);
       
    70 
       
    71 _LIT(KTestParams,"-testparams");
       
    72 enum TCmdLineArgs
       
    73 	{
       
    74 	EArgProgramName,
       
    75 	EArgTestParams,
       
    76 	EArgServerAddress,
       
    77 	EArgLogin,
       
    78 	EArgPassword,
       
    79 	EArgEnd
       
    80 	};
       
    81 
       
    82 
       
    83 LOCAL_C TInt readConsoleLineBuf(const TDesC& aPrompt, TDes& aBuf)
       
    84 	{
       
    85 	test.Printf(_L("\n "));
       
    86 	TBool quit = EFalse;
       
    87 	_LIT(KDeleteChar, "\b \b");
       
    88 
       
    89 	test.Printf(aPrompt);
       
    90 	while (!quit)
       
    91 		{
       
    92 		TKeyCode k = test.Getch();
       
    93 		TChar key = k;
       
    94 		switch (key)
       
    95 			{
       
    96 		case EKeyBackspace:
       
    97 			if (aBuf.Length() > 0)
       
    98 				{
       
    99 				test.Printf(KDeleteChar);
       
   100 				aBuf = aBuf.Left(aBuf.Length() - 1);
       
   101 				}
       
   102 			break;
       
   103 		case EKeyEnter:
       
   104 			if (aBuf.Length())
       
   105 				quit = ETrue;
       
   106 			else
       
   107 				test.Printf(_L("\nLogin, password and server address cannot be zero length "));
       
   108 			break;
       
   109 		default:
       
   110 			test.Printf(_L("%c"), key);
       
   111 			aBuf.Append(key);
       
   112 			break;
       
   113 			}
       
   114 		}
       
   115 	return(KErrNone);
       
   116 	}
       
   117 
       
   118 
       
   119 
       
   120 class CImpsTestHarness : public CActive
       
   121 	{
       
   122 public:
       
   123 	static CImpsTestHarness* NewLC();
       
   124 	void SetUpImpsServiceL();
       
   125 	~CImpsTestHarness();
       
   126 	void StartTestL();
       
   127 	void EndTestL();
       
   128 	void RunL();
       
   129 	void DoCancel();
       
   130 private:
       
   131 	CImpsTestHarness();
       
   132 	void ConstructL();
       
   133 private:
       
   134 	TInt				iTestError;
       
   135 	TInt				iTestNumber;
       
   136 	CImap4ServerMtm*	imapServerMtm;
       
   137 	CMsvEntrySelection* iSel;
       
   138 	TImap4Cmds iCommand;
       
   139 	};
       
   140 
       
   141 
       
   142 //
       
   143 
       
   144 CImpsTestHarness  *CImpsTestHarness::NewLC()
       
   145 	{
       
   146 	CImpsTestHarness* self=new (ELeave) CImpsTestHarness();
       
   147 	CleanupStack::PushL(self);
       
   148 	self->ConstructL();
       
   149 	return self;
       
   150 	}
       
   151 
       
   152 CImpsTestHarness::~CImpsTestHarness()
       
   153 	{
       
   154 	delete iSel;
       
   155 	}
       
   156 
       
   157 CImpsTestHarness::CImpsTestHarness()
       
   158 	: CActive(-1)
       
   159 	{
       
   160 	}
       
   161 
       
   162 void CImpsTestHarness::ConstructL()
       
   163 	{
       
   164 	CActiveScheduler::Add(this); // add to active scheduler
       
   165 	testUtils->CreateAllTestDirectories();
       
   166 	testUtils->FileSession().SetSessionPath(_L("C:\\"));
       
   167 	testUtils->CleanMessageFolderL();
       
   168 	testUtils->GoServerSideL();
       
   169 	// Get server MTM.
       
   170 	testUtils->Reset();
       
   171 	testUtils->CleanMessageFolderL();
       
   172 	testUtils->GoServerSideL();
       
   173 	SetUpImpsServiceL();
       
   174 	testUtils->InstantiateImapServerMtmL();
       
   175 	imapServerMtm=(CImap4ServerMtm*)testUtils->iImapServerMtm;
       
   176 	}
       
   177 
       
   178 void CImpsTestHarness::SetUpImpsServiceL()
       
   179 	{
       
   180 	// Handle command line arguments
       
   181 	CCommandLineArguments* cmdLineArg=CCommandLineArguments::NewLC();
       
   182 	TInt count = cmdLineArg->Count();
       
   183 	TBool isCmdLine=FALSE;
       
   184 	if (count>2)	// Command line arguments?
       
   185 		{
       
   186 		if ((!(cmdLineArg->Arg(EArgTestParams).Compare(KTestParams))) && count==EArgEnd)
       
   187 			isCmdLine=TRUE;
       
   188 		}	
       
   189 
       
   190 	imapService=testUtils->CreateImapServiceL();
       
   191 	testUtils->iServerEntry->SetEntry(imapService);
       
   192 
       
   193 	CImImap4Settings* settings = new(ELeave) CImImap4Settings();
       
   194 	CleanupStack::PushL(settings);
       
   195 	settings->Reset();
       
   196 
       
   197 
       
   198 	TBuf<100> buf;
       
   199 	if (isCmdLine)
       
   200 		{
       
   201 		TLex lex(cmdLineArg->Arg(EArgServerAddress));
       
   202 		buf=lex.NextToken();
       
   203 		test.Printf(_L("Server address: %S\n"),&buf);
       
   204 		}
       
   205 	else
       
   206 		{
       
   207 		readConsoleLineBuf(_L("Server address: "),buf);
       
   208 		}
       
   209 	settings->SetServerAddressL(buf);
       
   210 	buf.Zero();
       
   211 	if (isCmdLine)
       
   212 		{
       
   213 		TLex lex(cmdLineArg->Arg(EArgLogin));
       
   214 		buf=lex.NextToken();
       
   215 		test.Printf(_L("Login: %S\n"),&buf);
       
   216 		}
       
   217 	else
       
   218 		{
       
   219 		readConsoleLineBuf(_L("login: "),buf);
       
   220 		}
       
   221 	TBuf8<64> data;
       
   222 	data.Copy(buf);
       
   223 	settings->SetLoginNameL(testUtils->MachineName());
       
   224 	data.Zero();
       
   225 	buf.Zero();
       
   226 	if (isCmdLine)
       
   227 		{
       
   228 		TLex lex(cmdLineArg->Arg(EArgPassword));
       
   229 		buf=lex.NextToken();
       
   230 		test.Printf(_L("Password: %S\n"),&buf);
       
   231 		}
       
   232 	else
       
   233 		{
       
   234 		readConsoleLineBuf(_L("password: "),buf);
       
   235 		}
       
   236 	data.Copy(buf);
       
   237 	settings->SetPasswordL(testUtils->MachineName());
       
   238 
       
   239 	settings->SetFolderPathL(_L8(""));
       
   240 	settings->SetPort(143);
       
   241 	settings->SetPathSeparator('/');
       
   242 	settings->SetDisconnectedUserMode(ETrue);
       
   243 	settings->SetDeleteEmailsWhenDisconnecting(ETrue);
       
   244 	settings->SetGetMailOptions(EGetImap4EmailBodyTextAndAttachments);
       
   245 	settings->SetSecureSockets(ETrue); 
       
   246 	settings->SetSubscribe(EUpdateNeither);
       
   247 	settings->SetSynchronise(EUseCombination);
       
   248 	
       
   249 	CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   250  	TImapAccount imapAccount;
       
   251  	accounts->GetImapAccountL(imapService, imapAccount);						
       
   252  	accounts->SaveImapSettingsL(imapAccount, *settings);
       
   253 	CleanupStack::PopAndDestroy(3, settings); // accounts, settings, cmdLineArg
       
   254 	}
       
   255 
       
   256 void CImpsTestHarness::StartTestL()
       
   257 	{
       
   258 	testUtils->TestStart(1);
       
   259 	iSel = new (ELeave) CMsvEntrySelection;
       
   260 	iSel->AppendL(imapService);
       
   261 	TBuf8<128> parameter;
       
   262 	testUtils->WriteComment(_L("Calling KIMAP4MTMConnect ")); 
       
   263 	iCommand=KIMAP4MTMConnect;
       
   264 	imapServerMtm->StartCommandL(*iSel, iCommand, parameter, iStatus);
       
   265 	SetActive();
       
   266 	}
       
   267 
       
   268 void CImpsTestHarness::EndTestL()
       
   269 	{
       
   270 	testUtils->TestFinish(1, iTestError);
       
   271 	}
       
   272 
       
   273 
       
   274 void  CImpsTestHarness::DoCancel()
       
   275 	{
       
   276 	imapServerMtm->Cancel();
       
   277 	}
       
   278 
       
   279 void CImpsTestHarness::RunL()
       
   280 	{
       
   281 	TImap4CompoundProgress temp;	
       
   282 	TPckgC<TImap4CompoundProgress> paramPack(temp);
       
   283 
       
   284 	const TDesC8& progBuf = imapServerMtm->Progress();	
       
   285 	paramPack.Set(progBuf);
       
   286 	TImap4CompoundProgress progress=paramPack();	
       
   287 	TInt err=progress.iGenericProgress.iErrorCode;
       
   288 
       
   289 	if (iStatus.Int()!=KErrNone || iCommand==KIMAP4MTMDisconnect || err)
       
   290 		{
       
   291 		testUtils->TestFinish(1,err);
       
   292 		CActiveScheduler::Stop();
       
   293 		return;
       
   294 		}
       
   295 	if (iCommand==KIMAP4MTMConnect)
       
   296 		{
       
   297 		testUtils->WriteComment(_L("Calling KIMAP4MTMInboxNewSync ")); 
       
   298 		iCommand=KIMAP4MTMInboxNewSync;
       
   299 		TBuf8<128> parameter;
       
   300 		imapServerMtm->StartCommandL(*iSel, iCommand, parameter, iStatus);
       
   301 		SetActive();
       
   302 		return;
       
   303 		}
       
   304 
       
   305 	if (iCommand==KIMAP4MTMInboxNewSync)
       
   306 		{
       
   307 		iCommand=KIMAP4MTMDisconnect;
       
   308 		TBuf8<128> parameter;
       
   309 		imapServerMtm->StartCommandL(*iSel, iCommand, parameter, iStatus);
       
   310 		SetActive();
       
   311 		return;
       
   312 
       
   313 		}
       
   314 	}
       
   315 
       
   316 //
       
   317 
       
   318 LOCAL_C void Init()
       
   319 	{
       
   320 	scheduler = new (ELeave) CActiveScheduler;
       
   321 	CActiveScheduler::Install( scheduler );
       
   322 
       
   323 	User::LeaveIfError(theFs.Connect());
       
   324 	theFs.SetSessionPath(_L("C:\\"));
       
   325 	}
       
   326 	
       
   327 LOCAL_C void Closedown()
       
   328 	{
       
   329 	theFs.Close();
       
   330 	CleanupStack::PopAndDestroy(testUtils);
       
   331 	delete scheduler;
       
   332 	CSecureSocketLibraryLoader::Unload();
       
   333 	}
       
   334 
       
   335 
       
   336 LOCAL_C void doMainL()
       
   337 	{
       
   338 	Init();
       
   339 
       
   340 
       
   341 	// Load the serial comms device drivers.  If this is not done,
       
   342 	// connecting via NT-RAS returns KErrNotFound (-1).
       
   343 	TInt driverErr;
       
   344 	driverErr=User::LoadPhysicalDevice(PDD_NAME);
       
   345 	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
       
   346 		User::Leave(driverErr);
       
   347 	driverErr=User::LoadLogicalDevice(LDD_NAME);
       
   348 	if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists)
       
   349 		User::Leave(driverErr);
       
   350 
       
   351 
       
   352 	theFs.SetSessionPath(_L("c:\\"));
       
   353 	theFs.MkDir(_L("c:\\logs\\"));
       
   354 	theFs.MkDir(_L("c:\\logs\\email\\"));
       
   355 	testUtils = CEmailTestUtils::NewLC(test);
       
   356 	testUtils->WriteComment(_L("T_IMPSECURESOCKETS  - Testing IMPS secure sockets"));
       
   357 	testUtils->WriteComment(_L("Before you run this test harness, please create commdb dat file ")); 
       
   358 	testUtils->WriteComment(_L("with server details which supports TLS."));
       
   359 	CImpsTestHarness* impsTestHarness = CImpsTestHarness::NewLC();
       
   360 	impsTestHarness->StartTestL();
       
   361  	CActiveScheduler::Start();
       
   362 	testUtils->TestHarnessCompleted();
       
   363 	CleanupStack::PopAndDestroy(impsTestHarness); 
       
   364 	Closedown();
       
   365 	}
       
   366 
       
   367 
       
   368 GLDEF_C TInt E32Main()
       
   369 	{
       
   370 	test.Title();
       
   371 	test.Start(_L("Testing IMPS Secure Socket connection"));
       
   372 	__UHEAP_MARK;
       
   373 
       
   374 	theCleanup=CTrapCleanup::New();
       
   375 	test (theCleanup!=NULL);
       
   376 	TRAPD(ret,doMainL());		
       
   377 	test (ret==KErrNone);
       
   378 	delete theCleanup;
       
   379 
       
   380 	__UHEAP_MARKEND;
       
   381 	test.End();
       
   382 	return 0;
       
   383 	}
       
   384