os/kernelhwsrv/kerneltest/f32test/fileshare/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) 1999-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\loader\handshare.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32svr.h>
sl@0
    19
#include <e32test.h>
sl@0
    20
#include "handshare.h"
sl@0
    21
sl@0
    22
#ifdef __VC32__
sl@0
    23
#pragma warning(disable:4706)
sl@0
    24
#endif
sl@0
    25
sl@0
    26
sl@0
    27
sl@0
    28
GLDEF_D RTest test(_L("HANDSHARE_SVR"));
sl@0
    29
sl@0
    30
#define PANIC()		FHSvrPanic(__LINE__)
sl@0
    31
#define FHS_ASSERT(c)	((void)((c)||(PANIC(),0)))
sl@0
    32
sl@0
    33
const TTimeIntervalMicroSeconds32 KHalfSecond(500000);
sl@0
    34
sl@0
    35
const TInt KHeapSize=0x2000;
sl@0
    36
sl@0
    37
void FHSvrPanic(TInt aLine)
sl@0
    38
	{
sl@0
    39
	User::Panic(_L("FHServer"),aLine);
sl@0
    40
	}
sl@0
    41
sl@0
    42
LOCAL_D TInt gTestDrive;
sl@0
    43
sl@0
    44
/******************************************************************************
sl@0
    45
 * Class Definitions
sl@0
    46
 ******************************************************************************/
sl@0
    47
sl@0
    48
sl@0
    49
class CFHServer : public CServer2
sl@0
    50
	{
sl@0
    51
public:
sl@0
    52
	static CFHServer* NewL();
sl@0
    53
	void ConstructL();
sl@0
    54
	virtual ~CFHServer();
sl@0
    55
	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
sl@0
    56
	virtual TInt RunError(TInt aError);
sl@0
    57
private:
sl@0
    58
	CFHServer();
sl@0
    59
	};
sl@0
    60
sl@0
    61
class CFHSession : public CSession2
sl@0
    62
	{
sl@0
    63
public:
sl@0
    64
	virtual ~CFHSession();
sl@0
    65
	virtual void CreateL();
sl@0
    66
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
    67
public:
sl@0
    68
	
sl@0
    69
	void GetFileHandle(const RMessage2& aMsg);
sl@0
    70
	void GetFileHandle2(const RMessage2& aMsg);
sl@0
    71
	void PassFileHandle(const RMessage2& aMsg);
sl@0
    72
	void PassFileHandleProcess(const RMessage2& aMsg);
sl@0
    73
	void PassInvalidFileHandle(const RMessage2& aMsg);
sl@0
    74
	};
sl@0
    75
sl@0
    76
// a second server so we can test passing file handles from 
sl@0
    77
// client to server to server2
sl@0
    78
class CFHServer2 : public CServer2
sl@0
    79
	{
sl@0
    80
public:
sl@0
    81
	static CFHServer2* NewL();
sl@0
    82
	void ConstructL();
sl@0
    83
	virtual ~CFHServer2();
sl@0
    84
	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
sl@0
    85
	virtual TInt RunError(TInt aError);
sl@0
    86
private:
sl@0
    87
	CFHServer2();
sl@0
    88
public:
sl@0
    89
	};
sl@0
    90
sl@0
    91
sl@0
    92
class CFHSession2 : public CSession2
sl@0
    93
	{
sl@0
    94
public:
sl@0
    95
	virtual ~CFHSession2();
sl@0
    96
	virtual void CreateL();
sl@0
    97
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
    98
public:
sl@0
    99
	void PassFileHandle(const RMessage2& aMsg);
sl@0
   100
	void GetFileHandle(const RMessage2& aMsg);
sl@0
   101
	};
sl@0
   102
sl@0
   103
sl@0
   104
/******************************************************************************
sl@0
   105
 * Class CFHSession/CFHServer
sl@0
   106
 ******************************************************************************/
sl@0
   107
void ExceptionHandler(TExcType)
sl@0
   108
	{
sl@0
   109
	User::Leave(KErrGeneral);
sl@0
   110
	}
sl@0
   111
sl@0
   112
sl@0
   113
sl@0
   114
CFHSession::~CFHSession()
sl@0
   115
	{
sl@0
   116
	}
sl@0
   117
sl@0
   118
void CFHSession::CreateL()
sl@0
   119
	{
sl@0
   120
sl@0
   121
	}
sl@0
   122
sl@0
   123
void CFHSession::ServiceL(const RMessage2& aMessage)
sl@0
   124
	{
sl@0
   125
	__UHEAP_MARK;
sl@0
   126
	TInt mid=aMessage.Function();
sl@0
   127
	switch(mid)
sl@0
   128
		{
sl@0
   129
		case RFileHandleSharer::EMsgGetFileHandle:
sl@0
   130
			GetFileHandle(aMessage);
sl@0
   131
			break;
sl@0
   132
sl@0
   133
		case RFileHandleSharer::EMsgGetFileHandle2:
sl@0
   134
			GetFileHandle2(aMessage);
sl@0
   135
			break;
sl@0
   136
sl@0
   137
		case RFileHandleSharer::EMsgPassFileHandle:
sl@0
   138
			PassFileHandle(aMessage);
sl@0
   139
			break;
sl@0
   140
		
sl@0
   141
		case RFileHandleSharer::EMsgPassFileHandleProcess:
sl@0
   142
			PassFileHandleProcess(aMessage);
sl@0
   143
			break;
sl@0
   144
sl@0
   145
		case RFileHandleSharer::EMsgPassInvalidFileHandle:
sl@0
   146
			PassInvalidFileHandle(aMessage);
sl@0
   147
			break;
sl@0
   148
sl@0
   149
		case RFileHandleSharer::EMsgExit:
sl@0
   150
			{
sl@0
   151
			// stop server2
sl@0
   152
			RFileHandleSharer2 handsvr2;
sl@0
   153
			TInt r=handsvr2.Connect();
sl@0
   154
			test(r==KErrNone);
sl@0
   155
			r = handsvr2.Exit();
sl@0
   156
			test(r==KErrNone || r == KErrServerTerminated);
sl@0
   157
			handsvr2.Close();
sl@0
   158
sl@0
   159
			aMessage.Complete(KErrNone);	
sl@0
   160
sl@0
   161
			CActiveScheduler::Stop();
sl@0
   162
			}
sl@0
   163
			break;
sl@0
   164
sl@0
   165
		case RFileHandleSharer::EMsgSync:
sl@0
   166
			aMessage.Complete(KErrNone);	
sl@0
   167
			break;
sl@0
   168
sl@0
   169
		case RFileHandleSharer::EMsgDrive:
sl@0
   170
			gTestDrive=aMessage.Int0();
sl@0
   171
			aMessage.Complete(KErrNone);	
sl@0
   172
			break;
sl@0
   173
		default:
sl@0
   174
			break;
sl@0
   175
		}
sl@0
   176
	__UHEAP_MARKEND;
sl@0
   177
	}
sl@0
   178
sl@0
   179
sl@0
   180
//
sl@0
   181
// Returns session and relevant file handle to Client in read mode
sl@0
   182
// This is to allow the client to test the deprcated function RFile::Adopt()
sl@0
   183
void CFHSession::GetFileHandle(const RMessage2& aMsg)
sl@0
   184
	{
sl@0
   185
	test.Printf(_L("Get file handle"));
sl@0
   186
sl@0
   187
	// get the requested file mode
sl@0
   188
	TInt fileMode = aMsg.Int1();
sl@0
   189
sl@0
   190
	RFs fs;
sl@0
   191
	TInt r=fs.Connect();
sl@0
   192
	r=fs.CreatePrivatePath(gTestDrive);
sl@0
   193
	test(r==KErrNone);
sl@0
   194
	r=fs.SetSessionToPrivate(gTestDrive);
sl@0
   195
	test(r==KErrNone);
sl@0
   196
	r=fs.ShareProtected();
sl@0
   197
	test(r==KErrNone);
sl@0
   198
	RFile file1;
sl@0
   199
	r=file1.Create(fs,KSvrFileName,EFileWrite);
sl@0
   200
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   201
	if (r==KErrAlreadyExists)
sl@0
   202
		{
sl@0
   203
		r=file1.Open(fs,KSvrFileName, EFileWrite);
sl@0
   204
		test(r==KErrNone);
sl@0
   205
		}
sl@0
   206
	r=file1.Write(KTestData1());
sl@0
   207
	test(r==KErrNone);
sl@0
   208
	file1.Close();
sl@0
   209
sl@0
   210
	r=file1.Open(fs,KSvrFileName, fileMode);
sl@0
   211
	test(r==KErrNone);
sl@0
   212
sl@0
   213
	TInt fssh=file1.SubSessionHandle();
sl@0
   214
	r=aMsg.Write(0, TPckgC<TInt>(fssh));
sl@0
   215
	test(r==KErrNone);
sl@0
   216
	aMsg.Complete(fs);
sl@0
   217
	fs.Close();
sl@0
   218
	}
sl@0
   219
sl@0
   220
//
sl@0
   221
//	Returns a file handle from server2
sl@0
   222
//
sl@0
   223
void CFHSession::GetFileHandle2(const RMessage2& aMsg)
sl@0
   224
	{
sl@0
   225
	test.Next(_L("RFile::AdoptFromServer()"));
sl@0
   226
sl@0
   227
	// pass the request on to FHServer2 - this will create a file
sl@0
   228
	// which we can then adopt before returning it to the client
sl@0
   229
sl@0
   230
	// get the requested file mode
sl@0
   231
	TInt fileMode = aMsg.Int1();
sl@0
   232
sl@0
   233
	RFileHandleSharer2 handsvr2;
sl@0
   234
	TInt r = handsvr2.Connect();
sl@0
   235
	test(r==KErrNone);
sl@0
   236
sl@0
   237
	TInt ssh;
sl@0
   238
	TInt fsh = handsvr2.GetFileHandle(ssh, TFileMode(fileMode));
sl@0
   239
	test(fsh >= 0);
sl@0
   240
sl@0
   241
	// adopt the file from FHServer2
sl@0
   242
	RFile file;
sl@0
   243
	r=file.AdoptFromServer(fsh, ssh);
sl@0
   244
	test(r==KErrNone);
sl@0
   245
sl@0
   246
	test.Next(_L("RFile::TransferToClient()"));
sl@0
   247
sl@0
   248
	// transfer the file to the client
sl@0
   249
	r = file.TransferToClient(aMsg, 0);
sl@0
   250
	test(r==KErrNone);
sl@0
   251
sl@0
   252
	// test we can still use the file
sl@0
   253
	TInt pos = 0;
sl@0
   254
	r = file.Seek(ESeekStart, pos);
sl@0
   255
	test(r==KErrNone);
sl@0
   256
	TBuf8<100> rbuf;
sl@0
   257
	r=file.Read(0,rbuf);
sl@0
   258
	test(r==KErrNone);
sl@0
   259
	r=rbuf.CompareF(KTestData1());
sl@0
   260
	test(r==KErrNone);
sl@0
   261
sl@0
   262
	handsvr2.Close();
sl@0
   263
sl@0
   264
	file.Close();
sl@0
   265
sl@0
   266
	RDebug::Print(_L("completed"));	
sl@0
   267
	}
sl@0
   268
sl@0
   269
void CFHSession::PassFileHandle(const RMessage2& aMsg)
sl@0
   270
//
sl@0
   271
// Adopts file from test program and tests what it can and can't do
sl@0
   272
// Uses new AdoptFromClient() API
sl@0
   273
//	
sl@0
   274
	{
sl@0
   275
	test.Next(_L("RFile::AdoptFromClient()"));
sl@0
   276
sl@0
   277
	// connect to FHServer2
sl@0
   278
	RFileHandleSharer2 handsvr2;
sl@0
   279
	TInt r = handsvr2.Connect();
sl@0
   280
	test(r==KErrNone);
sl@0
   281
sl@0
   282
	RFile file;
sl@0
   283
sl@0
   284
	// Message slot 0 is a RFs handle
sl@0
   285
	// Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
sl@0
   286
	r = file.AdoptFromClient(aMsg, 0, 1);
sl@0
   287
	test(r==KErrNone);
sl@0
   288
sl@0
   289
sl@0
   290
sl@0
   291
	TBuf8<100> rbuf;
sl@0
   292
	r=file.Read(0,rbuf);
sl@0
   293
	test(r==KErrNone);
sl@0
   294
	r=rbuf.CompareF(KTestData());
sl@0
   295
	test(r==KErrNone);
sl@0
   296
	r=file.Write(KTestData1());
sl@0
   297
	test(r==KErrAccessDenied);
sl@0
   298
	r=file.ChangeMode(EFileWrite);
sl@0
   299
	test(r==KErrArgument);
sl@0
   300
	r=file.Rename(_L("\\newname.txt"));
sl@0
   301
	test(r==KErrPermissionDenied || r==KErrAccessDenied);
sl@0
   302
//	should try a delete
sl@0
   303
sl@0
   304
	// pass the file handle to FHServer2
sl@0
   305
	test.Next(_L("RFile::TransferToServer()"));
sl@0
   306
sl@0
   307
	TIpcArgs ipcArgs;
sl@0
   308
	file.TransferToServer(ipcArgs, 0, 1);
sl@0
   309
	r = handsvr2.PassFileHandle(ipcArgs);
sl@0
   310
	test(r==KErrNone);
sl@0
   311
sl@0
   312
	TInt pos = 0;
sl@0
   313
	r = file.Seek(ESeekStart, pos);
sl@0
   314
	test(r==KErrNone);
sl@0
   315
	r=file.Read(0,rbuf);
sl@0
   316
	test(r==KErrNone);
sl@0
   317
	r=rbuf.CompareF(KTestData());
sl@0
   318
	test(r==KErrNone);
sl@0
   319
	
sl@0
   320
	file.Close();
sl@0
   321
sl@0
   322
	handsvr2.Close();
sl@0
   323
sl@0
   324
	aMsg.Complete(KErrNone);
sl@0
   325
	}
sl@0
   326
sl@0
   327
//
sl@0
   328
// Adopts file from test program and tests what it can and can't do
sl@0
   329
// Uses new AdoptFromCreator() API
sl@0
   330
//	
sl@0
   331
void CFHSession::PassFileHandleProcess(const RMessage2& aMsg)
sl@0
   332
	{
sl@0
   333
	test.Next(_L("RFile::AdoptFromCreator()"));
sl@0
   334
sl@0
   335
	RFile file;
sl@0
   336
	TInt r = file.AdoptFromCreator(1, 2);
sl@0
   337
	test(r == KErrNone);
sl@0
   338
sl@0
   339
	TBuf8<100> rbuf;
sl@0
   340
	r=file.Read(0,rbuf);
sl@0
   341
	test(r==KErrNone);
sl@0
   342
	r=rbuf.CompareF(KTestData());
sl@0
   343
	test(r==KErrNone);
sl@0
   344
sl@0
   345
	test.Next(_L("RFile::Rename()"));
sl@0
   346
sl@0
   347
	// define a filename in our private path
sl@0
   348
	RFs fs;
sl@0
   349
	r=fs.Connect();
sl@0
   350
	test(r==KErrNone);
sl@0
   351
sl@0
   352
	TFileName sessionp;
sl@0
   353
	fs.SessionPath(sessionp);
sl@0
   354
	r = fs.MkDirAll(sessionp);
sl@0
   355
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   356
sl@0
   357
	r=fs.ShareProtected();
sl@0
   358
	test(r==KErrNone);
sl@0
   359
sl@0
   360
	r=fs.CreatePrivatePath(gTestDrive);
sl@0
   361
	test(r==KErrNone);
sl@0
   362
	r=fs.SetSessionToPrivate(gTestDrive);
sl@0
   363
	test(r==KErrNone);
sl@0
   364
sl@0
   365
	TPath newPath;
sl@0
   366
	fs.PrivatePath(newPath);
sl@0
   367
	TFileName newFileName;
sl@0
   368
	newFileName = newPath;
sl@0
   369
	newFileName.Append(_L("newname.txt"));
sl@0
   370
	
sl@0
   371
	// delete the file before we try to rename anything to it
sl@0
   372
	r = fs.Delete(newFileName);
sl@0
   373
	test(r == KErrNone || r == KErrNotFound);
sl@0
   374
sl@0
   375
	r=file.Rename(newFileName);
sl@0
   376
	test(r==KErrNone);
sl@0
   377
sl@0
   378
	file.Close();
sl@0
   379
sl@0
   380
	// Next verify that we can delete the file (which should now 
sl@0
   381
	// have been moved to our private directory)
sl@0
   382
	test.Next(_L("RFs::Delete()"));
sl@0
   383
	r = fs.Delete(newFileName);
sl@0
   384
	test(r == KErrNone);
sl@0
   385
sl@0
   386
	fs.Close();
sl@0
   387
sl@0
   388
	
sl@0
   389
	aMsg.Complete(KErrNone);
sl@0
   390
	}
sl@0
   391
sl@0
   392
void CFHSession::PassInvalidFileHandle(const RMessage2& aMsg)
sl@0
   393
//
sl@0
   394
// Attempts to adopt an invalid file handle from test program 
sl@0
   395
// and tests that KErrBadHandle is returned by AdoptFromClient()
sl@0
   396
//	
sl@0
   397
	{
sl@0
   398
	test.Next(_L("PassInvalidFileHandle - RFile::AdoptFromClient()"));
sl@0
   399
sl@0
   400
	RFile file;
sl@0
   401
sl@0
   402
	// Message slot 0 is a RFs handle
sl@0
   403
	// Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
sl@0
   404
	TInt r = file.AdoptFromClient(aMsg, 0, 1);
sl@0
   405
	test(r==KErrBadHandle);
sl@0
   406
sl@0
   407
sl@0
   408
	aMsg.Complete(r);
sl@0
   409
	}
sl@0
   410
sl@0
   411
CFHServer* CFHServer::NewL()
sl@0
   412
	{
sl@0
   413
	CFHServer* server = new (ELeave) CFHServer;
sl@0
   414
	CleanupStack::PushL(server);
sl@0
   415
	server->ConstructL();
sl@0
   416
	CleanupStack::Pop(server);
sl@0
   417
	return server;
sl@0
   418
	}
sl@0
   419
sl@0
   420
void CFHServer::ConstructL()
sl@0
   421
	{
sl@0
   422
	}
sl@0
   423
sl@0
   424
CFHServer::CFHServer()
sl@0
   425
	: CServer2(0,ESharableSessions)
sl@0
   426
	{
sl@0
   427
	}
sl@0
   428
sl@0
   429
CFHServer::~CFHServer()
sl@0
   430
	{
sl@0
   431
	}
sl@0
   432
sl@0
   433
CSession2* CFHServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
sl@0
   434
//
sl@0
   435
//	 Create New Session
sl@0
   436
//
sl@0
   437
	{
sl@0
   438
	(void)aVersion;
sl@0
   439
	CFHSession* s = new (ELeave) CFHSession;
sl@0
   440
	return s;
sl@0
   441
	}
sl@0
   442
sl@0
   443
_LIT(KErr,"FHSERVER_ERR");
sl@0
   444
CFHServer2* CFHServer2::NewL()
sl@0
   445
	{
sl@0
   446
	CFHServer2* server = new (ELeave) CFHServer2;
sl@0
   447
	CleanupStack::PushL(server);
sl@0
   448
	server->ConstructL();
sl@0
   449
	CleanupStack::Pop(server);
sl@0
   450
	return server;
sl@0
   451
	}
sl@0
   452
sl@0
   453
void CFHServer2::ConstructL()
sl@0
   454
	{
sl@0
   455
	}
sl@0
   456
sl@0
   457
TInt CFHServer::RunError(TInt aError)
sl@0
   458
	{
sl@0
   459
	User::Panic(KErr,aError);
sl@0
   460
	return 0;
sl@0
   461
	}
sl@0
   462
sl@0
   463
// File handle server #2
sl@0
   464
CFHServer2::CFHServer2()
sl@0
   465
	: CServer2(0,ESharableSessions)
sl@0
   466
	{
sl@0
   467
	}
sl@0
   468
sl@0
   469
CFHServer2::~CFHServer2()
sl@0
   470
	{
sl@0
   471
	}
sl@0
   472
sl@0
   473
CSession2* CFHServer2::NewSessionL(const TVersion& aVersion, const RMessage2&) const
sl@0
   474
//
sl@0
   475
//	 Create New Session
sl@0
   476
//
sl@0
   477
	{
sl@0
   478
	(void)aVersion;
sl@0
   479
	CFHSession2* s = new (ELeave) CFHSession2;
sl@0
   480
	return s;
sl@0
   481
	}
sl@0
   482
sl@0
   483
_LIT(KErr2,"FHSERVER2_ERR");
sl@0
   484
TInt CFHServer2::RunError(TInt aError)
sl@0
   485
	{
sl@0
   486
	User::Panic(KErr2,aError);
sl@0
   487
	return 0;
sl@0
   488
	}
sl@0
   489
sl@0
   490
CFHSession2::~CFHSession2()
sl@0
   491
	{
sl@0
   492
	}
sl@0
   493
sl@0
   494
void CFHSession2::CreateL()
sl@0
   495
	{
sl@0
   496
sl@0
   497
	}
sl@0
   498
sl@0
   499
void CFHSession2::ServiceL(const RMessage2& aMessage)
sl@0
   500
	{
sl@0
   501
	__UHEAP_MARK;
sl@0
   502
	TInt mid=aMessage.Function();
sl@0
   503
	switch(mid)
sl@0
   504
		{
sl@0
   505
		case RFileHandleSharer::EMsgPassFileHandle:
sl@0
   506
			PassFileHandle(aMessage);
sl@0
   507
			break;
sl@0
   508
sl@0
   509
		case RFileHandleSharer::EMsgGetFileHandle:
sl@0
   510
			GetFileHandle(aMessage);
sl@0
   511
			break;
sl@0
   512
sl@0
   513
		case RFileHandleSharer::EMsgExit:
sl@0
   514
			aMessage.Complete(KErrNone);	
sl@0
   515
			CActiveScheduler::Stop();
sl@0
   516
			break;
sl@0
   517
sl@0
   518
		default:
sl@0
   519
			break;
sl@0
   520
		}
sl@0
   521
	__UHEAP_MARKEND;
sl@0
   522
	}
sl@0
   523
sl@0
   524
sl@0
   525
//
sl@0
   526
// Adopts file from server 1
sl@0
   527
//	
sl@0
   528
void CFHSession2::PassFileHandle(const RMessage2& aMsg)
sl@0
   529
	{
sl@0
   530
	RFile file;
sl@0
   531
sl@0
   532
	// Message slot 0 is a RFs handle
sl@0
   533
	// Message slot 1 is a RFile Subsession handle (RFile::SubSessionHandle())
sl@0
   534
	TInt r = file.AdoptFromClient(aMsg, 0, 1);
sl@0
   535
	if (r != KErrNone)
sl@0
   536
		{
sl@0
   537
		aMsg.Complete(r);
sl@0
   538
		return;
sl@0
   539
		}
sl@0
   540
sl@0
   541
sl@0
   542
	TBuf8<100> rbuf;
sl@0
   543
	
sl@0
   544
	if (r == KErrNone)
sl@0
   545
		r=file.Read(0,rbuf);
sl@0
   546
sl@0
   547
	if (r == KErrNone)
sl@0
   548
		r = rbuf.CompareF(KTestData());
sl@0
   549
sl@0
   550
	if (r == KErrNone)
sl@0
   551
		{
sl@0
   552
		r = file.Write(KTestData1());
sl@0
   553
		if (r == KErrAccessDenied)
sl@0
   554
			r = KErrNone;
sl@0
   555
		}
sl@0
   556
sl@0
   557
	if (r == KErrNone)
sl@0
   558
		{
sl@0
   559
		r = file.ChangeMode(EFileWrite);
sl@0
   560
		if (r == KErrArgument)
sl@0
   561
			r = KErrNone;
sl@0
   562
		}
sl@0
   563
sl@0
   564
	if (r == KErrNone)
sl@0
   565
		{
sl@0
   566
		r = file.Rename(_L("\\newname.txt"));
sl@0
   567
		if (r == KErrPermissionDenied || r == KErrAccessDenied)
sl@0
   568
			r = KErrNone;
sl@0
   569
		}
sl@0
   570
sl@0
   571
	file.Close();
sl@0
   572
sl@0
   573
	aMsg.Complete(r);
sl@0
   574
	}
sl@0
   575
sl@0
   576
sl@0
   577
void CFHSession2::GetFileHandle(const RMessage2& aMsg)
sl@0
   578
//
sl@0
   579
//	Returns a file handle in write mode
sl@0
   580
//
sl@0
   581
	{
sl@0
   582
	RFs fs;
sl@0
   583
	TInt r = fs.Connect();
sl@0
   584
sl@0
   585
	if (r == KErrNone)
sl@0
   586
		r = fs.CreatePrivatePath(gTestDrive);
sl@0
   587
sl@0
   588
	if (r == KErrNone)
sl@0
   589
		r = fs.SetSessionToPrivate(gTestDrive);
sl@0
   590
sl@0
   591
	if (r == KErrNone)
sl@0
   592
		r = fs.ShareProtected();
sl@0
   593
sl@0
   594
	// make sure file exists & has valid data in it
sl@0
   595
	RFile file1;
sl@0
   596
	if (r == KErrNone)
sl@0
   597
		r = file1.Replace(fs,KSvrFileName,EFileWrite);
sl@0
   598
sl@0
   599
	if (r == KErrNone)
sl@0
   600
		r = file1.Write(KTestData1());
sl@0
   601
		
sl@0
   602
	file1.Close();
sl@0
   603
sl@0
   604
sl@0
   605
	// re-open the file with the mode the client has requested & pass it to the client
sl@0
   606
	TFileMode fileMode = TFileMode(aMsg.Int1());
sl@0
   607
	if (r == KErrNone)
sl@0
   608
		r = file1.Open(fs,KSvrFileName, fileMode);
sl@0
   609
	if (r == KErrNone)
sl@0
   610
		r = file1.TransferToClient(aMsg, 0);
sl@0
   611
	file1.Close();
sl@0
   612
	
sl@0
   613
	fs.Close();
sl@0
   614
sl@0
   615
	if (r != KErrNone)
sl@0
   616
		aMsg.Complete(r);
sl@0
   617
	}
sl@0
   618
sl@0
   619
LOCAL_C TInt FHServer2(TAny * /*anArg*/)
sl@0
   620
	{	
sl@0
   621
	RTest test(_L("FHServer2"));
sl@0
   622
sl@0
   623
	// Remember the number of open handles. Just for a sanity check ....
sl@0
   624
	TInt start_thc, start_phc;
sl@0
   625
	RThread().HandleCount(start_phc, start_thc);
sl@0
   626
sl@0
   627
sl@0
   628
	CTrapCleanup* cleanup;
sl@0
   629
	cleanup=CTrapCleanup::New();
sl@0
   630
sl@0
   631
	CActiveScheduler* sched=new CActiveScheduler;
sl@0
   632
	FHS_ASSERT(sched);
sl@0
   633
	CActiveScheduler::Install(sched);
sl@0
   634
sl@0
   635
	CFHServer2* svr2 = NULL;
sl@0
   636
	TRAP_IGNORE(svr2 = CFHServer2::NewL());
sl@0
   637
	FHS_ASSERT(svr2);
sl@0
   638
	FHS_ASSERT(svr2->Start(_L("FHServer2"))==KErrNone);
sl@0
   639
sl@0
   640
	CActiveScheduler::Start();
sl@0
   641
	
sl@0
   642
	delete svr2;
sl@0
   643
	delete sched;
sl@0
   644
sl@0
   645
	delete cleanup;
sl@0
   646
sl@0
   647
	// Sanity check for open handles
sl@0
   648
	TInt end_thc, end_phc;
sl@0
   649
	RThread().HandleCount(end_phc, end_thc);
sl@0
   650
	test(start_thc == end_thc);
sl@0
   651
	test(start_phc == end_phc);
sl@0
   652
	// and also for pending requests ...
sl@0
   653
	test(RThread().RequestCount() == 0);
sl@0
   654
sl@0
   655
	
sl@0
   656
	return KErrNone;
sl@0
   657
	}
sl@0
   658
sl@0
   659
sl@0
   660
sl@0
   661
GLDEF_C TInt E32Main()
sl@0
   662
//
sl@0
   663
// Test Server for file handle sharing
sl@0
   664
//
sl@0
   665
	{
sl@0
   666
	test.Title();
sl@0
   667
	test.Start(_L("Starting FHServer..."));
sl@0
   668
sl@0
   669
	// Remember the number of open handles. Just for a sanity check ....
sl@0
   670
	TInt start_thc, start_phc;
sl@0
   671
	RThread().HandleCount(start_phc, start_thc);
sl@0
   672
sl@0
   673
	CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   674
sl@0
   675
	FHS_ASSERT(cleanup);
sl@0
   676
	CActiveScheduler* sched=new CActiveScheduler;
sl@0
   677
	FHS_ASSERT(sched);
sl@0
   678
	CActiveScheduler::Install(sched);
sl@0
   679
sl@0
   680
	// start server1
sl@0
   681
	CFHServer* svr = NULL;
sl@0
   682
	TRAP_IGNORE(svr = CFHServer::NewL());
sl@0
   683
	FHS_ASSERT(svr);
sl@0
   684
	FHS_ASSERT(svr->Start(_L("FHServer"))==KErrNone);
sl@0
   685
sl@0
   686
	test.Title();
sl@0
   687
	test.Start(_L("Starting tests..."));
sl@0
   688
sl@0
   689
	// start server2 in a seperate thread
sl@0
   690
	RThread server2Thread;
sl@0
   691
	TInt r = server2Thread.Create(_L("FHServer2"), FHServer2, KDefaultStackSize, KHeapSize, KHeapSize, NULL);	
sl@0
   692
	test(r==KErrNone);	
sl@0
   693
	TRequestStatus statq;
sl@0
   694
	server2Thread.Logon(statq);
sl@0
   695
	server2Thread.Resume();
sl@0
   696
sl@0
   697
	CActiveScheduler::Start();
sl@0
   698
sl@0
   699
sl@0
   700
	// wait for server2's thread to end gracefully
sl@0
   701
	User::WaitForRequest(statq);
sl@0
   702
sl@0
   703
sl@0
   704
	server2Thread.Close();
sl@0
   705
sl@0
   706
	RFs cleanupfs;
sl@0
   707
	r = cleanupfs.Connect();
sl@0
   708
	test(r==KErrNone);
sl@0
   709
	r=cleanupfs.SetSessionToPrivate(gTestDrive);
sl@0
   710
	test(r==KErrNone);
sl@0
   711
	r=cleanupfs.Delete(KSvrFileName);
sl@0
   712
	test(r==KErrNone || r==KErrNotFound);
sl@0
   713
	cleanupfs.Close();
sl@0
   714
sl@0
   715
sl@0
   716
	test.End();
sl@0
   717
sl@0
   718
	delete svr;
sl@0
   719
	delete sched;
sl@0
   720
	delete cleanup;
sl@0
   721
sl@0
   722
	// Sanity check for open handles and pending requests
sl@0
   723
	TInt end_thc, end_phc;
sl@0
   724
	RThread().HandleCount(end_phc, end_thc);
sl@0
   725
	test(start_thc == end_thc);
sl@0
   726
	test(start_phc == end_phc);
sl@0
   727
	test(RThread().RequestCount() == 0);
sl@0
   728
	
sl@0
   729
	return 0;
sl@0
   730
	}