First public contribution.
1 // Copyright (c) 1996-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\server\t_handshare.cpp
21 #include "handshare.h"
23 GLDEF_D RTest test(_L("T_HANDSHARE"));
25 LOCAL_D TInt drivenum;
29 // test of (deprecated) RFile::Adopt()
31 // Request an open file (read only)
33 GLDEF_C void RequestFileDeprecatedAdopt()
35 test.Next(_L("RFile::Adopt()"));
38 RFileHandleSharer handsvr;
43 while(r==KErrNotFound);
45 r=handsvr.SetTestDrive(drivenum);
49 TInt fsh = handsvr.GetFileHandle(ssh, EFileRead);
51 r = fs1.SetReturnedHandle(fsh);
55 r=file.Adopt(fs1,ssh);
62 r=rbuf.CompareF(KTestData1());
65 r=file.Write(KTestData());
66 test(r==KErrAccessDenied);
68 r=file.ChangeMode(EFileWrite);
69 test(r==KErrArgument);
71 r=file.Rename(_L("\\newname.txt"));
72 test(r==KErrPermissionDenied || r==KErrAccessDenied);
76 fsh = handsvr.GetFileHandle(ssh, EFileRead);
77 r = fs1.SetReturnedHandle(fsh);
80 // Adopt a bad sub-session handle
82 r=file.Adopt(fs1, KNullHandle);
83 test(r==KErrBadHandle);
85 r=file.Adopt(fs1, -1);
86 test(r==KErrBadHandle);
91 RDebug::Print(_L("End Of Tests"));
98 GLDEF_C void Duplicate()
100 test.Next(_L("RFile::Duplicate()"));
106 // Check the number of open file handles
107 TInt resCount = fs.ResourceCount();
110 // create a file & fill it with data
112 r=file1.Replace(fs,KCliFileName,EFileWrite);
114 r=file1.Write(KTestData());
118 // open the file for read
119 r = file1.Open(fs,KCliFileName,EFileRead);
122 r = file1.Read(0,rbuf);
124 r=rbuf.CompareF(KTestData);
130 r = file2.Duplicate(file1);
133 // make sure file positions are the same
135 r = file1.Seek(ESeekCurrent, pos1);
138 r = file2.Seek(ESeekCurrent, pos2);
142 // change the position on the duplcated file handle &
143 // verify that the original file handle's position is unchanged
146 const TInt newPos2 = 5;
148 r = file2.Seek(ESeekStart, pos2);
152 r = file1.Seek(ESeekCurrent, pos1);
154 test(pos1 == oldPos1);
157 r = file2.Seek(ESeekCurrent, pos2);
159 test(pos2 == newPos2);
162 // close the parent file and check we can still use the duplicated one.
165 // Check the number of open file handles - should be 1 (the duplicated one)
166 resCount = fs.ResourceCount();
175 r = file2.Seek(ESeekStart, pos2);
178 r=file2.Read(0,rbuf);
180 r=rbuf.CompareF(KTestData);
184 // start again - this time we're going to close the duplicated file first
185 // and check we can still use the parent file
190 // Make a note of the number of open resources
191 fs.ResourceCountMarkStart();
193 // open the file for read
194 r = file1.Open(fs,KCliFileName,EFileRead);
198 // clone the file & check we can read it
199 r = file2.Duplicate(file1, EOwnerThread);
202 r = file2.Read(0,rbuf);
204 r=rbuf.CompareF(KTestData);
208 // close the duplicated file and check we can still use the parent one.
213 // check we can read the parent file
214 r=file1.Read(0,rbuf);
216 r=rbuf.CompareF(KTestData);
222 // Check the number of open file handles
223 fs.ResourceCountMarkEnd();
224 resCount = fs.ResourceCount();
231 // Request an open file (write mode) from the server
232 // using RFile::AdoptFromServer()
233 GLDEF_C void RequestFileWrite()
236 RFileHandleSharer handsvr;
241 while(r==KErrNotFound);
244 r=handsvr.SetTestDrive(drivenum);
248 TInt fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
251 // Closing the handle to the server ensures the server has closed it's
252 // RFs and RFile handles - this provides a means of testing whether we
253 // can still adopt the RFile even if the server has closed it's one.
255 handsvr.Sync(); // make sure server has finished doing what it's doing
258 // adopt the file handle from FHServer
259 test.Next(_L("RFile::AdoptFromServer()"));
262 r=file.AdoptFromServer(fsh, ssh);
269 // server should write KTestData1 ("Server Write Server Write") to file
270 r=rbuf.CompareF(KTestData1);
275 r = file.Seek(ESeekStart, pos);
278 // overwrite with KTestData ("Client Write Client Write") to file
279 r=file.Write(KTestData());
284 r=rbuf.CompareF(KTestData);
287 // !!! Disable platform security tests until we get the new APIs
288 // r=file.Rename(_L("\\newname.txt"));
289 // test(r==KErrPermissionDenied);
291 test.Next(_L("RFile::Name()"));
293 // retrieve the file name from the server
297 r = name.Compare(KSvrFileName());
301 test.Next(_L("RFile::Duplicate()"));
303 r = file2.Duplicate(file);
308 r = file.Seek(ESeekCurrent, pos1);
311 r = file2.Seek(ESeekCurrent, pos2);
315 // close the parent file and check we can still use the duplicated one.
322 r = file2.Seek(ESeekStart, pos2);
325 r=file2.Read(0,rbuf);
327 r=rbuf.CompareF(KTestData);
333 // Request a test file & test what we can do with it
334 // i.e CFileMan::Copy, RFile::Rename() and RFs:Delete()
336 void RequestFileTest()
343 r=fs.ShareProtected();
347 // define a filename in our private path
349 fs.PrivatePath(newPath);
350 TFileName newFileName;
351 newFileName = newPath;
352 newFileName.Append(_L("newname.txt"));
354 r=fs.CreatePrivatePath(drivenum);
356 r=fs.SetSessionToPrivate(drivenum);
358 RFileHandleSharer handsvr;
363 while(r==KErrNotFound);
366 r=handsvr.SetTestDrive(drivenum);
369 // Next verify that we can copy a file retrieved from the server
370 // using CFileMan::Copy()
372 test.Next(_L("CFileMan::Copy()"));
375 TInt fsh = handsvr.GetFileHandle2(ssh, EFileRead);
379 r=file.AdoptFromServer(fsh, ssh);
383 CFileMan* fileMan = NULL;
384 TRAP(r, fileMan = CFileMan::NewL(fs));
385 test(r == KErrNone && fileMan != NULL);
387 // copy to file, overwrite
388 r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
391 // copy to file, don't overwrite
392 r = fileMan->Copy(file, newFileName, 0);
393 test(r == KErrAlreadyExists);
395 // copy to file, overwrite
396 r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
400 r = fileMan->Copy(file, newPath, CFileMan::EOverWrite);
403 // copy to file, overwrite, asynchnonous
404 TRequestStatus status(KRequestPending);
405 r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite, status);
407 User::WaitForRequest(status);
408 test(status == KErrNone);
415 // copy to very long but valid path (no filename) which will overflow
416 // when the filename is appended to it
417 newLongPath = newPath;
418 for (len=newLongPath.Length(); len< KMaxPath -4; len = newLongPath.Length())
419 newLongPath.Append(_L("x\\"));
420 r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
421 test(r == KErrBadName);
423 // copy to very long but valid path (no filename) which will overflow
424 // when drive letter is pre-pended to it
425 newLongPath = newPath;
426 for (len=newLongPath.Length(); len< KMaxPath -2; len = newLongPath.Length())
427 newLongPath.Append(_L("x\\"));
428 r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
429 test(r == KErrBadName);
431 // copy to very long but valid path and filename which will overflow
432 // when drive letter is pre-pended to it
433 newLongPath.Append(_L("y"));
434 r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
435 test(r == KErrBadName);
437 // copy to badly formed path
438 newLongPath = newPath;
439 newLongPath.Append(_L("\\y"));
440 r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
441 test(r == KErrBadName);
443 // copy to correctly formed path which doesn't exist
444 newLongPath = newPath;
445 newLongPath.Append(_L("x\\y\\z"));
446 r = fileMan->Copy(file, newLongPath, CFileMan::EOverWrite);
447 test(r == KErrPathNotFound);
449 delete fileMan; fileMan = NULL;
454 // First verify that we CANNOT rename a file retrieved from the server
455 // that has not been opened in EFileShareExclusive mode
457 test.Next(_L("negative test of RFile::Rename()"));
459 fsh = handsvr.GetFileHandle2(ssh, TFileMode(EFileShareAny | EFileWrite));
462 // adopt the file handle from FHServer
463 r=file.AdoptFromServer(fsh, ssh);
466 r=file.Rename(_L("newname.txt"));
467 test(r==KErrPermissionDenied || r==KErrAccessDenied);
469 // delete the file before we try to rename anything to it
470 r = fs.Delete(newFileName);
471 test(r == KErrNone || r == KErrNotFound);
473 r=file.Rename(newFileName);
474 test(r==KErrPermissionDenied || r==KErrAccessDenied);
479 // Next verify that we CAN rename a file retrieved from the server
480 // that HAS been opened in EFileShareExclusive mode
482 test.Next(_L("RFile::Rename()"));
484 fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
487 r=file.AdoptFromServer(fsh, ssh);
490 // delete the file before we try to rename anything to it
491 r = fs.Delete(newFileName);
492 test(r == KErrNone || r == KErrNotFound);
494 r=file.Rename(newFileName);
499 // Next verify that we can delete the file (which should now
500 // have been moved to our private directory)
501 test.Next(_L("RFs::Delete()"));
502 r = fs.Delete(newFileName);
511 // Pass an open file to the server
512 // The server will use RFile::AdoptFromClient()
514 GLDEF_C void PassFile()
520 // Check the number of open file handles
521 TInt resCount = fs.ResourceCount();
524 r=fs.ShareProtected();
527 r=fs.CreatePrivatePath(drivenum);
529 r=fs.SetSessionToPrivate(drivenum);
533 r=file1.Replace(fs,KCliFileName,EFileWrite);
535 r=file1.Write(KTestData());
539 RFileHandleSharer handsvr;
544 while(r==KErrNotFound);
547 r=handsvr.SetTestDrive(drivenum);
550 r=fs.SetSessionToPrivate(drivenum);
553 r=file1.Open(fs,KCliFileName,EFileRead);
556 // pass the file handle to FHServer
557 test.Next(_L("RFile::TransferToServer()"));
560 file1.TransferToServer(ipcArgs, 0, 1);
562 r = handsvr.PassFileHandle(ipcArgs);
565 // verify that the original file handle's position is unchanged
567 r = file1.Seek(ESeekCurrent, pos);
571 // make sure we can still use it
573 r=file1.Read(0,rbuf);
581 r=fs.MkDir(_L("C:\\mdir"));
582 test(r==KErrNone || r==KErrAlreadyExists);
584 // Check the number of open file handles
585 resCount = fs.ResourceCount();
593 // Pass an invalid file handle to the server
594 // The server will use RFile::AdoptFromClient()
596 GLDEF_C void PassInvalidFile()
603 // Check the number of open file handles
604 TInt resCount = fs.ResourceCount();
607 r=fs.ShareProtected();
610 r=fs.CreatePrivatePath(drivenum);
612 r=fs.SetSessionToPrivate(drivenum);
616 r=file1.Replace(fs,KCliFileName,EFileWrite);
618 r=file1.Write(KTestData());
622 RFileHandleSharer handsvr;
627 while(r==KErrNotFound);
630 r=handsvr.SetTestDrive(drivenum);
633 r=fs.SetSessionToPrivate(drivenum);
636 r=file1.Open(fs,KCliFileName,EFileRead);
639 // check the resoure count - there should be 1 open file handle
640 resCount = fs.ResourceCount();
643 // pass an invalid file handle to FHServer
644 // by overwriting the IPC slots
645 test.Next(_L("PassInvalidFileHandle - RFile::TransferToServer()"));
649 // Pass a bad RFs handle
650 file1.TransferToServer(ipcArgs, 0, 1);
652 // check the resoure count - there should be 2 open file handles
653 resCount = fs.ResourceCount();
656 ipcArgs.Set(0, 0); // invalidate the RFs handle
657 r = handsvr.PassInvalidFileHandle(ipcArgs);
658 test (r == KErrBadHandle);
660 // Pass a bad RFile handle
661 file1.TransferToServer(ipcArgs, 0, 1);
663 // check the resoure count - there should be 3 open file handles
664 resCount = fs.ResourceCount();
667 ipcArgs.Set(1, 0); // invalidate the RFile handle
668 r = handsvr.PassInvalidFileHandle(ipcArgs);
669 test (r == KErrBadHandle);
671 // Pass bad RFs and RFile handles
672 file1.TransferToServer(ipcArgs, 0, 1);
674 // check the resoure count - there should be 4 open file handles
675 resCount = fs.ResourceCount();
679 ipcArgs.Set(0, 0); // invalidate the RFs handle
680 ipcArgs.Set(1, 0); // invalidate the RFile handle
681 r = handsvr.PassInvalidFileHandle(ipcArgs);
682 test (r == KErrBadHandle);
688 // Check the number of open file handles
689 resCount = fs.ResourceCount();
696 // Pass RFs/RFile handles to the server using
697 // process parameter slots (RProcess::SetParameter()),
698 // resume the process and wait for it to read the file...
699 // The server will use RFile::AdoptFromCreator()
700 GLDEF_C void PassFile(RProcess& aProcess)
707 // Check the number of open file handles
708 TInt resCount = fs.ResourceCount();
711 r=fs.ShareProtected();
714 r=fs.CreatePrivatePath(drivenum);
716 r=fs.SetSessionToPrivate(drivenum);
720 r=file1.Replace(fs,KCliFileName,EFileWrite);
722 r=file1.Write(KTestData());
726 r=file1.Open(fs, KCliFileName, EFileWrite);
730 // NB slot 0 is reserved for the command line
732 test.Next(_L("RFile::TransferToProcess()"));
734 r = file1.TransferToProcess(aProcess, 1, 2);
736 r = aProcess.SetParameter(3, drivenum);
739 r=fs.SetSessionToPrivate(drivenum);
742 // make sure we can still read from the file
744 r=file1.Read(0,rbuf);
746 r=rbuf.CompareF(KTestData());
750 r=fs.MkDir(_L("C:\\mdir"));
751 test(r==KErrNone || r==KErrAlreadyExists);
753 // Check the number of open file handles -
754 // should be 1 (the one duplicated for the other process)
755 resCount = fs.ResourceCount();
760 // Start the server thread
765 // connect to the server
766 RFileHandleSharer handsvr;
771 while(r==KErrNotFound);
775 r=handsvr.SetTestDrive(drivenum);
778 // wait for server to read the file
779 r = handsvr.PassFileHandleProcess();
780 test (r == KErrNone);
787 GLDEF_C void CallTestsL(void)
789 RFs::CharToDrive(gDriveToTest,drivenum);
791 // make sure the session path exists
793 TInt r = fs.Connect();
797 fs.SessionPath(sessionp);
798 r = fs.MkDirAll(sessionp);
799 test(r==KErrNone || r==KErrAlreadyExists);
802 // Remember the number of open handles. Just for a sanity check ....
803 TInt start_thc, start_phc;
804 RThread().HandleCount(start_phc, start_thc);
805 test.Printf(_L("Handles: start_phc %d, start_thc %d\n"), start_phc, start_thc);
809 r = p.Create(_L("FHServer.exe"), KNullDesC);
812 // RFile::Duplicate() tests
815 // Pass RFs/RFile handles to the server using RFile::TransferToProcess()
816 // process parameter slots (RProcess::SetParameter()),
817 // resume the server process and wait for it to read the file...
820 // Transfer the file handle to FHServer using RFile::TransferToServer().
821 // Get FHServer to transfer it to FHServer2
825 // Get an open writeable file from FHServer2 via FHServer
826 // using RFile::AdoptFromServer()
829 // Deprecated RFile::Adopt() test
830 RequestFileDeprecatedAdopt();
833 // Pass an invalid file handle to the server
837 // Get an open file handle from FHServer2 via FHServer
838 // and test whether we can copy/rename/delete it etc
839 // be able to rename it.
843 RFileHandleSharer handsvr;
850 // delete the test file
852 r=cleanupfs.Connect();
854 r=cleanupfs.SetSessionToPrivate(drivenum);
856 r=cleanupfs.Delete(KCliFileName);
857 test(r==KErrNone || r==KErrNotFound);
861 // wait for server process to end
862 TRequestStatus status;
864 User::WaitForRequest(status);
865 test (status == KErrNone);
870 // Sanity check for open handles
871 TInt end_thc, end_phc;
872 RThread().HandleCount(end_phc, end_thc);
873 test.Printf(_L("Handles: end_phc %d, end_thc %d\n"), end_phc, end_thc);
875 test(start_thc == end_thc);
876 test(start_phc == end_phc);
878 // and also for pending requests ...
879 test(RThread().RequestCount() == 0);
882 RDebug::Print(_L("End Of Tests"));