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