email/pop3andsmtpmtm/imapservermtm/test/src/T_DEF062024_IMAP21cnServer_AttachmentCannotBeOpenedStep.cpp
changeset 25 84d9eb65b26f
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
       
     1 // Copyright (c) 2005-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 // The sever contains and retrieves an email with an attachment (a v-card), the aim of th test is to check that this
       
    15 // attachmenet is fetched correctly by comparing its size and centent to the size and content of the source file attached.
       
    16 // 2. The client connects to the server, requests a sync and a populate
       
    17 // 3. The server reads the protocol information from a file specified in the ini file (ImapAttachmentTest.ini).
       
    18 // 4. The client disconnects.
       
    19 // 5. The attachment of the mail fetched is compared with the corresponding source file. The test passes if its size and content is exactly the same as the size and content of the file originally attached to this email, otherwise itv fails.
       
    20 // 
       
    21 //
       
    22 
       
    23 /**
       
    24  @SYMTestCaseID DEF062024   
       
    25  @SYMTestType UT					
       
    26  @SYMTestPriority High			
       
    27  @SYMDEF DEF062024					
       
    28  @SYMTestCaseDesc Implements an IMAP server to facilitate offline testing of imap parsing. 
       
    29  @SYMTestActions  1. The Server is started and listens on port 143.
       
    30  @SYMTestExpectedResults The attachment file should have the same size and content as the source file originally attached.
       
    31 */
       
    32 
       
    33 #include <testexecutelog.h>
       
    34 #include "t_DEF062024_IMAP21cnServer_AttachmentCannotBeOpenedStep.h"
       
    35 
       
    36 
       
    37 RTest test(_L("IMAP Get and check attachment Test"));
       
    38 const TInt KImapPort = 143;
       
    39 const TInt KChunkReadFile = 512;
       
    40 const TInt KSizeDateTimeBuffer = 45;
       
    41 
       
    42  
       
    43 LOCAL_C void FormatDateTime(TDes& aBuffer, const TTime& aTime)
       
    44 	{ 
       
    45 	aBuffer.Zero();
       
    46 	_LIT(KFormat4,"%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B");
       
    47 	aTime.FormatL(aBuffer,KFormat4); 
       
    48 	}
       
    49 		
       
    50 CTestImapAttachmentStep::~CTestImapAttachmentStep()
       
    51 	{
       
    52 	delete iConsole;
       
    53 	delete iSpoofServer;	
       
    54 	delete iTestUtils;
       
    55 	delete iImapClient;	
       
    56 	delete iScheduler;
       
    57 	iSession->CloseMessageServer();
       
    58 	delete iSession;
       
    59 	delete iSessionObserver;
       
    60 	}
       
    61 
       
    62 CTestImapAttachmentStep::CTestImapAttachmentStep()
       
    63 	{
       
    64 	// Call base class method to set up the human readable name for logging
       
    65 	SetTestStepName(KTestImapAttachment);
       
    66 	}
       
    67 
       
    68 void CTestImapAttachmentStep::TestComplete(TInt aErrorCode)
       
    69 	{
       
    70 	CActiveScheduler::Stop();	
       
    71 	
       
    72 	if(aErrorCode!=KErrNone && aErrorCode!=KErrEof)
       
    73 		{
       
    74 		ERR_PRINTF2(_L("Error: %d"),aErrorCode);	
       
    75 		}
       
    76 	}
       
    77  
       
    78 		
       
    79 void CTestImapAttachmentStep::PrintEntryTime(TTime* aTIme)
       
    80 	{
       
    81 	TBuf<KSizeDateTimeBuffer> dateTimeString;
       
    82 	FormatDateTime(dateTimeString,*aTIme); 
       
    83 	_LIT(KDateTimeString, "%S");
       
    84 	INFO_PRINTF2(KDateTimeString,&dateTimeString);	
       
    85 	}
       
    86 		
       
    87 		
       
    88 void CTestImapAttachmentStep::GetDetailsFromIniFileL()
       
    89 	{
       
    90 	//get the script file path
       
    91 	_LIT(KScriptFile,"ScriptFile");	
       
    92 	if(!GetStringFromConfig(ConfigSection(),KScriptFile,iScriptFile))
       
    93 		{
       
    94 		_LIT(KErrorScriptFile, "Error reading script file path from ini file");
       
    95 		ERR_PRINTF1(KErrorScriptFile);
       
    96 		User::Leave(KErrArgument);	
       
    97 		}
       
    98 	// get the source path of the file attached to the mail
       
    99 	_LIT(KSourceAttach,"AttachmentSourcePath");
       
   100 	if(!GetStringFromConfig(ConfigSection(),KSourceAttach,iAttachmentSourcePath))
       
   101 		{
       
   102 		_LIT(KErrorSourceAttach, "Error reading attachment source file path from ini file");
       
   103 		ERR_PRINTF1(KErrorSourceAttach);
       
   104 		User::Leave(KErrArgument);	
       
   105 		}
       
   106 	// get the index of the attachment in this mail
       
   107 	_LIT(KAttachIndex, "AttachmentIndex");
       
   108 	if(!GetIntFromConfig(ConfigSection(),KAttachIndex,iAttachmentIndex))
       
   109 		{
       
   110 		_LIT(KErrorAttachIndex, "Error reading attachment index from ini file");
       
   111 		ERR_PRINTF1(KErrorAttachIndex);
       
   112 		User::Leave(KErrArgument);	
       
   113 		}
       
   114 	}	
       
   115 
       
   116 TVerdict CTestImapAttachmentStep::doTestStepPreambleL()
       
   117 	{
       
   118 	SetTestStepResult(EPass);
       
   119 	iScheduler = new (ELeave) CActiveScheduler;
       
   120 	CActiveScheduler::Install(iScheduler);
       
   121 	
       
   122 	iTestUtils = CEmailTestUtils::NewL(test);	
       
   123 	iTestUtils->FileSession().SetSessionPath(_L("C:\\"));
       
   124 	iTestUtils->CleanMessageFolderL();
       
   125 	iTestUtils->ClearEmailAccountsL();
       
   126 	
       
   127 	iTestUtils->GoClientSideL();	
       
   128 	
       
   129 	//start the imap server
       
   130 	GetDetailsFromIniFileL();
       
   131 	_LIT(KIMAPTest, "IMAP Email Attachment Test");
       
   132 	iConsole=Console::NewL(KIMAPTest,TSize(KConsFullScreen,KConsFullScreen));
       
   133 	iSpoofServer = CSpoofServer::NewL(*this,iScriptFile);
       
   134 	iSpoofServer->StartL(KImapPort);
       
   135 	//start the imap client
       
   136 	iImapClient = CActiveImapClient::NewL(*this);
       
   137 	iImapClient->StartL();
       
   138 	
       
   139 	CActiveScheduler::Start();
       
   140 	return TestStepResult();
       
   141 	}
       
   142 
       
   143 TVerdict CTestImapAttachmentStep::CheckAttachmentL() 
       
   144 	{
       
   145 	TVerdict result = EPass;
       
   146 	// Session observer. Needed to create a session
       
   147 	iSessionObserver = new (ELeave) TDummySessionObserver;
       
   148 	// Session. Needed to create a client registry.
       
   149 	iSession=CMsvSession::OpenSyncL(*iSessionObserver);
       
   150 	CMsvEntry* entry = iSession->GetEntryL(iImapClient->GetMessageId());
       
   151 	CleanupStack::PushL(entry);
       
   152 	CImEmailMessage* emailMsg = CImEmailMessage::NewL(*entry);
       
   153 	CleanupStack::PushL(emailMsg);
       
   154 	MMsvAttachmentManager& manager = emailMsg->AttachmentManager();
       
   155 	TInt attachmentCount = manager.AttachmentCount();
       
   156 	// Get the file attachment
       
   157 	RFile fileAttachment = manager.GetAttachmentFileL(iAttachmentIndex);
       
   158 	CleanupClosePushL(fileAttachment);
       
   159 	RFs fs;
       
   160 	User::LeaveIfError(fs.Connect());
       
   161 	CleanupClosePushL(fs);
       
   162 	RFile dataFile;
       
   163 	// Open the source file attached to the email
       
   164 	User::LeaveIfError(dataFile.Open(fs, iAttachmentSourcePath, EFileRead|EFileShareReadersOnly));
       
   165 	CleanupClosePushL(fs);
       
   166 	TInt fileSize = 0;
       
   167 	TInt attachmentSize = 0;
       
   168 	// Get the size of the source file
       
   169 	User::LeaveIfError(dataFile.Size(fileSize));
       
   170 	// Get the size of the attachment file
       
   171 	User::LeaveIfError(fileAttachment.Size(attachmentSize));
       
   172 	
       
   173 	// Check the sizes first, the source and attachment files must have the same size
       
   174 	if(fileSize!=attachmentSize)
       
   175 		{
       
   176 		result = EFail;				
       
   177 		}
       
   178 	else
       
   179 		{
       
   180 		// We compare the contents
       
   181 		TInt compare;
       
   182 		TBuf8<KChunkReadFile> dataBuffer;
       
   183 		TBuf8<KChunkReadFile> attachmentData;
       
   184 		if(fileSize < KChunkReadFile)
       
   185 			{			
       
   186 			dataFile.Read(0,dataBuffer);
       
   187 			fileAttachment.Read(0,attachmentData);
       
   188 			
       
   189 			compare = attachmentData.Compare(dataBuffer);
       
   190 			if(compare!=0)
       
   191 				{
       
   192 				result = EFail;
       
   193 				}
       
   194 			else
       
   195 				{
       
   196 				result = EPass;
       
   197 				}				
       
   198 			}
       
   199 		else
       
   200 			{
       
   201 			// The size of the files is big, we read them by chunks and compare them
       
   202 			TInt bytesRead = KChunkReadFile;
       
   203 			TInt ii = 0;
       
   204 			while (bytesRead < fileSize)	
       
   205 				{
       
   206 				// Get the i-th chunk of data of the source file
       
   207 				dataFile.Read(ii*KChunkReadFile,dataBuffer);
       
   208 				// Get the i-th chunk of data of the attachment file
       
   209 				fileAttachment.Read(ii*KChunkReadFile,attachmentData);
       
   210 				
       
   211 				// Compare the chunks
       
   212 				compare = attachmentData.Compare(dataBuffer);
       
   213 				if(compare != 0)
       
   214 					{
       
   215 					result = EFail;
       
   216 					break;
       
   217 					}
       
   218 				else
       
   219 					result = EPass;
       
   220 				bytesRead += KChunkReadFile;
       
   221 				++ii;
       
   222 				}
       
   223 			}		
       
   224 		}
       
   225 		
       
   226 	CleanupStack::PopAndDestroy(5, entry);
       
   227 	
       
   228 	return result;	
       
   229 	}
       
   230 		
       
   231 TVerdict CTestImapAttachmentStep::doTestStepL()
       
   232 	{
       
   233 	TVerdict result;	
       
   234 	
       
   235 	result = CheckAttachmentL();
       
   236 	
       
   237 	SetTestStepResult(result);
       
   238 	
       
   239 	return TestStepResult();
       
   240 	}
       
   241 	
       
   242 
       
   243 TVerdict CTestImapAttachmentStep::doTestStepPostambleL()
       
   244 	{
       
   245 	return TestStepResult();
       
   246 	}
       
   247 	
       
   248 	
       
   249 
       
   250