emailservices/emailstore/tsrc/message_store_test/src/EncryptionTests.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 "EncryptionTests.h"
       
    21 //#include <ismsgstorepropertykeys.h>
       
    22 //#include "messagestoreclientserver.h"
       
    23 
       
    24 // This must match that's defined in ContainerStoreDefs.h
       
    25 enum TEncryptionState
       
    26 {
       
    27     EMsgStoreESIdle,
       
    28     EMsgStoreESEncrypting,
       
    29     EMsgStoreESDecrypting
       
    30 }; // end TEncryptionState
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 CEncryptionTests* CEncryptionTests::NewL( CStifLogger* aLog )
       
    35     {
       
    36     CEncryptionTests* self = new(ELeave) CEncryptionTests( aLog );
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // Constructor
       
    45 // -----------------------------------------------------------------------------
       
    46 CEncryptionTests::CEncryptionTests( CStifLogger* aLog ) : CAsyncTestCaseBase( aLog ) 
       
    47     {
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // Destructor
       
    52 // -----------------------------------------------------------------------------
       
    53 CEncryptionTests::~CEncryptionTests()
       
    54     {
       
    55     iTestContentArray.Reset();
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // 2nd phase constructor
       
    60 // -----------------------------------------------------------------------------
       
    61 void CEncryptionTests::ConstructL()
       
    62     {
       
    63     CAsyncTestCaseBase::ConstructL();
       
    64     iTestContentArray.AppendL( _L8("This is the first test content!") );
       
    65     iTestContentArray.AppendL( _L8("This is the second test content!") );
       
    66     iTestContentArray.AppendL( _L8("This is the first test content!") );
       
    67     iTestContentArray.AppendL( _L8("What-ever!!!!") );
       
    68     iTestContentArray.AppendL( _L8("ok fine!!!") );
       
    69     iTestContentArray.AppendL( _L8("Here is a long content (>300 bytes).   Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.") );
       
    70     iTestContentArray.AppendL( _L8("Is that long enough") );
       
    71     iTestContentArray.AppendL( _L8("aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff") );
       
    72     iTestContentArray.AppendL( _L8("Here is a >300 unicode characters (> 600 bytes).   Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.  Here is a long content.") );
       
    73     iTestContentArray.AppendL( _L8("Hi") );
       
    74     iTestContentArray.AppendL( _L8("") );
       
    75     iTestContentArray.AppendL( _L8("This is another test content") );
       
    76     iTestContentArray.AppendL( _L8("How many more?") );
       
    77     iTestContentArray.AppendL( _L8("Six more contents to go") );
       
    78     iTestContentArray.AppendL( _L8("Five more contents to go") );
       
    79     iTestContentArray.AppendL( _L8("     ") );
       
    80     iTestContentArray.AppendL( _L8("Three more...") );
       
    81     iTestContentArray.AppendL( _L8("Two more...") );
       
    82     iTestContentArray.AppendL( _L8("One more...") );
       
    83     iTestContentArray.AppendL( _L8("Last one! We are done") );
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // ExecuteL : start executing the test case
       
    88 // -----------------------------------------------------------------------------
       
    89 TBool CEncryptionTests::ExecuteL()
       
    90     {
       
    91     LogHeader( _L("Encryption") );
       
    92     
       
    93     iLog->Log( _L("Case %d: Testing SetPasswordL()"), iCaseId++ );
       
    94     iMsgStore->SetPasswordL( _L("mypassword") );
       
    95     
       
    96     iLog->Log( _L("Case %d: Testing AuthenticatedL()"), iCaseId++ );
       
    97     CheckCondition( _L("AuthenticatedL() is TRUE"), iMsgStore->AuthenticatedL() );
       
    98     
       
    99     iLog->Log( _L("Case %d: Testing HasPasswordL()"), iCaseId++ );
       
   100     CheckCondition( _L("HasPasswordL() is TRUE"), iMsgStore->HasPasswordL() );
       
   101     
       
   102     iLog->Log( _L("Case %d: Create Mailboxes"), iCaseId++ );
       
   103     RPointerArray<CMsgStoreAccount> accountArray;
       
   104     CMsgStoreAccount* account1 = CMsgStoreAccount::NewL(1234, _L("Account 1") );
       
   105     accountArray.Append( account1 );                                            
       
   106     
       
   107     CMsgStoreAccount* account2 = CMsgStoreAccount::NewL(1234, _L("") );
       
   108     accountArray.Append( account2 );                                            
       
   109     
       
   110     CMsgStoreAccount* account3 = CMsgStoreAccount::NewL(5678, _L("Account 3") );
       
   111     accountArray.Append( account3 );                                            
       
   112     
       
   113     CMsgStoreMailBox* mailBox1 = iMsgStore->CreateAccountL( *account1 );
       
   114     CleanupStack::PushL( mailBox1 );                                            //+1
       
   115     
       
   116     CMsgStoreMailBox* mailBox2 = iMsgStore->CreateAccountL( *account2 );
       
   117     CleanupStack::PushL( mailBox2 );                                            //+2
       
   118     
       
   119     CMsgStoreMailBox* mailBox3 = iMsgStore->CreateAccountL( *account3 );
       
   120     CleanupStack::PushL( mailBox3 );                                            //+3
       
   121     
       
   122     CMsgStorePropertyContainer* properties = CMsgStorePropertyContainer::NewL();
       
   123     CleanupStack::PushL( properties );                                          //+4
       
   124     
       
   125     properties->AddPropertyL( _L8("1"), _L("PROPERTY 1") );
       
   126     properties->AddPropertyL( _L8("2"), 12345 );
       
   127     properties->AddPropertyL( _L8("3"), _L8("ANOTHER PROPERTY") );
       
   128     
       
   129     iLog->Log( _L("Case %d: Create Folders"), iCaseId++ );
       
   130     
       
   131     TMsgStoreId inbox1Id = mailBox1->CreateFolderL( mailBox1->Id(), *properties );
       
   132     TMsgStoreId sentFolderId = mailBox2->CreateFolderL( mailBox2->Id(), *properties );
       
   133     TMsgStoreId deletedFolderId = mailBox3->CreateFolderL( mailBox3->Id(), *properties );
       
   134     
       
   135     iLog->Log( _L("  inbox=%x, sent=%x, deleted=%x"), inbox1Id, sentFolderId, deletedFolderId  );
       
   136     
       
   137     TInt msgCountPerFolder = 20;
       
   138     
       
   139     iLog->Log( _L("Case %d: Create messages in Inbox"), iCaseId++ );
       
   140     //keep ids, message ids will be stored in iMatches
       
   141     CreateRandomMessagesL( mailBox1, inbox1Id, msgCountPerFolder, ETrue ); 
       
   142     
       
   143     iLog->Log( _L("Case %d: Create messages in Sent folder"), iCaseId++ );
       
   144     CreateRandomMessagesL( mailBox2, sentFolderId, msgCountPerFolder );
       
   145     
       
   146     iLog->Log( _L("Case %d: Create messages in Deleted folder"), iCaseId++ );
       
   147     CreateRandomMessagesL( mailBox3, deletedFolderId, msgCountPerFolder );
       
   148     
       
   149     iLog->Log( _L("Case %d: Set msg contents and store them for comparison"), iCaseId++ );
       
   150     for ( TInt i = 0 ; i < iMatches.Count() ; i++ )
       
   151         {
       
   152         CMsgStoreMessage* msg = mailBox1->FetchMessageL( iMatches[i], inbox1Id );
       
   153         SetMessageContentL( msg, iTestContentArray[i] );
       
   154         iMessages.Append( msg );
       
   155         }
       
   156 
       
   157     iLog->Log( _L("Case %d: Store Msg Ids Sorted by Subject and Sender for comparison"), iCaseId++ );
       
   158     RArray<TMsgStoreId> idsSortdBySubject;
       
   159     GetSortedIdsL( mailBox2, sentFolderId, EMsgStoreSortBySubject, msgCountPerFolder, idsSortdBySubject );
       
   160     
       
   161     RArray<TMsgStoreId> idsSortdByFrom;
       
   162     GetSortedIdsL( mailBox3, deletedFolderId, EMsgStoreSortBySender, msgCountPerFolder, idsSortdByFrom );
       
   163     
       
   164     iLog->Log( _L("Case %d: Start Encryption"), iCaseId++ );
       
   165     iMsgStore->EnableEncryptionL();
       
   166     
       
   167     iLog->Log( _L("  Wait until Encryption completes") );
       
   168     WaitUntilEncryptDecryptCompleteL( EMsgStoreESEncrypting, mailBox1 );
       
   169     
       
   170     iLog->Log( _L("  Encryption completed") );
       
   171     
       
   172     iLog->Log( _L("Case %d: Comparing messages"), iCaseId++ );
       
   173     CompareMessagesL( mailBox1 );
       
   174     
       
   175     iLog->Log( _L("Case %d: Comparing Accounts"), iCaseId++ );
       
   176     CompareAccountsL( iMsgStore, accountArray );
       
   177 
       
   178     iLog->Log( _L("Case %d: Comparing msg IDs sorted by Subject"), iCaseId++ );
       
   179     RArray<TMsgStoreId> aSortedIds;
       
   180     GetSortedIdsL( mailBox2, sentFolderId, EMsgStoreSortBySubject, msgCountPerFolder, aSortedIds );
       
   181     CompareSortedIdArraysL( aSortedIds, idsSortdBySubject );
       
   182     aSortedIds.Reset();
       
   183     
       
   184     iLog->Log( _L("Case %d: Comparing msg IDs sorted by Sender"), iCaseId++ );
       
   185     GetSortedIdsL( mailBox3, deletedFolderId, EMsgStoreSortBySender, msgCountPerFolder, aSortedIds );
       
   186     CompareSortedIdArraysL( aSortedIds, idsSortdByFrom );
       
   187     aSortedIds.Reset();
       
   188     
       
   189     iLog->Log( _L("Case %d: Start Decryption"), iCaseId++ );
       
   190     
       
   191     iMsgStore->DisableEncryptionL();
       
   192     
       
   193     iLog->Log( _L("  Wait until Decryption completes") );
       
   194     
       
   195     WaitUntilEncryptDecryptCompleteL( EMsgStoreESDecrypting, mailBox1 );
       
   196     
       
   197     iLog->Log( _L("  Decryption completed") );
       
   198     
       
   199     iLog->Log( _L("Case %d: Comparing messages"), iCaseId++ );
       
   200     CompareMessagesL( mailBox1 );
       
   201     
       
   202     iLog->Log( _L("Case %d: Comparing Accounts"), iCaseId++ );
       
   203     CompareAccountsL( iMsgStore, accountArray );
       
   204     
       
   205     iLog->Log( _L("Case %d: Comparing msg IDs sorted by Subject"), iCaseId++ );
       
   206     GetSortedIdsL( mailBox2, sentFolderId, EMsgStoreSortBySubject, msgCountPerFolder, aSortedIds );
       
   207     CompareSortedIdArraysL( aSortedIds, idsSortdBySubject );
       
   208     aSortedIds.Reset();
       
   209     
       
   210     iLog->Log( _L("Case %d: Comparing msg IDs sorted by Sender"), iCaseId++ );
       
   211     GetSortedIdsL( mailBox3, deletedFolderId, EMsgStoreSortBySender, msgCountPerFolder, aSortedIds );
       
   212     CompareSortedIdArraysL( aSortedIds, idsSortdByFrom );
       
   213     aSortedIds.Reset();
       
   214     
       
   215     idsSortdBySubject.Close();
       
   216     idsSortdByFrom.Close();
       
   217     accountArray.ResetAndDestroy();
       
   218     iMatches.Reset();
       
   219     iMessages.ResetAndDestroy();
       
   220     
       
   221     CleanupStack::PopAndDestroy( 4 );
       
   222     
       
   223     //test authentication
       
   224     iLog->Log( _L("Case %d: Test AuthenticateL"), iCaseId++ );
       
   225     TRAPD(err, iMsgStore->AuthenticateL( _L("mypassword")) );
       
   226     CheckCondition( _L("AuthenticateL successful"), err == KErrNone );
       
   227     
       
   228     iLog->Log( _L("Case %d: Test ChangePasswordL"), iCaseId++ );
       
   229     TRAP(err, iMsgStore->ChangePasswordL( _L("mypassword"), _L("newpassword") ) );
       
   230     CheckCondition( _L("ChangePasswordL successful"), err == KErrNone );
       
   231         
       
   232     iLog->Log( _L("Case %d: Test ClearAuthenticationL"), iCaseId++ );
       
   233     TRAP(err, iMsgStore->ClearAuthenticationL() );
       
   234     CheckCondition( _L("ClearAuthenticationL successful"), err == KErrNone );
       
   235     
       
   236     //we are done 
       
   237     return ETrue;
       
   238     }
       
   239 
       
   240 
       
   241 void CEncryptionTests::WaitUntilEncryptDecryptCompleteL( TInt aCurrentState, CMsgStoreMailBox* aMailBox )
       
   242     {
       
   243     RDebugSession debugSession;
       
   244     User::LeaveIfError( debugSession.Connect() );
       
   245     
       
   246     TInt encryptionState = aCurrentState; 
       
   247     TInt loopCount = 0;
       
   248     TMsgStoreId folderId;
       
   249     while( encryptionState !=  EMsgStoreESIdle && loopCount < 4000 )
       
   250         {
       
   251         Yield( 20000 );
       
   252         loopCount++;
       
   253         if ( loopCount % 20 == 0 )
       
   254             {
       
   255             encryptionState = debugSession.GetEncryptionStateL();
       
   256             }
       
   257         
       
   258         if ( loopCount == 2 && encryptionState == aCurrentState )
       
   259             {
       
   260             iLog->Log( _L("Case %d: Testing fetch msgs while encryption/decription is in progress"), iCaseId++ );
       
   261             CompareMessagesL( aMailBox );
       
   262             }
       
   263         
       
   264         if ( loopCount == 5 && encryptionState == aCurrentState )
       
   265             {
       
   266             iLog->Log( _L("Case %d: Testing create msgs while encryption/decription is in progress"), iCaseId++ );
       
   267             CMsgStorePropertyContainer* properties = CMsgStorePropertyContainer::NewL();
       
   268             folderId = aMailBox->CreateFolderL( aMailBox->Id(), *properties);
       
   269             delete properties;
       
   270             CreateRandomMessagesL(aMailBox, folderId, 5);
       
   271             }
       
   272         
       
   273         if ( loopCount == 8 && encryptionState == aCurrentState )
       
   274             {
       
   275             iLog->Log( _L("Case %d: Testing delete folder while encryption/decription is in progress"), iCaseId++ );
       
   276             aMailBox->DeleteFolderL(folderId);
       
   277             }
       
   278         
       
   279         if ( encryptionState != aCurrentState && encryptionState != EMsgStoreESIdle )
       
   280             {
       
   281             break;
       
   282             }
       
   283         }
       
   284     
       
   285     debugSession.Close();
       
   286     if ( encryptionState != EMsgStoreESIdle )
       
   287         {
       
   288         iLog->Log( _L("  Encryption did NOT complete") );
       
   289         User::Leave( KErrCancel );
       
   290         }
       
   291     }
       
   292 
       
   293 void CEncryptionTests::CompareMessagesL( CMsgStoreMailBox* aMailBox )
       
   294     {
       
   295     iLog->Log( _L("  >> CompareMessagesL") );
       
   296     
       
   297     RBuf8 contentBuf;
       
   298     contentBuf.Create( 1024 );
       
   299     contentBuf.CleanupClosePushL();
       
   300     
       
   301     for( TInt i = 0 ; i < iMessages.Count() ; i++ )
       
   302         {
       
   303         CMsgStoreMessage* msg = aMailBox->FetchMessageL( iMessages[i]->Id(), KMsgStoreInvalidId );
       
   304         CleanupStack::PushL( msg );                               //+1
       
   305         
       
   306         ComparePropertiesL( *msg, *(iMessages[i]) );
       
   307         
       
   308         iLog->Log( _L("   Comparing contents...") );
       
   309         
       
   310         RPointerArray<CMsgStoreMessagePart> parts;
       
   311         msg->ChildPartsL( parts );
       
   312         CMsgStoreMessagePart* part =parts[0];
       
   313         
       
   314         RPointerArray<CMsgStoreMessagePart> childParts;
       
   315         part->ChildPartsL( childParts );
       
   316         CMsgStoreMessagePart* plain_text_part = childParts[0];
       
   317         
       
   318         plain_text_part->FetchContentToBufferL( contentBuf );
       
   319         if ( contentBuf == iTestContentArray[i] )
       
   320             {
       
   321             iLog->Log( _L("   Contents match!") );
       
   322             }
       
   323         else
       
   324             {
       
   325             iLog->Log( _L("   Error: Contents don't match! index=%d"), i );
       
   326             User::Leave( KErrNotFound );
       
   327             }
       
   328 
       
   329         childParts.ResetAndDestroy();
       
   330         parts.ResetAndDestroy();
       
   331         CleanupStack::PopAndDestroy( msg );
       
   332         }
       
   333     CleanupStack::PopAndDestroy( &contentBuf );
       
   334     
       
   335     iLog->Log( _L("  << CompareMessagesL - PASSED") );
       
   336     }
       
   337 
       
   338 void CEncryptionTests::CompareAccountsL( CMsgStore* aSession, RPointerArray<CMsgStoreAccount>& aAccounts )
       
   339     {
       
   340     iLog->Log( _L("  >> CompareAccountsL") );
       
   341     
       
   342     RPointerArray<CMsgStoreAccount> accs;
       
   343     aSession->AccountsL( accs );
       
   344 
       
   345     CheckCondition( _L("Counts match"), accs.Count() == aAccounts.Count() );
       
   346     
       
   347     for ( TInt i = 0 ; i < accs.Count() ; i++ )
       
   348         {
       
   349         CMsgStoreAccount* acc = accs[i];
       
   350         TBool found = EFalse;
       
   351         for (TInt j = 0 ; !found && j < aAccounts.Count() ; j++ )
       
   352             {
       
   353             CMsgStoreAccount* acc2 = aAccounts[j];
       
   354             if ( acc->Owner() == acc2->Owner() && acc->Name() == acc2->Name() )
       
   355                 {
       
   356                 found = ETrue;
       
   357                 }
       
   358             }
       
   359         if ( !found )
       
   360             {
       
   361             iLog->Log( _L("  Account:%X %S not found!"), acc->Owner(), &( acc->Name() ) );
       
   362             User::Leave(KErrNotFound);
       
   363             }
       
   364         }
       
   365     
       
   366     accs.ResetAndDestroy();
       
   367     
       
   368     iLog->Log( _L("  << CompareAccountsL - PASSED") );
       
   369     }
       
   370 
       
   371 void CEncryptionTests::GetSortedIdsL( CMsgStoreMailBox* aMailBox, TMsgStoreId aFolderId, TMsgStoreSortByField aSortedBy, TInt aCount, RArray<TMsgStoreId>& aSortedIds )
       
   372     {
       
   373     iLog->Log( _L("  >> GetSortedIdsL") );
       
   374     
       
   375     RMsgStoreSortCriteria criteria;
       
   376     CleanupClosePushL( criteria );
       
   377     
       
   378     criteria.iFolderId = aFolderId;
       
   379     criteria.AddResultPropertyL( KMsgStorePropertyFrom );
       
   380     criteria.AddResultPropertyL( KMsgStorePropertySubject );
       
   381     criteria.iSortBy   = aSortedBy;
       
   382     criteria.iSortOrder = EMsgStoreSortDescending;
       
   383     
       
   384     CMsgStoreSortResultIterator* iterator = aMailBox->SortL( criteria ); 
       
   385     CleanupStack::PushL( iterator );
       
   386     
       
   387     RPointerArray<CMsgStorePropertyContainer> results;
       
   388     
       
   389     iterator->NextL( KMsgStoreSortResultTop, aCount, results );
       
   390     for ( TInt i = 0 ; i < results.Count() ; i++ )
       
   391         {
       
   392         TMsgStoreId msgId = results[i]->Id();
       
   393         aSortedIds.Append( msgId );
       
   394         }
       
   395 
       
   396     results.ResetAndDestroy();
       
   397     CleanupStack::PopAndDestroy( iterator );
       
   398     CleanupStack::PopAndDestroy( &criteria );
       
   399     
       
   400     iLog->Log( _L("  << GetSortedIdsL") );
       
   401     }
       
   402 
       
   403 void CEncryptionTests::CompareSortedIdArraysL( RArray<TMsgStoreId>& aIDs1, RArray<TMsgStoreId>& aIDs2 )
       
   404     {
       
   405     iLog->Log( _L("  >> CompareSortedIdArraysL") );
       
   406     
       
   407     CheckCondition(_L("Counts match"), aIDs1.Count() == aIDs2.Count() );
       
   408     
       
   409     for ( TInt i = 0 ; i < aIDs1.Count() ; i++ )
       
   410         {
       
   411         if ( aIDs1[i] != aIDs2[i] )
       
   412             {
       
   413             iLog->Log( _L("  Arrays don't match. Index=%d, val1=%u, val2=%u") , i, aIDs1[i], aIDs2[i] );
       
   414             User::Leave( KErrNotFound );
       
   415             }
       
   416         }
       
   417     
       
   418     iLog->Log( _L("  << CompareSortedIdArraysL - PASSED") );
       
   419     }
       
   420 
       
   421 //  End of File