First public contribution.
1 // Copyright (c) 1999-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 the License "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.
14 // f32test\loader\handshare.cpp
20 #include "handshare.h"
23 #pragma warning(disable:4706)
28 GLDEF_D RTest test(_L("HANDSHARE_SVR"));
30 #define PANIC() FHSvrPanic(__LINE__)
31 #define FHS_ASSERT(c) ((void)((c)||(PANIC(),0)))
33 const TTimeIntervalMicroSeconds32 KHalfSecond(500000);
35 const TInt KHeapSize=0x2000;
37 void FHSvrPanic(TInt aLine)
39 User::Panic(_L("FHServer"),aLine);
42 LOCAL_D TInt gTestDrive;
44 /******************************************************************************
46 ******************************************************************************/
49 class CFHServer : public CServer2
52 static CFHServer* NewL();
55 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
56 virtual TInt RunError(TInt aError);
61 class CFHSession : public CSession2
64 virtual ~CFHSession();
65 virtual void CreateL();
66 virtual void ServiceL(const RMessage2& aMessage);
69 void GetFileHandle(const RMessage2& aMsg);
70 void GetFileHandle2(const RMessage2& aMsg);
71 void PassFileHandle(const RMessage2& aMsg);
72 void PassFileHandleProcess(const RMessage2& aMsg);
73 void PassInvalidFileHandle(const RMessage2& aMsg);
76 // a second server so we can test passing file handles from
77 // client to server to server2
78 class CFHServer2 : public CServer2
81 static CFHServer2* NewL();
83 virtual ~CFHServer2();
84 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
85 virtual TInt RunError(TInt aError);
92 class CFHSession2 : public CSession2
95 virtual ~CFHSession2();
96 virtual void CreateL();
97 virtual void ServiceL(const RMessage2& aMessage);
99 void PassFileHandle(const RMessage2& aMsg);
100 void GetFileHandle(const RMessage2& aMsg);
104 /******************************************************************************
105 * Class CFHSession/CFHServer
106 ******************************************************************************/
107 void ExceptionHandler(TExcType)
109 User::Leave(KErrGeneral);
114 CFHSession::~CFHSession()
118 void CFHSession::CreateL()
123 void CFHSession::ServiceL(const RMessage2& aMessage)
126 TInt mid=aMessage.Function();
129 case RFileHandleSharer::EMsgGetFileHandle:
130 GetFileHandle(aMessage);
133 case RFileHandleSharer::EMsgGetFileHandle2:
134 GetFileHandle2(aMessage);
137 case RFileHandleSharer::EMsgPassFileHandle:
138 PassFileHandle(aMessage);
141 case RFileHandleSharer::EMsgPassFileHandleProcess:
142 PassFileHandleProcess(aMessage);
145 case RFileHandleSharer::EMsgPassInvalidFileHandle:
146 PassInvalidFileHandle(aMessage);
149 case RFileHandleSharer::EMsgExit:
152 RFileHandleSharer2 handsvr2;
153 TInt r=handsvr2.Connect();
156 test(r==KErrNone || r == KErrServerTerminated);
159 aMessage.Complete(KErrNone);
161 CActiveScheduler::Stop();
165 case RFileHandleSharer::EMsgSync:
166 aMessage.Complete(KErrNone);
169 case RFileHandleSharer::EMsgDrive:
170 gTestDrive=aMessage.Int0();
171 aMessage.Complete(KErrNone);
181 // Returns session and relevant file handle to Client in read mode
182 // This is to allow the client to test the deprcated function RFile::Adopt()
183 void CFHSession::GetFileHandle(const RMessage2& aMsg)
185 test.Printf(_L("Get file handle"));
187 // get the requested file mode
188 TInt fileMode = aMsg.Int1();
192 r=fs.CreatePrivatePath(gTestDrive);
194 r=fs.SetSessionToPrivate(gTestDrive);
196 r=fs.ShareProtected();
199 r=file1.Create(fs,KSvrFileName,EFileWrite);
200 test(r==KErrNone || r==KErrAlreadyExists);
201 if (r==KErrAlreadyExists)
203 r=file1.Open(fs,KSvrFileName, EFileWrite);
206 r=file1.Write(KTestData1());
210 r=file1.Open(fs,KSvrFileName, fileMode);
213 TInt fssh=file1.SubSessionHandle();
214 r=aMsg.Write(0, TPckgC<TInt>(fssh));
221 // Returns a file handle from server2
223 void CFHSession::GetFileHandle2(const RMessage2& aMsg)
225 test.Next(_L("RFile::AdoptFromServer()"));
227 // pass the request on to FHServer2 - this will create a file
228 // which we can then adopt before returning it to the client
230 // get the requested file mode
231 TInt fileMode = aMsg.Int1();
233 RFileHandleSharer2 handsvr2;
234 TInt r = handsvr2.Connect();
238 TInt fsh = handsvr2.GetFileHandle(ssh, TFileMode(fileMode));
241 // adopt the file from FHServer2
243 r=file.AdoptFromServer(fsh, ssh);
246 test.Next(_L("RFile::TransferToClient()"));
248 // transfer the file to the client
249 r = file.TransferToClient(aMsg, 0);
252 // test we can still use the file
254 r = file.Seek(ESeekStart, pos);
259 r=rbuf.CompareF(KTestData1());
266 RDebug::Print(_L("completed"));
269 void CFHSession::PassFileHandle(const RMessage2& aMsg)
271 // Adopts file from test program and tests what it can and can't do
272 // Uses new AdoptFromClient() API
275 test.Next(_L("RFile::AdoptFromClient()"));
277 // connect to FHServer2
278 RFileHandleSharer2 handsvr2;
279 TInt r = handsvr2.Connect();
284 // Message slot 0 is a RFs handle
285 // Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
286 r = file.AdoptFromClient(aMsg, 0, 1);
294 r=rbuf.CompareF(KTestData());
296 r=file.Write(KTestData1());
297 test(r==KErrAccessDenied);
298 r=file.ChangeMode(EFileWrite);
299 test(r==KErrArgument);
300 r=file.Rename(_L("\\newname.txt"));
301 test(r==KErrPermissionDenied || r==KErrAccessDenied);
302 // should try a delete
304 // pass the file handle to FHServer2
305 test.Next(_L("RFile::TransferToServer()"));
308 file.TransferToServer(ipcArgs, 0, 1);
309 r = handsvr2.PassFileHandle(ipcArgs);
313 r = file.Seek(ESeekStart, pos);
317 r=rbuf.CompareF(KTestData());
324 aMsg.Complete(KErrNone);
328 // Adopts file from test program and tests what it can and can't do
329 // Uses new AdoptFromCreator() API
331 void CFHSession::PassFileHandleProcess(const RMessage2& aMsg)
333 test.Next(_L("RFile::AdoptFromCreator()"));
336 TInt r = file.AdoptFromCreator(1, 2);
342 r=rbuf.CompareF(KTestData());
345 test.Next(_L("RFile::Rename()"));
347 // define a filename in our private path
353 fs.SessionPath(sessionp);
354 r = fs.MkDirAll(sessionp);
355 test(r==KErrNone || r==KErrAlreadyExists);
357 r=fs.ShareProtected();
360 r=fs.CreatePrivatePath(gTestDrive);
362 r=fs.SetSessionToPrivate(gTestDrive);
366 fs.PrivatePath(newPath);
367 TFileName newFileName;
368 newFileName = newPath;
369 newFileName.Append(_L("newname.txt"));
371 // delete the file before we try to rename anything to it
372 r = fs.Delete(newFileName);
373 test(r == KErrNone || r == KErrNotFound);
375 r=file.Rename(newFileName);
380 // Next verify that we can delete the file (which should now
381 // have been moved to our private directory)
382 test.Next(_L("RFs::Delete()"));
383 r = fs.Delete(newFileName);
389 aMsg.Complete(KErrNone);
392 void CFHSession::PassInvalidFileHandle(const RMessage2& aMsg)
394 // Attempts to adopt an invalid file handle from test program
395 // and tests that KErrBadHandle is returned by AdoptFromClient()
398 test.Next(_L("PassInvalidFileHandle - RFile::AdoptFromClient()"));
402 // Message slot 0 is a RFs handle
403 // Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
404 TInt r = file.AdoptFromClient(aMsg, 0, 1);
405 test(r==KErrBadHandle);
411 CFHServer* CFHServer::NewL()
413 CFHServer* server = new (ELeave) CFHServer;
414 CleanupStack::PushL(server);
415 server->ConstructL();
416 CleanupStack::Pop(server);
420 void CFHServer::ConstructL()
424 CFHServer::CFHServer()
425 : CServer2(0,ESharableSessions)
429 CFHServer::~CFHServer()
433 CSession2* CFHServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
435 // Create New Session
439 CFHSession* s = new (ELeave) CFHSession;
443 _LIT(KErr,"FHSERVER_ERR");
444 CFHServer2* CFHServer2::NewL()
446 CFHServer2* server = new (ELeave) CFHServer2;
447 CleanupStack::PushL(server);
448 server->ConstructL();
449 CleanupStack::Pop(server);
453 void CFHServer2::ConstructL()
457 TInt CFHServer::RunError(TInt aError)
459 User::Panic(KErr,aError);
463 // File handle server #2
464 CFHServer2::CFHServer2()
465 : CServer2(0,ESharableSessions)
469 CFHServer2::~CFHServer2()
473 CSession2* CFHServer2::NewSessionL(const TVersion& aVersion, const RMessage2&) const
475 // Create New Session
479 CFHSession2* s = new (ELeave) CFHSession2;
483 _LIT(KErr2,"FHSERVER2_ERR");
484 TInt CFHServer2::RunError(TInt aError)
486 User::Panic(KErr2,aError);
490 CFHSession2::~CFHSession2()
494 void CFHSession2::CreateL()
499 void CFHSession2::ServiceL(const RMessage2& aMessage)
502 TInt mid=aMessage.Function();
505 case RFileHandleSharer::EMsgPassFileHandle:
506 PassFileHandle(aMessage);
509 case RFileHandleSharer::EMsgGetFileHandle:
510 GetFileHandle(aMessage);
513 case RFileHandleSharer::EMsgExit:
514 aMessage.Complete(KErrNone);
515 CActiveScheduler::Stop();
526 // Adopts file from server 1
528 void CFHSession2::PassFileHandle(const RMessage2& aMsg)
532 // Message slot 0 is a RFs handle
533 // Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
534 TInt r = file.AdoptFromClient(aMsg, 0, 1);
548 r = rbuf.CompareF(KTestData());
552 r = file.Write(KTestData1());
553 if (r == KErrAccessDenied)
559 r = file.ChangeMode(EFileWrite);
560 if (r == KErrArgument)
566 r = file.Rename(_L("\\newname.txt"));
567 if (r == KErrPermissionDenied || r == KErrAccessDenied)
577 void CFHSession2::GetFileHandle(const RMessage2& aMsg)
579 // Returns a file handle in write mode
583 TInt r = fs.Connect();
586 r = fs.CreatePrivatePath(gTestDrive);
589 r = fs.SetSessionToPrivate(gTestDrive);
592 r = fs.ShareProtected();
594 // make sure file exists & has valid data in it
597 r = file1.Replace(fs,KSvrFileName,EFileWrite);
600 r = file1.Write(KTestData1());
605 // re-open the file with the mode the client has requested & pass it to the client
606 TFileMode fileMode = TFileMode(aMsg.Int1());
608 r = file1.Open(fs,KSvrFileName, fileMode);
610 r = file1.TransferToClient(aMsg, 0);
619 LOCAL_C TInt FHServer2(TAny * /*anArg*/)
621 RTest test(_L("FHServer2"));
623 // Remember the number of open handles. Just for a sanity check ....
624 TInt start_thc, start_phc;
625 RThread().HandleCount(start_phc, start_thc);
628 CTrapCleanup* cleanup;
629 cleanup=CTrapCleanup::New();
631 CActiveScheduler* sched=new CActiveScheduler;
633 CActiveScheduler::Install(sched);
635 CFHServer2* svr2 = NULL;
636 TRAP_IGNORE(svr2 = CFHServer2::NewL());
638 FHS_ASSERT(svr2->Start(_L("FHServer2"))==KErrNone);
640 CActiveScheduler::Start();
647 // Sanity check for open handles
648 TInt end_thc, end_phc;
649 RThread().HandleCount(end_phc, end_thc);
650 test(start_thc == end_thc);
651 test(start_phc == end_phc);
652 // and also for pending requests ...
653 test(RThread().RequestCount() == 0);
661 GLDEF_C TInt E32Main()
663 // Test Server for file handle sharing
667 test.Start(_L("Starting FHServer..."));
669 // Remember the number of open handles. Just for a sanity check ....
670 TInt start_thc, start_phc;
671 RThread().HandleCount(start_phc, start_thc);
673 CTrapCleanup* cleanup=CTrapCleanup::New();
676 CActiveScheduler* sched=new CActiveScheduler;
678 CActiveScheduler::Install(sched);
681 CFHServer* svr = NULL;
682 TRAP_IGNORE(svr = CFHServer::NewL());
684 FHS_ASSERT(svr->Start(_L("FHServer"))==KErrNone);
687 test.Start(_L("Starting tests..."));
689 // start server2 in a seperate thread
690 RThread server2Thread;
691 TInt r = server2Thread.Create(_L("FHServer2"), FHServer2, KDefaultStackSize, KHeapSize, KHeapSize, NULL);
693 TRequestStatus statq;
694 server2Thread.Logon(statq);
695 server2Thread.Resume();
697 CActiveScheduler::Start();
700 // wait for server2's thread to end gracefully
701 User::WaitForRequest(statq);
704 server2Thread.Close();
707 r = cleanupfs.Connect();
709 r=cleanupfs.SetSessionToPrivate(gTestDrive);
711 r=cleanupfs.Delete(KSvrFileName);
712 test(r==KErrNone || r==KErrNotFound);
722 // Sanity check for open handles and pending requests
723 TInt end_thc, end_phc;
724 RThread().HandleCount(end_phc, end_thc);
725 test(start_thc == end_thc);
726 test(start_phc == end_phc);
727 test(RThread().RequestCount() == 0);