emailservices/emailstore/tsrc/message_store_test/src/ContentTests.cpp
changeset 1 12c456ceeff2
child 8 e1b6206813b4
equal deleted inserted replaced
0:8466d47a6819 1:12c456ceeff2
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Base class for all test cases class member functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <BAUTILS.H>
       
    20 #include "ContentTests.h"
       
    21 //#include <ismsgstorepropertykeys.h>
       
    22 #include "messagestoreclientserver.h"
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 CContentTests* CContentTests::NewL( CStifLogger* aLog )
       
    27     {
       
    28     CContentTests* self = new(ELeave) CContentTests( aLog );
       
    29     CleanupStack::PushL( self );
       
    30     self->ConstructL();
       
    31     CleanupStack::Pop( self );
       
    32     return self;
       
    33     }
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // Constructor
       
    37 // -----------------------------------------------------------------------------
       
    38 CContentTests::CContentTests( CStifLogger* aLog ) : CAsyncTestCaseBase( aLog ) 
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // Destructor
       
    44 // -----------------------------------------------------------------------------
       
    45 CContentTests::~CContentTests()
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // 2nd phase constructor
       
    51 // -----------------------------------------------------------------------------
       
    52 void CContentTests::ConstructL()
       
    53     {
       
    54     CAsyncTestCaseBase::ConstructL();
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // ExecuteL : start executing the test case
       
    59 // -----------------------------------------------------------------------------
       
    60 TBool CContentTests::ExecuteL()
       
    61     {
       
    62     LogHeader( _L("Content") );
       
    63     
       
    64     // Create messages for later use.
       
    65     CMsgStoreAccount* account = CMsgStoreAccount::NewLC(1234, _L("new account") );     //+1
       
    66     CMsgStoreMailBox* mailBox1 = iMsgStore->CreateAccountL( *account );
       
    67     CleanupStack::PushL( mailBox1 );                                                 //+2
       
    68     
       
    69     CMsgStorePropertyContainer* properties = CMsgStorePropertyContainer::NewL();
       
    70     CleanupStack::PushL( properties );                                              //+3
       
    71     
       
    72     TMsgStoreId inbox1Id = mailBox1->CreateFolderL( mailBox1->Id(), *properties );
       
    73     CMsgStoreFolder* inbox1 = mailBox1->FolderL( inbox1Id );
       
    74     CleanupStack::PushL( inbox1 );                                                    //+4
       
    75     
       
    76     CMsgStoreMessage* message1 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
    77     CleanupStack::PushL( message1 );                                                  //+5
       
    78     message1->CommitL();
       
    79     
       
    80     CMsgStoreMessage* message2 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
    81     CleanupStack::PushL( message2 );                                                  //+6
       
    82     message2->CommitL();
       
    83     
       
    84     CMsgStoreMessage* message3 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
    85     CleanupStack::PushL( message3 );                                                  //+7
       
    86     message3->CommitL();
       
    87     
       
    88     // BUFFER-ORIENTED TESTS
       
    89 
       
    90     RBuf8 expectedContent;
       
    91     RBuf8 contentBuf;
       
    92     expectedContent.CreateL( 1000 );
       
    93     contentBuf.CreateL( 1000 );
       
    94     
       
    95     iLog->Log( _L("Case %d: Fetch content to buffer before content added"), iCaseId++ );
       
    96     
       
    97     message1->FetchContentToBufferL( contentBuf );
       
    98     CheckCondition( _L("no content"), (contentBuf.Length() == 0) && (message1->ContentLengthL() == 0) );
       
    99     
       
   100     iLog->Log( _L("Case %d: ReplaceContentL"), iCaseId++ );
       
   101     
       
   102     _LIT8( KContent1, "THIS IS SOME CONTENT" );
       
   103     message1->ReplaceContentL( KContent1 );
       
   104     CheckCondition( _L("ContentLength matches"), message1->ContentLengthL() == KContent1().Length() );
       
   105     
       
   106     iLog->Log( _L("Case %d: FetchContentToBufferL"), iCaseId++ );
       
   107     
       
   108     message1->FetchContentToBufferL( contentBuf );
       
   109     CheckCondition( _L("content matches"), contentBuf.Compare( KContent1 ) == 0 );
       
   110 
       
   111     iLog->Log( _L("Case %d: Fetch content with start offset"), iCaseId++ );
       
   112     
       
   113     message1->FetchContentToBufferL( contentBuf, 4 );
       
   114     CheckCondition( _L("content matches"), contentBuf.Compare( KContent1().Mid( 4 ) ) == 0 );
       
   115 
       
   116     iLog->Log( _L("Case %d: Append to content"), iCaseId++ );
       
   117 
       
   118     _LIT8( KContent2, "MORE CONTENT" );
       
   119     message1->AppendToContentL( KContent2 );
       
   120     TInt contentLength = message1->ContentLengthL();
       
   121     TInt expectedLength = KContent1().Length() + KContent2().Length();
       
   122     CheckCondition( _L("ContentLength matches"), contentLength == expectedLength );
       
   123 
       
   124     iLog->Log( _L("Case %d: Append null buffer to content"), iCaseId++ );
       
   125 
       
   126     _LIT8( KNullBuffer, "" );
       
   127     message1->AppendToContentL( KNullBuffer );
       
   128     contentLength = message1->ContentLengthL();
       
   129     CheckCondition( _L("ContentLength matches"), contentLength == expectedLength );
       
   130 
       
   131     iLog->Log( _L("Case %d: Append null buffer to content"), iCaseId++ );
       
   132     
       
   133     expectedContent.Copy( KContent1 );
       
   134     expectedContent.Append( KContent2 );
       
   135     message1->FetchContentToBufferL( contentBuf );
       
   136     CheckCondition( _L("content matches"), contentBuf.Compare( expectedContent ) == 0 );
       
   137 
       
   138     iLog->Log( _L("Case %d: Long content"), iCaseId++ );
       
   139     
       
   140     expectedContent.Fill( 'C', 998 );
       
   141     expectedContent.Insert( 0, _L8("A") );
       
   142     expectedContent.Append( _L8("B") );
       
   143     message1->ReplaceContentL( expectedContent );
       
   144     message1->FetchContentToBufferL( contentBuf );
       
   145     contentLength = message1->ContentLengthL();
       
   146     expectedLength = expectedContent.Length();
       
   147     TBool passed = (contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0);
       
   148     //if( !passed )
       
   149     //    {
       
   150     //    __LOG_HEX_DUMP_ERROR( "expected", expectedContent )
       
   151     //    __LOG_HEX_DUMP_ERROR( "actual", contentBuf )
       
   152     //    }   
       
   153     CheckCondition( _L("content matches"), passed );
       
   154     
       
   155     iLog->Log( _L("Case %d: Open the original content file"), iCaseId++ );
       
   156     RFile testFile;
       
   157     TRAPD(err, message1->OpenOriginalContentFileL( testFile ) );
       
   158         
       
   159         //try to read from the file
       
   160         CheckCondition( _L("OpenOriginalContentFileL succeeded"), err == KErrNone );
       
   161         
       
   162         testFile.Read( contentBuf );
       
   163         CheckCondition( _L("Content in original file"), contentBuf.Compare( expectedContent ) == 0 );
       
   164         
       
   165         iLog->Log( _L("Case %d: Fetch while client has original file open"), iCaseId++ );
       
   166         
       
   167         message1->FetchContentToBufferL( contentBuf );
       
   168         CheckCondition( _L("Fetch while client has original file open"), contentBuf.Compare( expectedContent ) == 0 );
       
   169         
       
   170         iLog->Log( _L("Case %d: ContentLength while client has original file open"), iCaseId++ );
       
   171         
       
   172         contentLength = message1->ContentLengthL();
       
   173         CheckCondition( _L("ContentLength while client has original file open"), contentLength == expectedLength );
       
   174         
       
   175         iLog->Log( _L("Case %d: ReplaceContent while client has original file open"), iCaseId++ );
       
   176         
       
   177         message1->ReplaceContentL( KContent1 );
       
   178         message1->FetchContentToBufferL( contentBuf );
       
   179         expectedContent.Copy( KContent1 );
       
   180         CheckCondition( _L("ReplaceContent while client has original file open"), contentLength == expectedLength );
       
   181         
       
   182         iLog->Log( _L("Case %d: Can't Remove Content while client has original file open"), iCaseId++ );
       
   183         
       
   184         TRAP( err, message1->RemoveContentL() );
       
   185         CheckCondition( _L("can't RemoveContentL"), err == KErrInUse );       
       
   186     
       
   187         iLog->Log( _L("Case %d: Delete message while client has original file open"), iCaseId++ );
       
   188         //delete message while content file is locked
       
   189         mailBox1->DeleteMessageL( message1->Id(), inbox1Id );
       
   190         
       
   191         iLog->Log( _L("  Wait 3 seconds for server delete handler to complete") );
       
   192         Yield(3000000);
       
   193         
       
   194         iLog->Log( _L("  Create a dummy message") );
       
   195         CMsgStoreMessage* message4 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
   196         CleanupStack::PushL( message4 );                                                         //+8
       
   197         message4->CommitL();
       
   198         
       
   199         testFile.Close();
       
   200         iLog->Log( _L("  Client closed content file") );
       
   201         iLog->Log( _L("  delete dummy message after the file is closed, to trigger the delete handle") );
       
   202         
       
   203         mailBox1->DeleteMessageL( message4->Id(), inbox1Id );
       
   204         
       
   205         iLog->Log( _L("  Wait 3 secs for server delete handler to complete") );
       
   206         Yield(3000000);
       
   207     
       
   208     CMsgStoreMessage* message5 = mailBox1->CreateMessageL( inbox1Id, *properties );
       
   209     CleanupStack::PushL( message5 );                                                         //+9
       
   210     message5->CommitL();
       
   211 
       
   212     iLog->Log( _L("  Make sure other messages are not affected") );
       
   213     
       
   214     message2->FetchContentToBufferL( contentBuf );
       
   215     CheckCondition( _L("no content"), (contentBuf.Length() == 0) && (message2->ContentLengthL() == 0) );
       
   216 
       
   217     message3->FetchContentToBufferL( contentBuf );
       
   218     CheckCondition( _L("no content"), (contentBuf.Length() == 0) && (message3->ContentLengthL() == 0) );
       
   219 
       
   220     // FILE-ORIENTED TESTS
       
   221 
       
   222     iLog->Log( _L("Case %d: FetchContentToFileL"), iCaseId++ );
       
   223     _LIT( KFilename, "c:\\content_test.txt" );
       
   224 
       
   225     expectedContent.Fill( 'C', 998 );
       
   226     expectedContent.Insert( 0, _L8("A") );
       
   227     expectedContent.Append( _L8("B") );
       
   228     message5->ReplaceContentL( expectedContent );
       
   229     
       
   230     message5->FetchContentToFileL( KFilename );
       
   231     
       
   232     iLog->Log( _L("Case %d: ReplaceContentWithFileL"), iCaseId++ );
       
   233 
       
   234     message2->ReplaceContentWithFileL( KFilename );
       
   235 
       
   236     contentBuf.SetLength(0);            
       
   237     message2->FetchContentToBufferL( contentBuf );
       
   238 
       
   239     CheckCondition( _L("content matches"), contentBuf.Compare( expectedContent ) == 0 );
       
   240 
       
   241     // REMOVE CONTENT
       
   242     
       
   243     iLog->Log( _L("Case %d: Remove content"), iCaseId++ );
       
   244     
       
   245     message5->RemoveContentL();
       
   246     message5->FetchContentToBufferL( contentBuf );
       
   247     
       
   248     TInt length1 = contentBuf.Length();
       
   249     TInt length2 = message5->ContentLengthL();
       
   250     CheckCondition( _L("no content"), length1 == 0 && length2 == 0 );       
       
   251 
       
   252     iLog->Log( _L("case %d: Fetch content to file with no content"), iCaseId++ );
       
   253 
       
   254     RFs fs;
       
   255     User::LeaveIfError( fs.Connect() );
       
   256     
       
   257     message5->FetchContentToFileL( KFilename );
       
   258     RFile file;
       
   259     User::LeaveIfError( file.Open( fs, KFilename, EFileRead ) );
       
   260     TInt size;
       
   261     User::LeaveIfError( file.Size( size ) );
       
   262     file.Close();
       
   263     CheckCondition( _L("file size = 0"), size == 0 );
       
   264 
       
   265     // TESTS WITH VARIOUS CONTENT SIZES, TO DETECT FENCEPOST ERRORS   
       
   266 
       
   267     iLog->Log( _L("Case %d: FENCEPOST ERRORS Detection"), iCaseId++ );
       
   268     
       
   269     // THESE SHOULD EXACTLY MATCH THE VALUES INSIDE ContainerStoreContentManager.cpp
       
   270     const TUint KBlockSize = 64;
       
   271     const TUint KBufferSize = 4096;            
       
   272 
       
   273     // THESE MUST BE IN INCREASING ORDER.
       
   274     const TInt KSizesToTest[] = 
       
   275         {
       
   276         0, 
       
   277         1, 
       
   278         KBlockSize-1, 
       
   279         KBlockSize, 
       
   280         KBlockSize+1, 
       
   281         KBufferSize-1, 
       
   282         KBufferSize,
       
   283         KBufferSize+1, 
       
   284         2*KBufferSize-1, 
       
   285         2*KBufferSize, 
       
   286         2*KBufferSize+1, 
       
   287         5*KBufferSize-1, 
       
   288         5*KBufferSize, 
       
   289         5*KBufferSize+1,
       
   290         -1
       
   291         };
       
   292 
       
   293     expectedContent.ReAllocL( 7*KBufferSize );
       
   294     expectedContent.SetLength( 0 );        
       
   295 
       
   296     contentBuf.ReAllocL( 7*KBufferSize );
       
   297     contentBuf.SetLength( 0 );        
       
   298 
       
   299     TBuf8<KBufferSize+1> appendBuffer;
       
   300                         
       
   301     TInt index = 0;
       
   302     while( KSizesToTest[index] != -1 )
       
   303         {
       
   304         TInt growSizeBy = KSizesToTest[index] - expectedContent.Length();
       
   305         expectedContent.SetLength( KSizesToTest[index] );
       
   306         expectedContent.RightTPtr( growSizeBy ).Fill( 'A' + index );
       
   307         
       
   308         contentBuf.SetLength( 0 );
       
   309         message5->ReplaceContentL( expectedContent );
       
   310         message5->FetchContentToBufferL( contentBuf );    
       
   311         CheckCondition( _L("content matches"), message5->ContentLengthL() == expectedContent.Length() && contentBuf.Compare( expectedContent ) == 0 );
       
   312 
       
   313         contentBuf.SetLength( 0 );
       
   314         BaflUtils::DeleteFile( fs, KFilename );
       
   315         message5->FetchContentToFileL( KFilename );        
       
   316         message2->ReplaceContentWithFileL( KFilename );
       
   317         message2->FetchContentToBufferL( contentBuf );    
       
   318         contentLength = message2->ContentLengthL();
       
   319         expectedLength = expectedContent.Length();
       
   320         CheckCondition( _L("content matches"), contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0 );
       
   321 
       
   322         message3->ReplaceContentWithFileL( KFilename );
       
   323 
       
   324         appendBuffer.Fill( 'w', 1 );
       
   325         expectedContent.Append( appendBuffer );
       
   326         message5->AppendToContentL( appendBuffer );
       
   327         message5->FetchContentToBufferL( contentBuf );    
       
   328         contentLength = message5->ContentLengthL();
       
   329         expectedLength = expectedContent.Length();
       
   330         CheckCondition( _L("content matches"), contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0 );
       
   331             
       
   332         appendBuffer.Fill( 'x', KBlockSize-1 );
       
   333         expectedContent.SetLength( KSizesToTest[index] );
       
   334         expectedContent.Append( appendBuffer );
       
   335         message2->AppendToContentL( appendBuffer );
       
   336         message2->FetchContentToBufferL( contentBuf );    
       
   337         contentLength = message2->ContentLengthL();
       
   338         expectedLength = expectedContent.Length();
       
   339         CheckCondition( _L("content matches"), contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0 );
       
   340             
       
   341         appendBuffer.Fill( 'y', KBlockSize );
       
   342         expectedContent.SetLength( KSizesToTest[index] );
       
   343         expectedContent.Append( appendBuffer );
       
   344         message3->AppendToContentL( appendBuffer );
       
   345         message3->FetchContentToBufferL( contentBuf );    
       
   346         contentLength = message3->ContentLengthL();
       
   347         expectedLength = expectedContent.Length();
       
   348         CheckCondition( _L("content matches"), contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0 );
       
   349         
       
   350         appendBuffer.Fill( 'z', KBufferSize+1 );
       
   351         expectedContent.Append( appendBuffer );
       
   352         message3->AppendToContentL( appendBuffer );
       
   353         message3->FetchContentToBufferL( contentBuf );
       
   354         contentLength  = message3->ContentLengthL();
       
   355         expectedLength = expectedContent.Length();   
       
   356         CheckCondition( _L("content matches"), contentLength == expectedLength && contentBuf.Compare( expectedContent ) == 0 );
       
   357         
       
   358         expectedContent.SetLength( KSizesToTest[index] );
       
   359         
       
   360         index++;
       
   361         } // end for
       
   362         
       
   363     expectedContent.Close();
       
   364     contentBuf.Close();
       
   365         
       
   366     fs.Close();
       
   367 
       
   368     CleanupStack::PopAndDestroy( 9 );
       
   369     
       
   370     //we are done 
       
   371     return ETrue;
       
   372     }
       
   373 
       
   374 
       
   375 //  End of File