persistentstorage/centralrepository/test/t_cenrep_helper.cpp
changeset 0 08ec8eefde2f
child 55 44f437012c90
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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 //
       
    15 
       
    16 #include "t_cenrep_helper.h"
       
    17 #include "srvdefs.h"
       
    18 #include <f32file.h> // RFs
       
    19 #include <e32test.h> // RTest
       
    20 #include <hal.h>
       
    21 #include "cachemgr.h"
       
    22 #include "clirep.h"
       
    23 
       
    24 #if defined(__CENTREP_SERVER_PERFTEST__) || defined(__CENTREP_SERVER_MEMTEST__) || defined(__CENTREP_SERVER_CACHETEST__)
       
    25 	#include "srvreqs.h"
       
    26 	#define CONDITIONAL_PARAM(x) x
       
    27 #else
       
    28 	#define CONDITIONAL_PARAM(x)
       
    29 #endif
       
    30 
       
    31 void PatchDrive(TDes& aPath)
       
    32 	{
       
    33 	TDriveNumber sysdrive = RFs::GetSystemDrive();
       
    34 	aPath[0] = 'a' + sysdrive-EDriveA; // Replace drive letter only.
       
    35 	}
       
    36 
       
    37 EXPORT_C void printDirL(const TDesC& aDirName)
       
    38 	{
       
    39 	CDir* fileList=NULL;
       
    40 	RFs fs;
       
    41 	User::LeaveIfError(fs.Connect());
       
    42 	CleanupClosePushL(fs);
       
    43 
       
    44 	TInt r = fs.GetDir(aDirName,KEntryAttNormal, ESortByDate, fileList);
       
    45 
       
    46 	if (r==KErrPathNotFound)
       
    47 		{
       
    48 		RDebug::Print(_L("No directory %S"), &aDirName);
       
    49 		}
       
    50 	else if (r==KErrNone)
       
    51 		{
       
    52 		TInt fileCount=fileList->Count();
       
    53 		RDebug::Print( _L("%02d files in %S\n"),fileCount, &aDirName);
       
    54 		for (TInt i = 0;i < fileCount; ++i)
       
    55 			{
       
    56 			TEntry entry=(*fileList)[i];
       
    57 
       
    58 			RDebug::Print( _L("File[%02d] - %S \n"),
       
    59 							i,
       
    60 							&(entry.iName)
       
    61 							);
       
    62 			}
       
    63 		}
       
    64 	else
       
    65 		{
       
    66 		RDebug::Print(_L("Error getting contents of directory %S"), &aDirName);
       
    67 		}
       
    68 	delete fileList;
       
    69 	CleanupStack::PopAndDestroy();	//fs
       
    70 	}
       
    71 
       
    72 EXPORT_C TInt KillProcess(const TDesC& aProcessName)
       
    73 	{
       
    74 	TFullName name;
       
    75 
       
    76 	RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
       
    77 
       
    78 	TBuf<64> pattern(aProcessName);
       
    79 	TInt length = pattern.Length();
       
    80 	pattern += _L("*");
       
    81 	TFindProcess procFinder(pattern);
       
    82 
       
    83 	while (procFinder.Next(name) == KErrNone)
       
    84 		{
       
    85 		if (name.Length() > length)
       
    86 			{//If found name is a string containing aProcessName string.
       
    87 			TChar c(name[length]);
       
    88 			if (c.IsAlphaDigit() ||
       
    89 				c == TChar('_') ||
       
    90 				c == TChar('-'))
       
    91 				{
       
    92 				// If the found name is other valid application name
       
    93 				// starting with aProcessName string.
       
    94 				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
       
    95 				continue;
       
    96 				}
       
    97 			}
       
    98 		RProcess proc;
       
    99 		if (proc.Open(name) == KErrNone)
       
   100 			{
       
   101 			proc.Kill(0);
       
   102 			RDebug::Print(_L("\"%S\" process killed.\n"), &name);
       
   103 			}
       
   104 		proc.Close();
       
   105 		}
       
   106 	return KErrNone;
       
   107 	}
       
   108 
       
   109 const TInt KSmallDelay = 2*1000;
       
   110 
       
   111 //File cleanup function
       
   112 EXPORT_C void CleanupCDriveL(TBool aRemoveRomCache)
       
   113 	{
       
   114 	RFs fs;
       
   115 	User::LeaveIfError(fs.Connect());
       
   116 	CleanupClosePushL(fs);
       
   117 
       
   118 	CFileMan* fm = CFileMan::NewL(fs);
       
   119 	CleanupStack::PushL(fm);
       
   120 	HBufC* file_buf = HBufC::NewLC(KMaxFileName);
       
   121 	TPtr file(file_buf->Des());
       
   122 	file.Copy(KCPersistsFiles);
       
   123 	PatchDrive(file);
       
   124 	TInt r = fm->Delete(file);
       
   125 
       
   126 	if (r != KErrNone &&
       
   127 		r != KErrNotFound &&
       
   128 		r != KErrPathNotFound)
       
   129 		{
       
   130 		User::Leave(r);
       
   131 		}
       
   132 		
       
   133 	if(aRemoveRomCache)
       
   134 		{
       
   135 		//Delete cached rom version file
       
   136 		file.Copy(KCRomVersionFiles);
       
   137 		PatchDrive(file);
       
   138 		fm->Attribs(file, 0, KEntryAttReadOnly, TTime(0), 0);
       
   139 		r = fm->Delete(file);
       
   140 		if (r != KErrNone &&
       
   141 			r != KErrNotFound &&
       
   142 			r != KErrPathNotFound &&
       
   143 			r != KErrPermissionDenied)
       
   144 			{
       
   145 			User::Leave(r);
       
   146 			}
       
   147 		
       
   148 		// Delete all install files
       
   149 		file.Copy(KCInstallFiles);
       
   150 		PatchDrive(file);	
       
   151 		r = fm->Delete(file);
       
   152 		if (r != KErrNone &&
       
   153 			r != KErrNotFound &&
       
   154 			r != KErrPathNotFound)
       
   155 			{
       
   156 			User::Leave(r);
       
   157 			}
       
   158 
       
   159 		// Give SW time to handle uninstall.
       
   160 		User::After(KSmallDelay);
       
   161 		}
       
   162 	CleanupStack::PopAndDestroy(3);
       
   163 	}
       
   164 
       
   165 //Function to remove all repositories from repository cache
       
   166 //Try not to use this function because it is time consuming (129.5 seconds)
       
   167 EXPORT_C void CleanupRepositoryCache()
       
   168 	{
       
   169 	// So we wait here until the cache is empty to correct the behaviour.
       
   170 	User::After(KDefaultEvictionTimeout+950000);
       
   171     }
       
   172 
       
   173 //Function to clean specific repository files
       
   174 EXPORT_C void CleanupFileFromCDriveL(const TUid aRepository)
       
   175 	{
       
   176 	RFs fs;
       
   177 	User::LeaveIfError(fs.Connect());
       
   178 	CleanupClosePushL(fs);
       
   179 
       
   180 	CFileMan* fm = CFileMan::NewL(fs);
       
   181 	CleanupStack::PushL(fm);
       
   182 	
       
   183 	HBufC* file_buf = HBufC::NewLC(KMaxFileName);
       
   184 	TPtr filename(file_buf->Des());
       
   185 	filename = KCPersistsDir;
       
   186 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
       
   187 	filename.Append(KTxtFileExt);
       
   188 	PatchDrive(filename);
       
   189 	// Delete txt file from persists dir
       
   190 	TInt r = fm->Delete(filename);
       
   191 
       
   192 	if (r != KErrNone &&
       
   193 		r != KErrNotFound &&
       
   194 		r != KErrPathNotFound)
       
   195 		{
       
   196 		User::Leave(r);
       
   197 		}
       
   198 
       
   199 	filename = KCPersistsDir;
       
   200 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
       
   201 	filename.Append(KCreFileExt);
       
   202 	PatchDrive(filename);
       
   203 	// Delete cre file from persists dir
       
   204 	r = fm->Delete(filename);
       
   205 
       
   206 	if (r != KErrNone &&
       
   207 		r != KErrNotFound &&
       
   208 		r != KErrPathNotFound)
       
   209 		{
       
   210 		User::Leave(r);
       
   211 		}
       
   212 
       
   213 	filename = KCInstallDir;
       
   214 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
       
   215 	filename.Append(KTxtFileExt);	
       
   216 	PatchDrive(filename);
       
   217 	// Delete txt file from install dir	
       
   218 	r = fm->Delete(filename);
       
   219 	if (r != KErrNone &&
       
   220 		r != KErrNotFound &&
       
   221 		r != KErrPathNotFound)
       
   222 		{
       
   223 		User::Leave(r);
       
   224 		}
       
   225 
       
   226 	filename = KCInstallDir;
       
   227 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
       
   228 	filename.Append(KCreFileExt);	
       
   229 	PatchDrive(filename);	
       
   230 	// Delete cre file from install dir	
       
   231 	r = fm->Delete(filename);
       
   232 	if (r != KErrNone &&
       
   233 		r != KErrNotFound &&
       
   234 		r != KErrPathNotFound)
       
   235 		{
       
   236 		User::Leave(r);
       
   237 		}
       
   238  
       
   239 	// Give SW time to handle uninstall.
       
   240 	User::After(KSmallDelay);
       
   241 
       
   242 	CleanupStack::PopAndDestroy(3);
       
   243 	}	
       
   244 	
       
   245 //This function copies files from a source folder to a target folder
       
   246 //and sets the file attributes to archive and read only
       
   247 EXPORT_C void CopyTestFilesL(CFileMan& aFm, const TDesC& aSrc, const TDesC& aDest)
       
   248 	{
       
   249 	TBuf<KMaxFileName> src, dest;	
       
   250 	src.Copy(aSrc);	
       
   251 	dest.Copy(aDest);
       
   252 				
       
   253 	PatchDrive(dest);
       
   254 
       
   255 	//copy test files
       
   256 	User::LeaveIfError(aFm.Copy(src, dest,CFileMan::ERecurse));
       
   257 	aFm.Attribs(dest,
       
   258 					KEntryAttArchive,
       
   259 					KEntryAttReadOnly,
       
   260 					TTime(0),
       
   261 					CFileMan::ERecurse);		
       
   262 	}
       
   263 
       
   264 //This function prints out the recorded time in milliseconds of aTime.
       
   265 EXPORT_C void RecordPerformanceTimingL(TUint32 aTime)
       
   266 	{
       
   267 	TInt freq = 0;
       
   268 	TInt Err = HAL::Get(HAL::EFastCounterFrequency, freq);
       
   269 	if(Err != KErrNone)
       
   270 	{
       
   271 		RDebug::Print(_L("HAL error <%d>\r\n"), Err);
       
   272 	}
       
   273 	const TInt KMicroSecIn1Sec = 1000000;
       
   274 	const TInt KMsIn1Sec = 1000;
       
   275 
       
   276 	double v = ((double)aTime * KMicroSecIn1Sec) / (double)freq; TInt v2 = (TInt)v;
       
   277 	RDebug::Print(_L("####Execution time: %d ms\r\n"), v2 / KMsIn1Sec);
       
   278 	}
       
   279 
       
   280 ///////////////////////////////////////////////////////////////////////////////////////
       
   281 ///////////////////////////////////////////////////////////////////////////////////////
       
   282 //Test macroes and functions
       
   283 
       
   284 EXPORT_C void CheckL(RTest& aTest, TInt aValue, TInt aLine)
       
   285 	{
       
   286 	if (!aValue)
       
   287 		{
       
   288 		CleanupCDriveL();
       
   289 		aTest(EFalse, aLine);
       
   290 		}
       
   291 	}
       
   292 EXPORT_C void CheckL(RTest& aTest, TInt aValue, TInt aExpected, TInt aLine)
       
   293 	{
       
   294 	if (aValue != aExpected)
       
   295 		{
       
   296 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"),
       
   297 					  aExpected, aValue);
       
   298 		CleanupCDriveL();
       
   299 		aTest(EFalse, aLine);
       
   300 		}
       
   301 	}
       
   302 
       
   303 /**
       
   304  *Retrieves transaction state of the session.
       
   305 */
       
   306 EXPORT_C TInt TransactionState(CRepository* aRep)
       
   307 	{
       
   308 	return (static_cast<CClientRepository*>(aRep))->TransactionState();
       
   309 	}
       
   310 
       
   311 /**
       
   312  *Sends EGetSetParameters to server.
       
   313 */
       
   314 EXPORT_C TInt SetGetParameters(const TIpcArgs& CONDITIONAL_PARAM(aArgs))
       
   315 	{
       
   316 #if defined(__CENTREP_SERVER_PERFTEST__) || defined(__CENTREP_SERVER_MEMTEST__) || defined(__CENTREP_SERVER_CACHETEST__)
       
   317 	RRepositorySession session;
       
   318 	TInt ret = session.Connect();
       
   319 	if (ret == KErrNone)
       
   320 		{
       
   321 		ret = session.SendReceive(EGetSetParameters, aArgs);
       
   322 		session.Close();
       
   323 		}
       
   324 	return ret;
       
   325 #else
       
   326 	return KErrNotSupported;
       
   327 #endif
       
   328 	}