os/security/cryptomgmtlibs/securitytestfw/test/testutil/server/testutilsession.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2004-2009 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 the License "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: 
    15 * TestUtil - server implementation
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file 
    22  @test
    23  @internalComponent
    24 */
    25 
    26 #include "testutilserver.h"
    27 #include "testutilsession.h"
    28 #include "testutilclientserver.h"
    29 
    30 // Timer implementation
    31 CGenericTimer* CGenericTimer::NewL(MTimeoutClient& aClient)
    32 	{
    33 	CGenericTimer* self = new(ELeave) CGenericTimer(aClient);
    34 	CleanupStack::PushL(self);
    35 	self->ConstructL(); // calls CTimer::Construct
    36 	CleanupStack::Pop(self);
    37 	return self;
    38 	}
    39 
    40 CGenericTimer::CGenericTimer(MTimeoutClient& aClient)
    41 	: CTimer(-1), iClient(aClient)
    42 	{
    43 	CActiveScheduler::Add(this);
    44 	}
    45 
    46 void CGenericTimer::RunL()
    47 	{
    48 	// When the timeout expires, then call the client's handler
    49 	iClient.HandleTimeout();
    50 	};
    51 
    52 // file detector implementation
    53 CTestFileDetector* CTestFileDetector::NewL(const RMessage2& aMessage, RFs& aFs)
    54 	{
    55 	CTestFileDetector* self = new (ELeave) CTestFileDetector(aMessage, aFs);
    56 	CleanupStack::PushL(self);
    57 	self->ConstructL();
    58 	CleanupStack::Pop(self);
    59 	return self;
    60 	}
    61 
    62 CTestFileDetector::CTestFileDetector(const RMessage2& aMessage, RFs& aFs)
    63 	:CActive(EPriorityNormal), iFs(aFs), iMessage(aMessage)
    64 	{
    65 	iTimeInterval = iMessage.Int1();
    66 	CActiveScheduler::Add(this);
    67 	}
    68 
    69 CTestFileDetector::~CTestFileDetector()
    70 	{
    71 	Cancel();
    72 	delete iTimer;
    73 	delete iFileName;
    74 	}
    75 
    76 void CTestFileDetector::ConstructL()
    77 	{
    78 	if (iTimeInterval!=0)
    79 		{
    80 		iTimer=CGenericTimer::NewL(*this);
    81 		}
    82 	iFileName = CTestUtilSessionCommon::AllocateInputBufferLC(iMessage, 0);
    83 	CleanupStack::Pop();
    84 	}
    85 
    86 void CTestFileDetector::DetectFile()
    87 	{
    88 	if (!iTimer)
    89 		{
    90 		CheckAndComplete();
    91 		}
    92 	else
    93 		{
    94 		TEntry entry;
    95 		TInt err=iFs.Entry(iFileName->Des(), entry);
    96 		if (err == KErrNone)
    97 			{
    98 			TPckgC<TBool> exists(ETrue);
    99 			iMessage.WriteL(2, exists);
   100 			iMessage.Complete(KErrNone);
   101 			}
   102 		else
   103 			{
   104 			iTimer->After(iTimeInterval*1000);
   105 			iFs.NotifyChange(ENotifyFile, 
   106 							iStatus, 
   107 							iFileName->Des());
   108 			SetActive();			
   109 			}
   110 		}
   111 	}
   112 
   113 void CTestFileDetector::RunL()
   114 	{
   115 	if (iTimer)
   116 		{
   117 		iTimer->Cancel();
   118 		}
   119 	CheckAndComplete();
   120 	}
   121 
   122 void CTestFileDetector::DoCancel()
   123 	{
   124 	iFs.NotifyChangeCancel(iStatus);
   125 	}
   126 
   127 void CTestFileDetector::HandleTimeout()
   128 	{
   129 	Cancel();
   130 	CheckAndComplete();
   131 	}
   132 
   133 void CTestFileDetector::CheckAndComplete()
   134 	{
   135 	TEntry entry;
   136 	TInt err=iFs.Entry(iFileName->Des(), entry);
   137 	if (err == KErrNone)
   138 		{
   139 		TPckgC<TBool> exists(ETrue);
   140 		iMessage.WriteL(2, exists);
   141 		iMessage.Complete(KErrNone);
   142 		}
   143 	else if (err == KErrNotFound 
   144 			|| err == KErrPathNotFound
   145 			|| err == KErrNotReady 
   146 			|| err == KErrCorrupt)
   147 		{
   148 		TPckgC<TBool> exists(EFalse);
   149 		iMessage.WriteL(2, exists);
   150 		iMessage.Complete(KErrNone);
   151 		}
   152 	else
   153 		{
   154 		iMessage.Complete(err);
   155 		}
   156 	}
   157 
   158 // CTestUtilSession Implementation
   159 CTestUtilSession::CTestUtilSession()
   160 	{
   161 	}
   162 
   163 CTestUtilSession::~CTestUtilSession()
   164 	{
   165 	Server().DropSession();
   166 	for (TInt i = 0;i < iLockedFileHandles.Count(); i++)
   167 		{
   168 		iLockedFileHandles[i].Close();
   169 		}
   170 	iLockedFileHandles.Close();
   171 	
   172 	delete iFileWatcher;
   173 	delete iDetector;
   174 	}
   175 	
   176 void CTestUtilSession::CreateL()
   177  	{
   178 	Server().AddSession();
   179   	}
   180 
   181 _LIT(KBP, ":\\");
   182 _LIT(KFAT,"Fat");
   183 	
   184 void CTestUtilSession::ServiceL(const RMessage2& aMessage)
   185 	{
   186 	switch (aMessage.Function())
   187 		{
   188 		case ECopy:
   189 			{
   190 			HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   191 			HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
   192 
   193 			TInt err = Server().FileMan().Copy(*source, *destination, CFileMan::ERecurse | CFileMan::EOverWrite);
   194 			if (err == KErrNone)
   195 				{
   196 				// Turn off the read only attributes
   197 				TTime time(0); // must specify 0, or a valid time, otherwise sets time to a random value and causes -6/-21 errors
   198 				err = Server().FileMan().Attribs(*destination, 0, KEntryAttReadOnly, time, CFileMan::ERecurse);
   199 				}
   200 			
   201 			CleanupStack::PopAndDestroy(destination);
   202 			CleanupStack::PopAndDestroy(source);
   203 
   204 			aMessage.Complete(err);
   205 			break;
   206 			}
   207 		case EMove:
   208 			{
   209 			HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   210 			HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
   211 
   212 			TInt err = Server().FS().Rename(*source,*destination);
   213 			if (err == KErrNone)
   214 				{
   215 				// Turn off the read only attributes
   216 				TTime time(0); // must specify 0, or a valid time, otherwise sets time to a random value and causes -6/-21 errors
   217 				err = Server().FileMan().Attribs(*destination, 0, KEntryAttReadOnly, time, CFileMan::ERecurse);
   218 				}
   219 			
   220 			CleanupStack::PopAndDestroy(destination);
   221 			CleanupStack::PopAndDestroy(source);
   222 			
   223 			aMessage.Complete(err);
   224 			break;
   225 			}
   226 		case EDelete:
   227 			{
   228 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   229 			TEntry entry;
   230 			TInt err = Server().FS().Entry(*fileName, entry);
   231 			if (err == KErrNone)
   232 				{
   233 				if (entry.IsDir())
   234 					{
   235 					TPath pathName(*fileName);
   236 					if (pathName[pathName.Length() - 1] != KPathDelimiter)
   237 						{
   238 						pathName.Append(KPathDelimiter);
   239 						}
   240 					err = Server().FileMan().RmDir(pathName);
   241 					}
   242 				else
   243 					{
   244 					err = Server().FS().Delete(*fileName);
   245 					}
   246 				}
   247 			CleanupStack::PopAndDestroy(fileName);
   248 			
   249 			aMessage.Complete(err);
   250 			break;
   251 			}
   252 		case ERmDir:
   253 			{
   254 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   255 			TParsePtrC parsePtr(*fileName);
   256 			if(parsePtr.IsRoot())
   257 				{
   258 				User::Leave(KErrAccessDenied);
   259 				}
   260 			TInt err = Server().FileMan().RmDir(*fileName);
   261 			CleanupStack::PopAndDestroy(fileName);
   262 			
   263 			aMessage.Complete(err);
   264 			break;
   265 			}
   266 		case EMkDirAll:
   267 			{
   268 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   269 			TInt err = Server().FS().MkDirAll(*fileName);
   270 			CleanupStack::PopAndDestroy(fileName);
   271 			
   272 			aMessage.Complete(err);
   273 			break;
   274 			}
   275 		case EFileExists:
   276 			{			
   277 			delete iDetector;
   278 			iDetector=CTestFileDetector::NewL(aMessage,
   279 												Server().FS());			
   280 			iDetector->DetectFile();
   281 			break;
   282 			}
   283 		case ELock:
   284 			{
   285 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   286 			RFile lockFile;
   287 			TInt err = lockFile.Open(Server().FS(), *fileName, EFileWrite);
   288 			if (err == KErrNone)
   289 				iLockedFileHandles.Append(lockFile);
   290 
   291 			CleanupStack::PopAndDestroy(fileName);			
   292 			aMessage.Complete(err);
   293 			break;
   294 			}			
   295 		case EUnlock:
   296 			{
   297 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   298 			TInt err = KErrNotFound;
   299 			TFileName lockedFileName;
   300 			for (TInt i = 0; i < iLockedFileHandles.Count() && err;i++)
   301 				{
   302 				TInt err2 = iLockedFileHandles[i].FullName(lockedFileName);
   303 				User::LeaveIfError(err2);
   304 				if (lockedFileName.MatchF(*fileName) != KErrNotFound)
   305 					{
   306 					iLockedFileHandles[i].Close();
   307 					iLockedFileHandles.Remove(i);
   308 					err = KErrNone;
   309 					}
   310 				}
   311 			CleanupStack::PopAndDestroy(fileName);			
   312 			aMessage.Complete(err);
   313 			break;
   314 			}
   315 			case EFormat:
   316 			{
   317 			TInt drive = aMessage.Int0();
   318 			TBool formatFatOnly = aMessage.Int1();
   319 			TChar aDriveChar;
   320 			User::LeaveIfError(Server().FS().DriveToChar(drive, aDriveChar));
   321 			TBuf<3> bfDrv;
   322 			bfDrv.Append(aDriveChar);
   323 			bfDrv.Append(KBP);
   324 	
   325 			RFormat format;
   326 			TInt count;
   327 			User::LeaveIfError(format.Open(Server().FS(), bfDrv, EHighDensity, count));
   328 			CleanupClosePushL(format);
   329 			
   330 			if (formatFatOnly)
   331    				{
   332    				User::LeaveIfError(format.Next(count));
   333    				}
   334 			else
   335 				{
   336 				while (count > 0)
   337 					{
   338 					User::LeaveIfError(format.Next(count));
   339 					}
   340 				}
   341 				
   342 			CleanupStack::PopAndDestroy(&format);
   343 			aMessage.Complete(KErrNone);
   344 			break;
   345 			}
   346 		case EMount:
   347 			{
   348 			TInt drive = aMessage.Int0();
   349 			User::LeaveIfError(Server().FS().Connect());
   350 			//Mount the drive synchronizely to make sure the drive is ready for the next operation
   351 			User::LeaveIfError(Server().FS().MountFileSystem(KFAT, drive, ETrue));
   352 			aMessage.Complete(KErrNone);
   353 			break;
   354 			}
   355 		case EUnMount:
   356 			{
   357 			TInt drive = aMessage.Int0();
   358 			TFileName fsName;
   359 			User::LeaveIfError(Server().FS().FileSystemName(fsName, drive));
   360 			User::LeaveIfError(Server().FS().DismountFileSystem(fsName, drive));
   361 			aMessage.Complete(KErrNone);
   362 			break;
   363 			}
   364 		case ESetReadOnly:
   365 			{
   366 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   367 			TInt setReadOnly = aMessage.Int1();
   368 			TUint setmask;
   369 			TUint clearmask;
   370 			if (setReadOnly)
   371 				{
   372 				// Setting read only attribute
   373 				setmask = KEntryAttReadOnly;
   374 				clearmask = 0;
   375 				}
   376 			else
   377 				{
   378 				// Clearing read only attribute
   379 				setmask = 0;
   380 				clearmask = KEntryAttReadOnly;				
   381 				}
   382 			
   383 			// Turn off the read only attributes
   384 			TTime time(0);
   385 			TInt err = Server().FileMan().Attribs(*fileName, setmask, clearmask, time);
   386 			CleanupStack::PopAndDestroy(fileName);			
   387 			aMessage.Complete(err);
   388 			break;
   389 			}
   390 		case EGetFileHandle:
   391 			{
   392 			HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0); 
   393 			RFile file;
   394 			CleanupClosePushL(file);
   395 			User::LeaveIfError(file.Open(Server().FS(), *fileName, EFileRead | EFileShareReadersOnly));
   396 			User::LeaveIfError(file.TransferToClient(aMessage, 1));
   397 			CleanupStack::PopAndDestroy(2, fileName); // file
   398 			break;
   399 			}
   400 		case EWatchFile:
   401 			{
   402 			if (iFileWatcher)
   403 				{
   404 				if (iFileWatcher->IsActive())
   405 					{
   406 					aMessage.Complete(KErrServerBusy);
   407 					break;
   408 					}
   409 				else
   410 					{
   411 					delete iFileWatcher;
   412 					iFileWatcher = NULL;
   413 					}
   414 				}
   415 			// Create a new file watcher for this session
   416 			iFileWatcher = CFileWatcher::NewL(Server().FS(), aMessage);
   417 			break;
   418 			}
   419 		case EWatchFileCancel:
   420 			{
   421 			if (iFileWatcher)
   422 				{
   423 				iFileWatcher->Cancel();
   424 				aMessage.Complete(KErrNone);
   425 				}
   426 			else
   427 				{
   428 				// No file watch request to cancel!
   429 				aMessage.Complete(KErrNotReady);
   430 				}
   431 			break;
   432 			}
   433 		case EGetNumFiles:
   434 			{
   435 			HBufC* dirPath = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
   436 			CDir* dirContents = NULL;
   437 			
   438 			User::LeaveIfError(Server().FS().GetDir(*dirPath, KEntryAttNormal, ESortNone, dirContents));
   439 			TPckg<TInt> numFiles(dirContents->Count());
   440 			
   441 			delete dirContents;
   442 			aMessage.WriteL(1, numFiles);
   443 			aMessage.Complete(KErrNone);
   444 			CleanupStack::PopAndDestroy(dirPath);
   445 			break;
   446 			}
   447 		case ESetSecureClock:
   448 			{
   449 			TTime currentTime(0);
   450 			currentTime.UniversalTime();
   451 			TTimeIntervalSeconds increment(aMessage.Int0());
   452 			currentTime += increment;
   453 			User::SetUTCTimeSecure(currentTime);
   454 			aMessage.Complete(KErrNone);
   455 
   456 			}
   457 			break;
   458 		default:
   459 			{
   460 			PanicClient(aMessage,EPanicIllegalFunction);
   461 			break;
   462 			}
   463 		}
   464 	}
   465 
   466 void CTestUtilSession::ServiceError(const RMessage2& aMessage,TInt aError)
   467 	{
   468 	if (aError==KErrBadDescriptor)
   469 		PanicClient(aMessage,EPanicBadDescriptor);
   470 	CSession2::ServiceError(aMessage,aError);
   471 	}
   472 // End of file