messagingfw/msgtestfw/TestActions/Drm/src/CMtfTestActionSmtpSendEmail.cpp
changeset 0 8e480a14352b
child 58 6c34d0baa0b1
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2004-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 // __ACTION_INFO_BEGIN__ 
       
    16 // [Action Name]
       
    17 // DrmSendEmail
       
    18 // [Action Parameters]
       
    19 // sender			input			HBufC8, user name of email sender
       
    20 // recipient		input			HBufC8, user name of email recipient
       
    21 // mailFile			input			HBufC, path and name of the meta data file to check
       
    22 // serverIP 		input			HBufC, IP address of SMTP server
       
    23 // [Action Description]
       
    24 // This test action will send an email from <sender> to <recipient> with the contents
       
    25 // of <mailfile> as message body using the SMTP server specified by <serverIP>.
       
    26 // [APIs Used]	
       
    27 // RFile::Open
       
    28 // RFile::Read
       
    29 // CImTextServerSession::QueueConnectL
       
    30 // CImTextServerSession::Send
       
    31 // CImTextServerSession::QueueReceiveNextTextLine
       
    32 // CImTextServerSession::GetCurrentTextLine
       
    33 // CImTextServerSession::Disconnect
       
    34 // CImIAPPreferences::NewLC
       
    35 // __ACTION_INFO_END__
       
    36 //
       
    37 
       
    38 //! @file
       
    39 
       
    40 #include "CMtfTestActionSmtpSendEmail.h"
       
    41 
       
    42 #include "CMtfTestCase.h"
       
    43 #include "CMtfTestActionParameters.h"
       
    44 #include "CMtfTestActionUtilsConfigFileMachineName.h"
       
    45 
       
    46 #include <iapprefs.h>
       
    47 
       
    48 
       
    49 _LIT8(KHeloString, "HELO\r\n");
       
    50 _LIT8(KMailFrom, "MAIL FROM:");
       
    51 _LIT8(KRcptTo, "RCPT TO:");
       
    52 _LIT8(KDataString, "DATA\r\n");
       
    53 _LIT8(KEndData, "\r\n.\r\n");
       
    54 _LIT8(KQuitString, "QUIT\r\n");
       
    55 _LIT8(KCrlfString, "\r\n");
       
    56 
       
    57 /**
       
    58   Function : NewL
       
    59   Description : 
       
    60   @internalTechnology
       
    61   @param : aTestCase - CMtfTestCase for the CMtfTestAction base class
       
    62   @param : aActionParams - CMtfTestActionParameters 
       
    63   @return : CMtfTestAction* - a base class pointer to the newly created CMtfTestActionSmtpSendEmail object
       
    64   @pre none
       
    65   @post none
       
    66 */
       
    67 CMtfTestAction* CMtfTestActionSmtpSendEmail::NewL(CMtfTestCase& aTestCase,CMtfTestActionParameters* aActionParameters)
       
    68 {
       
    69 	CMtfTestActionSmtpSendEmail* self = new (ELeave) CMtfTestActionSmtpSendEmail(aTestCase);
       
    70 	CleanupStack::PushL(self);
       
    71 	self->ConstructL(aActionParameters);
       
    72 	CleanupStack::Pop(self);
       
    73 	return self;
       
    74 }
       
    75 
       
    76 /**
       
    77   Function : CMtfTestActionSmtpSendEmail
       
    78   Description : Constructor
       
    79   @internalTechnology
       
    80   @param : aTestCase - CMtfTestCase for the CMtfTestAction base class
       
    81   @return : N/A
       
    82   @pre none
       
    83   @post none
       
    84 */
       
    85 CMtfTestActionSmtpSendEmail::CMtfTestActionSmtpSendEmail(CMtfTestCase& aTestCase)
       
    86  : CMtfTestAction(aTestCase)
       
    87 {
       
    88 }
       
    89 	
       
    90 /**
       
    91   Function : ~CMtfTestActionSmtpSendEmail
       
    92   Description : Destructor
       
    93   @internalTechnology
       
    94   @param :
       
    95   @return : 
       
    96   @pre 
       
    97   @post 
       
    98 */
       
    99 CMtfTestActionSmtpSendEmail::~CMtfTestActionSmtpSendEmail()
       
   100 {
       
   101 	iFile.Close();
       
   102 	iFs.Close();
       
   103 	delete iImSocket;
       
   104 	delete iIapPrefs;
       
   105 }
       
   106 
       
   107 /**
       
   108   Function : ExecuteActionL
       
   109   Description : Entry point for the this test action in the test framework
       
   110   @internalTechnology
       
   111   @param : none
       
   112   @return : void
       
   113   @pre none 
       
   114   @post none
       
   115 */
       
   116 void CMtfTestActionSmtpSendEmail::ExecuteActionL()
       
   117 {
       
   118 	TestCase().INFO_PRINTF2(_L("Test Action %S start..."), &KTestActionSmtpSendEmail);
       
   119 	CActiveScheduler::Add(this);
       
   120 	HBufC* serverIP = ObtainParameterReferenceL<HBufC>(TestCase(), ActionParameters().Parameter(3));
       
   121 
       
   122 	HBufC* emailAddressExt = ObtainParameterReferenceL<HBufC>(TestCase(), ActionParameters().Parameter(4), (*serverIP).Alloc());
       
   123 	
       
   124 	CMtfTestActionUtilsConfigFileMachineName* machineNameFile = CMtfTestActionUtilsConfigFileMachineName::NewLC(*emailAddressExt);
       
   125 
       
   126 	TPtrC machineNameEmail(machineNameFile->MachineNameEmail());
       
   127 	iSender = ObtainParameterReferenceL<HBufC>(TestCase(), ActionParameters().Parameter(0), machineNameEmail.Alloc());
       
   128 	iRecipient = ObtainParameterReferenceL<HBufC>(TestCase(), ActionParameters().Parameter(1), machineNameEmail.Alloc());
       
   129 	
       
   130 	HBufC* mailFile = ObtainParameterReferenceL<HBufC>(TestCase(), ActionParameters().Parameter(2));
       
   131 	
       
   132 	iFs.Connect();
       
   133 	iFile.Open(iFs, *mailFile, EFileShareAny|EFileRead);
       
   134 	
       
   135 	iImSocket = CImTextServerSession::NewL();
       
   136 	iIapPrefs = CImIAPPreferences::NewLC();
       
   137 	CleanupStack::Pop();
       
   138 	
       
   139 	iImSocket->QueueConnectL(iStatus, serverIP->Des(), 25, *iIapPrefs, ETrue);
       
   140 	
       
   141 	CleanupStack::PopAndDestroy(machineNameFile);
       
   142 	
       
   143 	iSendState = ESmtpConnecting;
       
   144 	SetActive();
       
   145 	TestCase().INFO_PRINTF2(_L("Test Action %S completed."), &KTestActionSmtpSendEmail);
       
   146 }
       
   147 
       
   148 /**
       
   149   Function : DoCancel
       
   150   Description : CActive derived function to handle Cancel requests
       
   151   @internalTechnology
       
   152   @param : none
       
   153   @return : void
       
   154   @pre none 
       
   155   @post none
       
   156 */
       
   157 void CMtfTestActionSmtpSendEmail::DoCancel()
       
   158 {
       
   159 	iImSocket->Cancel();
       
   160 }
       
   161 
       
   162 /**
       
   163   Function : RunL
       
   164   Description : CActive derived function to handle the state machine
       
   165   @internalTechnology
       
   166   @param : none
       
   167   @return : void
       
   168   @pre none 
       
   169   @post none
       
   170 */
       
   171 void CMtfTestActionSmtpSendEmail::RunL()
       
   172 {
       
   173 	// handle send state
       
   174 	switch (iSendState)
       
   175 	{
       
   176 		case ESmtpNotConnected:
       
   177 		{	// ok, we're done. send completed.
       
   178 			TestCase().ActionCompletedL(*this);
       
   179 			break;
       
   180 		}
       
   181 		case ESmtpConnecting:
       
   182 		{	// connection attempt completed.
       
   183 			if (iStatus != KErrNone)
       
   184 			{
       
   185 				// error
       
   186 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to connect: [%d]."), iStatus.Int());
       
   187 				TestCase().SetTestStepResult(EFail);
       
   188 				User::Leave(iStatus.Int());
       
   189 			}
       
   190 			
       
   191 			// ok successful connect, next send HELO
       
   192 			iSendState = ESmtpSendHELO;
       
   193 			iImSocket->Send(iStatus, KHeloString());
       
   194 			SetActive();
       
   195 			break;
       
   196 		}
       
   197 		case ESmtpSendHELO:
       
   198 		{	// HELO was sent
       
   199 			if (iStatus != KErrNone)
       
   200 			{
       
   201 				// error
       
   202 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to send HELO: [%d]."), iStatus.Int());
       
   203 				TestCase().SetTestStepResult(EFail);
       
   204 				User::Leave(iStatus.Int());
       
   205 			}
       
   206 			
       
   207 			// ok we sent HELO, now receive reply
       
   208 			iSendState = ESmtpReceiveReply;
       
   209 			iRecvState = ESmtpHeloReply;
       
   210 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   211 			SetActive();
       
   212 			break;
       
   213 		}
       
   214 		case ESmtpSendMailFrom:
       
   215 		{	// MAIL FROM was sent.
       
   216 			if (iStatus != KErrNone)
       
   217 			{
       
   218 				// error
       
   219 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to send MAIL FROM: [%d]."), iStatus.Int());
       
   220 				TestCase().SetTestStepResult(EFail);
       
   221 				User::Leave(iStatus.Int());
       
   222 			}
       
   223 			
       
   224 			// ok we sent MAIL FROM, now receive reply
       
   225 			iSendState = ESmtpReceiveReply;
       
   226 			iRecvState = ESmtpFromReply;
       
   227 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   228 			SetActive();
       
   229 			break;
       
   230 		}
       
   231 		case ESmtpSendRcptTo:
       
   232 		{	// RCPT TO was sent.
       
   233 			if (iStatus != KErrNone)
       
   234 			{
       
   235 				// error
       
   236 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to send RCPT TO: [%d]."), iStatus.Int());
       
   237 				TestCase().SetTestStepResult(EFail);
       
   238 				User::Leave(iStatus.Int());
       
   239 			}
       
   240 			
       
   241 			// ok we sent RCPT TO, now receive reply
       
   242 			iSendState = ESmtpReceiveReply;
       
   243 			iRecvState = ESmtpRcptReply;
       
   244 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   245 			SetActive();
       
   246 			break;
       
   247 		}
       
   248 		case ESmtpSendData:
       
   249 		{	// DATA was sent.
       
   250 			if (iStatus != KErrNone)
       
   251 			{
       
   252 				// error
       
   253 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to send DATA: [%d]."), iStatus.Int());
       
   254 				TestCase().SetTestStepResult(EFail);
       
   255 				User::Leave(iStatus.Int());
       
   256 			}
       
   257 			
       
   258 			// ok we sent DATA, now receive reply
       
   259 			iSendState = ESmtpReceiveReply;
       
   260 			iRecvState = ESmtpDataReply;
       
   261 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   262 			SetActive();
       
   263 			break;
       
   264 		}
       
   265 		case ESmtpSendContent:
       
   266 		{	//content was sent.
       
   267 			// read and send file. send file in chunks of 100 bytes.
       
   268 			TBuf8<100> buffer;
       
   269 			User::LeaveIfError(iFile.Read(buffer, 100));
       
   270 			if (buffer.Length() == 0)
       
   271 			{
       
   272 				// ok, end of file. send end of email.
       
   273 				iSendState = ESmtpSendEndData;
       
   274 				iImSocket->Send(iStatus, KEndData());
       
   275 			}
       
   276 			else
       
   277 			{
       
   278 				// we still have data in file. read and send.
       
   279 				iSendState = ESmtpSendContent;
       
   280 				iImSocket->Send(iStatus, buffer);
       
   281 			}
       
   282 			SetActive();
       
   283 			break;
       
   284 		}
       
   285 		case ESmtpSendEndData:
       
   286 		{	// end of data was sent.
       
   287 			if (iStatus != KErrNone)
       
   288 			{
       
   289 				// error
       
   290 				TestCase().ERR_PRINTF2(_L("CMtfTestActionSmtpSendEmail: failed to send end-of-data marker: [%d]."), iStatus.Int());
       
   291 				TestCase().SetTestStepResult(EFail);
       
   292 				User::Leave(iStatus.Int());
       
   293 			}
       
   294 			
       
   295 			// ok we sent end-of-data, now receive reply
       
   296 			iSendState = ESmtpReceiveReply;
       
   297 			iRecvState = ESmtpContentReply;
       
   298 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   299 			SetActive();
       
   300 			break;
       
   301 		}
       
   302 		case ESmtpSendLogout:
       
   303 		{
       
   304 			iSendState = ESmtpReceiveReply;
       
   305 			iRecvState = ESmtpLogoutReply;
       
   306 			iImSocket->QueueReceiveNextTextLine(iStatus);
       
   307 			SetActive();
       
   308 			break;
       
   309 		}
       
   310 		case ESmtpReceiveReply:
       
   311 		{
       
   312 			if (iStatus != KErrNone)
       
   313 			{
       
   314 				// error
       
   315 				TestCase().ERR_PRINTF3(_L("CMtfTestActionSmtpSendEmail: failed to receive reply: error [%d] recvstate [%d]."), iStatus.Int(), iRecvState);
       
   316 				TestCase().SetTestStepResult(EFail);
       
   317 				User::Leave(iStatus.Int());
       
   318 			}
       
   319 			
       
   320 			// ok, read and print out the reply
       
   321 			TBuf8<50> buffer;
       
   322 			TBuf<50> buffer16;
       
   323 			TImLineType replyLine = iImSocket->GetCurrentTextLine(buffer);
       
   324 			buffer16.Copy(buffer);
       
   325 			TestCase().INFO_PRINTF1(buffer16);
       
   326 			
       
   327 			if (replyLine == EBufferTooSmall)
       
   328 			{
       
   329 				while (replyLine != ECRLFTerminated)
       
   330 				{
       
   331 					replyLine = iImSocket->GetCurrentTextLine(buffer);
       
   332 					buffer16.Copy(buffer);
       
   333 					TestCase().INFO_PRINTF1(buffer16);
       
   334 				}
       
   335 			}
       
   336 			
       
   337 			// handle reply state
       
   338 			switch (iRecvState)
       
   339 			{
       
   340 				case ESmtpHeloReply:
       
   341 				{
       
   342 					// send MAIL FROM
       
   343 					iSendState = ESmtpSendMailFrom;
       
   344 					TBuf8<100> from;
       
   345 					from.Append(KMailFrom);
       
   346 					from.Append(*iSender);
       
   347 					from.Append(KCrlfString);
       
   348 					iImSocket->Send(iStatus, from);
       
   349 					break;
       
   350 				}
       
   351 				case ESmtpFromReply:
       
   352 				{
       
   353 					// send RCPT TO
       
   354 					iSendState = ESmtpSendRcptTo;
       
   355 					TBuf8<100> to;
       
   356 					to.Append(KRcptTo);
       
   357 					to.Append(*iRecipient);
       
   358 					to.Append(KCrlfString);
       
   359 					iImSocket->Send(iStatus, to);
       
   360 					break;
       
   361 				}
       
   362 				case ESmtpRcptReply:
       
   363 				{
       
   364 					// send DATA
       
   365 					iSendState = ESmtpSendData;
       
   366 					iImSocket->Send(iStatus, KDataString());
       
   367 					break;
       
   368 				}
       
   369 				case ESmtpDataReply:
       
   370 				{
       
   371 					// send content
       
   372 					iSendState = ESmtpSendContent;
       
   373 					TBuf8<100> buffer;
       
   374 					User::LeaveIfError(iFile.Read(buffer, 100));
       
   375 					if (buffer.Length() == 0)
       
   376 					{
       
   377 						// ok, end of file. since this is first read it would mean the file
       
   378 						// is empty! we should maybe leave here or??
       
   379 						iSendState = ESmtpSendEndData;
       
   380 						iImSocket->Send(iStatus, KEndData());
       
   381 					}
       
   382 					else
       
   383 					{
       
   384 						// we still have data in file
       
   385 						iSendState = ESmtpSendContent;
       
   386 						iImSocket->Send(iStatus, buffer);
       
   387 					}
       
   388 					break;
       
   389 				}
       
   390 				case ESmtpContentReply:
       
   391 				{
       
   392 					// send QUIT
       
   393 					iSendState = ESmtpSendLogout;
       
   394 					iImSocket->Send(iStatus, KQuitString());
       
   395 					break;
       
   396 				}
       
   397 				case ESmtpLogoutReply:
       
   398 				{
       
   399 					iSendState = ESmtpNotConnected;
       
   400 					iImSocket->Disconnect(iStatus);
       
   401 					break;
       
   402 				}
       
   403 				default:
       
   404 				{
       
   405 					User::Panic(_L("Unkown receive state!"), iRecvState);
       
   406 					break;
       
   407 				}
       
   408 			} // End switch (iRecvState)
       
   409 			
       
   410 			SetActive();
       
   411 			break;
       
   412 		}
       
   413 		default:
       
   414 		{
       
   415 			User::Panic(_L("Unkown send state!"), iSendState);
       
   416 			break;
       
   417 		}
       
   418 	} // End switch (iSendState)
       
   419 }