email/pop3andsmtpmtm/servermtmutils/test/src/T_IMSK01.cpp
changeset 0 72b543305e3a
child 76 60a8a215b0ec
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1998-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 "testenv.h"
       
    17 #include <agentclient.h>
       
    18 #include "nd_err.h"
       
    19 
       
    20 #include "imsk.h"
       
    21 
       
    22 #include <s32mem.h>
       
    23 #include <s32file.h>
       
    24 #include <iapprefs.h>
       
    25 #include "EmailTestUtils.h"
       
    26 
       
    27 const TInt KMaxSizeOfBinaryData = 200;
       
    28 LOCAL_D RTest test(_L("IMSK CImTextServerSession Test"));
       
    29 LOCAL_D CTrapCleanup* theCleanup;
       
    30 LOCAL_D TBuf8<KMaxSizeOfBinaryData> someBinaryData=_L8("");
       
    31 
       
    32 LOCAL_D RFs theFs;	
       
    33 LOCAL_D CActiveScheduler* scheduler;
       
    34 
       
    35 LOCAL_D CEmailTestUtils* testUtils; 
       
    36 
       
    37 
       
    38 #define EActivePriorityLow		-1
       
    39 const TUid KUidMsvMessageDriveStream={0x1000163E};
       
    40 
       
    41 LOCAL_C void DisplayErrorReason(TInt& aReason)
       
    42 	{
       
    43 	if (aReason!=KErrNone)
       
    44 		{
       
    45 		testUtils->WriteComment(_L("\nERROR - "));
       
    46 		}
       
    47 
       
    48 	TBuf<64> logString;
       
    49 	switch (aReason)
       
    50 		{
       
    51 	case KErrNone:
       
    52 		testUtils->WriteComment(_L("Session completed successfully"));
       
    53 		break;
       
    54 	case KErrCancel:
       
    55 		testUtils->WriteComment(_L("Session cancelled"));
       
    56 		logString.Format(_L(" Session Cancelled %d"),KErrCancel);
       
    57 		testUtils->WriteComment(logString);
       
    58 //		log->AppendError(_L8("\t\tSession Cancelled"),KErrCancel);
       
    59 		break;
       
    60 	case KErrAlreadyExists:
       
    61 		testUtils->WriteComment(_L(" something Already Exists"));
       
    62 		logString.Format(_L(" something Already Exists %d"),KErrAlreadyExists);
       
    63 		testUtils->WriteComment(logString);
       
    64 //		log->AppendError(_L8("\t\tsomething Already Exists"),KErrAlreadyExists);
       
    65 		break;
       
    66 	case KErrEof:
       
    67 		testUtils->WriteComment(_L("No messages to send"));
       
    68 		logString.Format(_L(" No messages to send %d"),KErrEof);
       
    69 		testUtils->WriteComment(logString);
       
    70 //		log->AppendError(_L8("\t\tNo messages to send"),KErrEof);
       
    71 		break;
       
    72 	case KErrNoMemory:
       
    73 		testUtils->WriteComment(_L("No memory"));
       
    74 		logString.Format(_L(" No Memory %d"),KErrNoMemory);
       
    75 		testUtils->WriteComment(logString);
       
    76 //		log->AppendError(_L8("\t\tNo Memory"),KErrNoMemory);
       
    77 		break;
       
    78 	case KErrAccessDenied:
       
    79 		testUtils->WriteComment(_L("Switch link off"));
       
    80 		logString.Format(_L(" Switch link off %d"),KErrAccessDenied);
       
    81 		testUtils->WriteComment(logString);
       
    82 	//	log->AppendError(_L8("\t\tSwitch link off"),KErrAccessDenied);
       
    83 		break;
       
    84 	case KErrExitNoModem:
       
    85 		testUtils->WriteComment(_L("No modem"));
       
    86 		logString.Format(_L(" No Modem %d"),KErrExitNoModem);
       
    87 		testUtils->WriteComment(logString);
       
    88 //		log->AppendError(_L8("\t\tNo Modem"),KErrExitNoModem);
       
    89 		break;
       
    90 	case KErrExitLoginFail:
       
    91 		testUtils->WriteComment(_L("Login failed"));
       
    92 		logString.Format(_L(" Login Failed %d"),KErrExitLoginFail);
       
    93 		testUtils->WriteComment(logString);
       
    94 //		log->AppendError(_L8("\t\tLogin Failed"),KErrExitLoginFail);
       
    95 		break;
       
    96 	case KErrExitModemError:
       
    97 		testUtils->WriteComment(_L("Modem error"));
       
    98 		logString.Format(_L(" Modem Error %d"),KErrExitModemError);
       
    99 		testUtils->WriteComment(logString);
       
   100 //		log->AppendError(_L8("\t\tModem Error"),KErrExitModemError);
       
   101 		break;
       
   102 	case KErrExitScriptError:
       
   103 		testUtils->WriteComment(_L("Script error"));
       
   104 		logString.Format(_L(" Script Error %d"),KErrExitScriptError);
       
   105 		testUtils->WriteComment(logString);
       
   106 //		log->AppendError(_L8("\t\tScript Error"),KErrExitScriptError);
       
   107 		break;
       
   108 	case KImskErrorDNSNotFound:
       
   109 		testUtils->WriteComment(_L("Failed to find SMTP server"));
       
   110 		logString.Format(_L(" Failed to find SMTP server %d"),KImskErrorDNSNotFound);
       
   111 		testUtils->WriteComment(logString);
       
   112 //		log->AppendError(_L8("\t\tFailed to find SMTP server"),KImskErrorDNSNotFound);
       
   113 		break;
       
   114 	case KErrTimedOut:
       
   115 		testUtils->WriteComment(_L("Timed out waiting to communicate to server"));
       
   116 		logString.Format(_L(" Timed out waiting to communicate to server %d"),KErrTimedOut);
       
   117 		testUtils->WriteComment(logString);
       
   118 //		log->AppendError(_L8("\t\tTimed out waiting to communicate to server"),KErrTimedOut);
       
   119 		break;
       
   120 	case KErrDisconnected:
       
   121 		testUtils->WriteComment(_L("Server disconnected from socket"));
       
   122 		logString.Format(_L(" Server disconnected from socket %d"),KErrDisconnected);
       
   123 		testUtils->WriteComment(logString);
       
   124 //		log->AppendError(_L8("\t\tServer disconnected from socket"),KErrDisconnected);
       
   125 		break;
       
   126 	default:
       
   127 		// unknown error 
       
   128 //		testUtils->WriteComment(TRefByValue<const TDesC>_L("%d"),aReason);
       
   129 		logString.Format(_L(" Error %d"),aReason);
       
   130 		testUtils->WriteComment(logString);
       
   131 	//	log->AppendError(_L8("\t\tError"),aReason);
       
   132 		break;
       
   133 		}
       
   134 	}
       
   135 
       
   136 // create our own active scheduler class
       
   137 
       
   138 class CExampleScheduler : public CActiveScheduler
       
   139 	{
       
   140 public:
       
   141 	void Error (TInt aError) const;
       
   142 	};
       
   143 
       
   144 void CExampleScheduler::Error(TInt aError) const
       
   145 	{
       
   146 	DisplayErrorReason(aError);
       
   147 //	User::Panic(_L("CExampleScheduler-error"),aError);
       
   148 	}
       
   149 
       
   150 
       
   151 // create an active object to send the message
       
   152 
       
   153 class CSendMessage : public CActive
       
   154 	{
       
   155 public: // functions
       
   156 	// construct/destruct
       
   157 	CSendMessage();
       
   158 	static CSendMessage *NewL();
       
   159 	static CSendMessage *NewLC();
       
   160 	void ConstructL();
       
   161 	~CSendMessage();
       
   162 	// issue request
       
   163 	void RequestSendL();
       
   164 	void QueueNextReceive();
       
   165 	
       
   166 	void RunL();
       
   167 	void DoCancel();
       
   168 
       
   169 private:
       
   170 	CImTextServerSession*	iImSocket;
       
   171 	TInt					iCtr;
       
   172 	TStreamId				iId;
       
   173 	HBufC*					iBuf;
       
   174 	TInt					iCurrentState;
       
   175 	TInt					iLineStatus;
       
   176 	TBool					iReceive;
       
   177 	TSockXfrLength			iLen;
       
   178 	CImIAPPreferences*		iIAPPreferences;
       
   179 	};
       
   180 
       
   181 CSendMessage::CSendMessage() // construct high-priority active object
       
   182 	: CActive(EActivePriorityLow)
       
   183 	{
       
   184 	}
       
   185 
       
   186 CSendMessage  *CSendMessage::NewLC()
       
   187 	{
       
   188 	CSendMessage* self=new (ELeave) CSendMessage();
       
   189 	CleanupStack::PushL(self);
       
   190 	self->ConstructL();
       
   191 	return self;
       
   192 	}
       
   193 
       
   194 CSendMessage  *CSendMessage::NewL()
       
   195 	{
       
   196 	CSendMessage* self=NewLC();
       
   197 	CleanupStack::Pop();
       
   198 	return self;
       
   199 	}
       
   200 
       
   201 void CSendMessage::ConstructL()
       
   202 	{
       
   203 	iCtr=0;
       
   204 	CActiveScheduler::Add(this); // add to active scheduler
       
   205 	iImSocket= CImTextServerSession::NewL();
       
   206 	iReceive=EFalse;
       
   207 	}
       
   208 
       
   209 CSendMessage::~CSendMessage()
       
   210 	{ 
       
   211 	Cancel(); // make sure we're cancelled
       
   212 	delete iImSocket;
       
   213 	delete iBuf;
       
   214 	delete iIAPPreferences;
       
   215 	}
       
   216 
       
   217 void  CSendMessage::DoCancel()
       
   218 	{
       
   219 	iImSocket->Cancel();
       
   220 	}
       
   221 
       
   222 void  CSendMessage::RunL()
       
   223 	{
       
   224 	TBuf<64> logString;
       
   225 	TInt error;
       
   226 	TInt length=0;
       
   227 	switch (iCtr)
       
   228 		{
       
   229 	case 0:	// iSocket.Connect()
       
   230 		if(iStatus.Int()!=KErrNone)
       
   231 			{
       
   232 			error=iStatus.Int();
       
   233 			DisplayErrorReason(error);
       
   234 			CActiveScheduler::Stop();
       
   235 			}
       
   236 		else
       
   237 			{	
       
   238 			iReceive=ETrue;
       
   239 			QueueNextReceive();
       
   240 			iCtr++;
       
   241 			}
       
   242 		break;
       
   243 
       
   244 	case 4:	// have set encryption
       
   245 		iReceive=EFalse;
       
   246 		RequestSendL();
       
   247 		iCtr++;
       
   248 		break;
       
   249 
       
   250 	case 3:
       
   251 		iCtr++;
       
   252 	case 1:	// response from sending user name
       
   253 	case 2:	// response from sending password
       
   254 	//case 3:
       
   255 	case 5:
       
   256 		someBinaryData=_L8("");
       
   257 		if(iStatus.Int()!=KErrNone)
       
   258 			{
       
   259 //			testUtils->WriteComment(TRefByValue<const TDesC>_L("Error during receipt %d"),iStatus.Int());
       
   260 			logString.Format(_L(" Error during receipt%d"),iStatus.Int());
       
   261 			testUtils->WriteComment(logString);
       
   262 	//		log->AppendError(_L8("\t\tError during receipt"),iStatus.Int());
       
   263 			CActiveScheduler::Stop();
       
   264 			}
       
   265 		else
       
   266 			{
       
   267 			TBuf8<40> buffer;
       
   268 			TBuf<40> buffer16;
       
   269 			iLineStatus=iImSocket->GetCurrentTextLine(buffer);
       
   270 			buffer16.Copy(buffer);
       
   271 			test.Printf(TRefByValue<const TDesC>_L("%S"),&buffer16);
       
   272 			if(iReceive)
       
   273 				{
       
   274 				if(iLineStatus==EBufferTooSmall)
       
   275 					{
       
   276 					while(iLineStatus!=ECRLFTerminated)
       
   277 						{
       
   278 						iLineStatus=iImSocket->GetCurrentTextLine(buffer);
       
   279 						test.Printf(TRefByValue<const TDesC>_L("%S"),&buffer);
       
   280 						}
       
   281 					}
       
   282 				iReceive=EFalse;
       
   283 				RequestSendL();
       
   284 				iCtr++;
       
   285 				}
       
   286 			else
       
   287 				{
       
   288 				iReceive=ETrue;
       
   289  				QueueNextReceive();
       
   290 				}
       
   291 			}
       
   292 		break;
       
   293 
       
   294 	case 6:	// test BinaryDataReceive()
       
   295 		if(iStatus.Int()!=KErrNone)
       
   296 			{
       
   297 			test.Printf(TRefByValue<const TDesC>_L("Error during receipt %d"),iStatus.Int());
       
   298 			logString.Format(_L(" Error during receipt%d"),iStatus.Int());
       
   299 			testUtils->WriteComment(logString);
       
   300 //			log->AppendError(_L8("\t\tError during receipt"),iStatus.Int());
       
   301 			CActiveScheduler::Stop();
       
   302 			break;
       
   303 			}
       
   304 
       
   305 		length=someBinaryData.Length();
       
   306 		if (length>2)
       
   307 			{
       
   308 			TBuf<KMaxSizeOfBinaryData> buffer16;
       
   309 			buffer16.Copy(someBinaryData);
       
   310 			test.Printf(TRefByValue<const TDesC>_L("%S"),&buffer16);
       
   311 			if ((someBinaryData[length-3]=='.' && someBinaryData[length-2]==0x0D && someBinaryData[length-1]==0x0A)
       
   312 				||(someBinaryData[length-3]=='-' && someBinaryData[length-2]=='E' && someBinaryData[length-1]=='R'))
       
   313 				{
       
   314 				// got ".CRLF" string (-ERR string) from LIST command, so go onto next state
       
   315 				iReceive=EFalse;
       
   316 				RequestSendL();
       
   317 				iCtr++;
       
   318 				break;
       
   319 				}
       
   320 			}
       
   321 		// queue a read
       
   322 		iReceive=ETrue;
       
   323 		iImSocket->ReceiveBinaryData(iStatus,someBinaryData,someBinaryData.MaxLength());
       
   324 		SetActive();
       
   325 		break;
       
   326 
       
   327 	default:
       
   328 		iCtr=6;
       
   329 		RequestSendL();
       
   330 		break;
       
   331 	case 7:
       
   332 		iImSocket->LogError(_L8("\t\tEnd of POP3 test session; completion code="),iStatus.Int());
       
   333 		TBuf8<KImMailMaxBufferSize> buffer;
       
   334 		iLineStatus=iImSocket->GetCurrentTextLine(buffer);
       
   335 		test.Printf(TRefByValue<const TDesC>_L("Final %S\n "),&buffer);
       
   336 		CActiveScheduler::Stop();
       
   337 		TInt error=iStatus.Int();
       
   338 		DisplayErrorReason(error);
       
   339 		if (error==KErrNone)
       
   340 			testUtils->WriteComment(_L("PASSED test"));
       
   341 		else
       
   342 			testUtils->WriteComment(_L("FAILED test"));
       
   343 		break;
       
   344 		} // end of switch
       
   345 	}
       
   346 
       
   347 
       
   348 void CSendMessage::RequestSendL()
       
   349 	{
       
   350 	_LIT8(KImUser,"USER abf75\r\n");
       
   351 	_LIT8(KImPass,"PASS oodifama\r\n");
       
   352 	_LIT8(KImStat,"STAT\r\n");
       
   353 	_LIT8(KImList,"LIST\r\n");
       
   354 	_LIT8(KImQuit,"QUIT\r\n");
       
   355 
       
   356 	TRequestStatus* p=&iStatus;
       
   357 	switch(iCtr)
       
   358 		{
       
   359 	case 0:
       
   360 		test.Next(_L("QueueConnect"));
       
   361 		delete iIAPPreferences;
       
   362 		iIAPPreferences = 0;
       
   363 		iIAPPreferences = CImIAPPreferences::NewLC();
       
   364 		CleanupStack::Pop(); // iIAPPrefernces
       
   365 		// Add IAP's to iIAPPreferences
       
   366 		TImIAPChoice iap;
       
   367 		iap.iIAP = 3;
       
   368 		iap.iDialogPref = ECommDbDialogPrefPrompt;
       
   369 //		iIAPPreferences->AddIAPL(iap,0);
       
   370 		iap.iIAP = 3;
       
   371 		iap.iDialogPref = ECommDbDialogPrefPrompt;
       
   372 //		iIAPPreferences->AddIAPL(iap,1);
       
   373 		iImSocket->QueueConnectL(iStatus,_L("158.43.128.67"),110,*iIAPPreferences);
       
   374 //		iImSocket->QueueConnectL(iStatus,_L("0:0:0:0:0:0:194.217.242.23"),110,*iIAPPreferences);
       
   375 		test.Next(_L("Connect Queued"));
       
   376 		testUtils->WriteComment(_L("Connect Queued"));
       
   377 		break;
       
   378 	case 1:
       
   379 //        iImSocket->LogText(_L8("First IAP should fail (scripted)"));
       
   380  //       iImSocket->LogText(_L8("Second IAP should fail (iap 30 doesn't exist)"));
       
   381   //      iImSocket->LogText(_L8("Third should work iap 3 demon"));
       
   382         testUtils->WriteComment(_L("IP address of client is "));
       
   383         testUtils->WriteComment(iImSocket->LocalName());
       
   384 //        test.Printf(_L("\n"));
       
   385 
       
   386 		TUint32 convalue;
       
   387 		TInt err;
       
   388 		err=iImSocket->GetIAPValue(convalue);
       
   389 		if(err==KErrNone)
       
   390 			test.Printf(_L("GetIAPValue returned %u\n"),convalue);
       
   391 		else 
       
   392 			test.Printf(_L("GetIAPValue error %d\n"),err);
       
   393 		err=iImSocket->GetIAPBearer(convalue);
       
   394 		if(err==KErrNone)
       
   395 			test.Printf(_L("GetIAPBearer returned %u\n"),convalue);
       
   396 		else 
       
   397 			test.Printf(_L("GetIAPBearer error %d\n"),err);
       
   398 		iImSocket->Send(iStatus,KImUser());
       
   399 		testUtils->WriteComment(_L("\tSent Message USER abf75"));
       
   400 		break;
       
   401 	case 2:
       
   402 		iImSocket->Send(iStatus,KImPass());
       
   403 		testUtils->WriteComment(_L("\tSent Message PASS oodifama"));
       
   404 		break;
       
   405 	case 3:
       
   406 /*		iImSocket->SetSecurity(EImskEncryptionTLS1);
       
   407 		iImSocket->LogText(_L8("**************************************"));
       
   408 		iImSocket->LogText(_L8("Testing the logging code"));
       
   409 		iImSocket->LogText(_L8("You can add your own logging text to this log file"));
       
   410 		iImSocket->LogText(_L8("**************************************"));
       
   411 */		User::RequestComplete(p,KErrNone);
       
   412 		break;
       
   413 	case 4:
       
   414 		iImSocket->Send(iStatus,KImStat());
       
   415 		testUtils->WriteComment(_L("\tSent Message STAT"));
       
   416 		break;
       
   417 	case 5:
       
   418 		iImSocket->Send(iStatus,KImList());
       
   419 		testUtils->WriteComment(_L("\tSent Message LIST"));
       
   420 		break;
       
   421 	case 6:
       
   422 		iImSocket->Send(iStatus,KImQuit());
       
   423 		testUtils->WriteComment(_L("\tSent Message QUIT"));
       
   424 		break;
       
   425 		}
       
   426 	
       
   427 	SetActive();
       
   428 	}
       
   429 
       
   430 void CSendMessage::QueueNextReceive()
       
   431 	{
       
   432 	iImSocket->QueueReceiveNextTextLine(iStatus);
       
   433 	SetActive();
       
   434 	}
       
   435 
       
   436 //
       
   437 
       
   438 LOCAL_C void Init()
       
   439 	{
       
   440 	scheduler = new (ELeave) CActiveScheduler;
       
   441 	CActiveScheduler::Install( scheduler );
       
   442 
       
   443 	User::LeaveIfError(theFs.Connect());
       
   444 	theFs.SetSessionPath(_L("C:\\"));
       
   445 	}
       
   446 	
       
   447 LOCAL_C void Closedown()
       
   448 	{
       
   449 	theFs.Close( );
       
   450 	delete scheduler;
       
   451 	}
       
   452 
       
   453 //
       
   454 
       
   455 LOCAL_C void doMainL()
       
   456 	{
       
   457 	Init();
       
   458 	theFs.SetSessionPath(_L("c:\\"));
       
   459 	theFs.MkDir(_L("c:\\logs\\"));
       
   460 	theFs.MkDir(_L("c:\\logs\\email\\"));
       
   461 	testUtils = CEmailTestUtils::NewLC(test);
       
   462 	testUtils->WriteComment(_L("T_IMSK01 Testing Imail Text Socket"));
       
   463 	testUtils->WriteComment(_L("Please make sure that pipex is the default IAP."));
       
   464 
       
   465 //	log=CImLog::NewL(_L("c:\\logs\\email\\T_IMSK.log"),EAppend);
       
   466 //	CleanupStack::PushL(log);
       
   467 
       
   468 //	log->AppendComment(_L8("********** T_IMSK01 Testing Imail Text Socket **********"));
       
   469 //	log->AppendComment(_L8("\tInit() Completed"));
       
   470 
       
   471 	// Set up the timeout in the comms database as it is needed by imsk.
       
   472 	TUint32 timeoutInMts = 5; // ( five minutes )
       
   473 	
       
   474 	
       
   475 
       
   476 //	TBuf<64> logString;
       
   477 //	logString.Format(_L(" IAP is %u"),IAP);
       
   478 //	testUtils->WriteComment(logString);
       
   479 
       
   480 	CSendMessage *aSendMessage = CSendMessage::NewLC();
       
   481 	aSendMessage->RequestSendL();
       
   482 
       
   483  	CActiveScheduler::Start();
       
   484 
       
   485 	testUtils->WriteComment(_L("**********       T_IMSK Tests Complete       **********"));
       
   486 	testUtils->TestHarnessCompleted();
       
   487 
       
   488 	CleanupStack::PopAndDestroy(aSendMessage);
       
   489 	CleanupStack::PopAndDestroy(testUtils);
       
   490 	Closedown();
       
   491 	}
       
   492 
       
   493 
       
   494 GLDEF_C TInt E32Main()
       
   495 	{
       
   496 	test.Title();
       
   497 	test.Start(_L("Testing Imail Text Socket"));
       
   498 	__UHEAP_MARK;
       
   499 
       
   500 	theCleanup=CTrapCleanup::New();
       
   501 	test (theCleanup!=NULL);
       
   502 	TRAPD(ret,doMainL());		
       
   503 	test (ret==KErrNone);
       
   504 	delete theCleanup;
       
   505 
       
   506 	__UHEAP_MARKEND;
       
   507 	test.End();
       
   508 	return 0;
       
   509 	}