email/pop3andsmtpmtm/smtpservermtm/test/src/t_smts00.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1999-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 "emailtestutils.h"
       
    17 #include "scripttestutils.h"
       
    18 #include <commdb.h>
       
    19 #include <cemailaccounts.h>
       
    20 
       
    21 _LIT(KSmtsTest, "T_SMTS00 - Testing SMTS Server MTM with script files");
       
    22 
       
    23 _LIT(KImskOutFile, "checkagainst\\imsk25.out");
       
    24 _LIT(KSmtsComponent,"SMTS");
       
    25 _LIT(KSmtsRFCEmails,"RFC822\\");
       
    26 _LIT(KSmtsScriptFile,"scripts\\smtslog.scr");
       
    27 
       
    28 
       
    29 
       
    30 RTest test(KSmtsTest);
       
    31 
       
    32 LOCAL_D CTrapCleanup* theCleanup;
       
    33 LOCAL_D CEmailTestUtils* testUtils; 
       
    34 LOCAL_D TMsvId smtpService;
       
    35 LOCAL_D CBaseServerMtm* serverMtm;
       
    36 LOCAL_D CActiveScheduler* scheduler;
       
    37 LOCAL_D TInt globalError;
       
    38 
       
    39 LOCAL_C void DisplayProgress()
       
    40 	{
       
    41  	TImSmtpProgress temp;	
       
    42 	TPckgC<TImSmtpProgress> paramPack(temp);
       
    43 
       
    44 	const TDesC8& progBuf =serverMtm->Progress();
       
    45 	paramPack.Set(progBuf);
       
    46 	TImSmtpProgress progress=paramPack();	
       
    47 	test(progress.ServiceId()==smtpService);
       
    48 	//  State of Smtp session?
       
    49 	TBuf<128> logString;
       
    50 	TInt state = progress.Status();
       
    51 	switch(state)
       
    52 		{
       
    53 		case EMsgOutboxProgressWaiting:
       
    54 			testUtils->WriteComment(_L("Waiting to start\n"));
       
    55 			break;
       
    56 		case EMsgOutboxProgressDone:
       
    57 			testUtils->WriteComment(_L("Connection Closed.\n"));
       
    58 			logString.Format(_L("%d messages: Sent %d, failed to send %d, didn't attempt to send %d.\n"), 
       
    59 				progress.SendTotal(),
       
    60 				progress.Sent(),
       
    61 				progress.FailedToSend(),
       
    62 				progress.NotSent());
       
    63 			testUtils->WriteComment(logString);
       
    64 			break;
       
    65 		case EMsgOutboxProgressConnecting:
       
    66 			logString.Format(_L("Connecting to SMTP server. Sending %d messages.\n"),
       
    67 				progress.SendTotal());
       
    68 			testUtils->WriteComment(logString);
       
    69 			break;
       
    70 		case EMsgOutboxProgressSending:
       
    71 			logString.Format(_L("Sending message No.%d/%d. Transmitted %d bytes of %d            \n"),     
       
    72 				progress.MsgNo()+1,
       
    73 				progress.SendTotal(),
       
    74 				progress.iSendFileProgress.iBytesSent,
       
    75 				progress.iSendFileProgress.iBytesToSend);
       
    76 			testUtils->WriteComment(logString);
       
    77 			break;
       
    78 		}
       
    79 	}
       
    80 
       
    81 class CSmtpTimer : public CTimer
       
    82 	{
       
    83 public:
       
    84 	static CSmtpTimer* NewL(CTestActive& aActive);
       
    85 	void StartL();
       
    86 private:
       
    87 	CSmtpTimer(CTestActive& aActive);
       
    88 	void RunL();	
       
    89 	CTestActive& iSmtpActive;
       
    90 	};
       
    91 
       
    92 CSmtpTimer::CSmtpTimer(CTestActive& aActive)
       
    93 : CTimer(EPriorityLow),	iSmtpActive(aActive)
       
    94 
       
    95 	{}
       
    96 
       
    97 
       
    98 CSmtpTimer* CSmtpTimer::NewL(CTestActive& aActive)
       
    99 	{
       
   100 	CSmtpTimer* self = new(ELeave) CSmtpTimer(aActive);
       
   101 	CleanupStack::PushL(self);
       
   102 	CActiveScheduler::Add(self);
       
   103 	CleanupStack::Pop();
       
   104 	return self;
       
   105 	}
       
   106 
       
   107 void CSmtpTimer::StartL()
       
   108 	{
       
   109 	CTimer::ConstructL();
       
   110 	TRequestStatus* status=&iStatus;
       
   111 	User::RequestComplete(status,KErrNone);
       
   112 	SetActive();
       
   113 	}
       
   114 
       
   115 void CSmtpTimer::RunL()
       
   116 	{
       
   117 	test.Console()->SetPos(0, 10);
       
   118 	DisplayProgress();
       
   119 
       
   120 	if (!iSmtpActive.IsActive())   //this is how you check if the MTM is finished or not!
       
   121 		{
       
   122 		iSmtpActive.Cancel();
       
   123 		CActiveScheduler::Stop();
       
   124 		return;
       
   125 		}
       
   126 	else
       
   127 		After(400000);
       
   128 	}
       
   129 
       
   130 
       
   131 LOCAL_C void TestSendingMessagesL()
       
   132 	{
       
   133 	CTestActive* testActive = new (ELeave) CTestActive();
       
   134 	CleanupStack::PushL(testActive);
       
   135 
       
   136 	CMsvEntrySelection* sendSel = new(ELeave) CMsvEntrySelection();
       
   137 	CleanupStack::PushL(sendSel);
       
   138 
       
   139 	testUtils->iServerEntry->SetEntry(KMsvGlobalOutBoxIndexEntryId);
       
   140 	User::LeaveIfError(testUtils->iServerEntry->GetChildren(*sendSel));
       
   141 	TInt count=sendSel->Count();
       
   142 	if (!count)
       
   143 		{
       
   144 		testUtils->WriteComment(_L("\t No messages to send in the outbox"));
       
   145 		CleanupStack::PopAndDestroy(2); //testActive, sendsel
       
   146 		globalError=KErrNotFound;
       
   147 		return;
       
   148 		}
       
   149 	TBuf<100> logString;
       
   150 	testUtils->WriteComment(_L("Issuing CopyFromLocal request...\n"));
       
   151 	logString.Format(_L("\t messages to send in the outbox - %d"), count);
       
   152 	testUtils->WriteComment(logString);
       
   153 	
       
   154 
       
   155 	serverMtm->CopyFromLocalL(*sendSel,smtpService, testActive->iStatus);
       
   156 	CSmtpTimer* timer=CSmtpTimer::NewL(*testActive);
       
   157 	CleanupStack::PushL(timer);
       
   158 
       
   159 	testActive->StartL();
       
   160 	timer->StartL();
       
   161 	CActiveScheduler::Start();
       
   162 	//check progress
       
   163 	DisplayProgress();
       
   164 	logString.Zero();
       
   165 	logString.Format(_L("\t CopyFromLocalL completed with %d"), testActive->iStatus.Int());
       
   166 	testUtils->WriteComment(logString);
       
   167  	TImSmtpProgress temp;	
       
   168 	TPckgC<TImSmtpProgress> paramPack(temp);
       
   169 
       
   170 	const TDesC8& progBuf =serverMtm->Progress();
       
   171 	paramPack.Set(progBuf);
       
   172 	TImSmtpProgress progress=paramPack();	
       
   173 	globalError=progress.Error();
       
   174 	CleanupStack::PopAndDestroy(3); //testActive, sendsel, /*timer*/
       
   175 
       
   176 	}
       
   177 
       
   178 LOCAL_C void ClosedownL()
       
   179 	{
       
   180 	if (globalError)
       
   181 		testUtils->TestHarnessFailed(globalError);
       
   182 	else
       
   183 		{
       
   184 		testUtils->TestHarnessCompleted();
       
   185 		#if !defined(_DEBUG)
       
   186 			// Cannot compare logs on REL builds, since the log file is not generated
       
   187 			testUtils->WriteComment(_L("!!!! Logs comparison skipped on REL builds !!!!"));
       
   188 			testUtils->WriteComment(_L("!!!! Run on DEB build for complete test    !!!!"));
       
   189 			testUtils->TestHarnessCompleted();
       
   190 			TBool trueB = ETrue; // avoid compiler warning
       
   191 			if (trueB) return;
       
   192 		#endif
       
   193 		CScriptTestUtils* utls=CScriptTestUtils::NewLC(*testUtils);
       
   194 		RFile file;
       
   195 		TParse check;
       
   196 		User::LeaveIfError(testUtils->ResolveFile(KSmtsComponent,KImskOutFile,check));
       
   197 		TInt err = file.Open(testUtils->FileSession(), check.FullName(), EFileRead);
       
   198 		file.Close();
       
   199 		if(err==KErrNone) // file does not exist - create it
       
   200 			{
       
   201 			if (!utls->CheckLogOutputL(25, check.FullName()))
       
   202 				testUtils->WriteComment(_L("imsk25.out didn't match"));
       
   203 			else
       
   204 				{
       
   205 				testUtils->WriteComment(_L("imsk25.out matched"));
       
   206 				testUtils->TestHarnessCompleted();
       
   207 				}
       
   208 			}
       
   209 		CleanupStack::PopAndDestroy();  //utls,
       
   210 
       
   211 		}
       
   212 	CleanupStack::PopAndDestroy(2);  //testUtils, ischeduler
       
   213 	}
       
   214 
       
   215 LOCAL_C void InitL()
       
   216 	{
       
   217 	scheduler = new (ELeave) CActiveScheduler;
       
   218 	CleanupStack::PushL(scheduler);
       
   219 	CActiveScheduler::Install( scheduler );
       
   220 
       
   221 	testUtils = CEmailTestUtils::NewLC(test);
       
   222 	testUtils->WriteComment(KSmtsTest);
       
   223 
       
   224 	testUtils->CleanMessageFolderL();
       
   225 	testUtils->ClearEmailAccountsL();
       
   226 
       
   227 	if (!testUtils->FileSession().MkDir(KEmailDataDir))
       
   228 		{
       
   229 		testUtils->Printf(_L("Created c:\\logs\\email directory\n"));
       
   230 		}
       
   231 
       
   232 	testUtils->GoClientSideL();
       
   233 	smtpService = testUtils->CreateSmtpServiceL();
       
   234 
       
   235 	CImSmtpSettings* settings = new(ELeave) CImSmtpSettings();
       
   236 	CleanupStack::PushL(settings);
       
   237 
       
   238 	CEmailAccounts* accounts = CEmailAccounts::NewLC();
       
   239 	
       
   240 	TSmtpAccount smtpAccountId;
       
   241 	accounts->GetSmtpAccountL(smtpService,smtpAccountId); 
       
   242 	accounts->LoadSmtpSettingsL(smtpAccountId, *settings);
       
   243 	
       
   244 	_LIT(KSmtpServer, "smtp.demon.co.uk");
       
   245 	settings->SetServerAddressL(KSmtpServer);
       
   246 	settings->SetEmailAliasL(_L("SMTP Test1"));
       
   247 	_LIT(KSmtpEmailAddress, "<test@psion9.demon.co.uk>");
       
   248 	settings->SetEmailAddressL(KSmtpEmailAddress);
       
   249 	settings->SetReplyToAddressL(KSmtpEmailAddress);
       
   250 	settings->SetBodyEncoding(EMsgOutboxMIME);
       
   251 	settings->SetReceiptAddressL(KSmtpEmailAddress);
       
   252 	settings->SetPort(25);
       
   253 
       
   254 	accounts->SaveSmtpSettingsL(smtpAccountId, *settings);
       
   255 
       
   256 	CleanupStack::PopAndDestroy(3, testUtils); // accounts, settings, testUtils
       
   257 	testUtils = CEmailTestUtils::NewLC(test, ETuGoServerSide);
       
   258 
       
   259 	testUtils->InstantiateSmtpServerMtmL();
       
   260 	serverMtm=testUtils->iSmtpServerMtm;
       
   261 	
       
   262 	TParse scriptFile;
       
   263 	testUtils->ResolveFile(KSmtsComponent,KSmtsScriptFile,scriptFile);
       
   264 	testUtils->CopyScriptFileL(scriptFile.FullName(), _L("25"));
       
   265 	}
       
   266 
       
   267 LOCAL_C void doMainL()
       
   268 	{
       
   269 	InitL();
       
   270 
       
   271 	//overwrite the setings with test code one.  Don't want the default settings.
       
   272 
       
   273 
       
   274 	testUtils->WriteComment(_L(" smtp server created"));
       
   275 
       
   276 	TParse dir;
       
   277 	testUtils->ResolveFile(KSmtsComponent,KSmtsRFCEmails,dir);
       
   278 	testUtils->CreateMessageFilesL(smtpService,KMsvGlobalOutBoxIndexEntryId,dir.FullName());
       
   279 	testUtils->WriteComment(_L(" Messages created in the outbox"));
       
   280 	testUtils->WriteComment(_L("\nPerforming Tests\n"));
       
   281 	testUtils->TestStart(1);
       
   282 	TestSendingMessagesL();
       
   283 	testUtils->TestFinish(1,globalError);
       
   284 	ClosedownL();
       
   285 	}
       
   286 
       
   287 
       
   288 GLDEF_C TInt E32Main()
       
   289 	{	
       
   290 	__UHEAP_MARK;
       
   291 	test.Start(_L("T_SMTS00 Test SMTP server MTM class"));
       
   292 	theCleanup=CTrapCleanup::New();
       
   293 	TRAPD(ret,doMainL());		
       
   294 	test(ret==KErrNone);
       
   295 	delete theCleanup;	
       
   296 	test.End();
       
   297 	test.Close();
       
   298 	__UHEAP_MARKEND;
       
   299 	User::Heap().Check();
       
   300 	return(KErrNone);
       
   301 	}