os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestFileUriServer.cpp
Update contrib.
1 // Copyright (c) 2004-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 #include <escapeutils.h>
25 #include "TestFileUriServer.h"
26 #include "TestCreateFileStep.h"
27 #include "TestGetFileNameFromUriStep.h"
28 #include "TestGenerateFileUriStep.h"
29 #include "TestDeleteFileStep.h"
30 #include "TestForAllFilesStep.h"
32 // The system-wide unique name for the test-server
33 _LIT(KServerName, "TestFileUriServer");
35 TBuf<KMaxDrives> CTestFileUriServer::iRemovableDrives(KNullDesC);
38 Static factory constructor. Creates and returns instance of the test server
39 @return A pointer to the newly created CTestFileUriServer object
41 CTestFileUriServer* CTestFileUriServer::NewL()
43 // Construct the server
44 CTestFileUriServer* server = new(ELeave) CTestFileUriServer();
45 CleanupStack::PushL(server);
47 // CServer base class call
48 // Name the server using the system-wide unique string
49 // Clients use this to create server sessions.
50 server->StartL(KServerName);
52 CleanupStack::Pop(server);
60 Creates the Active Scheduler, then creates the test-server, synchronises the
61 thread with the client and then enters the active scheduler.
63 This is EKA1 version of MainL(). Uses sempahore to sync with client
64 as Rendezvous calls are not available
68 // Create and install the active scheduler.
69 CActiveScheduler* sched = NULL;
70 sched = new(ELeave) CActiveScheduler;
71 CleanupStack::PushL(sched);
72 CActiveScheduler::Install(sched);
74 // Create the server inside trap harness
75 CTestFileUriServer *server = NULL;
76 TRAPD(err, server = CTestFileUriServer::NewL());
79 CleanupStack::PushL(server);
82 // The client API of TestExecute will already have created the
83 // semaphore and will be waiting on it.
84 User::LeaveIfError(sem.OpenGlobal(KServerName));
86 CleanupStack::Pop(server);
92 // Enter the active scheduler
95 CleanupStack::Pop(sched);
101 EKA2 version of MainL()
102 Uses the new Rendezvous call isntead of the older semaphore.
106 // For platform security
107 #if (defined __DATA_CAGING__)
108 RProcess().DataCaging(RProcess::EDataCagingOn);
109 RProcess().SecureApi(RProcess::ESecureApiOn);
111 CActiveScheduler* sched = NULL;
112 sched = new(ELeave) CActiveScheduler;
113 CActiveScheduler::Install(sched);
114 CTestFileUriServer* server = NULL;
116 // Create the test-server
117 TRAPD(err, server = CTestFileUriServer::NewL());
121 // Sync with the client and enter the active scheduler
122 RProcess::Rendezvous(KErrNone);
128 #endif // #if (!defined EKA2)
131 #if (defined __WINS__ && !defined EKA2)
133 DLL entry-point for EKA1 emulator builds.
135 GLDEF_C TInt E32Dll(enum TDllReason /*aDllReason*/)
142 Exe entry point code, for EKA1 hardware and EKA2 builds.
144 GLDEF_C TInt E32Main()
147 CTrapCleanup *cleanup = CTrapCleanup::New();
158 #endif // #if (defined __WINS__ && !defined EKA2)
160 #if (defined __WINS__ && !defined EKA2)
162 For EKA1 emulator builds. This function is called when the thread is first
163 resumed. Has the standard thread entry siganture.
165 TInt ThreadFunc (TAny* /*aParam*/)
168 CTrapCleanup* cleanup = CTrapCleanup::New();
181 For EKA1 emulator builds. Creates and starts a thread for the server to run.
183 EXPORT_C TInt NewServer()
185 _LIT(KThread, "Thread");
188 // Name the thread as "<Server-Name>Thread" making it hopefully unique
189 TBuf<KMaxTestExecuteNameLength> threadName(KServerName);
190 threadName.Append(KThread);
192 const TInt KMaxHeapSize = 0x1000000;
195 TInt err = thread.Create(threadName, ThreadFunc, KDefaultStackSize,
196 KMinHeapSize, KMaxHeapSize, NULL, EOwnerProcess);
202 // Start the thread -> effectively calls ThreadFunc
209 #endif // #if (defined __WINS__ && !defined EKA2)
213 Base class pure virtual
214 @return - Instance of the test step
216 CTestStep* CTestFileUriServer::CreateTestStep(const TDesC& aStepName)
218 CTestStep *testStep = NULL;
220 if (aStepName == KTestCreateFileStep)
222 testStep = new (ELeave) CTestCreateFileStep;
224 else if (aStepName == KTestGetFileNameFromUriStep)
226 testStep = new (ELeave) CTestGetFileNameFromUriStep;
228 else if (aStepName == KTestGenerateFileUriStep)
230 testStep = new (ELeave) CTestGenerateFileUriStep;
232 else if (aStepName == KTestDeleteFileStep)
234 testStep = new (ELeave) CTestDeleteFileStep;
236 else if (aStepName == KTestForAllFilesStep)
238 testStep = new (ELeave) CTestForAllFilesStep();
244 Returns the equivalent drive number of a drive
246 void CTestFileUriServer::GetDriveNumber(const TDesC& aDrive, TDriveNumber& aDriveNum)
248 TBuf<1> driveLetter(aDrive.Left(1));
249 driveLetter.LowerCase();
250 aDriveNum = static_cast <TDriveNumber> (driveLetter[0] - KLetterA);
254 Checks whether a specific drive is a removable drive
256 TInt CTestFileUriServer::IsRemovableDrive(const TDriveNumber& aDriveNum, TBool& aResult)
259 TDriveInfo driveInfo;
265 err = fs.Drive(driveInfo, aDriveNum);
266 if (err == KErrNone && driveInfo.iDriveAtt & KDriveAttRemovable)
276 Replaces the drive placeholder <drive> with the actual drive
278 HBufC16* CTestFileUriServer::CheckAndFillDriveNameL(const TPtrC& aFileUri, const TPtrC& aDrive)
280 HBufC16* expectedUriWithDrive = aFileUri.AllocL();
281 TInt placeHolderPos = aFileUri.Find(KDrivePlaceHolder);
282 if(placeHolderPos >= KErrNone)
284 if(aDrive == KExtMedia)
285 {// Create a descriptor that is big enough
286 // Just in case ReAllocL leaves
287 CleanupStack::PushL(expectedUriWithDrive);
288 expectedUriWithDrive = expectedUriWithDrive->ReAllocL(aFileUri.Length() + (KExtMedia().Length() - KDrivePlaceHolder().Length()));
289 CleanupStack::Pop(); // expectedUriWithDrive
291 expectedUriWithDrive->Des().Replace(placeHolderPos, KDrivePlaceHolder().Length(), aDrive);
293 return expectedUriWithDrive;
297 Private function used to find the remobale drives and populate iRemovableDrives
299 TInt CTestFileUriServer::PopulateRemovableDrivesBuf(const RFs& aFs)
302 TDriveInfo driveInfo;
304 for (driveNum = EDriveA; driveNum <= EDriveZ; driveNum++)
306 // Populate iRemovableDrives with all removable drives in alphabetical order
307 err = aFs.Drive(driveInfo, driveNum);
308 if (err == KErrNone && (driveInfo.iDriveAtt & KDriveAttRemovable))
310 iRemovableDrives.Append(TInt16('a' + driveNum));
317 Searches whether a file with same name and path exists in any other removable drive
319 TInt CTestFileUriServer::FirstRemovableDriveWithSameFileName(const TDesC& aFileName, TBuf<1>& aCorrectDrive)
321 aCorrectDrive = aFileName.Left(1);
324 if((err = fs.Connect()) != KErrNone)
329 if(iRemovableDrives == KNullDesC)
331 if((err = PopulateRemovableDrivesBuf(fs)) != KErrNone)
337 HBufC* tempFileName = NULL;
338 if((tempFileName = aFileName.Alloc()) == NULL)
342 for(index = 0; index < iRemovableDrives.Length(); ++index)
345 // change the drive in the filename and check whether such a file exists
346 tempFileName->Des()[0] = iRemovableDrives[index];
347 err = fs.Att(tempFileName->Des(), attValue);
350 aCorrectDrive[0] = iRemovableDrives[index];
354 if(index >= iRemovableDrives.Length())
355 {// File not found on any removable drive
356 aCorrectDrive = KNullDesC;
364 Obtains the private directory of the application and appends it along with the
365 relative filename and drive to create the fully qualified filename
367 TInt CTestFileUriServer::CreateFullyQualifiedName(const TPtrC& aRelativeName, const TPtrC& aDrive, TFileName& aFullyQualifiedName)
370 TInt err = fs.Connect();
375 // Get private dir name
376 err = fs.PrivatePath(aFullyQualifiedName);
382 // Construct fully-qualified filename
383 aFullyQualifiedName.Insert(0, KDriveSeparator);
384 aFullyQualifiedName.Insert(0, aDrive);
386 // If backslash already exists dont include again
387 if(aRelativeName.Left(1) == KBackSlash)
391 aFullyQualifiedName.Append(aRelativeName.Right(aRelativeName.Length() - position));