sl@0: // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #include "t_cenrep_helper.h"
sl@0: #include "srvdefs.h"
sl@0: #include <f32file.h> // RFs
sl@0: #include <e32test.h> // RTest
sl@0: #include <hal.h>
sl@0: #include "cachemgr.h"
sl@0: #include "clirep.h"
sl@0: 
sl@0: #if defined(__CENTREP_SERVER_PERFTEST__) || defined(__CENTREP_SERVER_MEMTEST__) || defined(__CENTREP_SERVER_CACHETEST__)
sl@0: 	#include "srvreqs.h"
sl@0: 	#define CONDITIONAL_PARAM(x) x
sl@0: #else
sl@0: 	#define CONDITIONAL_PARAM(x)
sl@0: #endif
sl@0: 
sl@0: void PatchDrive(TDes& aPath)
sl@0: 	{
sl@0: 	TDriveNumber sysdrive = RFs::GetSystemDrive();
sl@0: 	aPath[0] = 'a' + sysdrive-EDriveA; // Replace drive letter only.
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void printDirL(const TDesC& aDirName)
sl@0: 	{
sl@0: 	CDir* fileList=NULL;
sl@0: 	RFs fs;
sl@0: 	User::LeaveIfError(fs.Connect());
sl@0: 	CleanupClosePushL(fs);
sl@0: 
sl@0: 	TInt r = fs.GetDir(aDirName,KEntryAttNormal, ESortByDate, fileList);
sl@0: 
sl@0: 	if (r==KErrPathNotFound)
sl@0: 		{
sl@0: 		RDebug::Print(_L("No directory %S"), &aDirName);
sl@0: 		}
sl@0: 	else if (r==KErrNone)
sl@0: 		{
sl@0: 		TInt fileCount=fileList->Count();
sl@0: 		RDebug::Print( _L("%02d files in %S\n"),fileCount, &aDirName);
sl@0: 		for (TInt i = 0;i < fileCount; ++i)
sl@0: 			{
sl@0: 			TEntry entry=(*fileList)[i];
sl@0: 
sl@0: 			RDebug::Print( _L("File[%02d] - %S \n"),
sl@0: 							i,
sl@0: 							&(entry.iName)
sl@0: 							);
sl@0: 			}
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		RDebug::Print(_L("Error getting contents of directory %S"), &aDirName);
sl@0: 		}
sl@0: 	delete fileList;
sl@0: 	CleanupStack::PopAndDestroy();	//fs
sl@0: 	}
sl@0: 
sl@0: EXPORT_C TInt KillProcess(const TDesC& aProcessName)
sl@0: 	{
sl@0: 	TFullName name;
sl@0: 
sl@0: 	RDebug::Print(_L("Find and kill \"%S\" process.\n"), &aProcessName);
sl@0: 
sl@0: 	TBuf<64> pattern(aProcessName);
sl@0: 	TInt length = pattern.Length();
sl@0: 	pattern += _L("*");
sl@0: 	TFindProcess procFinder(pattern);
sl@0: 
sl@0: 	while (procFinder.Next(name) == KErrNone)
sl@0: 		{
sl@0: 		if (name.Length() > length)
sl@0: 			{//If found name is a string containing aProcessName string.
sl@0: 			TChar c(name[length]);
sl@0: 			if (c.IsAlphaDigit() ||
sl@0: 				c == TChar('_') ||
sl@0: 				c == TChar('-'))
sl@0: 				{
sl@0: 				// If the found name is other valid application name
sl@0: 				// starting with aProcessName string.
sl@0: 				RDebug::Print(_L(":: Process name: \"%S\".\n"), &name);
sl@0: 				continue;
sl@0: 				}
sl@0: 			}
sl@0: 		RProcess proc;
sl@0: 		if (proc.Open(name) == KErrNone)
sl@0: 			{
sl@0: 			proc.Kill(0);
sl@0: 			RDebug::Print(_L("\"%S\" process killed.\n"), &name);
sl@0: 			}
sl@0: 		proc.Close();
sl@0: 		}
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: const TInt KSmallDelay = 2*1000;
sl@0: 
sl@0: //File cleanup function
sl@0: EXPORT_C void CleanupCDriveL(TBool aRemoveRomCache)
sl@0: 	{
sl@0: 	RFs fs;
sl@0: 	User::LeaveIfError(fs.Connect());
sl@0: 	CleanupClosePushL(fs);
sl@0: 
sl@0: 	CFileMan* fm = CFileMan::NewL(fs);
sl@0: 	CleanupStack::PushL(fm);
sl@0: 	HBufC* file_buf = HBufC::NewLC(KMaxFileName);
sl@0: 	TPtr file(file_buf->Des());
sl@0: 	file.Copy(KCPersistsFiles);
sl@0: 	PatchDrive(file);
sl@0: 	TInt r = fm->Delete(file);
sl@0: 
sl@0: 	if (r != KErrNone &&
sl@0: 		r != KErrNotFound &&
sl@0: 		r != KErrPathNotFound)
sl@0: 		{
sl@0: 		User::Leave(r);
sl@0: 		}
sl@0: 		
sl@0: 	if(aRemoveRomCache)
sl@0: 		{
sl@0: 		//Delete cached rom version file
sl@0: 		file.Copy(KCRomVersionFiles);
sl@0: 		PatchDrive(file);
sl@0: 		fm->Attribs(file, 0, KEntryAttReadOnly, TTime(0), 0);
sl@0: 		r = fm->Delete(file);
sl@0: 		if (r != KErrNone &&
sl@0: 			r != KErrNotFound &&
sl@0: 			r != KErrPathNotFound &&
sl@0: 			r != KErrPermissionDenied)
sl@0: 			{
sl@0: 			User::Leave(r);
sl@0: 			}
sl@0: 		
sl@0: 		// Delete all install files
sl@0: 		file.Copy(KCInstallFiles);
sl@0: 		PatchDrive(file);	
sl@0: 		r = fm->Delete(file);
sl@0: 		if (r != KErrNone &&
sl@0: 			r != KErrNotFound &&
sl@0: 			r != KErrPathNotFound)
sl@0: 			{
sl@0: 			User::Leave(r);
sl@0: 			}
sl@0: 
sl@0: 		// Give SW time to handle uninstall.
sl@0: 		User::After(KSmallDelay);
sl@0: 		}
sl@0: 	CleanupStack::PopAndDestroy(3);
sl@0: 	}
sl@0: 
sl@0: //Function to remove all repositories from repository cache
sl@0: //Try not to use this function because it is time consuming (129.5 seconds)
sl@0: EXPORT_C void CleanupRepositoryCache()
sl@0: 	{
sl@0: 	// So we wait here until the cache is empty to correct the behaviour.
sl@0: 	User::After(KDefaultEvictionTimeout+950000);
sl@0:     }
sl@0: 
sl@0: //Function to clean specific repository files
sl@0: EXPORT_C void CleanupFileFromCDriveL(const TUid aRepository)
sl@0: 	{
sl@0: 	RFs fs;
sl@0: 	User::LeaveIfError(fs.Connect());
sl@0: 	CleanupClosePushL(fs);
sl@0: 
sl@0: 	CFileMan* fm = CFileMan::NewL(fs);
sl@0: 	CleanupStack::PushL(fm);
sl@0: 	
sl@0: 	HBufC* file_buf = HBufC::NewLC(KMaxFileName);
sl@0: 	TPtr filename(file_buf->Des());
sl@0: 	filename = KCPersistsDir;
sl@0: 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
sl@0: 	filename.Append(KTxtFileExt);
sl@0: 	PatchDrive(filename);
sl@0: 	// Delete txt file from persists dir
sl@0: 	TInt r = fm->Delete(filename);
sl@0: 
sl@0: 	if (r != KErrNone &&
sl@0: 		r != KErrNotFound &&
sl@0: 		r != KErrPathNotFound)
sl@0: 		{
sl@0: 		User::Leave(r);
sl@0: 		}
sl@0: 
sl@0: 	filename = KCPersistsDir;
sl@0: 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
sl@0: 	filename.Append(KCreFileExt);
sl@0: 	PatchDrive(filename);
sl@0: 	// Delete cre file from persists dir
sl@0: 	r = fm->Delete(filename);
sl@0: 
sl@0: 	if (r != KErrNone &&
sl@0: 		r != KErrNotFound &&
sl@0: 		r != KErrPathNotFound)
sl@0: 		{
sl@0: 		User::Leave(r);
sl@0: 		}
sl@0: 
sl@0: 	filename = KCInstallDir;
sl@0: 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
sl@0: 	filename.Append(KTxtFileExt);	
sl@0: 	PatchDrive(filename);
sl@0: 	// Delete txt file from install dir	
sl@0: 	r = fm->Delete(filename);
sl@0: 	if (r != KErrNone &&
sl@0: 		r != KErrNotFound &&
sl@0: 		r != KErrPathNotFound)
sl@0: 		{
sl@0: 		User::Leave(r);
sl@0: 		}
sl@0: 
sl@0: 	filename = KCInstallDir;
sl@0: 	filename.AppendNumFixedWidth(aRepository.iUid,EHex,8);
sl@0: 	filename.Append(KCreFileExt);	
sl@0: 	PatchDrive(filename);	
sl@0: 	// Delete cre file from install dir	
sl@0: 	r = fm->Delete(filename);
sl@0: 	if (r != KErrNone &&
sl@0: 		r != KErrNotFound &&
sl@0: 		r != KErrPathNotFound)
sl@0: 		{
sl@0: 		User::Leave(r);
sl@0: 		}
sl@0:  
sl@0: 	// Give SW time to handle uninstall.
sl@0: 	User::After(KSmallDelay);
sl@0: 
sl@0: 	CleanupStack::PopAndDestroy(3);
sl@0: 	}	
sl@0: 	
sl@0: //This function copies files from a source folder to a target folder
sl@0: //and sets the file attributes to archive and read only
sl@0: EXPORT_C void CopyTestFilesL(CFileMan& aFm, const TDesC& aSrc, const TDesC& aDest)
sl@0: 	{
sl@0: 	TBuf<KMaxFileName> src, dest;	
sl@0: 	src.Copy(aSrc);	
sl@0: 	dest.Copy(aDest);
sl@0: 				
sl@0: 	PatchDrive(dest);
sl@0: 
sl@0: 	//copy test files
sl@0: 	User::LeaveIfError(aFm.Copy(src, dest,CFileMan::ERecurse));
sl@0: 	aFm.Attribs(dest,
sl@0: 					KEntryAttArchive,
sl@0: 					KEntryAttReadOnly,
sl@0: 					TTime(0),
sl@0: 					CFileMan::ERecurse);		
sl@0: 	}
sl@0: 
sl@0: //This function prints out the recorded time in milliseconds of aTime.
sl@0: EXPORT_C void RecordPerformanceTimingL(TUint32 aTime)
sl@0: 	{
sl@0: 	TInt freq = 0;
sl@0: 	TInt Err = HAL::Get(HAL::EFastCounterFrequency, freq);
sl@0: 	if(Err != KErrNone)
sl@0: 	{
sl@0: 		RDebug::Print(_L("HAL error <%d>\r\n"), Err);
sl@0: 	}
sl@0: 	const TInt KMicroSecIn1Sec = 1000000;
sl@0: 	const TInt KMsIn1Sec = 1000;
sl@0: 
sl@0: 	double v = ((double)aTime * KMicroSecIn1Sec) / (double)freq; TInt v2 = (TInt)v;
sl@0: 	RDebug::Print(_L("####Execution time: %d ms\r\n"), v2 / KMsIn1Sec);
sl@0: 	}
sl@0: 
sl@0: ///////////////////////////////////////////////////////////////////////////////////////
sl@0: ///////////////////////////////////////////////////////////////////////////////////////
sl@0: //Test macroes and functions
sl@0: 
sl@0: EXPORT_C void CheckL(RTest& aTest, TInt aValue, TInt aLine)
sl@0: 	{
sl@0: 	if (!aValue)
sl@0: 		{
sl@0: 		CleanupCDriveL();
sl@0: 		aTest(EFalse, aLine);
sl@0: 		}
sl@0: 	}
sl@0: EXPORT_C void CheckL(RTest& aTest, TInt aValue, TInt aExpected, TInt aLine)
sl@0: 	{
sl@0: 	if (aValue != aExpected)
sl@0: 		{
sl@0: 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"),
sl@0: 					  aExpected, aValue);
sl@0: 		CleanupCDriveL();
sl@0: 		aTest(EFalse, aLine);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  *Retrieves transaction state of the session.
sl@0: */
sl@0: EXPORT_C TInt TransactionState(CRepository* aRep)
sl@0: 	{
sl@0: 	return (static_cast<CClientRepository*>(aRep))->TransactionState();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  *Sends EGetSetParameters to server.
sl@0: */
sl@0: EXPORT_C TInt SetGetParameters(const TIpcArgs& CONDITIONAL_PARAM(aArgs))
sl@0: 	{
sl@0: #if defined(__CENTREP_SERVER_PERFTEST__) || defined(__CENTREP_SERVER_MEMTEST__) || defined(__CENTREP_SERVER_CACHETEST__)
sl@0: 	RRepositorySession session;
sl@0: 	TInt ret = session.Connect();
sl@0: 	if (ret == KErrNone)
sl@0: 		{
sl@0: 		ret = session.SendReceive(EGetSetParameters, aArgs);
sl@0: 		session.Close();
sl@0: 		}
sl@0: 	return ret;
sl@0: #else
sl@0: 	return KErrNotSupported;
sl@0: #endif
sl@0: 	}