applayerpluginsandutils/bookmarksupport/test/tbackuptest.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2003-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 <bookmarkdatabase.h>
       
    17 #include "testutils.h"
       
    18 #include "tbookmarktests.h"
       
    19 
       
    20 #include <e32test.h>
       
    21 #include <f32file.h>
       
    22 #include <badesca.h>
       
    23 #include <connect/sbdefs.h>
       
    24 #include <connect/sbeclient.h>
       
    25 
       
    26 #include "srvrepos.h"
       
    27 #include "srvres.h"
       
    28 #include "cachemgr.h"
       
    29 #include "backup.h"
       
    30 
       
    31 #define TEST2(aValue, aExpected) ::CheckL(aValue, aExpected)
       
    32 
       
    33 _LIT ( KMyFolder, "BackupFolder" );
       
    34 _LIT(KTxtBookmark1, "Bookmark 1");
       
    35 _LIT8(KBookmarkUri, "www.ft.com");
       
    36 _LIT(KTitle, "Bookmark Backup Restore Test");
       
    37 LOCAL_D RTest gTest(KTitle());
       
    38 
       
    39 LOCAL_D CTestWrapper* gTestWrapper;
       
    40 CActiveScheduler* globalAS;
       
    41 
       
    42 CRepositoryBackupClient* backupClient;
       
    43 
       
    44 CDesC8ArraySeg* originalData1;
       
    45 CDesC8ArraySeg* originalData2;
       
    46 
       
    47 _LIT_SECURE_ID( SecureId1, 0x102078CB );
       
    48 
       
    49 _LIT( K10205af8, "10205AF8.txt" );
       
    50 _LIT( K10205af9, "10205AF9.txt" );
       
    51 _LIT( K10205afa, "10205AFA.txt" );
       
    52 _LIT( K10205afb, "10205AFB.txt" );
       
    53 	
       
    54 const TUid KUidBookmarkDatabaseRepository   = { 0x10205AF8 };
       
    55 const TUid KUidFolderRepository             = { 0x10205AF9 };
       
    56 const TUid KUidBookmarkRepository           = { 0x10205AFA };
       
    57 const TUid KUidIconRepository               = { 0x10205AFB };
       
    58 	
       
    59 
       
    60 LOCAL_C	void ResetRepositoryL ( TUid aUid )
       
    61 	{
       
    62 	CRepository* repository = CRepository::NewL ( aUid );
       
    63 	CleanupStack::PushL ( repository );
       
    64 	User::LeaveIfError ( repository->Reset() );	
       
    65 	CleanupStack::PopAndDestroy ( repository );
       
    66 	}
       
    67 
       
    68 
       
    69 
       
    70 /**
       
    71 Creates a database.
       
    72 Creates a folder.
       
    73 Creates a bookmark.
       
    74 */
       
    75 LOCAL_C void CreateBookmarkInFolderL()
       
    76 	{
       
    77 	
       
    78 	//This will ensure that the old repositories are deleted
       
    79 	ResetRepositoryL ( KUidBookmarkDatabaseRepository );
       
    80 	ResetRepositoryL ( KUidFolderRepository );
       
    81 	ResetRepositoryL ( KUidBookmarkRepository );
       
    82 	ResetRepositoryL ( KUidIconRepository );
       
    83 	
       
    84 	// Open the database
       
    85 	RBkDatabase database;
       
    86 	database.OpenL();
       
    87 	CleanupClosePushL(database);
       
    88 			
       
    89 	//Create a folder
       
    90 	RBkFolder folder = database.CreateFolderL(KMyFolder);
       
    91 	CleanupClosePushL(folder);
       
    92 		
       
    93 	// Create a bookmark in the new folder
       
    94 	RBkBookmark bmrk = database.CreateBookmarkL(&folder);
       
    95 	CleanupClosePushL(bmrk);
       
    96 		
       
    97 	// Set some bookmark properties
       
    98 	bmrk.SetTitleL(KTxtBookmark1);
       
    99 	bmrk.SetUriL(KBookmarkUri);
       
   100 		
       
   101 	// Close the bookmark
       
   102 	CleanupStack::PopAndDestroy(&bmrk);
       
   103 	
       
   104 	// Close the folder
       
   105 	CleanupStack::PopAndDestroy(&folder);
       
   106 	
       
   107 	// Commit the changes to the database and close it
       
   108 	database.CommitL();
       
   109 	CleanupStack::PopAndDestroy(&database);
       
   110 		
       
   111 	gTest.Printf(_L("\n*** Created database, folder, bookmark ***\n"));		
       
   112 	}
       
   113 	
       
   114 	
       
   115 /**
       
   116 Deletes all the files in the temporary repository folder.
       
   117 The temporary repository folder used is 10281FE5.
       
   118 */
       
   119 LOCAL_C void DeleteFilesL()
       
   120 	{
       
   121 	_LIT( KOldInstallFiles, "c:\\private\\10281FE5\\*.*" );
       
   122 	_LIT( KOldPersistFiles, "c:\\private\\10281FE5\\persists\\*.*" );
       
   123 	
       
   124 	CFileMan* fm = CFileMan::NewL( TServerResources::iFs );
       
   125 	CleanupStack::PushL( fm );
       
   126 
       
   127 	TInt r = fm->Delete( KOldInstallFiles );
       
   128 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   129 		User::Leave(r);
       
   130 	r = fm->Delete( KOldPersistFiles );
       
   131 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   132 		User::Leave(r);
       
   133 	
       
   134 	gTest.Printf(_L("\n*** Deleted files from temporary repository ***\n"));
       
   135 	CleanupStack::PopAndDestroy( fm );
       
   136 	}
       
   137 		
       
   138 		
       
   139 /**
       
   140 Deletes all the files in the original bookmarks repository folder.
       
   141 The bookmarks repository folder is 10202be9
       
   142 */
       
   143 LOCAL_C void DeleteRepositoryFilesL()
       
   144 	{
       
   145 	_LIT( KOldInstallFiles, "c:\\private\\10202be9\\*.cre" );
       
   146 	_LIT( KOldPersistFiles, "c:\\private\\10202be9\\persists\\*.cre" );
       
   147 	
       
   148 	CFileMan* fm = CFileMan::NewL( TServerResources::iFs );
       
   149 	CleanupStack::PushL( fm );
       
   150 
       
   151 	TInt r = fm->Delete( KOldInstallFiles );
       
   152 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   153 		User::Leave(r);
       
   154 	r = fm->Delete( KOldPersistFiles );
       
   155 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   156 		User::Leave(r);
       
   157 	
       
   158 	gTest.Printf(_L("\n*** Deleted files from original repository ***\n"));
       
   159 	CleanupStack::PopAndDestroy( fm );
       
   160 	}
       
   161 			
       
   162 
       
   163 LOCAL_C void CheckL( TInt aValue, TInt aExpected)
       
   164 	{
       
   165 	if ( aValue != aExpected )
       
   166 		{
       
   167 		DeleteFilesL();
       
   168 		RDebug::Print( _L( "*** Expected error: %d, got: %d\r\n"), aExpected, aValue );
       
   169 		}
       
   170 	}
       
   171 
       
   172 /**
       
   173 Does a copy of the config files from the ROM area to the temporary repository folder.
       
   174 The bookmark component has 4 config files 10205AF8.txt, 10205AF9.txt, 10205AFA.txt and
       
   175 10205AFB.txt.
       
   176 */	
       
   177 LOCAL_C void InstallFileSetL(const TDesC& afilename)
       
   178 	{
       
   179 	_LIT( KFileSrcPath, "\\private\\10202be9\\" );
       
   180 	_LIT( KInstallFileDestPath, "\\private\\10281FE5\\" );
       
   181 	_LIT( KPersistFileDestPath, "\\private\\10281FE5\\persists\\" );
       
   182 	_LIT( KDriveC, "c:" );
       
   183 	_LIT( KDriveZ, "z:" );
       
   184 
       
   185 	TBuf<KMaxFileName> dest;
       
   186 	TBuf<KMaxFileName> src;
       
   187 	TBuf<KMaxFileName> src2;
       
   188 	TBuf<KMaxFileName> dest2;
       
   189 	TInt r;
       
   190 
       
   191 	CFileMan* fm = CFileMan::NewL( TServerResources::iFs );
       
   192 	CleanupStack::PushL( fm );
       
   193 
       
   194 	dest.Copy( KDriveC );
       
   195 	dest.Append( KInstallFileDestPath );
       
   196 	dest.Append( afilename );
       
   197 	r = fm->Delete( dest );
       
   198 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   199 	User::Leave( r );
       
   200 	r = TServerResources::iFs.MkDirAll( dest );
       
   201 	if ( r != KErrNone && r != KErrAlreadyExists )
       
   202 	User::Leave( r );
       
   203 		
       
   204 	src.Copy( KDriveZ );
       
   205 	src.Append( KFileSrcPath );
       
   206 	src.Append( afilename );
       
   207 	User::LeaveIfError( fm->Copy( src, dest ) );
       
   208 	r = fm->Attribs( dest, KEntryAttArchive, KEntryAttReadOnly, TTime( 0 ), CFileMan::ERecurse );
       
   209 	TEST2( r, KErrNone );
       
   210 	
       
   211 	dest2.Copy( KDriveC );
       
   212 	dest2.Append( KPersistFileDestPath );
       
   213 	dest2.Append( afilename  );
       
   214 	r = fm->Delete( dest2 );
       
   215 	if ( r != KErrNone && r != KErrNotFound && r != KErrPathNotFound )
       
   216 	User::Leave( r );
       
   217 	r = TServerResources::iFs.MkDirAll( dest2 );
       
   218 	if ( r != KErrNone && r != KErrAlreadyExists )
       
   219 	User::Leave( r );
       
   220 	src2.Copy( KDriveZ );
       
   221 	src2.Append( KFileSrcPath );
       
   222 	src2.Append( afilename );
       
   223 	User::LeaveIfError( fm->Copy( src2, dest2 ) );
       
   224 	r = fm->Attribs( dest2, KEntryAttArchive, KEntryAttReadOnly, TTime( 0 ), CFileMan::ERecurse );
       
   225 	TEST2( r, KErrNone );
       
   226 	
       
   227 	CleanupStack::PopAndDestroy( fm );
       
   228 	}
       
   229 
       
   230 
       
   231 /**
       
   232 Does the backup of the repository.
       
   233 */
       
   234 LOCAL_C void BackupRepositoryL( CDesC8ArraySeg* aDataStream )
       
   235 	{
       
   236 	backupClient->CompleteOwnerIdLookupTableL();
       
   237 	
       
   238 	//The SecureId1 has to be the same as the value for [owner] tag in each of the
       
   239 	//config files
       
   240 	backupClient->InitialiseGetProxyBackupDataL( SecureId1, EDriveC );
       
   241 	
       
   242 	HBufC8* transferBuffer = HBufC8::NewL( 128 );
       
   243 	CleanupStack::PushL( transferBuffer );
       
   244 	TPtr8 bufferToSend = transferBuffer->Des();
       
   245 	TBool finished = EFalse;
       
   246 
       
   247 	aDataStream->Reset();
       
   248 	
       
   249 	do 
       
   250 		{
       
   251 		bufferToSend.Zero();
       
   252 		backupClient->GetBackupDataSectionL( bufferToSend, finished );
       
   253 		aDataStream->AppendL( bufferToSend );
       
   254 		} while ( !finished );
       
   255 		
       
   256 	gTest.Printf(_L("\n*** Backup process completed successfully ***\n"));	
       
   257 	CleanupStack::PopAndDestroy( transferBuffer );
       
   258 	}
       
   259 
       
   260 
       
   261 /**
       
   262 Does a restore of the backed up data.
       
   263 @param aDataStream
       
   264 	Carries the backed up data.	
       
   265 */
       
   266 LOCAL_C void RestoreRepositoryL( CDesC8ArraySeg* aDataStream )
       
   267 	{
       
   268 	backupClient->InitialiseRestoreProxyBaseDataL( SecureId1, EDriveC );
       
   269 	
       
   270 	HBufC8* transferBuffer = HBufC8::NewL( 128 );
       
   271 	CleanupStack::PushL( transferBuffer );
       
   272 	TPtr8 bufferToSend = transferBuffer->Des();
       
   273 
       
   274 	TBool finished = EFalse;
       
   275 	TInt count = aDataStream->Count();
       
   276 	TInt element = 0;
       
   277 	
       
   278 	for ( element = 0; element < count; element++ ) 
       
   279 		{
       
   280 		bufferToSend.Zero();
       
   281 		bufferToSend.Append( (*aDataStream)[ element ] );
       
   282 		if ( element + 1 == count )
       
   283 			finished = ETrue;
       
   284 		backupClient->RestoreBaseDataSectionL( bufferToSend, finished );
       
   285 		}
       
   286 	backupClient->RestoreComplete( EDriveC );
       
   287 		
       
   288 	gTest.Printf(_L("\n*** Restoration completed successfully ***\n"));
       
   289 	CleanupStack::PopAndDestroy( transferBuffer );
       
   290 	}
       
   291 
       
   292 
       
   293 /**
       
   294 Initializes the descriptors required for the backup and restore process
       
   295 */
       
   296 LOCAL_C void InitializationLC()    
       
   297     {
       
   298     originalData1 = new(ELeave)CDesC8ArraySeg( 128 );
       
   299 	CleanupStack::PushL( originalData1 );
       
   300 	originalData2 = new(ELeave)CDesC8ArraySeg( 128 );
       
   301 	CleanupStack::PushL( originalData2 );
       
   302 	}
       
   303 		
       
   304 		
       
   305 /**
       
   306 Cleans up the used resources
       
   307 */
       
   308 LOCAL_C void CleanupL()
       
   309     {
       
   310     CleanupStack::PopAndDestroy( originalData2 );
       
   311     CleanupStack::PopAndDestroy( originalData1 );
       
   312     }
       
   313 
       
   314 
       
   315 /**
       
   316 Move the restored (.cre) files from the temporary repository folder to the original repository 
       
   317 folder.
       
   318 */
       
   319 LOCAL_C void MoveFilesToOriginalRepositoryL()
       
   320 {
       
   321 	_LIT( KFileSrcPath, "\\private\\10281FE5\\persists\\" );
       
   322 	_LIT( KFileDestPath, "\\private\\10202be9\\persists\\" );
       
   323 	_LIT( KDriveC, "c:" );
       
   324 	_LIT( KAllFiles, "*.*");
       
   325 	
       
   326 	TBuf<KMaxFileName> dest;
       
   327 	TBuf<KMaxFileName> src;
       
   328 	TInt r;
       
   329 	
       
   330 	CFileMan* fm = CFileMan::NewL( TServerResources::iFs );
       
   331 						CleanupStack::PushL( fm );
       
   332 
       
   333 	dest.Copy( KDriveC );
       
   334 	dest.Append( KFileDestPath );
       
   335 	dest.Append( KAllFiles );
       
   336 	r = TServerResources::iFs.MkDirAll( dest );
       
   337 	if ( r != KErrNone && r != KErrAlreadyExists )
       
   338 	User::Leave( r );
       
   339 	src.Copy( KDriveC );
       
   340 	src.Append( KFileSrcPath );
       
   341 	src.Append( KAllFiles );
       
   342 	User::LeaveIfError( fm->Copy( src, dest ) );
       
   343 	r = fm->Attribs( dest, KEntryAttArchive, KEntryAttReadOnly, TTime( 0 ), CFileMan::ERecurse );
       
   344 	TEST2( r, KErrNone );
       
   345 	
       
   346 	gTest.Printf(_L("\n*** Restored files to original repository ***\n"));
       
   347 	CleanupStack::PopAndDestroy( fm );
       
   348 }
       
   349 
       
   350 
       
   351 LOCAL_D TBool IsItemInFolderL(const TDesC& aName, RBkFolder& aFolder)	
       
   352 	{
       
   353 	RBkNode item;
       
   354 	TBool found = EFalse;
       
   355 	TInt index = aFolder.Count() - 1;
       
   356 	for (;index >= 0; --index)
       
   357 		{
       
   358 		item = aFolder.OpenItemL(index);
       
   359 		CleanupClosePushL(item);
       
   360 		const TDesC& title = item.Title();
       
   361 		if (title.Compare(aName) == 0)
       
   362 			{
       
   363 			found = ETrue;
       
   364 			}
       
   365 		CleanupStack::PopAndDestroy(&item);
       
   366 		if (found)
       
   367 			{
       
   368 			break;
       
   369 			}
       
   370 		}
       
   371 	return found;
       
   372 	}
       
   373 	
       
   374 
       
   375 /**
       
   376 Checks if the Repository is restored along with the foleder and the bookmark created
       
   377 previously
       
   378 */
       
   379 LOCAL_C void CheckRepositoryL()
       
   380 {
       
   381 	RBkDatabase database1;
       
   382 	database1.OpenL();
       
   383 	CleanupClosePushL(database1);
       
   384 	
       
   385 	RBkFolder folder;
       
   386 	TRAPD(err1, folder = database1.OpenFolderL(KMyFolder));
       
   387 	gTestWrapper->TEST(err1 == KErrNone);
       
   388 	CleanupClosePushL(folder);
       
   389 	
       
   390 	gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark1, folder));
       
   391 	
       
   392 	CleanupStack::PopAndDestroy(&folder);
       
   393 	CleanupStack::PopAndDestroy(&database1);
       
   394 }
       
   395 
       
   396 
       
   397 
       
   398 /**
       
   399 Creates a bookmark database. Cretes a temporary repository folder with the repositories in it. 
       
   400 Does a backup and a restore to the temporary repository and then to the original repository and 
       
   401 deletes the temporary repository.
       
   402 */
       
   403 LOCAL_C void INC081573L()
       
   404 	{
       
   405 	// create and install the active scheduler we need
       
   406 	CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
   407 	CleanupStack::PushL( s );
       
   408 	CActiveScheduler::Replace( s );
       
   409 		
       
   410 	//Create folder and bookmark
       
   411 	CreateBookmarkInFolderL();
       
   412 	
       
   413 	backupClient = CRepositoryBackupClient::NewLC(TServerResources::iFs);
       
   414 		
       
   415 	TServerResources::iCacheManager->DisableCache();
       
   416 	
       
   417 	gTest.Printf(_L("\n*** File installation started ***\n"));
       
   418 	
       
   419 	// Install known files
       
   420 	InstallFileSetL(K10205af8);
       
   421 	InstallFileSetL(K10205af9);
       
   422 	InstallFileSetL(K10205afa);
       
   423 	InstallFileSetL(K10205afb);
       
   424 	
       
   425 	gTest.Printf(_L("\n*** File installation completed successfully ***\n"));
       
   426 	
       
   427 	// backup known files, 
       
   428 	BackupRepositoryL( originalData1 );
       
   429 
       
   430 	// delete files
       
   431 	DeleteFilesL();
       
   432 	
       
   433 	//delete repository files
       
   434 	DeleteRepositoryFilesL();
       
   435 	
       
   436 	// restore when other files do not exist
       
   437 	RestoreRepositoryL( originalData1 );
       
   438 	
       
   439 	//move files back to repository
       
   440 	MoveFilesToOriginalRepositoryL();
       
   441 	
       
   442 	//delete the temporary repository
       
   443 	DeleteFilesL();
       
   444 	
       
   445 	CleanupStack::PopAndDestroy( backupClient );
       
   446 	
       
   447 	// Cleanup the scheduler
       
   448 	CActiveScheduler::Replace( globalAS );
       
   449 	CleanupStack::PopAndDestroy( s );
       
   450 	
       
   451 	CheckRepositoryL();
       
   452 	}
       
   453 
       
   454 
       
   455 /**
       
   456 Starts the backup and restore tests
       
   457 */
       
   458 void DoBackupTestL()
       
   459 	{
       
   460 	InitializationLC();
       
   461 	
       
   462 	INC081573L();
       
   463 	
       
   464 	TServerResources::iOwnerIdLookUpTable.Reset();
       
   465 	
       
   466 	CleanupL();
       
   467 	}
       
   468 
       
   469 
       
   470 LOCAL_C void MainL()
       
   471 	{
       
   472 	__UHEAP_MARK;
       
   473 	gTest.Start( _L( "\n*** Backup and restore tests ***\n" ) );
       
   474 	
       
   475 	gTest.Printf(_L("@SYMTestCaseID IWS-APPPROTOCOLS-BOOKMARKS-TBACKUPTEST-0001 "));	
       
   476 	
       
   477 	// create and install the active scheduler we need for the cache manager in TServerResources::InitialiseL
       
   478 	globalAS=new(ELeave) CActiveScheduler;
       
   479 	CleanupStack::PushL(globalAS);
       
   480 	CActiveScheduler::Install(globalAS);
       
   481 
       
   482 	
       
   483 	TServerResources::InitialiseL();
       
   484 	
       
   485 	gTestWrapper = CTestWrapper::NewLC(gTest);
       
   486 	gTestWrapper->Start(_L("\n*** Bookmark Test ***\n"));
       
   487 
       
   488 	DoBackupTestL();
       
   489 
       
   490 	gTestWrapper->End();
       
   491 	CleanupStack::PopAndDestroy(gTestWrapper);
       
   492 
       
   493 	TServerResources::Close();
       
   494 	CleanupStack::PopAndDestroy(globalAS);
       
   495 	
       
   496 	gTest.End();
       
   497 	gTest.Close();
       
   498 	__UHEAP_MARKEND;
       
   499 	}
       
   500 
       
   501 
       
   502 TInt E32Main()
       
   503 	{	
       
   504 	__UHEAP_MARK;
       
   505 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   506 	if ( !cleanup )
       
   507 		return KErrNoMemory;
       
   508 
       
   509 	TRAPD( err, MainL()  );
       
   510 	if ( err != KErrNone )
       
   511 		User::Panic( _L( "Testing failed: " ), err );
       
   512 	
       
   513 	delete cleanup;
       
   514 	__UHEAP_MARKEND;
       
   515 	
       
   516 	return 0;	
       
   517 	}
       
   518 
       
   519