kerneltest/f32test/fileshare/t_handshare.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // f32test\server\t_handshare.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <f32file.h>
       
    19 #include <e32test.h>
       
    20 #include "t_server.h"
       
    21 #include "handshare.h"
       
    22 
       
    23 GLDEF_D RTest test(_L("T_HANDSHARE"));
       
    24 
       
    25 LOCAL_D TInt drivenum;
       
    26 
       
    27 
       
    28 
       
    29 // test of (deprecated) RFile::Adopt()
       
    30 //
       
    31 // Request an open file (read only)
       
    32 //
       
    33 GLDEF_C void RequestFileDeprecatedAdopt()
       
    34 	{
       
    35 	test.Next(_L("RFile::Adopt()"));
       
    36 
       
    37 	TInt r;
       
    38 	RFileHandleSharer handsvr;
       
    39 	do
       
    40 		{
       
    41 		r=handsvr.Connect();
       
    42 		}
       
    43 		while(r==KErrNotFound);
       
    44 	test(r==KErrNone);
       
    45 	r=handsvr.SetTestDrive(drivenum);
       
    46 	test(r==KErrNone);
       
    47 	TInt ssh;
       
    48 	RFs fs1;
       
    49 	TInt fsh = handsvr.GetFileHandle(ssh, EFileRead);
       
    50 
       
    51 	r = fs1.SetReturnedHandle(fsh);
       
    52 	test(r==KErrNone);
       
    53 	
       
    54 	RFile file;
       
    55 	r=file.Adopt(fs1,ssh);
       
    56 	test(r==KErrNone);
       
    57 	
       
    58 	TBuf8<100> rbuf;
       
    59 	r=file.Read(0,rbuf);
       
    60 	test(r==KErrNone);
       
    61 	
       
    62 	r=rbuf.CompareF(KTestData1());
       
    63 	test(r==KErrNone);
       
    64 	
       
    65 	r=file.Write(KTestData());
       
    66 	test(r==KErrAccessDenied);
       
    67 	
       
    68 	r=file.ChangeMode(EFileWrite);
       
    69 	test(r==KErrArgument);
       
    70 	
       
    71 	r=file.Rename(_L("\\newname.txt"));
       
    72 	test(r==KErrPermissionDenied || r==KErrAccessDenied);
       
    73 
       
    74 	file.Close();
       
    75 
       
    76 	fsh = handsvr.GetFileHandle(ssh, EFileRead);
       
    77 	r = fs1.SetReturnedHandle(fsh);
       
    78 	test(r==KErrNone);
       
    79 
       
    80 	// Adopt a bad sub-session handle
       
    81 
       
    82 	r=file.Adopt(fs1, KNullHandle);
       
    83 	test(r==KErrBadHandle);
       
    84 
       
    85 	r=file.Adopt(fs1, -1);
       
    86 	test(r==KErrBadHandle);
       
    87 
       
    88 
       
    89 	handsvr.Close();
       
    90 	file.Close();
       
    91 	RDebug::Print(_L("End Of Tests"));
       
    92 	fs1.Close();
       
    93 	}
       
    94 
       
    95 
       
    96 
       
    97 // Cloning tests
       
    98 GLDEF_C void Duplicate()
       
    99 	{
       
   100 	test.Next(_L("RFile::Duplicate()"));
       
   101 
       
   102 	RFs fs;
       
   103 	TInt r=fs.Connect();
       
   104 	test(r==KErrNone);
       
   105 
       
   106 	// Check the number of open file handles
       
   107 	TInt resCount = fs.ResourceCount();
       
   108 	test(resCount == 0);
       
   109 
       
   110 	// create a file & fill it with data	
       
   111 	RFile file1;
       
   112 	r=file1.Replace(fs,KCliFileName,EFileWrite);
       
   113 	test(r==KErrNone);
       
   114 	r=file1.Write(KTestData());
       
   115 	test(r==KErrNone);
       
   116 	file1.Close();
       
   117 
       
   118 	// open the file for read
       
   119 	r = file1.Open(fs,KCliFileName,EFileRead);
       
   120 	test(r==KErrNone);
       
   121 	TBuf8<100> rbuf;
       
   122 	r = file1.Read(0,rbuf);
       
   123 	test(r==KErrNone);
       
   124 	r=rbuf.CompareF(KTestData);
       
   125 	test(r==KErrNone);
       
   126 
       
   127 
       
   128 	// clone the file
       
   129 	RFile file2;
       
   130 	r = file2.Duplicate(file1);
       
   131 	test(r==0);
       
   132 
       
   133 	// make sure file positions are the same
       
   134 	TInt pos1 = 0;
       
   135 	r = file1.Seek(ESeekCurrent, pos1);
       
   136 	test(r==KErrNone);
       
   137 	TInt pos2 = 0;
       
   138 	r = file2.Seek(ESeekCurrent, pos2);
       
   139 	test(r==KErrNone);
       
   140 	test(pos1 == pos2);
       
   141 
       
   142 	// change the position on the duplcated file handle & 
       
   143 	// verify that the original file handle's position is unchanged
       
   144 	TInt oldPos1 = pos1;
       
   145 	
       
   146 	const TInt newPos2 = 5;
       
   147 	pos2 = newPos2;
       
   148 	r = file2.Seek(ESeekStart, pos2);
       
   149 	test(r==KErrNone);
       
   150 	
       
   151 	pos1 = 0;
       
   152 	r = file1.Seek(ESeekCurrent, pos1);
       
   153 	test(r==KErrNone);
       
   154 	test(pos1 == oldPos1);
       
   155 	
       
   156 	pos2 = 0;
       
   157 	r = file2.Seek(ESeekCurrent, pos2);
       
   158 	test(r==KErrNone);
       
   159 	test(pos2 == newPos2);
       
   160 	test(pos1 != pos2);
       
   161 
       
   162 	// close the parent file and check we can still use the duplicated one.
       
   163 	file1.Close();
       
   164 
       
   165 	// Check the number of open file handles - should be 1 (the duplicated one)
       
   166 	resCount = fs.ResourceCount();
       
   167 	test(resCount == 1);
       
   168 
       
   169 	fs.Close();
       
   170 
       
   171 	rbuf.FillZ();
       
   172 
       
   173 	// reset to pos 0
       
   174 	pos2 = 0;
       
   175 	r = file2.Seek(ESeekStart, pos2);
       
   176 	test(r==KErrNone);
       
   177 	
       
   178 	r=file2.Read(0,rbuf);
       
   179 	test(r==KErrNone);
       
   180 	r=rbuf.CompareF(KTestData);
       
   181 	test(r==KErrNone);
       
   182 	file2.Close();
       
   183 
       
   184 	// start again - this time we're going to close the duplicated file first
       
   185 	// and check we can still use the parent file
       
   186 
       
   187 	r = fs.Connect();
       
   188 	test(r==KErrNone);
       
   189 
       
   190 	// Make a note of the number of open resources
       
   191 	fs.ResourceCountMarkStart();
       
   192 
       
   193 	// open the file for read
       
   194 	r = file1.Open(fs,KCliFileName,EFileRead);
       
   195 	test(r==KErrNone);
       
   196 
       
   197 
       
   198 	// clone the file & check we can read it
       
   199 	r = file2.Duplicate(file1, EOwnerThread);
       
   200 	test(r==0);
       
   201 	rbuf.FillZ();
       
   202 	r = file2.Read(0,rbuf);
       
   203 	test(r==KErrNone);
       
   204 	r=rbuf.CompareF(KTestData);
       
   205 	test(r==KErrNone);
       
   206 
       
   207 
       
   208 	// close the duplicated file and check we can still use the parent one.
       
   209 	file2.Close();
       
   210 
       
   211 	rbuf.FillZ();
       
   212 
       
   213 	// check we can read the parent file
       
   214 	r=file1.Read(0,rbuf);
       
   215 	test(r==KErrNone);
       
   216 	r=rbuf.CompareF(KTestData);
       
   217 	test(r==KErrNone);
       
   218 
       
   219 	// close the parent
       
   220 	file1.Close();
       
   221 
       
   222 	// Check the number of open file handles
       
   223 	fs.ResourceCountMarkEnd();
       
   224 	resCount = fs.ResourceCount();
       
   225 	test(resCount == 0);
       
   226 
       
   227 	fs.Close();
       
   228 	}
       
   229 
       
   230 
       
   231 // Request an open file (write mode) from the server
       
   232 // using RFile::AdoptFromServer()
       
   233 GLDEF_C void RequestFileWrite()
       
   234 	{
       
   235 	TInt r;
       
   236 	RFileHandleSharer handsvr;
       
   237 	do
       
   238 		{
       
   239 		r=handsvr.Connect();
       
   240 		}
       
   241 	while(r==KErrNotFound);
       
   242 	test(r==KErrNone);
       
   243 
       
   244 	r=handsvr.SetTestDrive(drivenum);
       
   245 	test(r==KErrNone);
       
   246 
       
   247 	TInt ssh;
       
   248 	TInt fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
       
   249 	test (fsh >= 0);
       
   250 
       
   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.
       
   254 
       
   255 	handsvr.Sync(); // make sure server has finished doing what it's doing
       
   256 	handsvr.Close();
       
   257 
       
   258 	// adopt the file handle from FHServer
       
   259 	test.Next(_L("RFile::AdoptFromServer()"));
       
   260 
       
   261 	RFile file;
       
   262 	r=file.AdoptFromServer(fsh, ssh);
       
   263 	test(r==KErrNone);
       
   264 
       
   265 	TBuf8<100> rbuf;
       
   266 	r=file.Read(0,rbuf);
       
   267 	test(r==KErrNone);
       
   268 
       
   269 	// server should write KTestData1 ("Server Write Server Write") to file
       
   270 	r=rbuf.CompareF(KTestData1);
       
   271 	test(r==KErrNone);
       
   272 
       
   273 	// reset to pos 0
       
   274 	TInt pos = 0;
       
   275 	r = file.Seek(ESeekStart, pos);
       
   276 	test(r==KErrNone);
       
   277 	
       
   278 	// overwrite with KTestData ("Client Write Client Write") to file
       
   279 	r=file.Write(KTestData());
       
   280 	test(r==KErrNone);
       
   281 	rbuf.FillZ();
       
   282 	r=file.Read(0,rbuf);
       
   283 	test(r==KErrNone);
       
   284 	r=rbuf.CompareF(KTestData);
       
   285 	test(r==KErrNone);
       
   286 
       
   287 // !!! Disable platform security tests until we get the new APIs
       
   288 //	r=file.Rename(_L("\\newname.txt"));
       
   289 //	test(r==KErrPermissionDenied);
       
   290 
       
   291 	test.Next(_L("RFile::Name()"));
       
   292 
       
   293 	// retrieve the file name from the server
       
   294 	TFileName name;
       
   295 	r = file.Name(name);
       
   296 	test(r==KErrNone);
       
   297 	r = name.Compare(KSvrFileName());
       
   298 	test(r==0);
       
   299 	
       
   300 
       
   301 	test.Next(_L("RFile::Duplicate()"));
       
   302 	RFile file2;
       
   303 	r = file2.Duplicate(file);
       
   304 	test(r==0);
       
   305 
       
   306 
       
   307 	TInt pos1 = 0;
       
   308 	r = file.Seek(ESeekCurrent, pos1);
       
   309 	test(r==KErrNone);
       
   310 	TInt pos2 = 0;
       
   311 	r = file2.Seek(ESeekCurrent, pos2);
       
   312 	test(r==KErrNone);
       
   313 	test(pos1 == pos2);
       
   314 
       
   315 	// close the parent file and check we can still use the duplicated one.
       
   316 	file.Close();
       
   317 
       
   318 	rbuf.FillZ();
       
   319 
       
   320 	// reset to pos 0
       
   321 	pos2 = 0;
       
   322 	r = file2.Seek(ESeekStart, pos2);
       
   323 	test(r==KErrNone);
       
   324 	
       
   325 	r=file2.Read(0,rbuf);
       
   326 	test(r==KErrNone);
       
   327 	r=rbuf.CompareF(KTestData);
       
   328 	test(r==KErrNone);
       
   329 	file2.Close();
       
   330 	}
       
   331 
       
   332 
       
   333 // Request a test file & test what we can do with it
       
   334 // i.e CFileMan::Copy, RFile::Rename() and RFs:Delete()
       
   335 //
       
   336 void RequestFileTest()
       
   337 	{
       
   338 	TInt r;
       
   339 
       
   340 	RFs fs;
       
   341 	r=fs.Connect();
       
   342 	test(r==KErrNone);
       
   343 	r=fs.ShareProtected();
       
   344 	test(r==KErrNone);
       
   345 
       
   346 	
       
   347 	// define a filename in our private path
       
   348 	TPath newPath;
       
   349 	fs.PrivatePath(newPath);
       
   350 	TFileName newFileName;
       
   351 	newFileName = newPath;
       
   352 	newFileName.Append(_L("newname.txt"));
       
   353 	
       
   354 	r=fs.CreatePrivatePath(drivenum);
       
   355 	test(r==KErrNone);
       
   356 	r=fs.SetSessionToPrivate(drivenum);
       
   357 
       
   358 	RFileHandleSharer handsvr;
       
   359 	do
       
   360 		{
       
   361 		r=handsvr.Connect();
       
   362 		}
       
   363 	while(r==KErrNotFound);
       
   364 	test(r==KErrNone);
       
   365 
       
   366 	r=handsvr.SetTestDrive(drivenum);
       
   367 	test(r==KErrNone);
       
   368 
       
   369 	// Next verify that we can copy a file retrieved from the server
       
   370 	// using CFileMan::Copy()
       
   371 
       
   372 	test.Next(_L("CFileMan::Copy()"));
       
   373 
       
   374 	TInt ssh;
       
   375 	TInt fsh = handsvr.GetFileHandle2(ssh, EFileRead);
       
   376 	test (fsh >= 0);
       
   377 
       
   378 	RFile file;
       
   379 	r=file.AdoptFromServer(fsh, ssh);
       
   380 	test(r==KErrNone);
       
   381 
       
   382 
       
   383 	CFileMan* fileMan = NULL;
       
   384 	TRAP(r, fileMan = CFileMan::NewL(fs));
       
   385 	test(r == KErrNone && fileMan != NULL);
       
   386 
       
   387 	// copy to file, overwrite 
       
   388 	r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
       
   389 	test(r == KErrNone);
       
   390 
       
   391 	// copy to file, don't overwrite 
       
   392 	r = fileMan->Copy(file, newFileName, 0);	
       
   393 	test(r == KErrAlreadyExists);
       
   394 
       
   395 	// copy to file, overwrite
       
   396 	r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite);
       
   397 	test(r == KErrNone);
       
   398 
       
   399 	// copy to path
       
   400 	r = fileMan->Copy(file, newPath, CFileMan::EOverWrite);	
       
   401 	test(r == KErrNone);
       
   402 
       
   403 	// copy to file, overwrite, asynchnonous
       
   404 	TRequestStatus status(KRequestPending);
       
   405 	r = fileMan->Copy(file, newFileName, CFileMan::EOverWrite, status);
       
   406 	test(r == KErrNone);
       
   407 	User::WaitForRequest(status);
       
   408 	test(status == KErrNone);
       
   409 
       
   410 
       
   411 	// Negative tests...
       
   412 	TPath newLongPath;
       
   413 	TInt len;
       
   414 
       
   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);
       
   422 
       
   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);
       
   430 
       
   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);
       
   436 
       
   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);
       
   442 
       
   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);
       
   448 
       
   449 	delete fileMan; fileMan = NULL;
       
   450 
       
   451 	file.Close();
       
   452 
       
   453 	
       
   454 	// First verify that we CANNOT rename a file retrieved from the server
       
   455 	// that has not been opened in EFileShareExclusive mode
       
   456 
       
   457 	test.Next(_L("negative test of RFile::Rename()"));
       
   458 
       
   459 	fsh = handsvr.GetFileHandle2(ssh, TFileMode(EFileShareAny | EFileWrite));
       
   460 	test (fsh >= 0);
       
   461 
       
   462 	// adopt the file handle from FHServer
       
   463 	r=file.AdoptFromServer(fsh, ssh);
       
   464 	test(r==KErrNone);
       
   465 
       
   466 	r=file.Rename(_L("newname.txt"));
       
   467 	test(r==KErrPermissionDenied || r==KErrAccessDenied);
       
   468 
       
   469 	// delete the file before we try to rename anything to it
       
   470 	r = fs.Delete(newFileName);
       
   471 	test(r == KErrNone || r == KErrNotFound);
       
   472 
       
   473 	r=file.Rename(newFileName);
       
   474 	test(r==KErrPermissionDenied || r==KErrAccessDenied);
       
   475 
       
   476 	file.Close();
       
   477 
       
   478 
       
   479 	// Next verify that we CAN rename a file retrieved from the server
       
   480 	// that HAS been opened in EFileShareExclusive mode
       
   481 
       
   482 	test.Next(_L("RFile::Rename()"));
       
   483 
       
   484 	fsh = handsvr.GetFileHandle2(ssh, EFileWrite);
       
   485 	test (fsh >= 0);
       
   486 
       
   487 	r=file.AdoptFromServer(fsh, ssh);
       
   488 	test(r==KErrNone);
       
   489 
       
   490 	// delete the file before we try to rename anything to it
       
   491 	r = fs.Delete(newFileName);
       
   492 	test(r == KErrNone || r == KErrNotFound);
       
   493 
       
   494 	r=file.Rename(newFileName);
       
   495 	test(r==KErrNone);
       
   496 
       
   497 	file.Close();
       
   498 
       
   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);
       
   503 	test(r == KErrNone);
       
   504 
       
   505 	handsvr.Close();
       
   506 
       
   507 	fs.Close();
       
   508 	}
       
   509 
       
   510 //
       
   511 // Pass an open file to the server 
       
   512 // The server will use RFile::AdoptFromClient()
       
   513 //
       
   514 GLDEF_C void PassFile()
       
   515 	{
       
   516 	RFs fs;
       
   517 	TInt r=fs.Connect();
       
   518 	test(r==KErrNone);
       
   519 
       
   520 	// Check the number of open file handles
       
   521 	TInt resCount = fs.ResourceCount();
       
   522 	test(resCount == 0);
       
   523 
       
   524 	r=fs.ShareProtected();
       
   525 	test(r==KErrNone);
       
   526 
       
   527 	r=fs.CreatePrivatePath(drivenum);
       
   528 	test(r==KErrNone);
       
   529 	r=fs.SetSessionToPrivate(drivenum);
       
   530 	
       
   531 	
       
   532 	RFile file1;
       
   533 	r=file1.Replace(fs,KCliFileName,EFileWrite);
       
   534 	test(r==KErrNone);
       
   535 	r=file1.Write(KTestData());
       
   536 	test(r==KErrNone);
       
   537 	file1.Close();
       
   538 
       
   539 	RFileHandleSharer handsvr;
       
   540 	do
       
   541 		{
       
   542 		r=handsvr.Connect();
       
   543 		}
       
   544 	while(r==KErrNotFound);
       
   545 	test(r==KErrNone);
       
   546 
       
   547 	r=handsvr.SetTestDrive(drivenum);
       
   548 	test(r==KErrNone);
       
   549 
       
   550 	r=fs.SetSessionToPrivate(drivenum);
       
   551 	test(r==KErrNone);
       
   552 
       
   553 	r=file1.Open(fs,KCliFileName,EFileRead);
       
   554 	test(r==KErrNone);
       
   555 	
       
   556 	// pass the file handle to FHServer
       
   557 	test.Next(_L("RFile::TransferToServer()"));
       
   558 
       
   559 	TIpcArgs ipcArgs;
       
   560 	file1.TransferToServer(ipcArgs, 0, 1);
       
   561 
       
   562 	r = handsvr.PassFileHandle(ipcArgs);
       
   563 
       
   564 
       
   565 	// verify that the original file handle's position is unchanged
       
   566 	TInt pos = 0;
       
   567 	r = file1.Seek(ESeekCurrent, pos);
       
   568 	test(r==KErrNone);
       
   569 	test(pos == 0);
       
   570 
       
   571 	// make sure we can still use it
       
   572 	TBuf8<100> rbuf;
       
   573 	r=file1.Read(0,rbuf);
       
   574 	test(r==KErrNone);
       
   575 
       
   576 	// Close the file
       
   577 	file1.Close();	
       
   578 
       
   579 	handsvr.Close();
       
   580 
       
   581 	r=fs.MkDir(_L("C:\\mdir"));
       
   582 	test(r==KErrNone || r==KErrAlreadyExists);
       
   583 	
       
   584 	// Check the number of open file handles
       
   585 	resCount = fs.ResourceCount();
       
   586 	test(resCount == 0);
       
   587 
       
   588 	fs.Close();
       
   589 	}
       
   590 
       
   591 
       
   592 //
       
   593 // Pass an invalid file handle to the server 
       
   594 // The server will use RFile::AdoptFromClient()
       
   595 //
       
   596 GLDEF_C void PassInvalidFile()
       
   597 	{
       
   598 
       
   599 	RFs fs;
       
   600 	TInt r=fs.Connect();
       
   601 	test(r==KErrNone);
       
   602 
       
   603 	// Check the number of open file handles
       
   604 	TInt resCount = fs.ResourceCount();
       
   605 	test(resCount == 0);
       
   606 
       
   607 	r=fs.ShareProtected();
       
   608 	test(r==KErrNone);
       
   609 
       
   610 	r=fs.CreatePrivatePath(drivenum);
       
   611 	test(r==KErrNone);
       
   612 	r=fs.SetSessionToPrivate(drivenum);
       
   613 	
       
   614 	
       
   615 	RFile file1;
       
   616 	r=file1.Replace(fs,KCliFileName,EFileWrite);
       
   617 	test(r==KErrNone);
       
   618 	r=file1.Write(KTestData());
       
   619 	test(r==KErrNone);
       
   620 	file1.Close();
       
   621 
       
   622 	RFileHandleSharer handsvr;
       
   623 	do
       
   624 		{
       
   625 		r=handsvr.Connect();
       
   626 		}
       
   627 	while(r==KErrNotFound);
       
   628 	test(r==KErrNone);
       
   629 
       
   630 	r=handsvr.SetTestDrive(drivenum);
       
   631 	test(r==KErrNone);
       
   632 
       
   633 	r=fs.SetSessionToPrivate(drivenum);
       
   634 	test(r==KErrNone);
       
   635 
       
   636 	r=file1.Open(fs,KCliFileName,EFileRead);
       
   637 	test(r==KErrNone);
       
   638 	
       
   639 	// check the resoure count - there should be 1 open file handle
       
   640 	resCount = fs.ResourceCount();
       
   641 	test(resCount == 1);
       
   642 
       
   643 	// pass an invalid file handle to FHServer
       
   644 	// by overwriting the IPC slots
       
   645 	test.Next(_L("PassInvalidFileHandle - RFile::TransferToServer()"));
       
   646 
       
   647 	TIpcArgs ipcArgs;
       
   648 
       
   649 	// Pass a bad RFs handle
       
   650 	file1.TransferToServer(ipcArgs, 0, 1);
       
   651 
       
   652 	// check the resoure count - there should be 2 open file handles
       
   653 	resCount = fs.ResourceCount();
       
   654 	test(resCount == 2);
       
   655 
       
   656 	ipcArgs.Set(0, 0);	// invalidate the RFs handle
       
   657 	r = handsvr.PassInvalidFileHandle(ipcArgs);
       
   658 	test (r == KErrBadHandle);
       
   659 
       
   660 	// Pass a bad RFile handle
       
   661 	file1.TransferToServer(ipcArgs, 0, 1);
       
   662 
       
   663 	// check the resoure count - there should be 3 open file handles
       
   664 	resCount = fs.ResourceCount();
       
   665 	test(resCount == 3);
       
   666 
       
   667 	ipcArgs.Set(1, 0);	// invalidate the RFile handle
       
   668 	r = handsvr.PassInvalidFileHandle(ipcArgs);
       
   669 	test (r == KErrBadHandle);
       
   670 
       
   671 	// Pass bad RFs and RFile handles
       
   672 	file1.TransferToServer(ipcArgs, 0, 1);
       
   673 
       
   674 	// check the resoure count - there should be 4 open file handles
       
   675 	resCount = fs.ResourceCount();
       
   676 	test(resCount == 4);
       
   677 
       
   678 
       
   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);
       
   683 
       
   684 	// Close the file
       
   685 	handsvr.Close();
       
   686 	file1.Close();	
       
   687 
       
   688 	// Check the number of open file handles
       
   689 	resCount = fs.ResourceCount();
       
   690 	test(resCount == 3);
       
   691 
       
   692 	fs.Close();
       
   693 	}
       
   694 
       
   695 
       
   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)
       
   701 	{
       
   702 	
       
   703 	RFs fs;
       
   704 	TInt r=fs.Connect();
       
   705 	test(r==KErrNone);
       
   706 
       
   707 	// Check the number of open file handles
       
   708 	TInt resCount = fs.ResourceCount();
       
   709 	test(resCount == 0);
       
   710 
       
   711 	r=fs.ShareProtected();
       
   712 	test(r==KErrNone);
       
   713 
       
   714 	r=fs.CreatePrivatePath(drivenum);
       
   715 	test(r==KErrNone);
       
   716 	r=fs.SetSessionToPrivate(drivenum);
       
   717 	
       
   718 	
       
   719 	RFile file1;
       
   720 	r=file1.Replace(fs,KCliFileName,EFileWrite);
       
   721 	test(r==KErrNone);
       
   722 	r=file1.Write(KTestData());
       
   723 	test(r==KErrNone);
       
   724 	file1.Close();
       
   725 
       
   726 	r=file1.Open(fs, KCliFileName, EFileWrite);
       
   727 
       
   728 	test(r==KErrNone);
       
   729 	
       
   730 	// NB slot 0 is reserved for the command line
       
   731 
       
   732 	test.Next(_L("RFile::TransferToProcess()"));
       
   733 
       
   734 	r = file1.TransferToProcess(aProcess, 1, 2);
       
   735 
       
   736 	r = aProcess.SetParameter(3, drivenum);
       
   737 	test(r==KErrNone);
       
   738 
       
   739 	r=fs.SetSessionToPrivate(drivenum);
       
   740 	test(r==KErrNone);
       
   741 
       
   742 	// make sure we can still read from the file
       
   743 	TBuf8<100> rbuf;
       
   744 	r=file1.Read(0,rbuf);
       
   745 	test(r==KErrNone);
       
   746 	r=rbuf.CompareF(KTestData());
       
   747 	test(r==KErrNone);
       
   748 	file1.Close();
       
   749 
       
   750 	r=fs.MkDir(_L("C:\\mdir"));
       
   751 	test(r==KErrNone || r==KErrAlreadyExists);
       
   752 	
       
   753 	// Check the number of open file handles - 
       
   754 	// should be 1 (the one duplicated for the other process)
       
   755 	resCount = fs.ResourceCount();
       
   756 	test(resCount == 1);
       
   757 
       
   758 	fs.Close();
       
   759 
       
   760 	// Start the server thread
       
   761 	aProcess.Resume();
       
   762 
       
   763 
       
   764 
       
   765 	// connect to the server
       
   766 	RFileHandleSharer handsvr;
       
   767 	do
       
   768 		{
       
   769 		r=handsvr.Connect();
       
   770 		}
       
   771 	while(r==KErrNotFound);
       
   772 	test(r==KErrNone);
       
   773 
       
   774 
       
   775 	r=handsvr.SetTestDrive(drivenum);
       
   776 	test(r==KErrNone);
       
   777 
       
   778 	// wait for server to read the file
       
   779 	r = handsvr.PassFileHandleProcess();
       
   780 	test (r == KErrNone);
       
   781 	
       
   782 	handsvr.Close();
       
   783 	}
       
   784 
       
   785 
       
   786 
       
   787 GLDEF_C void CallTestsL(void)
       
   788     {
       
   789 	RFs::CharToDrive(gDriveToTest,drivenum);
       
   790 
       
   791 	// make sure the session path exists
       
   792 	RFs fs;
       
   793 	TInt r = fs.Connect();
       
   794 	test(r==KErrNone);
       
   795 
       
   796 	TFileName sessionp;
       
   797 	fs.SessionPath(sessionp);
       
   798 	r = fs.MkDirAll(sessionp);
       
   799 	test(r==KErrNone || r==KErrAlreadyExists);
       
   800 	fs.Close();
       
   801 
       
   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);
       
   806 
       
   807 	//create test server
       
   808 	RProcess p;
       
   809 	r = p.Create(_L("FHServer.exe"), KNullDesC);
       
   810 	test(r==KErrNone);
       
   811 
       
   812 	// RFile::Duplicate() tests
       
   813 	Duplicate();
       
   814 
       
   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...
       
   818 	PassFile(p);
       
   819 	
       
   820 	// Transfer the file handle to FHServer using RFile::TransferToServer(). 
       
   821 	// Get FHServer to transfer it to FHServer2
       
   822 	PassFile();
       
   823 
       
   824 	
       
   825 	// Get an open writeable file from FHServer2 via FHServer 
       
   826 	// using RFile::AdoptFromServer()
       
   827 	RequestFileWrite();
       
   828 
       
   829 	// Deprecated RFile::Adopt() test
       
   830 	RequestFileDeprecatedAdopt();
       
   831 
       
   832 	// negative test 
       
   833 	// Pass an invalid file handle to the server 
       
   834 	PassInvalidFile();
       
   835 
       
   836 
       
   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.
       
   840 	RequestFileTest();
       
   841 
       
   842 	// stop the servers
       
   843 	RFileHandleSharer handsvr;
       
   844 	r=handsvr.Connect();
       
   845 	test(r==KErrNone);
       
   846 	r = handsvr.Exit();
       
   847 	test(r == KErrNone);
       
   848 	handsvr.Close();
       
   849 	
       
   850 	// delete the test file
       
   851 	RFs cleanupfs;
       
   852 	r=cleanupfs.Connect();
       
   853 	test(r==KErrNone);
       
   854 	r=cleanupfs.SetSessionToPrivate(drivenum);
       
   855 	test(r==KErrNone);
       
   856 	r=cleanupfs.Delete(KCliFileName);
       
   857 	test(r==KErrNone || r==KErrNotFound);
       
   858 	cleanupfs.Close();
       
   859 
       
   860 
       
   861 	// wait for server process to end
       
   862 	TRequestStatus status;
       
   863 	p.Logon(status);
       
   864 	User::WaitForRequest(status);
       
   865 	test (status == KErrNone);
       
   866 
       
   867 	// cleanup
       
   868 	p.Close();
       
   869 
       
   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);
       
   874 
       
   875 	test(start_thc == end_thc);
       
   876 	test(start_phc == end_phc);
       
   877 	
       
   878 	// and also for pending requests ...
       
   879 	test(RThread().RequestCount() == 0);
       
   880 	
       
   881 	
       
   882 	RDebug::Print(_L("End Of Tests"));
       
   883 	}
       
   884 	
       
   885 
       
   886