email/pop3andsmtpmtm/servermtmutils/test/src/T_SECURESOCKETS.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 of IMSK using POP commands
       
    16 // Input files required to run test harness:
       
    17 // The following files should be copied to <drive>:\msgtest\imut\
       
    18 // scriptwithSSLTLS.scr
       
    19 // scriptwitoutSSLTLS.scr
       
    20 // Output files produced by running test harness:
       
    21 // <DRIVE>:\msglogs\T_SECURESOCKETS.<PLATFORM>.<VARIANT>.LOG.txt
       
    22 // Description of how to build test harness:
       
    23 // cd \messaging\msg\imut\
       
    24 // bldmake bldfiles
       
    25 // abld test build t_SECURESOCKETS
       
    26 // Description of how to run test harness:
       
    27 // The following instructions are the same for all platforms:
       
    28 // 2. Build the test utilities:
       
    29 // cd \messaging\msg\testutils\group\
       
    30 // bldmake bldfiles
       
    31 // abld build
       
    32 // WINS running instructions:
       
    33 // 1. \epoc32\release\wins\<VARIANT>\T_SECURESOCKETS.exe can be used at the command prompt
       
    34 // or executable can be run from Windows Explorer.
       
    35 // All other platform running instructions:
       
    36 // 1. Copy \epoc32\release\<PLATFORM>\<VARIANT>\T_SECURESOCKETS.exe onto the other platform
       
    37 // 2. Copy \epoc32\release\<PLATFORM>\<VARIANT>\MSVTESTUTILS.DLL into 
       
    38 // <DRIVE>:\system\libs on the other platform
       
    39 // 3. Copy \epoc32\release\<PLATFORM>\<VARIANT>\EMAILTESTUTILS.DLL into 
       
    40 // <DRIVE>:\system\libs on the other platform
       
    41 // 4. Copy \epoc32\wins\c\msgtest\imut\*.scr to
       
    42 // <DRIVE>:\msgtest\imut\
       
    43 // Note that this drive should be a CF card as the test files are large
       
    44 // 5. Run T_SECURESOCKETS.exe on the other platform with the CF card
       
    45 // 
       
    46 //
       
    47 
       
    48 #include <imsk.h>
       
    49 
       
    50 #include <s32mem.h>
       
    51 #include <s32file.h>
       
    52 #include <iapprefs.h>
       
    53 
       
    54 #include "emailtestutils.h"
       
    55 
       
    56 TInt EBufferNotTerminated= -9999;
       
    57 LOCAL_D RTest test(_L("CImTextServerSession SSL/TLS Test using scripts"));
       
    58 LOCAL_D CTrapCleanup* theCleanup;
       
    59 
       
    60 LOCAL_D RFs theFs;	
       
    61 LOCAL_D CActiveScheduler* scheduler;
       
    62 LOCAL_D CEmailTestUtils* testUtils; 
       
    63 
       
    64 _LIT(T_SSLSCRIPT, "scriptwithSSLTLS.scr");
       
    65 _LIT(T_WITOUTSSLSCRIPT, "scriptwithoutSSLTLS.scr");
       
    66 
       
    67 #define EActivePriorityLow		-1
       
    68 
       
    69 // create an active object to send the message
       
    70 
       
    71 class CSendMessage : public CActive
       
    72 	{
       
    73 public: // functions
       
    74 	// construct/destruct
       
    75 	CSendMessage();
       
    76 	static CSendMessage *NewL();
       
    77 	static CSendMessage *NewLC();
       
    78 	void ConstructL();
       
    79 	~CSendMessage();
       
    80 	void ChangeStateL();	
       
    81 	void RunL();
       
    82 	void DoCancel();
       
    83 	void SetTestNumber(TInt aTestNo);
       
    84 
       
    85 private:
       
    86 	CImTextServerSession*	iImSocket;
       
    87 	TInt					iSessionState;
       
    88 	CImIAPPreferences*		iPrefs;
       
    89 	TInt iTestNo;
       
    90 	TBuf8<512>	iReceive;
       
    91 
       
    92 private:
       
    93 	enum				// States of "session" state machine
       
    94 		{
       
    95 		EConnecting,
       
    96 		EWaitingForReply,
       
    97 		ERequestingTLS,
       
    98 		EAuthorisingUser,
       
    99 		EAuthorisingPass,
       
   100 		ESelectInbox,
       
   101 		EStat,
       
   102 		EDisconnect
       
   103 		};
       
   104 
       
   105 	};
       
   106 
       
   107 CSendMessage::CSendMessage() // construct high-priority active object
       
   108 	: CActive(EActivePriorityLow)
       
   109 	{
       
   110 	}
       
   111 
       
   112 CSendMessage  *CSendMessage::NewLC()
       
   113 	{
       
   114 	CSendMessage* self=new (ELeave) CSendMessage();
       
   115 	CleanupStack::PushL(self);
       
   116 	self->ConstructL();
       
   117 	return self;
       
   118 	}
       
   119 
       
   120 CSendMessage  *CSendMessage::NewL()
       
   121 	{
       
   122 	CSendMessage* self=NewLC();
       
   123 	CleanupStack::Pop();
       
   124 	return self;
       
   125 	}
       
   126 
       
   127 void CSendMessage::ConstructL()
       
   128 	{
       
   129 	CActiveScheduler::Add(this); // add to active scheduler
       
   130 	iImSocket= CImTextServerSession::NewL();
       
   131 	iSessionState=EConnecting;
       
   132 	}
       
   133 
       
   134 CSendMessage::~CSendMessage()
       
   135 	{ 
       
   136 	Cancel(); // make sure we're cancelled
       
   137 	delete iPrefs;
       
   138 	delete iImSocket;
       
   139 	}
       
   140 
       
   141 void CSendMessage::SetTestNumber(TInt aTestNo)
       
   142 	{iTestNo=aTestNo;}
       
   143 
       
   144 void  CSendMessage::DoCancel()
       
   145 	{
       
   146 	iImSocket->Cancel();
       
   147 	}
       
   148 
       
   149 
       
   150 void CSendMessage::RunL()
       
   151 	{
       
   152 	if (iStatus.Int()!=KErrNone)
       
   153 		{
       
   154 		if (iStatus.Int()==KImskSSLTLSNegotiateFailed)
       
   155 			testUtils->TestFinish(iTestNo,KErrNone);
       
   156 		else
       
   157 			testUtils->TestFinish(iTestNo,iStatus.Int());
       
   158 		CActiveScheduler::Stop();
       
   159 		return;
       
   160 		}
       
   161 	switch (iSessionState)
       
   162 	   {
       
   163 	case EConnecting:
       
   164 		iSessionState=EWaitingForReply;
       
   165 		break;
       
   166 	case EWaitingForReply:
       
   167 		iSessionState=ERequestingTLS;
       
   168 		break;
       
   169 	case ERequestingTLS:
       
   170 		iSessionState=EAuthorisingUser;
       
   171 		break;
       
   172 	case EAuthorisingUser:
       
   173 		iSessionState=EAuthorisingPass;
       
   174 		break;
       
   175 	case EAuthorisingPass:
       
   176 		iSessionState=EStat;
       
   177 		break;
       
   178 	case EDisconnect:
       
   179 		testUtils->TestFinish(iTestNo,iStatus.Int());
       
   180 		CActiveScheduler::Stop();
       
   181 		return;
       
   182 	default:
       
   183 		iSessionState=EDisconnect;
       
   184 		break;
       
   185 	   };
       
   186 	ChangeStateL();
       
   187 	}
       
   188 
       
   189 
       
   190 void CSendMessage::ChangeStateL()
       
   191 	{
       
   192    switch (iSessionState)
       
   193 	   {
       
   194 	case EConnecting:
       
   195 		iPrefs = CImIAPPreferences::NewLC();
       
   196 		CleanupStack::Pop(iPrefs);
       
   197 		iImSocket->QueueConnectL(iStatus,_L("abc.def.uk"),110,*iPrefs);
       
   198 		break;
       
   199 	case EWaitingForReply:
       
   200 		iImSocket->QueueReceiveNextTextLine(iStatus);
       
   201 		break;		
       
   202 	case ERequestingTLS:
       
   203 		iImSocket->SetSSLTLSResponseL(_L8("+OK"));
       
   204 		iImSocket->SendQueueReceive(iStatus,_L8("STLS\r\n"));
       
   205 		break;
       
   206 	case EAuthorisingUser:
       
   207 		iImSocket->SendQueueReceive(iStatus, _L8("USER abcd\r\n"));
       
   208 		break;
       
   209 	case EAuthorisingPass:
       
   210 		iImSocket->SendQueueReceive(iStatus,_L8("PASS efgh\r\n"));
       
   211 		break;
       
   212 	case EStat:
       
   213 		iImSocket->SendQueueReceive(iStatus,_L8("STAT\r\n"));
       
   214 		break;
       
   215 	case EDisconnect:
       
   216 		iImSocket->SendQueueReceive(iStatus,_L8("QUIT\r\n"));
       
   217 		break;
       
   218 	default: 	
       
   219 		iImSocket->Disconnect();
       
   220 		break;
       
   221 		}
       
   222 	SetActive();
       
   223 	}
       
   224 
       
   225 
       
   226 LOCAL_C void CopyScriptFileL(const TDesC& aScriptFile,TInt aPortNo)
       
   227 	{
       
   228 	TParse parsedScriptFile;
       
   229 	parsedScriptFile.Set(aScriptFile,0,0);
       
   230 	if (!parsedScriptFile.DrivePresent())
       
   231 		// This isn't a full path name so we need to resolve it
       
   232 		{
       
   233 		testUtils->ResolveFile(_L("IMUT"), aScriptFile, parsedScriptFile);
       
   234 		}
       
   235 	if (aPortNo==143)
       
   236 		testUtils->CopyScriptFileL(parsedScriptFile.FullName(), _L("143"));
       
   237 	else if (aPortNo==110)
       
   238 		testUtils->CopyScriptFileL(parsedScriptFile.FullName(), _L("110"));
       
   239 	else
       
   240 		testUtils->CopyScriptFileL(parsedScriptFile.FullName(), _L("25"));
       
   241 	}
       
   242 
       
   243 //
       
   244 
       
   245 LOCAL_C void Init()
       
   246 	{
       
   247 	scheduler = new (ELeave) CActiveScheduler;
       
   248 	CActiveScheduler::Install( scheduler );
       
   249 
       
   250 	User::LeaveIfError(theFs.Connect());
       
   251 	theFs.SetSessionPath(_L("C:\\"));
       
   252 	}
       
   253 	
       
   254 LOCAL_C void Closedown()
       
   255 	{
       
   256 	theFs.Close();
       
   257 	CleanupStack::PopAndDestroy(testUtils);
       
   258 	delete scheduler;
       
   259 	}
       
   260 
       
   261 
       
   262 
       
   263 //
       
   264 
       
   265 LOCAL_C void doMainL()
       
   266 	{
       
   267 	Init();
       
   268 	theFs.SetSessionPath(_L("c:\\"));
       
   269 	theFs.MkDir(_L("c:\\logs\\"));
       
   270 	theFs.MkDir(_L("c:\\logs\\email\\"));
       
   271 	testUtils = CEmailTestUtils::NewLC(test);
       
   272 	testUtils->WriteComment(_L("T_SECURESOCKETS Testing CImTextServerSesion secure socket"));
       
   273 
       
   274 	CSendMessage *sendMessage = CSendMessage::NewLC();
       
   275 	testUtils->TestStart(1,_L("testing for pop using failed secure sockets"));
       
   276 	CopyScriptFileL(T_WITOUTSSLSCRIPT,110);
       
   277 	sendMessage->SetTestNumber(1);
       
   278 	sendMessage->ChangeStateL();
       
   279  	CActiveScheduler::Start();
       
   280 	CleanupStack::PopAndDestroy(sendMessage);
       
   281 	sendMessage=NULL;
       
   282 
       
   283 	sendMessage = CSendMessage::NewLC();
       
   284 	testUtils->TestStart(2,_L("testing for pop using secure sockets"));
       
   285 	CopyScriptFileL(T_SSLSCRIPT,110);
       
   286 	sendMessage->SetTestNumber(2);
       
   287 	sendMessage->ChangeStateL();
       
   288  	CActiveScheduler::Start();
       
   289 	CleanupStack::PopAndDestroy(sendMessage);
       
   290 
       
   291 	testUtils->WriteComment(_L("**********       T_SECURESOCKETS Tests Complete       **********"));
       
   292 	testUtils->TestHarnessCompleted();
       
   293 
       
   294 	Closedown();
       
   295 	}
       
   296 
       
   297 
       
   298 GLDEF_C TInt E32Main()
       
   299 	{
       
   300 	test.Title();
       
   301 	test.Start(_L("Testing CImTextServerSession Secure Sockets"));
       
   302 	__UHEAP_MARK;
       
   303 
       
   304 	theCleanup=CTrapCleanup::New();
       
   305 	test (theCleanup!=NULL);
       
   306 	TRAPD(ret,doMainL());		
       
   307 	test (ret==KErrNone);
       
   308 	delete theCleanup;
       
   309 
       
   310 	__UHEAP_MARKEND;
       
   311 	test.End();
       
   312 	return 0;
       
   313 	}