os/kernelhwsrv/kerneltest/e32test/secure/t_sserver.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) 2001-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
// e32test\secure\t_sserver.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test the security aspects of server connect by handle.
sl@0
    17
// API Information:
sl@0
    18
// CSession2, CServer2
sl@0
    19
// Details:
sl@0
    20
// - Test asynchronous server connect in various ways. Verify results
sl@0
    21
// are as expected.
sl@0
    22
// - Attempt to create a protected server without and without 
sl@0
    23
// KCapabilityProtServ. Verify results are as expected.
sl@0
    24
// - Attempt to connect to a serve with different Secure IDs, Vendor IDs
sl@0
    25
// and capabilities. Verify results are as expected.
sl@0
    26
// - Test creating a sub-session, sending it messages and closing it.
sl@0
    27
// Verify the results.
sl@0
    28
// - Test TIpcArgs::Set and IPC argument passing. Verify results are
sl@0
    29
// as expected.
sl@0
    30
// - Test TIpcArgs:: Also checks the integrity of the server and Kernel if client passes bad descriptor
sl@0
    31
// for IPC transfer and the integrity of the client and Kernel if the server does the same.
sl@0
    32
// - Test CServer2::RunError and CSession2::ServiceError and session
sl@0
    33
// resource counting.
sl@0
    34
// Platforms/Drives/Compatibility:
sl@0
    35
// All.
sl@0
    36
// Assumptions/Requirement/Pre-requisites:
sl@0
    37
// Failures and causes:
sl@0
    38
// Base Port information:
sl@0
    39
// 
sl@0
    40
//
sl@0
    41
sl@0
    42
#include <e32test.h>
sl@0
    43
#include "../misc/prbs.h"
sl@0
    44
sl@0
    45
LOCAL_D RTest test(_L("T_SSERVER"));
sl@0
    46
sl@0
    47
_LIT_SECURE_ID(KTestSecureId,0x101f534d);
sl@0
    48
_LIT_SECURE_ID(KTestSecureId2,0x101f534e);
sl@0
    49
_LIT_VENDOR_ID(KTestVendorId,0x01234567);
sl@0
    50
_LIT_VENDOR_ID(KTestVendorId2,0x01234568);
sl@0
    51
const TInt KTestRunErrorModifier = 111;
sl@0
    52
const TInt KTestServiceErrorModifier = 222;
sl@0
    53
_LIT(KProtectedServerName,"!T_SSERVER-protected-server");
sl@0
    54
sl@0
    55
sl@0
    56
sl@0
    57
enum TTestProcessFunctions
sl@0
    58
	{
sl@0
    59
	ETestProcessServer,
sl@0
    60
	ETestProcessCreateProtectedServer,
sl@0
    61
	};
sl@0
    62
sl@0
    63
#include "testprocess.h"
sl@0
    64
sl@0
    65
sl@0
    66
sl@0
    67
//
sl@0
    68
// RTestThread
sl@0
    69
//
sl@0
    70
sl@0
    71
class RTestThread : public RThread
sl@0
    72
	{
sl@0
    73
public:
sl@0
    74
	void Create(TThreadFunction aFunction,TAny* aArg=0);
sl@0
    75
	};
sl@0
    76
sl@0
    77
void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
sl@0
    78
	{
sl@0
    79
	TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
sl@0
    80
	test(r==KErrNone);
sl@0
    81
	}
sl@0
    82
sl@0
    83
sl@0
    84
sl@0
    85
//
sl@0
    86
// CTestSession
sl@0
    87
//
sl@0
    88
sl@0
    89
enum TTestSessionFuntions
sl@0
    90
	{
sl@0
    91
	ETestShutdown,EPing,
sl@0
    92
	ETestArgUnspecified,ETestArgDesC8,ETestArgDesC16,ETestArgDes8,ETestArgDes16,
sl@0
    93
	ETestResourceCountPass=0x01234567,ETestResourceCountFail,ETestServiceLeave,
sl@0
    94
	ETestCreateSubSession,ETestCloseSubSession,ETestCloseSubSessionHandle,
sl@0
    95
	ETestEchoArgs,ETestEmptySubSessionMessage, ETestBadClientDescRead, ETestBadClientDescWrite,
sl@0
    96
	ETestBadServerDescRead, ETestBadServerDescWrite
sl@0
    97
	};
sl@0
    98
sl@0
    99
class CTestSession : public CSession2
sl@0
   100
	{
sl@0
   101
public:
sl@0
   102
	CTestSession();
sl@0
   103
	virtual void ServiceL(const RMessage2& aMessage);
sl@0
   104
	virtual void ServiceError(const RMessage2& aMessage,TInt aError);
sl@0
   105
private:
sl@0
   106
	TInt CountResources();
sl@0
   107
private:
sl@0
   108
	TInt iResourceCount;
sl@0
   109
	TInt iSubSessionCloseHandle;
sl@0
   110
	RMessage2 iMessage;
sl@0
   111
	};
sl@0
   112
sl@0
   113
class CTestSecureServer : public CServer2
sl@0
   114
	{
sl@0
   115
public:
sl@0
   116
	CTestSecureServer(TInt aPriority);
sl@0
   117
	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
sl@0
   118
private:
sl@0
   119
	void CustomL(const RMessage2& aMessage);
sl@0
   120
	void QueryL(const RMessage2& aMessage,HBufC8*& aQueryParams);
sl@0
   121
	TInt RunError(TInt aError);
sl@0
   122
private:
sl@0
   123
	friend class CTestSession;
sl@0
   124
	};
sl@0
   125
sl@0
   126
CTestSession::CTestSession()
sl@0
   127
	: CSession2()
sl@0
   128
	{}
sl@0
   129
sl@0
   130
void CTestSession::ServiceL(const RMessage2& aMessage)
sl@0
   131
	{
sl@0
   132
	TInt r = KErrNotSupported;
sl@0
   133
	TBuf8<100> buf8;
sl@0
   134
	buf8.Append(_L8("12345"));
sl@0
   135
	TBuf16<100> buf16;
sl@0
   136
	buf16.Append(_L16("12345"));
sl@0
   137
	TInt testArg = ((TUint)aMessage.Function())>>28;
sl@0
   138
sl@0
   139
	TInt badDescriptorError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)&&PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)
sl@0
   140
		? KErrBadDescriptor : KErrNone;
sl@0
   141
sl@0
   142
	switch(aMessage.Function()&0x0fffffff)
sl@0
   143
		{
sl@0
   144
		case ETestShutdown:
sl@0
   145
			CActiveScheduler::Stop();
sl@0
   146
			return;
sl@0
   147
sl@0
   148
		case EPing:
sl@0
   149
			r=aMessage.Int0();
sl@0
   150
			break;
sl@0
   151
sl@0
   152
		case ETestArgUnspecified:
sl@0
   153
			r = aMessage.GetDesLength(testArg);
sl@0
   154
			if(r!=KErrBadDescriptor)
sl@0
   155
				goto fail;
sl@0
   156
			r = aMessage.GetDesMaxLength(testArg);
sl@0
   157
			if(r!=KErrBadDescriptor)
sl@0
   158
				goto fail;
sl@0
   159
			r = aMessage.Read(testArg,buf8);
sl@0
   160
			if(r!=badDescriptorError)
sl@0
   161
				goto fail;
sl@0
   162
			TRAP(r,aMessage.ReadL(testArg,buf8));
sl@0
   163
			if(r!=badDescriptorError)
sl@0
   164
				goto fail;
sl@0
   165
			r = aMessage.Read(testArg,buf16);
sl@0
   166
			if(r!=badDescriptorError)
sl@0
   167
				goto fail;
sl@0
   168
			TRAP(r,aMessage.ReadL(testArg,buf16));
sl@0
   169
			if(r!=badDescriptorError)
sl@0
   170
				goto fail;
sl@0
   171
			r = aMessage.Write(testArg,buf8);
sl@0
   172
			if(r!=badDescriptorError)
sl@0
   173
				goto fail;
sl@0
   174
			TRAP(r,aMessage.WriteL(testArg,buf8));
sl@0
   175
			if(r!=badDescriptorError)
sl@0
   176
				goto fail;
sl@0
   177
			r = aMessage.Write(testArg,buf16);
sl@0
   178
			if(r!=badDescriptorError)
sl@0
   179
				goto fail;
sl@0
   180
			TRAP(r,aMessage.WriteL(testArg,buf16));
sl@0
   181
			if(r!=badDescriptorError)
sl@0
   182
				goto fail;
sl@0
   183
			goto pass;
sl@0
   184
sl@0
   185
		case ETestArgDesC8:
sl@0
   186
			r = aMessage.GetDesLength(testArg);
sl@0
   187
			if(r<0)
sl@0
   188
				goto fail;
sl@0
   189
			r = aMessage.GetDesMaxLength(testArg);
sl@0
   190
			if(r<0)
sl@0
   191
				goto fail;
sl@0
   192
			r = aMessage.Read(testArg,buf8);
sl@0
   193
			if(r!=KErrNone)
sl@0
   194
				goto fail;
sl@0
   195
			TRAP(r,aMessage.ReadL(testArg,buf8));
sl@0
   196
			if(r!=KErrNone)
sl@0
   197
				goto fail;
sl@0
   198
			r = aMessage.Read(testArg,buf16);
sl@0
   199
			if(r!=badDescriptorError)
sl@0
   200
				goto fail;
sl@0
   201
			TRAP(r,aMessage.ReadL(testArg,buf16));
sl@0
   202
			if(r!=badDescriptorError)
sl@0
   203
				goto fail;
sl@0
   204
			r = aMessage.Write(testArg,buf8);
sl@0
   205
			if(r!=badDescriptorError)
sl@0
   206
				goto fail;
sl@0
   207
			TRAP(r,aMessage.WriteL(testArg,buf8));
sl@0
   208
			if(r!=badDescriptorError)
sl@0
   209
				goto fail;
sl@0
   210
			r = aMessage.Write(testArg,buf16);
sl@0
   211
			if(r!=badDescriptorError)
sl@0
   212
				goto fail;
sl@0
   213
			TRAP(r,aMessage.WriteL(testArg,buf16));
sl@0
   214
			if(r!=badDescriptorError)
sl@0
   215
				goto fail;
sl@0
   216
			goto pass;
sl@0
   217
sl@0
   218
		case ETestArgDesC16:
sl@0
   219
			r = aMessage.GetDesLength(testArg);
sl@0
   220
			if(r<0)
sl@0
   221
				goto fail;
sl@0
   222
			r = aMessage.GetDesMaxLength(testArg);
sl@0
   223
			if(r<0)
sl@0
   224
				goto fail;
sl@0
   225
			r = aMessage.Read(testArg,buf8);
sl@0
   226
			if(r!=badDescriptorError)
sl@0
   227
				goto fail;
sl@0
   228
			TRAP(r,aMessage.ReadL(testArg,buf8));
sl@0
   229
			if(r!=badDescriptorError)
sl@0
   230
				goto fail;
sl@0
   231
			r = aMessage.Read(testArg,buf16);
sl@0
   232
			if(r!=KErrNone)
sl@0
   233
				goto fail;
sl@0
   234
			TRAP(r,aMessage.ReadL(testArg,buf16));
sl@0
   235
			if(r!=KErrNone)
sl@0
   236
				goto fail;
sl@0
   237
			r = aMessage.Write(testArg,buf8);
sl@0
   238
			if(r!=badDescriptorError)
sl@0
   239
				goto fail;
sl@0
   240
			TRAP(r,aMessage.WriteL(testArg,buf8));
sl@0
   241
			if(r!=badDescriptorError)
sl@0
   242
				goto fail;
sl@0
   243
			r = aMessage.Write(testArg,buf16);
sl@0
   244
			if(r!=badDescriptorError)
sl@0
   245
				goto fail;
sl@0
   246
			TRAP(r,aMessage.WriteL(testArg,buf16));
sl@0
   247
			if(r!=badDescriptorError)
sl@0
   248
				goto fail;
sl@0
   249
			goto pass;
sl@0
   250
sl@0
   251
		case ETestArgDes8:
sl@0
   252
			r = aMessage.GetDesLength(testArg);
sl@0
   253
			if(r<0)
sl@0
   254
				goto fail;
sl@0
   255
			r = aMessage.GetDesMaxLength(testArg);
sl@0
   256
			if(r<0)
sl@0
   257
				goto fail;
sl@0
   258
			r = aMessage.Read(testArg,buf8);
sl@0
   259
			if(r!=KErrNone)
sl@0
   260
				goto fail;
sl@0
   261
			TRAP(r,aMessage.ReadL(testArg,buf8));
sl@0
   262
			if(r!=KErrNone)
sl@0
   263
				goto fail;
sl@0
   264
			r = aMessage.Read(testArg,buf16);
sl@0
   265
			if(r!=badDescriptorError)
sl@0
   266
				goto fail;
sl@0
   267
			TRAP(r,aMessage.ReadL(testArg,buf16));
sl@0
   268
			if(r!=badDescriptorError)
sl@0
   269
				goto fail;
sl@0
   270
			r = aMessage.Write(testArg,buf8);
sl@0
   271
			if(r!=KErrNone)
sl@0
   272
				goto fail;
sl@0
   273
			TRAP(r,aMessage.WriteL(testArg,buf8));
sl@0
   274
			if(r!=KErrNone)
sl@0
   275
				goto fail;
sl@0
   276
			r = aMessage.Write(testArg,buf16);
sl@0
   277
			if(r!=badDescriptorError)
sl@0
   278
				goto fail;
sl@0
   279
			TRAP(r,aMessage.WriteL(testArg,buf16));
sl@0
   280
			if(r!=badDescriptorError)
sl@0
   281
				goto fail;
sl@0
   282
			goto pass;
sl@0
   283
sl@0
   284
		case ETestArgDes16:
sl@0
   285
			r = aMessage.GetDesLength(testArg);
sl@0
   286
			if(r<0)
sl@0
   287
				goto fail;
sl@0
   288
			r = aMessage.GetDesMaxLength(testArg);
sl@0
   289
			if(r<0)
sl@0
   290
				goto fail;
sl@0
   291
			r = aMessage.Read(testArg,buf8);
sl@0
   292
			if(r!=badDescriptorError)
sl@0
   293
				goto fail;
sl@0
   294
			TRAP(r,aMessage.ReadL(testArg,buf8));
sl@0
   295
			if(r!=badDescriptorError)
sl@0
   296
				goto fail;
sl@0
   297
			r = aMessage.Read(testArg,buf16);
sl@0
   298
			if(r!=KErrNone)
sl@0
   299
				goto fail;
sl@0
   300
			TRAP(r,aMessage.ReadL(testArg,buf16));
sl@0
   301
			if(r!=KErrNone)
sl@0
   302
				goto fail;
sl@0
   303
			r = aMessage.Write(testArg,buf8);
sl@0
   304
			if(r!=badDescriptorError)
sl@0
   305
				goto fail;
sl@0
   306
			TRAP(r,aMessage.WriteL(testArg,buf8));
sl@0
   307
			if(r!=badDescriptorError)
sl@0
   308
				goto fail;
sl@0
   309
			r = aMessage.Write(testArg,buf16);
sl@0
   310
			if(r!=KErrNone)
sl@0
   311
				goto fail;
sl@0
   312
			TRAP(r,aMessage.WriteL(testArg,buf16));
sl@0
   313
			if(r!=KErrNone)
sl@0
   314
				goto fail;
sl@0
   315
			goto pass;
sl@0
   316
		
sl@0
   317
		case ETestBadClientDescRead:
sl@0
   318
			//Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
sl@0
   319
			{
sl@0
   320
			r = aMessage.GetDesLength(testArg);
sl@0
   321
			TUint8* buff;
sl@0
   322
			buff = (TUint8*)User::Alloc(r);
sl@0
   323
			if (!buff)
sl@0
   324
				{
sl@0
   325
				r=KErrNoMemory;
sl@0
   326
				break;
sl@0
   327
				}
sl@0
   328
			TPtr8 ptr8(buff, r, r);
sl@0
   329
			r=aMessage.Read(testArg,ptr8);
sl@0
   330
			User::Free(buff);
sl@0
   331
			}
sl@0
   332
			break;
sl@0
   333
sl@0
   334
		case ETestBadClientDescWrite:
sl@0
   335
			//Testing the integrity of the server and Kernel if the client passes bad descriptor for IPC transfer
sl@0
   336
			{
sl@0
   337
			r = aMessage.GetDesLength(testArg);
sl@0
   338
			TUint8* buff;
sl@0
   339
			buff = (TUint8*)User::Alloc(r);
sl@0
   340
			if (!buff)
sl@0
   341
				{
sl@0
   342
				r=KErrNoMemory;
sl@0
   343
				break;
sl@0
   344
				}
sl@0
   345
			TPtr8 ptr8(buff, r, r);
sl@0
   346
			r=aMessage.Write(testArg,ptr8);
sl@0
   347
			User::Free(buff);
sl@0
   348
			}
sl@0
   349
			break;
sl@0
   350
sl@0
   351
		case ETestBadServerDescRead:
sl@0
   352
		case ETestBadServerDescWrite:
sl@0
   353
			//Testing the integrity of the client and Kernel if server passes bad descriptor for IPC transfer
sl@0
   354
			{
sl@0
   355
			//Create a chunk with a hole between addresses 0x1000 and 0x2000
sl@0
   356
			RChunk c;
sl@0
   357
			r=c.CreateDisconnectedLocal(0,0,0x200000);
sl@0
   358
			test(r==KErrNone);
sl@0
   359
			r=c.Commit(0,0x1000);
sl@0
   360
			test(r==KErrNone);
sl@0
   361
			r=c.Commit(0x2000,0x1000);
sl@0
   362
			test(r==KErrNone);
sl@0
   363
			
sl@0
   364
			TInt base,len;
sl@0
   365
			switch(aMessage.Function()>>28)
sl@0
   366
				{ 
sl@0
   367
				case 0:base=0x1000;len=0x500;break;
sl@0
   368
				case 1:base=0x1001;len=0x500;break;
sl@0
   369
				case 2:base=0x1007;len=0x500;break;
sl@0
   370
				case 3:base=0x1ff0;len=0x100;break;
sl@0
   371
				case 4:base=0x1ff1;len=0x100;break;
sl@0
   372
				case 5:base=0x1ff2;len=0x100;break;
sl@0
   373
				case 6:base=0xff3;len=0x100;break;	
sl@0
   374
				default:base=0xfff;len=0x100;break;
sl@0
   375
				}
sl@0
   376
sl@0
   377
			TPtr8 ptr (c.Base()+base,len,len);
sl@0
   378
			if ((aMessage.Function()&0x0fffffff) == ETestBadServerDescRead)
sl@0
   379
				aMessage.Read(0,ptr);	//The server should panic here
sl@0
   380
			else
sl@0
   381
				aMessage.Write(0,ptr);	//The server should panic here
sl@0
   382
			}
sl@0
   383
			break;
sl@0
   384
sl@0
   385
		case ETestResourceCountPass:
sl@0
   386
			r=aMessage.Function();
sl@0
   387
			ResourceCountMarkStart();
sl@0
   388
			ResourceCountMarkEnd(aMessage);
sl@0
   389
			break;
sl@0
   390
sl@0
   391
		case ETestResourceCountFail:
sl@0
   392
			r=aMessage.Function();
sl@0
   393
			ResourceCountMarkStart();
sl@0
   394
			++iResourceCount;
sl@0
   395
			ResourceCountMarkEnd(aMessage);
sl@0
   396
			break;
sl@0
   397
sl@0
   398
		case ETestServiceLeave:
sl@0
   399
			iMessage = aMessage;
sl@0
   400
			User::Leave(aMessage.Int0());
sl@0
   401
			break;
sl@0
   402
sl@0
   403
		case ETestCreateSubSession:
sl@0
   404
			{
sl@0
   405
			TInt reply = aMessage.Int0();
sl@0
   406
			r = aMessage.Write(3,TPtrC8((TUint8*)&reply,sizeof(reply)));
sl@0
   407
			}
sl@0
   408
			break;
sl@0
   409
sl@0
   410
		case ETestCloseSubSession:
sl@0
   411
			iSubSessionCloseHandle = aMessage.Int3();
sl@0
   412
			r = KErrNone;
sl@0
   413
			break;
sl@0
   414
sl@0
   415
		case ETestCloseSubSessionHandle:
sl@0
   416
			r = iSubSessionCloseHandle;
sl@0
   417
			iSubSessionCloseHandle = 0;
sl@0
   418
			break;
sl@0
   419
sl@0
   420
		case ETestEchoArgs:
sl@0
   421
			{
sl@0
   422
			TInt reply[4];
sl@0
   423
			reply[0] = aMessage.Int0();
sl@0
   424
			reply[1] = aMessage.Int1();
sl@0
   425
			reply[2] = aMessage.Int2();
sl@0
   426
			reply[3] = aMessage.Int3();
sl@0
   427
			r = aMessage.Write(0,TPtrC8((TUint8*)&reply,sizeof(reply)));
sl@0
   428
			}
sl@0
   429
			break;
sl@0
   430
sl@0
   431
		case ETestEmptySubSessionMessage:
sl@0
   432
			r = aMessage.Int3();
sl@0
   433
			break;
sl@0
   434
sl@0
   435
		default:
sl@0
   436
			break;
sl@0
   437
		}
sl@0
   438
	if(aMessage.Handle())
sl@0
   439
		aMessage.Complete(r);
sl@0
   440
	return;
sl@0
   441
fail:
sl@0
   442
	aMessage.Complete(KErrGeneral);
sl@0
   443
	return;
sl@0
   444
pass:
sl@0
   445
	aMessage.Complete(KErrNone);
sl@0
   446
	return;
sl@0
   447
	}
sl@0
   448
sl@0
   449
TInt CTestSession::CountResources()
sl@0
   450
	{
sl@0
   451
	return iResourceCount;
sl@0
   452
	}
sl@0
   453
sl@0
   454
void CTestSession::ServiceError(const RMessage2& aMessage,TInt aError)
sl@0
   455
	{
sl@0
   456
	if(aMessage!=iMessage) 
sl@0
   457
		aError = KErrGeneral;   // We got given the right message
sl@0
   458
	else
sl@0
   459
		aError += KTestServiceErrorModifier;   // Let test harnes know we came through this routine
sl@0
   460
	CSession2::ServiceError(aMessage,aError);
sl@0
   461
	}
sl@0
   462
sl@0
   463
//
sl@0
   464
// CTestSecureServer
sl@0
   465
//
sl@0
   466
sl@0
   467
CTestSecureServer::CTestSecureServer(TInt aPriority)
sl@0
   468
	: CServer2(aPriority,EGlobalSharableSessions)
sl@0
   469
	{
sl@0
   470
	}
sl@0
   471
sl@0
   472
CSession2* CTestSecureServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
sl@0
   473
	{
sl@0
   474
	if(*(TInt*)&aVersion)
sl@0
   475
		User::Leave(KErrNotSupported); // Only accept version 0.0.00
sl@0
   476
	return new (ELeave) CTestSession();
sl@0
   477
	}
sl@0
   478
sl@0
   479
TInt CTestSecureServer::RunError(TInt aError)
sl@0
   480
	{
sl@0
   481
	return CServer2::RunError(aError+KTestRunErrorModifier);    // Let test harnes know we came through this routine
sl@0
   482
	}
sl@0
   483
sl@0
   484
sl@0
   485
//
sl@0
   486
// CTestActiveScheduler
sl@0
   487
//
sl@0
   488
sl@0
   489
class CTestActiveScheduler : public CActiveScheduler
sl@0
   490
	{
sl@0
   491
public:
sl@0
   492
	virtual void Error(TInt anError) const;
sl@0
   493
	};
sl@0
   494
sl@0
   495
void CTestActiveScheduler::Error(TInt anError) const
sl@0
   496
	{
sl@0
   497
	User::Panic(_L("TestServer Error"),anError);
sl@0
   498
	}
sl@0
   499
sl@0
   500
sl@0
   501
sl@0
   502
//
sl@0
   503
// Server thread
sl@0
   504
//
sl@0
   505
sl@0
   506
_LIT(KServerName,"T_SSERVER-server");
sl@0
   507
const TInt KServerRendezvous = KRequestPending+1;
sl@0
   508
sl@0
   509
void DoStartServer()
sl@0
   510
	{
sl@0
   511
	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
sl@0
   512
	CActiveScheduler::Install(activeScheduler);
sl@0
   513
	CleanupStack::PushL(activeScheduler);
sl@0
   514
sl@0
   515
	CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
sl@0
   516
	CleanupStack::PushL(server);
sl@0
   517
sl@0
   518
	User::LeaveIfError(server->Start(KServerName));
sl@0
   519
sl@0
   520
	RProcess::Rendezvous(KServerRendezvous);
sl@0
   521
sl@0
   522
	CActiveScheduler::Start();
sl@0
   523
sl@0
   524
	CleanupStack::PopAndDestroy(2);
sl@0
   525
	}
sl@0
   526
sl@0
   527
TInt StartServer()
sl@0
   528
	{
sl@0
   529
	CTrapCleanup* cleanupStack = CTrapCleanup::New();
sl@0
   530
	if(!cleanupStack)
sl@0
   531
		return KErrNoMemory;
sl@0
   532
	TRAPD(leaveError,DoStartServer())
sl@0
   533
	delete cleanupStack;
sl@0
   534
	return leaveError;
sl@0
   535
	}
sl@0
   536
sl@0
   537
sl@0
   538
sl@0
   539
//
sl@0
   540
// RTestSession
sl@0
   541
//
sl@0
   542
sl@0
   543
class RTestSession : public RSessionBase
sl@0
   544
	{
sl@0
   545
public:
sl@0
   546
	inline TInt Connect()
sl@0
   547
		{
sl@0
   548
		TInt r=CreateSession(KServerName,TVersion());
sl@0
   549
		if(r) return r;
sl@0
   550
		return ShareAuto();
sl@0
   551
		}
sl@0
   552
	inline TInt Connect(const TSecurityPolicy* aPolicy)
sl@0
   553
		{
sl@0
   554
		return CreateSession(KServerName,TVersion(),-1,EIpcSession_Unsharable,aPolicy,0);
sl@0
   555
		}
sl@0
   556
	inline TInt Connect(TVersion aVersion,TRequestStatus* aStatus)
sl@0
   557
		{
sl@0
   558
		return CreateSession(KServerName,aVersion,-1,EIpcSession_Unsharable,0,aStatus);
sl@0
   559
		}
sl@0
   560
	inline TInt Send(TInt aFunction)
sl@0
   561
		{ return RSessionBase::SendReceive(aFunction); }
sl@0
   562
	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
sl@0
   563
		{ return RSessionBase::SendReceive(aFunction,aArgs); }
sl@0
   564
	inline void Send(TInt aFunction,TRequestStatus& aStatus)
sl@0
   565
		{ RSessionBase::SendReceive(aFunction,aStatus); }
sl@0
   566
	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
sl@0
   567
		{ RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
sl@0
   568
	};
sl@0
   569
sl@0
   570
sl@0
   571
sl@0
   572
//
sl@0
   573
// RTestSubSession
sl@0
   574
//
sl@0
   575
sl@0
   576
class RTestSubSession : public RSubSessionBase
sl@0
   577
	{
sl@0
   578
public:
sl@0
   579
	inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction,const TIpcArgs& aArgs)
sl@0
   580
		{ return RSubSessionBase::CreateSubSession(aSession,aFunction,aArgs); }
sl@0
   581
	inline TInt CreateSubSession(RSessionBase& aSession,TInt aFunction)
sl@0
   582
		{ return RSubSessionBase::CreateSubSession(aSession,aFunction); }
sl@0
   583
	inline void CloseSubSession(TInt aFunction)
sl@0
   584
		{ RSubSessionBase::CloseSubSession(aFunction); }
sl@0
   585
	inline TInt Send(TInt aFunction)
sl@0
   586
		{ return RSubSessionBase::SendReceive(aFunction); }
sl@0
   587
	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
sl@0
   588
		{ return RSubSessionBase::SendReceive(aFunction,aArgs); }
sl@0
   589
	inline void Send(TInt aFunction,TRequestStatus& aStatus)
sl@0
   590
		{ RSubSessionBase::SendReceive(aFunction,aStatus); }
sl@0
   591
	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
sl@0
   592
		{ RSubSessionBase::SendReceive(aFunction,aArgs,aStatus); }
sl@0
   593
	};
sl@0
   594
sl@0
   595
sl@0
   596
sl@0
   597
RTestSession Session;
sl@0
   598
TInt TestParam = 0;
sl@0
   599
sl@0
   600
TInt TestThreadFunction(TAny* aParam)
sl@0
   601
	{
sl@0
   602
	return Session.Send((TInt)aParam,TIpcArgs(TestParam));
sl@0
   603
	}
sl@0
   604
sl@0
   605
void DoTest(TInt aFunction,TExitType aExitType,TInt aExitReason,TInt aParam=0)
sl@0
   606
	{
sl@0
   607
	TBuf<256> title;
sl@0
   608
	title.AppendFormat(_L("Function %d"),aFunction);
sl@0
   609
	test.Next(title);
sl@0
   610
sl@0
   611
	RTestThread thread;
sl@0
   612
	thread.Create(TestThreadFunction,(TAny*)aFunction);
sl@0
   613
	TRequestStatus logon;
sl@0
   614
	thread.Logon(logon);
sl@0
   615
	TestParam = aParam;
sl@0
   616
sl@0
   617
	User::SetJustInTime(EFalse);
sl@0
   618
	thread.Resume();
sl@0
   619
	User::WaitForRequest(logon);
sl@0
   620
	User::SetJustInTime(ETrue);
sl@0
   621
sl@0
   622
	TExitType exitType=thread.ExitType();
sl@0
   623
	TInt exitReason=thread.ExitReason();
sl@0
   624
	test(exitType==aExitType);
sl@0
   625
	test(exitReason==aExitReason);
sl@0
   626
	CLOSE_AND_WAIT(thread);
sl@0
   627
	}
sl@0
   628
sl@0
   629
//
sl@0
   630
// Anonymous server
sl@0
   631
//
sl@0
   632
sl@0
   633
class RTestAnonymousSession : public RTestSession
sl@0
   634
	{
sl@0
   635
public:
sl@0
   636
	inline TInt Connect(RServer2 aServer)
sl@0
   637
		{
sl@0
   638
		TInt r=CreateSession(aServer,TVersion());
sl@0
   639
		if(r) return r;
sl@0
   640
		return ShareProtected();
sl@0
   641
		}
sl@0
   642
	inline TInt Connect()
sl@0
   643
		{
sl@0
   644
		return CreateSession(_L(""),TVersion());
sl@0
   645
		}
sl@0
   646
	};
sl@0
   647
sl@0
   648
CTestSecureServer* AnonymousServer;
sl@0
   649
sl@0
   650
void DoStartAnonymousServer()
sl@0
   651
	{
sl@0
   652
	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
sl@0
   653
	CActiveScheduler::Install(activeScheduler);
sl@0
   654
	CleanupStack::PushL(activeScheduler);
sl@0
   655
sl@0
   656
	CTestSecureServer* server = new (ELeave) CTestSecureServer(0);
sl@0
   657
	CleanupStack::PushL(server);
sl@0
   658
sl@0
   659
	User::LeaveIfError(server->Start(KNullDesC));
sl@0
   660
sl@0
   661
	AnonymousServer = server;
sl@0
   662
	RThread::Rendezvous(KServerRendezvous);
sl@0
   663
sl@0
   664
	CActiveScheduler::Start();
sl@0
   665
sl@0
   666
	CleanupStack::PopAndDestroy(2);
sl@0
   667
	}
sl@0
   668
sl@0
   669
TInt StartAnonymousServer(TAny* /*aPtr*/)
sl@0
   670
	{
sl@0
   671
	CTrapCleanup* cleanupStack = CTrapCleanup::New();
sl@0
   672
	if(!cleanupStack)
sl@0
   673
		return KErrNoMemory;
sl@0
   674
	TRAPD(leaveError,DoStartAnonymousServer())
sl@0
   675
	delete cleanupStack;
sl@0
   676
	return leaveError;
sl@0
   677
	}
sl@0
   678
sl@0
   679
void TestConnectByHandle()
sl@0
   680
	{
sl@0
   681
	RTestAnonymousSession session;
sl@0
   682
	TRequestStatus logon;
sl@0
   683
	TInt r;
sl@0
   684
sl@0
   685
	test.Start(_L("Starting a local server"));
sl@0
   686
	RTestThread thread;
sl@0
   687
	thread.Create(StartAnonymousServer);
sl@0
   688
	TRequestStatus rendezvous;
sl@0
   689
	TRequestStatus svrstat;
sl@0
   690
	thread.Rendezvous(rendezvous);
sl@0
   691
	thread.Logon(logon);
sl@0
   692
	thread.NotifyDestruction(svrstat);
sl@0
   693
	thread.Resume();
sl@0
   694
	User::WaitForRequest(rendezvous);
sl@0
   695
	test(rendezvous.Int()==KServerRendezvous);
sl@0
   696
	thread.Close();
sl@0
   697
sl@0
   698
	test.Next(_L("Check connect with null name fails"));
sl@0
   699
	test((r=session.Connect())!=KErrNone);
sl@0
   700
sl@0
   701
	test.Next(_L("Connecting to local server"));
sl@0
   702
	test((r=session.Connect(AnonymousServer->Server()))==KErrNone);
sl@0
   703
sl@0
   704
	test.Next(_L("Test the connection"));
sl@0
   705
	test((r=session.Send(EPing,TIpcArgs(1234)))==1234);
sl@0
   706
	test((r=session.Send(EPing,TIpcArgs(2345)))==2345);
sl@0
   707
sl@0
   708
	test.Next(_L("Shutting server down"));
sl@0
   709
	test(logon.Int()==KRequestPending);
sl@0
   710
	test((r=session.Send(ETestShutdown))==KErrServerTerminated);
sl@0
   711
	test(r==KErrServerTerminated);
sl@0
   712
	session.Close();
sl@0
   713
	User::WaitForRequest(logon);
sl@0
   714
	test(logon.Int()==KErrNone);
sl@0
   715
	User::WaitForRequest(svrstat);
sl@0
   716
sl@0
   717
	test.End();
sl@0
   718
	}
sl@0
   719
sl@0
   720
TRequestStatus SvrStat;
sl@0
   721
sl@0
   722
void RestartTestServer()
sl@0
   723
	{
sl@0
   724
	RTestProcess server;
sl@0
   725
	TRequestStatus rendezvous;
sl@0
   726
	server.Create(0,ETestProcessServer);
sl@0
   727
	server.NotifyDestruction(SvrStat);
sl@0
   728
	server.Rendezvous(rendezvous);
sl@0
   729
	server.Resume();
sl@0
   730
	User::WaitForRequest(rendezvous);
sl@0
   731
	test(rendezvous==KServerRendezvous);
sl@0
   732
	server.Close();
sl@0
   733
	test(Session.Connect()==KErrNone);
sl@0
   734
	}
sl@0
   735
sl@0
   736
void TestIpc()
sl@0
   737
	{
sl@0
   738
	TBuf8<10> buf8((TUint8*)"abcdefghij");
sl@0
   739
	TBuf16<10> buf16((TUint16*)L"abcdefghij");
sl@0
   740
	TInt r;
sl@0
   741
sl@0
   742
	test.Start(_L("Testing TIpcArgs::Set"));
sl@0
   743
	{
sl@0
   744
	TIpcArgs a(TIpcArgs::ENothing,123,(TAny*)&buf8);
sl@0
   745
	TIpcArgs b;
sl@0
   746
	b.Set(0,TIpcArgs::ENothing);
sl@0
   747
	b.Set(1,123);
sl@0
   748
	b.Set(2,(TAny*)&buf8);
sl@0
   749
	test(a.iFlags==b.iFlags);
sl@0
   750
	// iArgs[0] is uninitialised so don't test
sl@0
   751
	test(a.iArgs[1]==b.iArgs[1]);
sl@0
   752
	test(a.iArgs[2]==b.iArgs[2]);
sl@0
   753
	}
sl@0
   754
	{
sl@0
   755
	TIpcArgs a((TDesC8*)&buf8,(TDesC16*)&buf16,&buf8,&buf16);
sl@0
   756
	TIpcArgs b;
sl@0
   757
	b.Set(0,(TDesC8*)&buf8);
sl@0
   758
	b.Set(1,(TDesC16*)&buf16);
sl@0
   759
	b.Set(2,&buf8);
sl@0
   760
	b.Set(3,&buf16);
sl@0
   761
	test(a.iFlags==b.iFlags);
sl@0
   762
	test(a.iArgs[0]==b.iArgs[0]);
sl@0
   763
	test(a.iArgs[1]==b.iArgs[1]);
sl@0
   764
	test(a.iArgs[2]==b.iArgs[2]);
sl@0
   765
	test(a.iArgs[3]==b.iArgs[3]);
sl@0
   766
	}
sl@0
   767
sl@0
   768
	test.Next(_L("Test Unspecified argument"));
sl@0
   769
	r = Session.Send(ETestArgUnspecified,TIpcArgs((TAny*)&buf8));
sl@0
   770
	test(r==KErrNone);
sl@0
   771
	r = Session.Send(ETestArgUnspecified+(1<<28),TIpcArgs(0,(TAny*)&buf8));
sl@0
   772
	test(r==KErrNone);
sl@0
   773
	r = Session.Send(ETestArgUnspecified+(2<<28),TIpcArgs(0,0,(TAny*)&buf8));
sl@0
   774
	test(r==KErrNone);
sl@0
   775
	r = Session.Send(ETestArgUnspecified+(3<<28),TIpcArgs(0,0,0,(TAny*)&buf8));
sl@0
   776
	test(r==KErrNone);
sl@0
   777
sl@0
   778
	test.Next(_L("Test DesC8 argument"));
sl@0
   779
	r = Session.Send(ETestArgDesC8,TIpcArgs((TDesC8*)&buf8));
sl@0
   780
	test(r==KErrNone);
sl@0
   781
	r = Session.Send(ETestArgDesC8+(1<<28),TIpcArgs(0,(TDesC8*)&buf8));
sl@0
   782
	test(r==KErrNone);
sl@0
   783
	r = Session.Send(ETestArgDesC8+(2<<28),TIpcArgs(0,0,(TDesC8*)&buf8));
sl@0
   784
	test(r==KErrNone);
sl@0
   785
	r = Session.Send(ETestArgDesC8+(3<<28),TIpcArgs(0,0,0,(TDesC8*)&buf8));
sl@0
   786
	test(r==KErrNone);
sl@0
   787
sl@0
   788
	test.Next(_L("Test DesC16 argument"));
sl@0
   789
	r = Session.Send(ETestArgDesC16,TIpcArgs((TDesC16*)&buf16));
sl@0
   790
	test(r==KErrNone);
sl@0
   791
	r = Session.Send(ETestArgDesC16+(1<<28),TIpcArgs(0,(TDesC16*)&buf16));
sl@0
   792
	test(r==KErrNone);
sl@0
   793
	r = Session.Send(ETestArgDesC16+(2<<28),TIpcArgs(0,0,(TDesC16*)&buf16));
sl@0
   794
	test(r==KErrNone);
sl@0
   795
	r = Session.Send(ETestArgDesC16+(3<<28),TIpcArgs(0,0,0,(TDesC16*)&buf16));
sl@0
   796
	test(r==KErrNone);
sl@0
   797
sl@0
   798
	test.Next(_L("Test Des8 argument"));
sl@0
   799
	r = Session.Send(ETestArgDes8,TIpcArgs(&buf8));
sl@0
   800
	test(r==KErrNone);
sl@0
   801
	r = Session.Send(ETestArgDes8+(1<<28),TIpcArgs(0,&buf8));
sl@0
   802
	test(r==KErrNone);
sl@0
   803
	r = Session.Send(ETestArgDes8+(2<<28),TIpcArgs(0,0,&buf8));
sl@0
   804
	test(r==KErrNone);
sl@0
   805
	r = Session.Send(ETestArgDes8+(3<<28),TIpcArgs(0,0,0,&buf8));
sl@0
   806
	test(r==KErrNone);
sl@0
   807
sl@0
   808
	test.Next(_L("Test Des16 argument"));
sl@0
   809
	r = Session.Send(ETestArgDes16,TIpcArgs(&buf16));
sl@0
   810
	test(r==KErrNone);
sl@0
   811
	r = Session.Send(ETestArgDes16+(1<<28),TIpcArgs(0,&buf16));
sl@0
   812
	test(r==KErrNone);
sl@0
   813
	r = Session.Send(ETestArgDes16+(2<<28),TIpcArgs(0,0,&buf16));
sl@0
   814
	test(r==KErrNone);
sl@0
   815
	r = Session.Send(ETestArgDes16+(3<<28),TIpcArgs(0,0,0,&buf16));
sl@0
   816
	test(r==KErrNone);
sl@0
   817
sl@0
   818
sl@0
   819
	test.Next(_L("Test Bad Client Descriptor"));
sl@0
   820
	//The test should ensure that both server and kernel are safe if client passes faulty descriptor to the server.
sl@0
   821
	{
sl@0
   822
	//Create a chunk with a hole between addresses 0x1000 & 0x2000
sl@0
   823
	RChunk c;
sl@0
   824
	r=c.CreateDisconnectedLocal(0,0,0x200000);
sl@0
   825
	test(r==KErrNone);
sl@0
   826
	r=c.Commit(0,0x1000);
sl@0
   827
	test(r==KErrNone);
sl@0
   828
	r=c.Commit(0x2000,0x1000);
sl@0
   829
	test(r==KErrNone);
sl@0
   830
sl@0
   831
	//Each of these steps will pass bad descriptor to the server for IPC transfer in both directions.
sl@0
   832
	//KErrBadDescriptor should be returned.
sl@0
   833
	{
sl@0
   834
	TPtr8 ptr (c.Base()+0x1000,0x100,0x100);
sl@0
   835
	r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   836
	test(KErrBadDescriptor == r);
sl@0
   837
	r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   838
	test(KErrBadDescriptor == r);
sl@0
   839
	}
sl@0
   840
	{
sl@0
   841
	TPtr8 ptr (c.Base()+0x1003,0x100,0x100);
sl@0
   842
	r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   843
	test(KErrBadDescriptor == r);
sl@0
   844
	r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   845
	test(KErrBadDescriptor == r);
sl@0
   846
	}
sl@0
   847
	{
sl@0
   848
	TPtr8 ptr (c.Base()+0xe00,0x500,0x500);
sl@0
   849
	r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   850
	test(KErrBadDescriptor == r);
sl@0
   851
	r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   852
	test(KErrBadDescriptor == r);
sl@0
   853
	}
sl@0
   854
	{
sl@0
   855
	TPtr8 ptr (c.Base()+0xdff,0x500,0x500);
sl@0
   856
	r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   857
	test(KErrBadDescriptor == r);
sl@0
   858
	r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   859
	test(KErrBadDescriptor == r);
sl@0
   860
	}
sl@0
   861
	{
sl@0
   862
	TPtr8 ptr (c.Base()+0x1ff1,0x500,0x500);
sl@0
   863
	r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   864
	test(KErrBadDescriptor == r);
sl@0
   865
	r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   866
	test(KErrBadDescriptor == r);
sl@0
   867
	}
sl@0
   868
sl@0
   869
	//The next step will send random descriptors for IPC transfer. Server should return either KErrNone or
sl@0
   870
	//KErrBadDescriptor
sl@0
   871
	{	
sl@0
   872
	TUint seed[2];
sl@0
   873
	seed[0]=User::TickCount();
sl@0
   874
	seed[1]=0;
sl@0
   875
	test.Printf(_L("The initial seed for the random function is:  S0=%xh S1=%xh\n"), seed[0], seed[1]);
sl@0
   876
sl@0
   877
	TInt i;
sl@0
   878
	TPtr8 ptr (0,0,0);
sl@0
   879
	TInt noErrors = 0;
sl@0
   880
	TInt badDescriptors = 0;
sl@0
   881
sl@0
   882
	for (i=0;i<100;i++)
sl@0
   883
		{
sl@0
   884
		TInt descAddress = (TInt) (c.Base() + (Random(seed) % 0x1000));
sl@0
   885
		TInt descSize=Random(seed)%0x1000;
sl@0
   886
sl@0
   887
		TUint* descData = (TUint*)&ptr;
sl@0
   888
		descData[0] = 0x20000000 + descSize;
sl@0
   889
		descData[1] = descSize;
sl@0
   890
		descData[2] = descAddress;
sl@0
   891
sl@0
   892
		r = Session.Send(ETestBadClientDescRead,TIpcArgs(&ptr));
sl@0
   893
		switch (r)
sl@0
   894
			{
sl@0
   895
			case KErrNone:			noErrors++;break;
sl@0
   896
			case KErrBadDescriptor: badDescriptors++;break;
sl@0
   897
			default:				test.Printf(_L("Error: %d returned"),r);
sl@0
   898
			}
sl@0
   899
sl@0
   900
		r = Session.Send(ETestBadClientDescWrite,TIpcArgs(&ptr));
sl@0
   901
		switch (r)
sl@0
   902
			{
sl@0
   903
			case KErrNone:			noErrors++;break;
sl@0
   904
			case KErrBadDescriptor: badDescriptors++;break;
sl@0
   905
			default:				test.Printf(_L("Error: %d returned"),r);
sl@0
   906
			}
sl@0
   907
		}
sl@0
   908
	test.Printf(_L("KErrNoError: %d  KErrBadDescriptor: %d"),noErrors, badDescriptors);
sl@0
   909
	}
sl@0
   910
sl@0
   911
	test.Next(_L("Test Bad Server Descriptor"));
sl@0
   912
	//The test should ensure that kernel is safe if server passes faulty descriptor for IPC transfer
sl@0
   913
	{
sl@0
   914
	TPtr8 ptr (c.Base(),0x1000,0x1000);
sl@0
   915
	TInt i;
sl@0
   916
	for (i=0;i<=7;i++)
sl@0
   917
		{
sl@0
   918
		r = Session.Send(ETestBadServerDescRead | (i<<28),TIpcArgs(&ptr));
sl@0
   919
		test(r==KErrServerTerminated);
sl@0
   920
		RestartTestServer();
sl@0
   921
		r = Session.Send(ETestBadServerDescWrite | (i<<28),TIpcArgs(&ptr));
sl@0
   922
		test(r==KErrServerTerminated);
sl@0
   923
		User::WaitForRequest(SvrStat);
sl@0
   924
		RestartTestServer();
sl@0
   925
		}
sl@0
   926
	}
sl@0
   927
sl@0
   928
	c.Close();
sl@0
   929
	}
sl@0
   930
	test.End();
sl@0
   931
	}
sl@0
   932
sl@0
   933
sl@0
   934
sl@0
   935
void TestSubSessions()
sl@0
   936
	{
sl@0
   937
	const TInt KSubSessionHandle = 0x87654321;
sl@0
   938
	RTestSubSession sub;
sl@0
   939
	TInt r;
sl@0
   940
sl@0
   941
	test.Start(_L("Creating a subsession with no arguments"));
sl@0
   942
	r = sub.CreateSubSession(Session,ETestCreateSubSession);	
sl@0
   943
	test(r==KErrNone);
sl@0
   944
	test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose));  // check sub.iSession
sl@0
   945
sl@0
   946
	test.Next(_L("Creating a subsession with arguments"));
sl@0
   947
	r = sub.CreateSubSession(Session,ETestCreateSubSession,TIpcArgs(KSubSessionHandle));	
sl@0
   948
	test(r==KErrNone);
sl@0
   949
	test(((TInt*)&sub)[0] == (*(TInt*)&Session | CObjectIx::ENoClose));  // check sub.iSession
sl@0
   950
sl@0
   951
	test(((TInt*)&sub)[1]==KSubSessionHandle);   // check sub.iSubSessionHandle
sl@0
   952
sl@0
   953
	test.Next(_L("Sending message with arguments"));
sl@0
   954
	TInt reply[4] = {0};
sl@0
   955
	TPtr8 replyDes((TUint8*)&reply,sizeof(reply));
sl@0
   956
	r = sub.Send(ETestEchoArgs,TIpcArgs(&replyDes,123,456,789));
sl@0
   957
	test(r==KErrNone);
sl@0
   958
	test(reply[0]==(TInt)&replyDes);
sl@0
   959
	test(reply[1]==123);
sl@0
   960
	test(reply[2]==456);
sl@0
   961
	test(reply[3]==KSubSessionHandle);
sl@0
   962
sl@0
   963
	test.Next(_L("Sending empty message"));
sl@0
   964
	r = sub.Send(ETestEmptySubSessionMessage);
sl@0
   965
	test(r==KSubSessionHandle);
sl@0
   966
sl@0
   967
	test.Next(_L("Closing subsession"));
sl@0
   968
	sub.CloseSubSession(ETestCloseSubSession);	
sl@0
   969
	test(((TInt*)&sub)[0]==0);  // check sub.iSession
sl@0
   970
	test(((TInt*)&sub)[1]==0);   // check sub.iSubSessionHandle
sl@0
   971
	r = Session.Send(ETestCloseSubSessionHandle);
sl@0
   972
	test(r==KSubSessionHandle);
sl@0
   973
sl@0
   974
	test.End();
sl@0
   975
	}
sl@0
   976
sl@0
   977
sl@0
   978
void TestProtectedServers()
sl@0
   979
	{
sl@0
   980
	RTestProcess process;
sl@0
   981
	TRequestStatus logonStatus;
sl@0
   982
	
sl@0
   983
	test.Start(_L("Trying to create a protected server without KCapabilityProtServ"));
sl@0
   984
	process.Create(~(1u<<ECapabilityProtServ),ETestProcessCreateProtectedServer);
sl@0
   985
	process.Logon(logonStatus);
sl@0
   986
	process.Resume();
sl@0
   987
	User::WaitForRequest(logonStatus);
sl@0
   988
	if(PlatSec::IsCapabilityEnforced(ECapabilityProtServ))
sl@0
   989
		{
sl@0
   990
		test(process.ExitType()==EExitPanic); // Process should have got a Platform Security panic
sl@0
   991
		test(logonStatus==EPlatformSecurityTrap);
sl@0
   992
		}
sl@0
   993
	else
sl@0
   994
		{
sl@0
   995
		test(process.ExitType()==EExitKill);
sl@0
   996
		test(logonStatus==KErrNone);
sl@0
   997
		}
sl@0
   998
	CLOSE_AND_WAIT(process);
sl@0
   999
sl@0
  1000
	test.Next(_L("Trying to create a protected server with KCapabilityProtServ"));
sl@0
  1001
	process.Create(1<<ECapabilityProtServ,ETestProcessCreateProtectedServer);
sl@0
  1002
	process.Logon(logonStatus);
sl@0
  1003
	process.Resume();
sl@0
  1004
	User::WaitForRequest(logonStatus);
sl@0
  1005
	test(process.ExitType()==EExitKill);
sl@0
  1006
	test(logonStatus==KErrNone);
sl@0
  1007
	CLOSE_AND_WAIT(process);
sl@0
  1008
sl@0
  1009
	test.End();
sl@0
  1010
	}
sl@0
  1011
sl@0
  1012
sl@0
  1013
void TestIdentifiedServers()
sl@0
  1014
	{
sl@0
  1015
	RTestSession session;
sl@0
  1016
	TInt r;
sl@0
  1017
	
sl@0
  1018
	test.Start(_L("Trying to connect to a server with wrong Secure ID"));
sl@0
  1019
	_LIT_SECURITY_POLICY_S0(wrongSid,KTestSecureId2);
sl@0
  1020
	r=session.Connect(&wrongSid);
sl@0
  1021
	test(r==(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)?KErrPermissionDenied:KErrNone));
sl@0
  1022
	session.Close();
sl@0
  1023
sl@0
  1024
	test.Next(_L("Test connecting to a server with correct Secure ID"));
sl@0
  1025
	_LIT_SECURITY_POLICY_S0(correctSid,KTestSecureId);
sl@0
  1026
	r=session.Connect(&correctSid);
sl@0
  1027
	test(r==KErrNone);
sl@0
  1028
	session.Close();
sl@0
  1029
sl@0
  1030
	test.Next(_L("Trying to connect to a server with wrong Vendor ID"));
sl@0
  1031
	_LIT_SECURITY_POLICY_V0(wrongVid,KTestVendorId2);
sl@0
  1032
	r=session.Connect(&wrongVid);
sl@0
  1033
	test(r==(PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)?KErrPermissionDenied:KErrNone));
sl@0
  1034
	session.Close();
sl@0
  1035
sl@0
  1036
	test.Next(_L("Test connecting to a server with correct Vendor ID"));
sl@0
  1037
	_LIT_SECURITY_POLICY_V0(correctVid,KTestVendorId);
sl@0
  1038
	r=session.Connect(&correctVid);
sl@0
  1039
	test(r==KErrNone);
sl@0
  1040
	session.Close();
sl@0
  1041
sl@0
  1042
	test.Next(_L("Trying to connect to a server with wrong capabilities"));
sl@0
  1043
	_LIT_SECURITY_POLICY_C1(wrongCaps,ECapabilityReadUserData);
sl@0
  1044
	r=session.Connect(&wrongCaps);
sl@0
  1045
	test(r==(PlatSec::IsCapabilityEnforced(ECapabilityReadUserData)?KErrPermissionDenied:KErrNone));
sl@0
  1046
	session.Close();
sl@0
  1047
sl@0
  1048
	test.Next(_L("Test connecting to a server with correct capabilities"));
sl@0
  1049
	_LIT_SECURITY_POLICY_C1(correctCaps,ECapability_None);
sl@0
  1050
	r=session.Connect(&correctCaps);
sl@0
  1051
	test(r==KErrNone);
sl@0
  1052
	session.Close();
sl@0
  1053
sl@0
  1054
	test.Next(_L("Test connecting to a server without specifying a policy"));
sl@0
  1055
	r=session.Connect(0);
sl@0
  1056
	test(r==KErrNone);
sl@0
  1057
	session.Close();
sl@0
  1058
sl@0
  1059
	test.End();
sl@0
  1060
	}
sl@0
  1061
sl@0
  1062
sl@0
  1063
sl@0
  1064
#include <e32panic.h>
sl@0
  1065
sl@0
  1066
TInt TestThreadAsyncConnect(TAny*)
sl@0
  1067
	{
sl@0
  1068
	RTestSession session;
sl@0
  1069
	TRequestStatus status;
sl@0
  1070
	TInt r=session.Connect(TVersion(1,1,1),&status);
sl@0
  1071
	if(r!=KErrNone)
sl@0
  1072
		return r;
sl@0
  1073
	if(status!=KRequestPending)   // server can't have created session yet because we have higher priority
sl@0
  1074
		return status.Int();
sl@0
  1075
	User::WaitForRequest(status);
sl@0
  1076
	if(status!=KErrNotSupported)
sl@0
  1077
		return status.Int();
sl@0
  1078
	return session.Send(EPing,TIpcArgs(1234));   // Should panic us
sl@0
  1079
	}
sl@0
  1080
sl@0
  1081
void TestAsynchronousConnect()
sl@0
  1082
	{
sl@0
  1083
	RTestSession session;
sl@0
  1084
	TRequestStatus status;
sl@0
  1085
	TInt r;
sl@0
  1086
	
sl@0
  1087
	test.Start(_L("Test successful asynchronous connect"));
sl@0
  1088
	r=session.Connect(TVersion(0,0,0),&status);
sl@0
  1089
	test(r==KErrNone);
sl@0
  1090
	test(status==KRequestPending);   // server can't have created session yet because we have higher priority
sl@0
  1091
	User::WaitForRequest(status);
sl@0
  1092
	test(status==KErrNone);
sl@0
  1093
	test((r=session.Send(EPing,TIpcArgs(1234)))==1234);
sl@0
  1094
	session.Close();
sl@0
  1095
sl@0
  1096
	test.Next(_L("Test unsuccessful asynchronous connect"));
sl@0
  1097
	r=session.Connect(TVersion(1,1,1),&status);
sl@0
  1098
	test(r==KErrNone);
sl@0
  1099
	test(status==KRequestPending);   // server can't have created session yet because we have higher priority
sl@0
  1100
	User::WaitForRequest(status);
sl@0
  1101
	test(status==KErrNotSupported);
sl@0
  1102
	session.Close();
sl@0
  1103
sl@0
  1104
	test.Next(_L("Test using unsuccessful asynchronous connect"));
sl@0
  1105
	RTestThread thread;
sl@0
  1106
	thread.Create(TestThreadAsyncConnect,0);
sl@0
  1107
	TRequestStatus logon;
sl@0
  1108
	thread.Logon(logon);
sl@0
  1109
	User::SetJustInTime(EFalse);
sl@0
  1110
	thread.Resume();
sl@0
  1111
	User::WaitForRequest(logon);
sl@0
  1112
	User::SetJustInTime(ETrue);
sl@0
  1113
	TExitType exitType=thread.ExitType();
sl@0
  1114
	TInt exitReason=thread.ExitReason();
sl@0
  1115
	test(exitType==EExitPanic);
sl@0
  1116
	test(exitReason==CServer2::ESessionNotConnected);
sl@0
  1117
	thread.Close();
sl@0
  1118
sl@0
  1119
	test.End();
sl@0
  1120
	}
sl@0
  1121
sl@0
  1122
sl@0
  1123
sl@0
  1124
TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
sl@0
  1125
	{
sl@0
  1126
	(void)aArg1;
sl@0
  1127
	(void)aArg2;
sl@0
  1128
	switch(aTestNum)
sl@0
  1129
		{
sl@0
  1130
sl@0
  1131
	case ETestProcessServer:
sl@0
  1132
		RProcess().SetPriority(EPriorityLow);
sl@0
  1133
		return StartServer();
sl@0
  1134
sl@0
  1135
	case ETestProcessCreateProtectedServer:
sl@0
  1136
		{
sl@0
  1137
		test.Title();
sl@0
  1138
		RServer2 server;
sl@0
  1139
		TInt r = server.CreateGlobal(KProtectedServerName);
sl@0
  1140
		server.Close();
sl@0
  1141
		return r;
sl@0
  1142
		}
sl@0
  1143
sl@0
  1144
	default:
sl@0
  1145
		User::Panic(_L("T_SSERVER"),1);
sl@0
  1146
		}
sl@0
  1147
sl@0
  1148
	return KErrNone;
sl@0
  1149
	}
sl@0
  1150
sl@0
  1151
sl@0
  1152
sl@0
  1153
GLDEF_C TInt E32Main()
sl@0
  1154
    {
sl@0
  1155
	TBuf16<512> cmd;
sl@0
  1156
	User::CommandLine(cmd);
sl@0
  1157
	if(cmd.Length() && TChar(cmd[0]).IsDigit())
sl@0
  1158
		{
sl@0
  1159
		TInt function = -1;
sl@0
  1160
		TInt arg1 = -1;
sl@0
  1161
		TInt arg2 = -1;
sl@0
  1162
		TLex lex(cmd);
sl@0
  1163
sl@0
  1164
		lex.Val(function);
sl@0
  1165
		lex.SkipSpace();
sl@0
  1166
		lex.Val(arg1);
sl@0
  1167
		lex.SkipSpace();
sl@0
  1168
		lex.Val(arg2);
sl@0
  1169
		return DoTestProcess(function,arg1,arg2);
sl@0
  1170
		}
sl@0
  1171
sl@0
  1172
	TInt r;
sl@0
  1173
sl@0
  1174
	test.Title();
sl@0
  1175
sl@0
  1176
	test.Start(_L("Testing Server Connect by Handle"));
sl@0
  1177
	TestConnectByHandle();
sl@0
  1178
sl@0
  1179
	test.Next(_L("Starting test server"));
sl@0
  1180
	RTestProcess server;
sl@0
  1181
	TRequestStatus rendezvous;
sl@0
  1182
	server.Create(0,ETestProcessServer);
sl@0
  1183
	server.Rendezvous(rendezvous);
sl@0
  1184
	server.Resume();
sl@0
  1185
	User::WaitForRequest(rendezvous);
sl@0
  1186
	test(rendezvous==KServerRendezvous);
sl@0
  1187
	server.Close();
sl@0
  1188
sl@0
  1189
	test.Next(_L("Conecting to test server"));
sl@0
  1190
	test((r=Session.Connect())==KErrNone);
sl@0
  1191
sl@0
  1192
	test.Next(_L("Test asynchronous server connect"));
sl@0
  1193
	TestAsynchronousConnect();
sl@0
  1194
sl@0
  1195
	test.Next(_L("Test protected servers"));
sl@0
  1196
	TestProtectedServers();
sl@0
  1197
sl@0
  1198
	test.Next(_L("Test connecting to identified servers"));
sl@0
  1199
	TestIdentifiedServers();
sl@0
  1200
sl@0
  1201
	test.Next(_L("Testing SubSessions"));
sl@0
  1202
	TestSubSessions();
sl@0
  1203
sl@0
  1204
	test.Next(_L("Testing IPC argument checking"));
sl@0
  1205
	TestIpc();
sl@0
  1206
sl@0
  1207
	test.Next(_L("Testing CServer2::RunError and CSession2::ServiceError()"));
sl@0
  1208
	const TInt KTestServiceLeaveValue = 555;
sl@0
  1209
	r = Session.Send(ETestServiceLeave,TIpcArgs(KTestServiceLeaveValue));
sl@0
  1210
	test(r==KTestServiceLeaveValue+KTestRunErrorModifier+KTestServiceErrorModifier);
sl@0
  1211
sl@0
  1212
	test.Next(_L("Testing session resource counting"));
sl@0
  1213
	test.Start(_L(""));
sl@0
  1214
	DoTest(ETestResourceCountPass,EExitKill,ETestResourceCountPass);
sl@0
  1215
	DoTest(ETestResourceCountFail,EExitPanic,CSession2::ESesFoundResCountHeaven);
sl@0
  1216
	test.End();
sl@0
  1217
sl@0
  1218
	test.Next(_L("Shutting server down"));
sl@0
  1219
	{
sl@0
  1220
	Session.Send(ETestShutdown,TIpcArgs());
sl@0
  1221
	Session.Close();
sl@0
  1222
	}
sl@0
  1223
	User::WaitForRequest(SvrStat);
sl@0
  1224
sl@0
  1225
	test.End();
sl@0
  1226
	return(0);
sl@0
  1227
    }
sl@0
  1228