os/security/cryptomgmtlibs/securitytestfw/test/testutil/server/testutilsession.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptomgmtlibs/securitytestfw/test/testutil/server/testutilsession.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,472 @@
1.4 +/*
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* TestUtil - server implementation
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +/**
1.24 + @file
1.25 + @test
1.26 + @internalComponent
1.27 +*/
1.28 +
1.29 +#include "testutilserver.h"
1.30 +#include "testutilsession.h"
1.31 +#include "testutilclientserver.h"
1.32 +
1.33 +// Timer implementation
1.34 +CGenericTimer* CGenericTimer::NewL(MTimeoutClient& aClient)
1.35 + {
1.36 + CGenericTimer* self = new(ELeave) CGenericTimer(aClient);
1.37 + CleanupStack::PushL(self);
1.38 + self->ConstructL(); // calls CTimer::Construct
1.39 + CleanupStack::Pop(self);
1.40 + return self;
1.41 + }
1.42 +
1.43 +CGenericTimer::CGenericTimer(MTimeoutClient& aClient)
1.44 + : CTimer(-1), iClient(aClient)
1.45 + {
1.46 + CActiveScheduler::Add(this);
1.47 + }
1.48 +
1.49 +void CGenericTimer::RunL()
1.50 + {
1.51 + // When the timeout expires, then call the client's handler
1.52 + iClient.HandleTimeout();
1.53 + };
1.54 +
1.55 +// file detector implementation
1.56 +CTestFileDetector* CTestFileDetector::NewL(const RMessage2& aMessage, RFs& aFs)
1.57 + {
1.58 + CTestFileDetector* self = new (ELeave) CTestFileDetector(aMessage, aFs);
1.59 + CleanupStack::PushL(self);
1.60 + self->ConstructL();
1.61 + CleanupStack::Pop(self);
1.62 + return self;
1.63 + }
1.64 +
1.65 +CTestFileDetector::CTestFileDetector(const RMessage2& aMessage, RFs& aFs)
1.66 + :CActive(EPriorityNormal), iFs(aFs), iMessage(aMessage)
1.67 + {
1.68 + iTimeInterval = iMessage.Int1();
1.69 + CActiveScheduler::Add(this);
1.70 + }
1.71 +
1.72 +CTestFileDetector::~CTestFileDetector()
1.73 + {
1.74 + Cancel();
1.75 + delete iTimer;
1.76 + delete iFileName;
1.77 + }
1.78 +
1.79 +void CTestFileDetector::ConstructL()
1.80 + {
1.81 + if (iTimeInterval!=0)
1.82 + {
1.83 + iTimer=CGenericTimer::NewL(*this);
1.84 + }
1.85 + iFileName = CTestUtilSessionCommon::AllocateInputBufferLC(iMessage, 0);
1.86 + CleanupStack::Pop();
1.87 + }
1.88 +
1.89 +void CTestFileDetector::DetectFile()
1.90 + {
1.91 + if (!iTimer)
1.92 + {
1.93 + CheckAndComplete();
1.94 + }
1.95 + else
1.96 + {
1.97 + TEntry entry;
1.98 + TInt err=iFs.Entry(iFileName->Des(), entry);
1.99 + if (err == KErrNone)
1.100 + {
1.101 + TPckgC<TBool> exists(ETrue);
1.102 + iMessage.WriteL(2, exists);
1.103 + iMessage.Complete(KErrNone);
1.104 + }
1.105 + else
1.106 + {
1.107 + iTimer->After(iTimeInterval*1000);
1.108 + iFs.NotifyChange(ENotifyFile,
1.109 + iStatus,
1.110 + iFileName->Des());
1.111 + SetActive();
1.112 + }
1.113 + }
1.114 + }
1.115 +
1.116 +void CTestFileDetector::RunL()
1.117 + {
1.118 + if (iTimer)
1.119 + {
1.120 + iTimer->Cancel();
1.121 + }
1.122 + CheckAndComplete();
1.123 + }
1.124 +
1.125 +void CTestFileDetector::DoCancel()
1.126 + {
1.127 + iFs.NotifyChangeCancel(iStatus);
1.128 + }
1.129 +
1.130 +void CTestFileDetector::HandleTimeout()
1.131 + {
1.132 + Cancel();
1.133 + CheckAndComplete();
1.134 + }
1.135 +
1.136 +void CTestFileDetector::CheckAndComplete()
1.137 + {
1.138 + TEntry entry;
1.139 + TInt err=iFs.Entry(iFileName->Des(), entry);
1.140 + if (err == KErrNone)
1.141 + {
1.142 + TPckgC<TBool> exists(ETrue);
1.143 + iMessage.WriteL(2, exists);
1.144 + iMessage.Complete(KErrNone);
1.145 + }
1.146 + else if (err == KErrNotFound
1.147 + || err == KErrPathNotFound
1.148 + || err == KErrNotReady
1.149 + || err == KErrCorrupt)
1.150 + {
1.151 + TPckgC<TBool> exists(EFalse);
1.152 + iMessage.WriteL(2, exists);
1.153 + iMessage.Complete(KErrNone);
1.154 + }
1.155 + else
1.156 + {
1.157 + iMessage.Complete(err);
1.158 + }
1.159 + }
1.160 +
1.161 +// CTestUtilSession Implementation
1.162 +CTestUtilSession::CTestUtilSession()
1.163 + {
1.164 + }
1.165 +
1.166 +CTestUtilSession::~CTestUtilSession()
1.167 + {
1.168 + Server().DropSession();
1.169 + for (TInt i = 0;i < iLockedFileHandles.Count(); i++)
1.170 + {
1.171 + iLockedFileHandles[i].Close();
1.172 + }
1.173 + iLockedFileHandles.Close();
1.174 +
1.175 + delete iFileWatcher;
1.176 + delete iDetector;
1.177 + }
1.178 +
1.179 +void CTestUtilSession::CreateL()
1.180 + {
1.181 + Server().AddSession();
1.182 + }
1.183 +
1.184 +_LIT(KBP, ":\\");
1.185 +_LIT(KFAT,"Fat");
1.186 +
1.187 +void CTestUtilSession::ServiceL(const RMessage2& aMessage)
1.188 + {
1.189 + switch (aMessage.Function())
1.190 + {
1.191 + case ECopy:
1.192 + {
1.193 + HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.194 + HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
1.195 +
1.196 + TInt err = Server().FileMan().Copy(*source, *destination, CFileMan::ERecurse | CFileMan::EOverWrite);
1.197 + if (err == KErrNone)
1.198 + {
1.199 + // Turn off the read only attributes
1.200 + TTime time(0); // must specify 0, or a valid time, otherwise sets time to a random value and causes -6/-21 errors
1.201 + err = Server().FileMan().Attribs(*destination, 0, KEntryAttReadOnly, time, CFileMan::ERecurse);
1.202 + }
1.203 +
1.204 + CleanupStack::PopAndDestroy(destination);
1.205 + CleanupStack::PopAndDestroy(source);
1.206 +
1.207 + aMessage.Complete(err);
1.208 + break;
1.209 + }
1.210 + case EMove:
1.211 + {
1.212 + HBufC* source = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.213 + HBufC* destination = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,1);
1.214 +
1.215 + TInt err = Server().FS().Rename(*source,*destination);
1.216 + if (err == KErrNone)
1.217 + {
1.218 + // Turn off the read only attributes
1.219 + TTime time(0); // must specify 0, or a valid time, otherwise sets time to a random value and causes -6/-21 errors
1.220 + err = Server().FileMan().Attribs(*destination, 0, KEntryAttReadOnly, time, CFileMan::ERecurse);
1.221 + }
1.222 +
1.223 + CleanupStack::PopAndDestroy(destination);
1.224 + CleanupStack::PopAndDestroy(source);
1.225 +
1.226 + aMessage.Complete(err);
1.227 + break;
1.228 + }
1.229 + case EDelete:
1.230 + {
1.231 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.232 + TEntry entry;
1.233 + TInt err = Server().FS().Entry(*fileName, entry);
1.234 + if (err == KErrNone)
1.235 + {
1.236 + if (entry.IsDir())
1.237 + {
1.238 + TPath pathName(*fileName);
1.239 + if (pathName[pathName.Length() - 1] != KPathDelimiter)
1.240 + {
1.241 + pathName.Append(KPathDelimiter);
1.242 + }
1.243 + err = Server().FileMan().RmDir(pathName);
1.244 + }
1.245 + else
1.246 + {
1.247 + err = Server().FS().Delete(*fileName);
1.248 + }
1.249 + }
1.250 + CleanupStack::PopAndDestroy(fileName);
1.251 +
1.252 + aMessage.Complete(err);
1.253 + break;
1.254 + }
1.255 + case ERmDir:
1.256 + {
1.257 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.258 + TParsePtrC parsePtr(*fileName);
1.259 + if(parsePtr.IsRoot())
1.260 + {
1.261 + User::Leave(KErrAccessDenied);
1.262 + }
1.263 + TInt err = Server().FileMan().RmDir(*fileName);
1.264 + CleanupStack::PopAndDestroy(fileName);
1.265 +
1.266 + aMessage.Complete(err);
1.267 + break;
1.268 + }
1.269 + case EMkDirAll:
1.270 + {
1.271 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.272 + TInt err = Server().FS().MkDirAll(*fileName);
1.273 + CleanupStack::PopAndDestroy(fileName);
1.274 +
1.275 + aMessage.Complete(err);
1.276 + break;
1.277 + }
1.278 + case EFileExists:
1.279 + {
1.280 + delete iDetector;
1.281 + iDetector=CTestFileDetector::NewL(aMessage,
1.282 + Server().FS());
1.283 + iDetector->DetectFile();
1.284 + break;
1.285 + }
1.286 + case ELock:
1.287 + {
1.288 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.289 + RFile lockFile;
1.290 + TInt err = lockFile.Open(Server().FS(), *fileName, EFileWrite);
1.291 + if (err == KErrNone)
1.292 + iLockedFileHandles.Append(lockFile);
1.293 +
1.294 + CleanupStack::PopAndDestroy(fileName);
1.295 + aMessage.Complete(err);
1.296 + break;
1.297 + }
1.298 + case EUnlock:
1.299 + {
1.300 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.301 + TInt err = KErrNotFound;
1.302 + TFileName lockedFileName;
1.303 + for (TInt i = 0; i < iLockedFileHandles.Count() && err;i++)
1.304 + {
1.305 + TInt err2 = iLockedFileHandles[i].FullName(lockedFileName);
1.306 + User::LeaveIfError(err2);
1.307 + if (lockedFileName.MatchF(*fileName) != KErrNotFound)
1.308 + {
1.309 + iLockedFileHandles[i].Close();
1.310 + iLockedFileHandles.Remove(i);
1.311 + err = KErrNone;
1.312 + }
1.313 + }
1.314 + CleanupStack::PopAndDestroy(fileName);
1.315 + aMessage.Complete(err);
1.316 + break;
1.317 + }
1.318 + case EFormat:
1.319 + {
1.320 + TInt drive = aMessage.Int0();
1.321 + TBool formatFatOnly = aMessage.Int1();
1.322 + TChar aDriveChar;
1.323 + User::LeaveIfError(Server().FS().DriveToChar(drive, aDriveChar));
1.324 + TBuf<3> bfDrv;
1.325 + bfDrv.Append(aDriveChar);
1.326 + bfDrv.Append(KBP);
1.327 +
1.328 + RFormat format;
1.329 + TInt count;
1.330 + User::LeaveIfError(format.Open(Server().FS(), bfDrv, EHighDensity, count));
1.331 + CleanupClosePushL(format);
1.332 +
1.333 + if (formatFatOnly)
1.334 + {
1.335 + User::LeaveIfError(format.Next(count));
1.336 + }
1.337 + else
1.338 + {
1.339 + while (count > 0)
1.340 + {
1.341 + User::LeaveIfError(format.Next(count));
1.342 + }
1.343 + }
1.344 +
1.345 + CleanupStack::PopAndDestroy(&format);
1.346 + aMessage.Complete(KErrNone);
1.347 + break;
1.348 + }
1.349 + case EMount:
1.350 + {
1.351 + TInt drive = aMessage.Int0();
1.352 + User::LeaveIfError(Server().FS().Connect());
1.353 + //Mount the drive synchronizely to make sure the drive is ready for the next operation
1.354 + User::LeaveIfError(Server().FS().MountFileSystem(KFAT, drive, ETrue));
1.355 + aMessage.Complete(KErrNone);
1.356 + break;
1.357 + }
1.358 + case EUnMount:
1.359 + {
1.360 + TInt drive = aMessage.Int0();
1.361 + TFileName fsName;
1.362 + User::LeaveIfError(Server().FS().FileSystemName(fsName, drive));
1.363 + User::LeaveIfError(Server().FS().DismountFileSystem(fsName, drive));
1.364 + aMessage.Complete(KErrNone);
1.365 + break;
1.366 + }
1.367 + case ESetReadOnly:
1.368 + {
1.369 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.370 + TInt setReadOnly = aMessage.Int1();
1.371 + TUint setmask;
1.372 + TUint clearmask;
1.373 + if (setReadOnly)
1.374 + {
1.375 + // Setting read only attribute
1.376 + setmask = KEntryAttReadOnly;
1.377 + clearmask = 0;
1.378 + }
1.379 + else
1.380 + {
1.381 + // Clearing read only attribute
1.382 + setmask = 0;
1.383 + clearmask = KEntryAttReadOnly;
1.384 + }
1.385 +
1.386 + // Turn off the read only attributes
1.387 + TTime time(0);
1.388 + TInt err = Server().FileMan().Attribs(*fileName, setmask, clearmask, time);
1.389 + CleanupStack::PopAndDestroy(fileName);
1.390 + aMessage.Complete(err);
1.391 + break;
1.392 + }
1.393 + case EGetFileHandle:
1.394 + {
1.395 + HBufC* fileName = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.396 + RFile file;
1.397 + CleanupClosePushL(file);
1.398 + User::LeaveIfError(file.Open(Server().FS(), *fileName, EFileRead | EFileShareReadersOnly));
1.399 + User::LeaveIfError(file.TransferToClient(aMessage, 1));
1.400 + CleanupStack::PopAndDestroy(2, fileName); // file
1.401 + break;
1.402 + }
1.403 + case EWatchFile:
1.404 + {
1.405 + if (iFileWatcher)
1.406 + {
1.407 + if (iFileWatcher->IsActive())
1.408 + {
1.409 + aMessage.Complete(KErrServerBusy);
1.410 + break;
1.411 + }
1.412 + else
1.413 + {
1.414 + delete iFileWatcher;
1.415 + iFileWatcher = NULL;
1.416 + }
1.417 + }
1.418 + // Create a new file watcher for this session
1.419 + iFileWatcher = CFileWatcher::NewL(Server().FS(), aMessage);
1.420 + break;
1.421 + }
1.422 + case EWatchFileCancel:
1.423 + {
1.424 + if (iFileWatcher)
1.425 + {
1.426 + iFileWatcher->Cancel();
1.427 + aMessage.Complete(KErrNone);
1.428 + }
1.429 + else
1.430 + {
1.431 + // No file watch request to cancel!
1.432 + aMessage.Complete(KErrNotReady);
1.433 + }
1.434 + break;
1.435 + }
1.436 + case EGetNumFiles:
1.437 + {
1.438 + HBufC* dirPath = CTestUtilSessionCommon::AllocateInputBufferLC(aMessage,0);
1.439 + CDir* dirContents = NULL;
1.440 +
1.441 + User::LeaveIfError(Server().FS().GetDir(*dirPath, KEntryAttNormal, ESortNone, dirContents));
1.442 + TPckg<TInt> numFiles(dirContents->Count());
1.443 +
1.444 + delete dirContents;
1.445 + aMessage.WriteL(1, numFiles);
1.446 + aMessage.Complete(KErrNone);
1.447 + CleanupStack::PopAndDestroy(dirPath);
1.448 + break;
1.449 + }
1.450 + case ESetSecureClock:
1.451 + {
1.452 + TTime currentTime(0);
1.453 + currentTime.UniversalTime();
1.454 + TTimeIntervalSeconds increment(aMessage.Int0());
1.455 + currentTime += increment;
1.456 + User::SetUTCTimeSecure(currentTime);
1.457 + aMessage.Complete(KErrNone);
1.458 +
1.459 + }
1.460 + break;
1.461 + default:
1.462 + {
1.463 + PanicClient(aMessage,EPanicIllegalFunction);
1.464 + break;
1.465 + }
1.466 + }
1.467 + }
1.468 +
1.469 +void CTestUtilSession::ServiceError(const RMessage2& aMessage,TInt aError)
1.470 + {
1.471 + if (aError==KErrBadDescriptor)
1.472 + PanicClient(aMessage,EPanicBadDescriptor);
1.473 + CSession2::ServiceError(aMessage,aError);
1.474 + }
1.475 +// End of file