os/security/cryptomgmtlibs/securitytestfw/test/testutil/server/testutilsession.cpp
Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * TestUtil - server implementation
26 #include "testutilserver.h"
27 #include "testutilsession.h"
28 #include "testutilclientserver.h"
30 // Timer implementation
31 CGenericTimer* CGenericTimer::NewL(MTimeoutClient& aClient)
33 CGenericTimer* self = new(ELeave) CGenericTimer(aClient);
34 CleanupStack::PushL(self);
35 self->ConstructL(); // calls CTimer::Construct
36 CleanupStack::Pop(self);
40 CGenericTimer::CGenericTimer(MTimeoutClient& aClient)
41 : CTimer(-1), iClient(aClient)
43 CActiveScheduler::Add(this);
46 void CGenericTimer::RunL()
48 // When the timeout expires, then call the client's handler
49 iClient.HandleTimeout();
52 // file detector implementation
53 CTestFileDetector* CTestFileDetector::NewL(const RMessage2& aMessage, RFs& aFs)
55 CTestFileDetector* self = new (ELeave) CTestFileDetector(aMessage, aFs);
56 CleanupStack::PushL(self);
58 CleanupStack::Pop(self);
62 CTestFileDetector::CTestFileDetector(const RMessage2& aMessage, RFs& aFs)
63 :CActive(EPriorityNormal), iFs(aFs), iMessage(aMessage)
65 iTimeInterval = iMessage.Int1();
66 CActiveScheduler::Add(this);
69 CTestFileDetector::~CTestFileDetector()
76 void CTestFileDetector::ConstructL()
80 iTimer=CGenericTimer::NewL(*this);
82 iFileName = CTestUtilSessionCommon::AllocateInputBufferLC(iMessage, 0);
86 void CTestFileDetector::DetectFile()
95 TInt err=iFs.Entry(iFileName->Des(), entry);
98 TPckgC<TBool> exists(ETrue);
99 iMessage.WriteL(2, exists);
100 iMessage.Complete(KErrNone);
104 iTimer->After(iTimeInterval*1000);
105 iFs.NotifyChange(ENotifyFile,
113 void CTestFileDetector::RunL()
122 void CTestFileDetector::DoCancel()
124 iFs.NotifyChangeCancel(iStatus);
127 void CTestFileDetector::HandleTimeout()
133 void CTestFileDetector::CheckAndComplete()
136 TInt err=iFs.Entry(iFileName->Des(), entry);
139 TPckgC<TBool> exists(ETrue);
140 iMessage.WriteL(2, exists);
141 iMessage.Complete(KErrNone);
143 else if (err == KErrNotFound
144 || err == KErrPathNotFound
145 || err == KErrNotReady
146 || err == KErrCorrupt)
148 TPckgC<TBool> exists(EFalse);
149 iMessage.WriteL(2, exists);
150 iMessage.Complete(KErrNone);
154 iMessage.Complete(err);
158 // CTestUtilSession Implementation
159 CTestUtilSession::CTestUtilSession()
163 CTestUtilSession::~CTestUtilSession()
165 Server().DropSession();
166 for (TInt i = 0;i < iLockedFileHandles.Count(); i++)
168 iLockedFileHandles[i].Close();
170 iLockedFileHandles.Close();
176 void CTestUtilSession::CreateL()
178 Server().AddSession();
184 void CTestUtilSession::ServiceL(const RMessage2& aMessage)
186 switch (aMessage.Function())
190 HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
191 HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
193 TInt err = Server().FileMan().Copy(*source, *destination, CFileMan::ERecurse | CFileMan::EOverWrite);
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);
201 CleanupStack::PopAndDestroy(destination);
202 CleanupStack::PopAndDestroy(source);
204 aMessage.Complete(err);
209 HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
210 HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
212 TInt err = Server().FS().Rename(*source,*destination);
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);
220 CleanupStack::PopAndDestroy(destination);
221 CleanupStack::PopAndDestroy(source);
223 aMessage.Complete(err);
228 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
230 TInt err = Server().FS().Entry(*fileName, entry);
235 TPath pathName(*fileName);
236 if (pathName[pathName.Length() - 1] != KPathDelimiter)
238 pathName.Append(KPathDelimiter);
240 err = Server().FileMan().RmDir(pathName);
244 err = Server().FS().Delete(*fileName);
247 CleanupStack::PopAndDestroy(fileName);
249 aMessage.Complete(err);
254 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
255 TParsePtrC parsePtr(*fileName);
256 if(parsePtr.IsRoot())
258 User::Leave(KErrAccessDenied);
260 TInt err = Server().FileMan().RmDir(*fileName);
261 CleanupStack::PopAndDestroy(fileName);
263 aMessage.Complete(err);
268 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
269 TInt err = Server().FS().MkDirAll(*fileName);
270 CleanupStack::PopAndDestroy(fileName);
272 aMessage.Complete(err);
278 iDetector=CTestFileDetector::NewL(aMessage,
280 iDetector->DetectFile();
285 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
287 TInt err = lockFile.Open(Server().FS(), *fileName, EFileWrite);
289 iLockedFileHandles.Append(lockFile);
291 CleanupStack::PopAndDestroy(fileName);
292 aMessage.Complete(err);
297 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
298 TInt err = KErrNotFound;
299 TFileName lockedFileName;
300 for (TInt i = 0; i < iLockedFileHandles.Count() && err;i++)
302 TInt err2 = iLockedFileHandles[i].FullName(lockedFileName);
303 User::LeaveIfError(err2);
304 if (lockedFileName.MatchF(*fileName) != KErrNotFound)
306 iLockedFileHandles[i].Close();
307 iLockedFileHandles.Remove(i);
311 CleanupStack::PopAndDestroy(fileName);
312 aMessage.Complete(err);
317 TInt drive = aMessage.Int0();
318 TBool formatFatOnly = aMessage.Int1();
320 User::LeaveIfError(Server().FS().DriveToChar(drive, aDriveChar));
322 bfDrv.Append(aDriveChar);
327 User::LeaveIfError(format.Open(Server().FS(), bfDrv, EHighDensity, count));
328 CleanupClosePushL(format);
332 User::LeaveIfError(format.Next(count));
338 User::LeaveIfError(format.Next(count));
342 CleanupStack::PopAndDestroy(&format);
343 aMessage.Complete(KErrNone);
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);
357 TInt drive = aMessage.Int0();
359 User::LeaveIfError(Server().FS().FileSystemName(fsName, drive));
360 User::LeaveIfError(Server().FS().DismountFileSystem(fsName, drive));
361 aMessage.Complete(KErrNone);
366 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
367 TInt setReadOnly = aMessage.Int1();
372 // Setting read only attribute
373 setmask = KEntryAttReadOnly;
378 // Clearing read only attribute
380 clearmask = KEntryAttReadOnly;
383 // Turn off the read only attributes
385 TInt err = Server().FileMan().Attribs(*fileName, setmask, clearmask, time);
386 CleanupStack::PopAndDestroy(fileName);
387 aMessage.Complete(err);
392 HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
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
404 if (iFileWatcher->IsActive())
406 aMessage.Complete(KErrServerBusy);
415 // Create a new file watcher for this session
416 iFileWatcher = CFileWatcher::NewL(Server().FS(), aMessage);
419 case EWatchFileCancel:
423 iFileWatcher->Cancel();
424 aMessage.Complete(KErrNone);
428 // No file watch request to cancel!
429 aMessage.Complete(KErrNotReady);
435 HBufC* dirPath = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
436 CDir* dirContents = NULL;
438 User::LeaveIfError(Server().FS().GetDir(*dirPath, KEntryAttNormal, ESortNone, dirContents));
439 TPckg<TInt> numFiles(dirContents->Count());
442 aMessage.WriteL(1, numFiles);
443 aMessage.Complete(KErrNone);
444 CleanupStack::PopAndDestroy(dirPath);
447 case ESetSecureClock:
449 TTime currentTime(0);
450 currentTime.UniversalTime();
451 TTimeIntervalSeconds increment(aMessage.Int0());
452 currentTime += increment;
453 User::SetUTCTimeSecure(currentTime);
454 aMessage.Complete(KErrNone);
460 PanicClient(aMessage,EPanicIllegalFunction);
466 void CTestUtilSession::ServiceError(const RMessage2& aMessage,TInt aError)
468 if (aError==KErrBadDescriptor)
469 PanicClient(aMessage,EPanicBadDescriptor);
470 CSession2::ServiceError(aMessage,aError);