kerneltest/e32test/secure/t_sobject.cpp
changeset 0 a41df078684a
child 109 b3a1d9898418
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\secure\t_sobject.cpp
       
    15 // Overview:
       
    16 // Test the security aspects of the TFind??? (TFindChunk etc.) classes.
       
    17 // API Information:
       
    18 // TFind??? (TFindChunk, TFindThread etc.)
       
    19 // Details:
       
    20 // - Test TFindPhysicalDevices and attempt to open the found device. 
       
    21 // Verify results are as expected.
       
    22 // - Test TFindLogicalDevices and attempt to open the found device. 
       
    23 // Duplicate the object in another thread. Verify results are as expected.
       
    24 // - Test TFindLibrary and attempt to open the found device. Verify results 
       
    25 // are as expected.
       
    26 // - Test TFindServer and attempt to open the found server. Test duplication 
       
    27 // of a named or unnamed server. Verify results are as expected.
       
    28 // - Test TFindProcess and attempt to open the found process. Test duplication 
       
    29 // of the object. Attempt to open the process by name and by id. Verify 
       
    30 // results are as expected.
       
    31 // - Test TFindThread and attempt to open the found thread. Test duplication 
       
    32 // of the object. Attempt to open the thread in various ways. Verify 
       
    33 // results are as expected.
       
    34 // - Test TFindChunk and attempt to open the found object. Test duplication 
       
    35 // of the object. Attempt to open the object in various ways. Verify 
       
    36 // results are as expected.
       
    37 // - Test TFindSemaphore and attempt to open the found object. Attempt to 
       
    38 // open the object in various ways. Test duplication of the object. Verify 
       
    39 // results are as expected.
       
    40 // - Test TFindMutex and attempt to open the found object. Attempt to 
       
    41 // open the object in various ways. Test duplication of the object. Verify 
       
    42 // results are as expected.
       
    43 // - Create some RMsgQueue objects. Attempt to open the objects in various ways. 
       
    44 // Test duplication of the objects. Verify results are as expected.
       
    45 // - Create some RCondVar objects. Attempt to open the objects in various ways. 
       
    46 // Test duplication of the objects. Verify results are as expected.
       
    47 // - Test passing a handle via IPC: test sending LogicalChannel, Chunk, 
       
    48 // Semaphore, Mutex, MsgQueue, CondVar and Session handles. Verify results
       
    49 // are as expected.
       
    50 // Platforms/Drives/Compatibility:
       
    51 // All.
       
    52 // Assumptions/Requirement/Pre-requisites:
       
    53 // Failures and causes:
       
    54 // Base Port information:
       
    55 // 
       
    56 //
       
    57 
       
    58 #include <e32test.h>
       
    59 #include <e32msgqueue.h>
       
    60 #include <f32file.h>
       
    61 #include "d_sldd.h"
       
    62 
       
    63 LOCAL_D RTest test(_L("T_SOBJECT"));
       
    64 
       
    65 TInt PlatSecProcessIsolationError = 1;
       
    66 TInt PlatSecFindError = 1;
       
    67 
       
    68 _LIT(KTestMutexName,"T_SOBJECT-test-mutex");
       
    69 _LIT(KTestSemaphoreName,"T_SOBJECT-test-semaphore");
       
    70 _LIT(KTestChunkName,"T_SOBJECT-test-chunk");
       
    71 _LIT(KTestMsgQueueName,"T_SOBJECT-test-msgqueue");
       
    72 _LIT(KTestCondVarName,"T_SOBJECT-test-condvar");
       
    73 
       
    74 TFullName Name;
       
    75 TFullName Name2;
       
    76 
       
    77 void SetName(RHandleBase a)
       
    78 	{
       
    79 	Name = a.FullName();
       
    80 	}
       
    81 
       
    82 TBool CheckName(RHandleBase a)
       
    83 	{
       
    84 	Name2 = a.FullName();
       
    85 	return Name==Name2;
       
    86 	}
       
    87 
       
    88 class RTestHandle : public RHandleBase
       
    89 	{
       
    90 public:
       
    91 	inline TInt Open(const TFindHandleBase& aHandle,TOwnerType aType=EOwnerThread)
       
    92 		{ return RHandleBase::Open(aHandle,aType); }
       
    93 	};
       
    94 
       
    95 
       
    96 
       
    97 enum TTestProcessFunctions
       
    98 	{
       
    99 	ETestProcessServer,
       
   100 	ETestProcessDuplicate,
       
   101 	ETestProcessOpenThreadById,
       
   102 	};
       
   103 
       
   104 #include "testprocess.h"
       
   105 
       
   106 
       
   107 TThreadId MainThreadId;
       
   108 
       
   109 TInt TestThreadDuplicate(TAny* aArg)
       
   110 	{
       
   111 	RHandleBase handle;
       
   112 	handle.SetHandle((TInt)aArg);
       
   113 	RThread thread;
       
   114 	TInt r = thread.Open(MainThreadId);
       
   115 	if(r!=KErrNone)
       
   116 		r = 999;
       
   117 	else
       
   118 		r = handle.Duplicate(thread);
       
   119 	thread.Close();
       
   120 	return r;
       
   121 	}
       
   122 
       
   123 TInt DuplicateInOtherThread(RHandleBase aHandle)
       
   124 	{
       
   125 	RThread thread;
       
   126 	TRequestStatus logonStatus;
       
   127 	thread.Create(_L(""),TestThreadDuplicate,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,(TAny*)aHandle.Handle());
       
   128 	MainThreadId = RThread().Id();
       
   129 	thread.Logon(logonStatus);
       
   130 	thread.Resume();
       
   131 	User::WaitForRequest(logonStatus);
       
   132 	test(thread.ExitType()==EExitKill);
       
   133 	CLOSE_AND_WAIT(thread);
       
   134 	return logonStatus.Int();
       
   135 	}
       
   136 
       
   137 TInt DuplicateInOtherProcess(RHandleBase aHandle)
       
   138 	{
       
   139 	RTestProcess process;
       
   140 	TRequestStatus logonStatus;
       
   141 	process.Create(ETestProcessDuplicate,RThread().Id(),aHandle.Handle());
       
   142 	process.Logon(logonStatus);
       
   143 	process.Resume();
       
   144 	User::WaitForRequest(logonStatus);
       
   145 	test(process.ExitType()==EExitKill);
       
   146 	CLOSE_AND_WAIT(process);
       
   147 	return logonStatus.Int();
       
   148 	}
       
   149 
       
   150 TInt OpenThreadByIdInOtherProcess(TThreadId aId)
       
   151 	{
       
   152 	RTestProcess process;
       
   153 	TRequestStatus logonStatus;
       
   154 	process.Create(ETestProcessOpenThreadById,aId);
       
   155 	process.Logon(logonStatus);
       
   156 	process.Resume();
       
   157 	User::WaitForRequest(logonStatus);
       
   158 	test(process.ExitType()==EExitKill);
       
   159 	CLOSE_AND_WAIT(process);
       
   160 	return logonStatus.Int();
       
   161 	}
       
   162 
       
   163 
       
   164 
       
   165 //
       
   166 // RTestSession
       
   167 //
       
   168 
       
   169 enum TServerName
       
   170 	{
       
   171 	EMainServer,EGlobalSharableServer,EAnonymousServer,ENumServerTypes
       
   172 	};
       
   173 
       
   174 _LIT(KServerName,"T_SOBJECT-MainServer");
       
   175 _LIT(KServerName2,"T_SOBJECT-GlobalSharableServer");
       
   176 
       
   177 inline const TDesC& ServerName(TServerName aName)
       
   178 	{
       
   179 	switch(aName)
       
   180 		{
       
   181 	case EMainServer:
       
   182 		return KServerName;
       
   183 	case EGlobalSharableServer:
       
   184 		return KServerName2;
       
   185 	default:
       
   186 		return KNullDesC;
       
   187 		}
       
   188 	}
       
   189 
       
   190 class RTestSession : public RSessionBase
       
   191 	{
       
   192 public:
       
   193 	inline TInt Connect(TServerName aName=EMainServer)
       
   194 		{
       
   195 		TInt r=CreateSession(ServerName(aName),TVersion());
       
   196 		if(r) return r;
       
   197 		return ShareAuto();
       
   198 		}
       
   199 	inline TInt ConnectProtected(TServerName aName=EMainServer)
       
   200 		{
       
   201 		TInt r=CreateSession(ServerName(aName),TVersion());
       
   202 		if(r) return r;
       
   203 		return ShareProtected();
       
   204 		}
       
   205 	inline TInt Open(RMessagePtr2 aMessage,TInt aParam,TOwnerType aType=EOwnerProcess)
       
   206 		{ return RSessionBase::Open(aMessage,aParam,aType); }
       
   207 	inline TInt Open(RMessagePtr2 aMessage,TInt aParam,const TSecurityPolicy& aServerPolicy,TOwnerType aType=EOwnerProcess)
       
   208 		{ return RSessionBase::Open(aMessage,aParam,aServerPolicy,aType); }
       
   209 	inline TInt Send(TInt aFunction)
       
   210 		{ return RSessionBase::SendReceive(aFunction); }
       
   211 	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
       
   212 		{ return RSessionBase::SendReceive(aFunction,aArgs); }
       
   213 	inline void Send(TInt aFunction,TRequestStatus& aStatus)
       
   214 		{ RSessionBase::SendReceive(aFunction,aStatus); }
       
   215 	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
       
   216 		{ RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
       
   217 	};
       
   218 
       
   219 
       
   220 
       
   221 //
       
   222 // CTestSession
       
   223 //
       
   224 
       
   225 class CTestSession : public CSession2
       
   226 	{
       
   227 public:
       
   228 	enum {EShutdown,EPing,ETestMutex,ETestSemaphore,ETestMsgQueue,ETestCondVar,ETestChunk,ETestChunkAdjust,ETestLdd,
       
   229 		ETestSession,ETestSession2,ETestSession3,ETestServerDuplicateInThread,ETestServerDuplicateInProcess};
       
   230 public:
       
   231 	CTestSession();
       
   232 	virtual void ServiceL(const RMessage2& aMessage);
       
   233 public:
       
   234 	};
       
   235 
       
   236 CTestSession::CTestSession()
       
   237 	: CSession2()
       
   238 	{
       
   239 	}
       
   240 
       
   241 const TInt KTestDataMaxLength8 = 20;
       
   242 const TInt KTestDataMaxLength16 = 40;
       
   243 _LIT8(KTestData8,"12345678");
       
   244 _LIT16(KTestData16,"1234567890123456");
       
   245 _LIT(KTestPanicCategory,"TEST PANIC");
       
   246 
       
   247 void CTestSession::ServiceL(const RMessage2& aMessage)
       
   248 	{
       
   249 	RMessagePtr2 m(aMessage);
       
   250 	TFullName name;
       
   251 	TInt r = KErrGeneral;
       
   252 
       
   253 	switch (aMessage.Function())
       
   254 		{
       
   255 		case CTestSession::EShutdown:
       
   256 			CActiveScheduler::Stop();
       
   257 			break;
       
   258 
       
   259 		case CTestSession::EPing:
       
   260 			r=aMessage.Int0();
       
   261 			break;
       
   262 
       
   263 		case CTestSession::ETestMutex:
       
   264 			{
       
   265 			RMutex object;
       
   266 
       
   267 			r = object.Open(m,0,EOwnerThread);
       
   268 			if(r!=KErrBadHandle || object.Handle())
       
   269 				goto fail;
       
   270 
       
   271 			r = object.Open(m,1,EOwnerProcess);
       
   272 			if(r!=KErrNone)
       
   273 				break;
       
   274 			name = object.FullName();
       
   275 			object.Close();
       
   276 			if(name!=KTestMutexName)
       
   277 				goto fail;
       
   278 
       
   279 			r = object.Open(m,2,EOwnerThread);
       
   280 			if(r!=KErrNone)
       
   281 				break;
       
   282 			SetName(object);
       
   283 			m.Read(3,Name2);
       
   284 			if(Name!=Name2)
       
   285 				goto fail;
       
   286 
       
   287 			m.Complete(object);
       
   288 			object.Close();
       
   289 			return;
       
   290 			}
       
   291 			break;
       
   292 
       
   293 		case CTestSession::ETestSemaphore:
       
   294 			{
       
   295 			RSemaphore object;
       
   296 
       
   297 			r = object.Open(m,0,EOwnerThread);
       
   298 			if(r!=KErrBadHandle || object.Handle())
       
   299 				goto fail;
       
   300 
       
   301 			r = object.Open(m,1,EOwnerProcess);
       
   302 			if(r!=KErrNone)
       
   303 				break;
       
   304 			name = object.FullName();
       
   305 			object.Close();
       
   306 			if(name!=KTestSemaphoreName)
       
   307 				goto fail;
       
   308 
       
   309 			r = object.Open(m,2,EOwnerThread);
       
   310 			if(r!=KErrNone)
       
   311 				break;
       
   312 			SetName(object);
       
   313 			m.Read(3,Name2);
       
   314 			if(Name!=Name2)
       
   315 				goto fail;
       
   316 
       
   317 			m.Complete(object);
       
   318 			object.Close();
       
   319 			return;
       
   320 			}
       
   321 			break;
       
   322 
       
   323 		case CTestSession::ETestMsgQueue:
       
   324 			{
       
   325 			RMsgQueue<TInt> object;
       
   326 
       
   327 			r = object.Open(m,0,EOwnerThread);
       
   328 			if(r!=KErrBadHandle || object.Handle())
       
   329 				goto fail;
       
   330 
       
   331 			r = object.Open(m,1,EOwnerProcess);
       
   332 			if(r!=KErrNone)
       
   333 				break;
       
   334 			name = object.FullName();
       
   335 			object.Close();
       
   336 			if(name!=KTestMsgQueueName)
       
   337 				goto fail;
       
   338 
       
   339 			r = object.Open(m,2,EOwnerThread);
       
   340 			if(r!=KErrNone)
       
   341 				break;
       
   342 			SetName(object);
       
   343 			m.Read(3,Name2);
       
   344 			if(Name!=Name2)
       
   345 				goto fail;
       
   346 
       
   347 			m.Complete(object);
       
   348 			object.Close();
       
   349 			return;
       
   350 			}
       
   351 			break;
       
   352 
       
   353 		case CTestSession::ETestCondVar:
       
   354 			{
       
   355 			RCondVar object;
       
   356 
       
   357 			r = object.Open(m,0,EOwnerThread);
       
   358 			if(r!=KErrBadHandle || object.Handle())
       
   359 				goto fail;
       
   360 
       
   361 			r = object.Open(m,1,EOwnerProcess);
       
   362 			if(r!=KErrNone)
       
   363 				break;
       
   364 			name = object.FullName();
       
   365 			object.Close();
       
   366 			if(name!=KTestCondVarName)
       
   367 				goto fail;
       
   368 
       
   369 			r = object.Open(m,2,EOwnerThread);
       
   370 			if(r!=KErrNone)
       
   371 				break;
       
   372 			SetName(object);
       
   373 			m.Read(3,Name2);
       
   374 			if(Name!=Name2)
       
   375 				goto fail;
       
   376 
       
   377 			m.Complete(object);
       
   378 			object.Close();
       
   379 			return;
       
   380 			}
       
   381 			break;
       
   382 
       
   383 		case CTestSession::ETestChunk:
       
   384 			{
       
   385 			RChunk object;
       
   386 
       
   387 			r = object.Open(m,0,EOwnerThread);
       
   388 			if(r!=KErrBadHandle || object.Handle())
       
   389 				goto fail;
       
   390 
       
   391 			r = object.Open(m,1,EOwnerProcess);
       
   392 			if(r!=KErrNone)
       
   393 				break;
       
   394 			name = object.FullName();
       
   395 			object.Close();
       
   396 			if(name!=KTestChunkName)
       
   397 				goto fail;
       
   398 
       
   399 			r = object.Open(m,2,EOwnerThread);
       
   400 			if(r!=KErrNone)
       
   401 				break;
       
   402 			SetName(object);
       
   403 			m.Read(3,Name2);
       
   404 			if(Name!=Name2)
       
   405 				goto fail;
       
   406 
       
   407 			m.Complete(object);
       
   408 			object.Close();
       
   409 			return;
       
   410 			}
       
   411 			break;
       
   412 
       
   413 		case CTestSession::ETestChunkAdjust:
       
   414 			{
       
   415 			RChunk object;
       
   416 			r = object.Open(m,0,EOwnerThread);
       
   417 			if(r==KErrNone)
       
   418 				r = object.Adjust(0x1000);
       
   419 			object.Close();
       
   420 			}
       
   421 			break;
       
   422 
       
   423 		case CTestSession::ETestLdd:
       
   424 			{
       
   425 			RLddTest object;
       
   426 			r = object.Open(m,0);
       
   427 			if(r!=KErrBadHandle || object.Handle())
       
   428 				goto fail;
       
   429 			r = object.Open(m,1);
       
   430 			if(r!=KErrNone)
       
   431 				break;
       
   432 			SetName(object);
       
   433 			m.Read(3,Name2);
       
   434 			if(Name!=Name2)
       
   435 				goto fail;
       
   436 			r = object.Test1();
       
   437 			if(r!=aMessage.Int2())
       
   438 				goto fail;
       
   439 			m.Complete(object);
       
   440 			object.Close();
       
   441 			return;
       
   442 			}
       
   443 
       
   444 		case CTestSession::ETestSession:
       
   445 			{
       
   446 			RTestSession object;
       
   447 			r = object.Open(m,0);
       
   448 			if(r!=KErrBadHandle || object.Handle())
       
   449 				goto fail;
       
   450 			r = object.Open(m,1,TSecurityPolicy(ECapabilityTCB));
       
   451 			if(r!=KErrPermissionDenied || object.Handle())
       
   452 				goto fail;
       
   453 			r = object.Open(m,1);
       
   454 			if(r!=KErrNone)
       
   455 				break;
       
   456 			SetName(object);
       
   457 			m.Read(3,Name2);
       
   458 			if(Name!=Name2)
       
   459 				goto fail;
       
   460 
       
   461 			RFs fs;
       
   462 			r = fs.Open(m,2,TSecurityPolicy(ECapabilityTCB));
       
   463 			if(r!=KErrNone)
       
   464 				goto fail;
       
   465 			fs.Close();
       
   466 
       
   467 			r=object.Send(CTestSession::EPing,TIpcArgs(234));
       
   468 			if(r!=234)
       
   469 				goto fail;
       
   470 
       
   471 			m.Complete(object);
       
   472 			object.Close();
       
   473 			return;
       
   474 			}
       
   475 
       
   476 		case CTestSession::ETestSession2:
       
   477 			{
       
   478 			RTestSession object;
       
   479 			object.Connect(EGlobalSharableServer);
       
   480 			m.Complete(object);
       
   481 			object.Close();
       
   482 			return;
       
   483 			}
       
   484 
       
   485 		case CTestSession::ETestSession3:
       
   486 			{
       
   487 			RFs object;
       
   488 			object.Connect();
       
   489 			object.ShareProtected();
       
   490 			m.Complete(object);
       
   491 			object.Close();
       
   492 			return;
       
   493 			}
       
   494 
       
   495 		case CTestSession::ETestServerDuplicateInThread:
       
   496 			r = DuplicateInOtherThread(Server()->Server());
       
   497 			break;
       
   498 
       
   499 		case CTestSession::ETestServerDuplicateInProcess:
       
   500 			r = DuplicateInOtherProcess(Server()->Server());
       
   501 			break;
       
   502 
       
   503 		default:
       
   504 			m.Complete(KErrNotSupported);
       
   505 			break;
       
   506 		}
       
   507 	m.Complete(r);
       
   508 	return;
       
   509 fail:
       
   510 	m.Complete(KErrGeneral);
       
   511 	return;
       
   512 	}
       
   513 
       
   514 RTestSession Session;
       
   515 
       
   516 
       
   517 
       
   518 //
       
   519 // CTestServer
       
   520 //
       
   521 
       
   522 class CTestServer : public CServer2
       
   523 	{
       
   524 public:
       
   525 	CTestServer(TInt aPriority,TInt aType=ESharableSessions);
       
   526 	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
       
   527 	};
       
   528 
       
   529 CTestServer::CTestServer(TInt aPriority,TInt aType)
       
   530 	: CServer2(aPriority,(TServerType)aType)
       
   531 	{
       
   532 	}
       
   533 
       
   534 CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const
       
   535 	{
       
   536 	return new (ELeave) CTestSession();
       
   537 	}
       
   538 
       
   539 
       
   540 
       
   541 //
       
   542 // CTestActiveScheduler
       
   543 //
       
   544 
       
   545 class CTestActiveScheduler : public CActiveScheduler
       
   546 	{
       
   547 public:
       
   548 	virtual void Error(TInt anError) const;
       
   549 	};
       
   550 
       
   551 void CTestActiveScheduler::Error(TInt anError) const
       
   552 	{
       
   553 	User::Panic(_L("TestServer Error"),anError);
       
   554 	}
       
   555 
       
   556 
       
   557 
       
   558 //
       
   559 // Server thread
       
   560 //
       
   561 
       
   562 const TInt KServerRendezvous = KRequestPending+1;
       
   563 
       
   564 RServer2 Servers[ENumServerTypes];
       
   565 
       
   566 void DoStartServer(TServerName aName)
       
   567 	{
       
   568 	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
       
   569 	CActiveScheduler::Install(activeScheduler);
       
   570 	CleanupStack::PushL(activeScheduler);
       
   571 
       
   572 	TInt type = 1; // ESharableSessions
       
   573 	if(aName==EGlobalSharableServer)
       
   574 		type = 2; // EGlobalSharableSessions;
       
   575 	CTestServer* server = new (ELeave) CTestServer(0,type);
       
   576 	CleanupStack::PushL(server);
       
   577 
       
   578 	User::LeaveIfError(server->Start(ServerName(aName)));
       
   579 
       
   580 	Servers[aName] = server->Server();
       
   581 	RProcess::Rendezvous(KServerRendezvous);
       
   582 	RThread::Rendezvous(KServerRendezvous);
       
   583 
       
   584 	CActiveScheduler::Start();
       
   585 
       
   586 	Servers[aName].SetHandle(0);
       
   587 	CleanupStack::PopAndDestroy(2);
       
   588 	}
       
   589 
       
   590 TInt StartServer(TServerName aName)
       
   591 	{
       
   592 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   593 	if(!cleanupStack)
       
   594 		return KErrNoMemory;
       
   595 	TRAPD(leaveError,DoStartServer(aName))
       
   596 	delete cleanupStack;
       
   597 	return leaveError;
       
   598 	}
       
   599 
       
   600 TInt DoThreadStartServer(TAny* aArg)
       
   601 	{
       
   602 	return StartServer((TServerName)(TInt)aArg);
       
   603 	}
       
   604 
       
   605 TRequestStatus ThrdSvrStat;
       
   606 
       
   607 TInt StartServerInThread(TServerName aName)
       
   608 	{
       
   609 	RThread thread;
       
   610 	TRequestStatus rendezvousStatus;
       
   611 	thread.Create(_L(""),DoThreadStartServer,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,(TAny*)aName);
       
   612 	thread.NotifyDestruction(ThrdSvrStat);
       
   613 	thread.Rendezvous(rendezvousStatus);
       
   614 	thread.Resume();
       
   615 	User::WaitForRequest(rendezvousStatus);
       
   616 	thread.Close();
       
   617 	if(rendezvousStatus.Int()!=KServerRendezvous)
       
   618 		return KErrGeneral;
       
   619 	return KErrNone;
       
   620 	}
       
   621 
       
   622 
       
   623 
       
   624 void TestPhysicalDevices()
       
   625 	{
       
   626 	TFullName name;
       
   627 	TInt r;
       
   628 
       
   629 	test.Start(_L("Test find named object"));
       
   630 	TFindPhysicalDevice find(_L("*"));
       
   631 	test((r=find.Next(name))==KErrNone);
       
   632 
       
   633 	test.Next(_L("Try open found object"));
       
   634 	RTestHandle testObject;
       
   635 	test((r=testObject.Open(find))==PlatSecProcessIsolationError);
       
   636 	testObject.Close();
       
   637 
       
   638 	test.End();
       
   639 	}
       
   640 
       
   641 
       
   642 
       
   643 void TestLogicalDevices()
       
   644 	{
       
   645 	TFullName name;
       
   646 	TInt r;
       
   647 	RDevice device;
       
   648 
       
   649 	test.Start(_L("Test find named object"));
       
   650 	TFindLogicalDevice find(_L("*"));
       
   651 	test((r=find.Next(name))==KErrNone);
       
   652 
       
   653 	test.Next(_L("Test open found object"));
       
   654 	test((r=device.Open(find))==KErrNone);
       
   655 
       
   656 	test.Next(_L("Test duplicate object in other thread"));
       
   657 	test((r=DuplicateInOtherThread(device))==KErrNone);
       
   658 
       
   659 	test.Next(_L("Test duplicate object in other process"));
       
   660 	test((r=DuplicateInOtherProcess(device))==KErrNone);
       
   661 
       
   662 	device.Close();
       
   663 
       
   664 	test.Next(_L("Test open device by name"));
       
   665 	test((r=device.Open(name))==KErrNone);
       
   666 	device.Close();
       
   667 
       
   668 	test.End();
       
   669 	}
       
   670 
       
   671 
       
   672 
       
   673 void TestLibraries()
       
   674 	{
       
   675 	TFullName name;
       
   676 	TInt r;
       
   677 
       
   678 	test.Start(_L("Test find named object"));
       
   679 	TFindLibrary find(_L("*"));
       
   680 	test((r=find.Next(name))==KErrNone);
       
   681 
       
   682 	test.Next(_L("Try open found object"));
       
   683 	RTestHandle testObject;
       
   684 	test((r=testObject.Open(find))==PlatSecProcessIsolationError);
       
   685 	testObject.Close();
       
   686 
       
   687 	test.End();
       
   688 	}
       
   689 
       
   690 
       
   691 
       
   692 void TestServers()
       
   693 	{
       
   694 	TFullName name;
       
   695 	TInt r;
       
   696 	RServer2 localObject(Servers[EAnonymousServer]);
       
   697 
       
   698 	test.Start(_L("Test find named object"));
       
   699 	TFindServer find(ServerName(EMainServer));
       
   700 	test((r=find.Next(name))==KErrNone);
       
   701 
       
   702 	test.Next(_L("Try open found object"));
       
   703 	RTestHandle testObject;
       
   704 	test((r=testObject.Open(find))==KErrPermissionDenied);
       
   705 
       
   706 	test.Next(_L("Test duplicate named server in other thread"));
       
   707 	test((r=Session.Send(CTestSession::ETestServerDuplicateInThread))==KErrNone);
       
   708 
       
   709 	test.Next(_L("Try duplicate named server in other process"));
       
   710 	test((r=Session.Send(CTestSession::ETestServerDuplicateInProcess))==KErrPermissionDenied);
       
   711 
       
   712 	test.Next(_L("Test duplicate unnamed server in other thread"));
       
   713 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
   714 
       
   715 	test.Next(_L("Try duplicate unnamed server in other process"));
       
   716 	test((r=DuplicateInOtherProcess(localObject))==KErrPermissionDenied);
       
   717 
       
   718 	test.End();
       
   719 	}
       
   720 
       
   721 
       
   722 
       
   723 void TestProcesses()
       
   724 	{
       
   725 	TFullName name;
       
   726 	TInt r;
       
   727 	RProcess process;
       
   728 
       
   729 	test.Start(_L("Test find named object"));
       
   730 	TFindProcess find(_L("EKern*"));
       
   731 	test((r=find.Next(name))==KErrNone);
       
   732 
       
   733 	test.Next(_L("Test open found object"));
       
   734 	test((r=process.Open(find))==KErrNone);
       
   735 
       
   736 	test.Next(_L("Test duplicate object in other thread"));
       
   737 	test((r=DuplicateInOtherThread(process))==KErrNone);
       
   738 
       
   739 	test.Next(_L("Test duplicate object in other process"));
       
   740 	test((r=DuplicateInOtherProcess(process))==KErrNone);
       
   741 
       
   742 	process.Close();
       
   743 
       
   744 	test.Next(_L("Test open process by name"));
       
   745 	test((r=process.Open(name))==KErrNone);
       
   746 	TProcessId id=process.Id();
       
   747 	process.Close();
       
   748 
       
   749 	test.Next(_L("Test open process by id"));
       
   750 	test((r=process.Open(id))==KErrNone);
       
   751 	test(name==process.FullName());
       
   752 	process.Close();
       
   753 
       
   754 	test.End();
       
   755 	}
       
   756 
       
   757 
       
   758 
       
   759 void TestThreads()
       
   760 	{
       
   761 	TFullName name;
       
   762 	TInt r;
       
   763 
       
   764 	test.Start(_L("Creating threads"));
       
   765 	RThread globalObject;
       
   766 	RThread localObject;
       
   767 	RThread testObject;
       
   768 	test((r=globalObject.Create(_L("T_SOBJECT-test-global-thread"),TestThreadDuplicate,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,NULL))==KErrNone);
       
   769 	test((r=localObject.Create(_L(""),TestThreadDuplicate,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,NULL))==KErrNone);
       
   770 
       
   771 	test.Next(_L("Test find named thread"));
       
   772 	TFindThread find(globalObject.FullName());
       
   773 	test((r=find.Next(name))==KErrNone);
       
   774 
       
   775 	test.Next(_L("Test open found object"));
       
   776 	test((r=testObject.Open(find))==KErrNone);
       
   777 	testObject.Close();
       
   778 
       
   779 	test.Next(_L("Check can't find unnamed thread"));
       
   780 	TName objectName(localObject.FullName());
       
   781 	find.Find(objectName);
       
   782 	test((r=find.Next(name))==PlatSecFindError);
       
   783 
       
   784 	test.Next(_L("Test open named thread by name"));
       
   785 	test((r=testObject.Open(globalObject.FullName()))==KErrNone);
       
   786 	testObject.Close();
       
   787 
       
   788 	test.Next(_L("Check can't open unnamed thread by name"));
       
   789 	test((r=testObject.Open(localObject.FullName()))==PlatSecFindError);
       
   790 	testObject.Close();
       
   791 
       
   792 	test.Next(_L("Check can't open with no name"));
       
   793 	test((r=testObject.Open(KNullDesC))==KErrNotFound);
       
   794 	testObject.Close();
       
   795 
       
   796 	test.Next(_L("Test open named thread by id (in same process)"));
       
   797 	test((r=testObject.Open(globalObject.Id()))==KErrNone);
       
   798 	testObject.Close();
       
   799 
       
   800 	test.Next(_L("Test open named thread by id (in other process)"));
       
   801 	test((r=OpenThreadByIdInOtherProcess(globalObject.Id()))==KErrNone);
       
   802 
       
   803 	test.Next(_L("Test open unnamed thread by id (in same process)"));
       
   804 	test((r=testObject.Open(localObject.Id()))==KErrNone);
       
   805 
       
   806 	test.Next(_L("Check can't open unnamed thread by id (in other process)"));
       
   807 	test((r=OpenThreadByIdInOtherProcess(localObject.Id()))==PlatSecProcessIsolationError);
       
   808 
       
   809 	test.Next(_L("Test duplicate named thread in other process"));
       
   810 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
   811 
       
   812 	test.Next(_L("Check can't duplicate unnamed thread in other process"));
       
   813 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
   814 
       
   815 	test.Next(_L("Test duplicate named thread in other thread"));
       
   816 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
   817 
       
   818 	test.Next(_L("Test duplicate unnamed thread in other thead"));
       
   819 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
   820 
       
   821 	test.Next(_L("Closing threads"));
       
   822 	globalObject.Close();
       
   823 	localObject.Close();
       
   824 
       
   825 	test.End();
       
   826 	}
       
   827 
       
   828 
       
   829 
       
   830 void TestChunks()
       
   831 	{
       
   832 	TFullName name;
       
   833 	TInt r;
       
   834 
       
   835 	test.Start(_L("Creating chunks"));
       
   836 	RChunk globalObject;
       
   837 	RChunk localObject;
       
   838 	RChunk testObject;
       
   839 	test((r=globalObject.CreateGlobal(_L("T_SOBJECT-test-global-chunk"),4096,1024*1024))==KErrNone);
       
   840 	test((r=localObject.CreateLocal(4096,1024*1024))==KErrNone);
       
   841 
       
   842 	test.Next(_L("Test find global object"));
       
   843 	TFindChunk find(globalObject.FullName());
       
   844 	test((r=find.Next(name))==KErrNone);
       
   845 
       
   846 	test.Next(_L("Test open found object"));
       
   847 	test((r=testObject.Open(find))==KErrNone);
       
   848 	testObject.Close();
       
   849 
       
   850 	test.Next(_L("Check can't find local object"));
       
   851 	TName objectName(localObject.FullName());
       
   852 	find.Find(objectName);
       
   853 	test((r=find.Next(name))==PlatSecFindError);
       
   854 
       
   855 	test.Next(_L("Test open with null name"));
       
   856 	test((r=testObject.OpenGlobal(KNullDesC,ETrue))==KErrNotFound);
       
   857 	testObject.Close();
       
   858 
       
   859 	test.Next(_L("Test open global object by name"));
       
   860 	test((r=testObject.OpenGlobal(globalObject.FullName(),ETrue))==KErrNone);
       
   861 	testObject.Close();
       
   862 
       
   863 	test.Next(_L("Check can't open local object by name"));
       
   864 	test((r=testObject.OpenGlobal(localObject.FullName(),ETrue))==PlatSecFindError);
       
   865 	testObject.Close();
       
   866 
       
   867 	test.Next(_L("Test duplicate global object in other process"));
       
   868 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
   869 
       
   870 	test.Next(_L("Check can't duplicate local object in other process"));
       
   871 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
   872 
       
   873 	test.Next(_L("Test duplicate global object in other thread"));
       
   874 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
   875 
       
   876 	test.Next(_L("Test duplicate local object in other thead"));
       
   877 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
   878 
       
   879 	test.Next(_L("Test Chunk protection"));
       
   880 	{
       
   881 	RChunk protectedChunk;
       
   882 	test((r=protectedChunk.CreateGlobal(KNullDesC,0x1000,0x100000,EOwnerProcess))==KErrNone);
       
   883 	test((r=Session.Send(CTestSession::ETestChunkAdjust,TIpcArgs(protectedChunk)))==KErrNone);
       
   884 	protectedChunk.SetRestrictions(RChunk::EPreventAdjust);
       
   885 	test((r=Session.Send(CTestSession::ETestChunkAdjust,TIpcArgs(protectedChunk)))==KErrAccessDenied);
       
   886 	protectedChunk.Close();
       
   887 	}
       
   888 
       
   889 	test.Next(_L("Closing chunks"));
       
   890 	globalObject.Close();
       
   891 	localObject.Close();
       
   892 
       
   893 	test.End();
       
   894 	}
       
   895 
       
   896 
       
   897 
       
   898 void TestSemaphores()
       
   899 	{
       
   900 	TFullName name;
       
   901 	TInt r;
       
   902 
       
   903 	test.Start(_L("Creating semaphores"));
       
   904 	RSemaphore globalObject;
       
   905 	RSemaphore localObject;
       
   906 	RSemaphore testObject;
       
   907 	test((r=globalObject.CreateGlobal(_L("T_SOBJECT-test-global-semaphore"),1))==KErrNone);
       
   908 	test((r=localObject.CreateLocal(1))==KErrNone);
       
   909 
       
   910 	test.Next(_L("Test find global object"));
       
   911 	TFindSemaphore find(globalObject.FullName());
       
   912 	test((r=find.Next(name))==KErrNone);
       
   913 
       
   914 	test.Next(_L("Test open found object"));
       
   915 	test((r=testObject.Open(find))==KErrNone);
       
   916 	testObject.Close();
       
   917 
       
   918 	test.Next(_L("Check can't find local object"));
       
   919 	TName objectName(localObject.FullName());
       
   920 	find.Find(objectName);
       
   921 	test((r=find.Next(name))==PlatSecFindError);
       
   922 
       
   923 	test.Next(_L("Test open with null name"));
       
   924 	test((r=testObject.OpenGlobal(KNullDesC))==KErrNotFound);
       
   925 	testObject.Close();
       
   926 
       
   927 	test.Next(_L("Test open global object by name"));
       
   928 	test((r=testObject.OpenGlobal(globalObject.FullName()))==KErrNone);
       
   929 	testObject.Close();
       
   930 
       
   931 	test.Next(_L("Check can't open local object by name"));
       
   932 	test((r=testObject.OpenGlobal(localObject.FullName()))==PlatSecFindError);
       
   933 	testObject.Close();
       
   934 
       
   935 	test.Next(_L("Test duplicate global object in other process"));
       
   936 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
   937 
       
   938 	test.Next(_L("Check can't duplicate local object in other process"));
       
   939 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
   940 
       
   941 	test.Next(_L("Test duplicate global object in other thread"));
       
   942 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
   943 
       
   944 	test.Next(_L("Test duplicate local object in other thead"));
       
   945 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
   946 
       
   947 	test.Next(_L("Closing Semaphores"));
       
   948 	globalObject.Close();
       
   949 	localObject.Close();
       
   950 
       
   951 	test.End();
       
   952 	}
       
   953 
       
   954 
       
   955 
       
   956 void TestMutexes()
       
   957 	{
       
   958 	TFullName name;
       
   959 	TInt r;
       
   960 
       
   961 	test.Start(_L("Creating mutexes"));
       
   962 	RMutex globalObject;
       
   963 	RMutex localObject;
       
   964 	RMutex testObject;
       
   965 	test((r=globalObject.CreateGlobal(_L("T_SOBJECT-test-global-mutex")))==KErrNone);
       
   966 	test((r=localObject.CreateLocal())==KErrNone);
       
   967 
       
   968 	test.Next(_L("Test find global object"));
       
   969 	TFindMutex find(globalObject.FullName());
       
   970 	test((r=find.Next(name))==KErrNone);
       
   971 
       
   972 	test.Next(_L("Test open found object"));
       
   973 	test((r=testObject.Open(find))==KErrNone);
       
   974 	testObject.Close();
       
   975 
       
   976 	test.Next(_L("Check can't find local object"));
       
   977 	TName objectName(localObject.FullName());
       
   978 	find.Find(objectName);
       
   979 	test((r=find.Next(name))==PlatSecFindError);
       
   980 
       
   981 	test.Next(_L("Test open with null name"));
       
   982 	test((r=testObject.OpenGlobal(KNullDesC))==KErrNotFound);
       
   983 	testObject.Close();
       
   984 
       
   985 	test.Next(_L("Test open global object by name"));
       
   986 	test((r=testObject.OpenGlobal(globalObject.FullName()))==KErrNone);
       
   987 	testObject.Close();
       
   988 
       
   989 	test.Next(_L("Check can't open local object by name"));
       
   990 	test((r=testObject.OpenGlobal(localObject.FullName()))==PlatSecFindError);
       
   991 	testObject.Close();
       
   992 
       
   993 	test.Next(_L("Test duplicate global object in other process"));
       
   994 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
   995 
       
   996 	test.Next(_L("Check can't duplicate local object in other process"));
       
   997 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
   998 
       
   999 	test.Next(_L("Test duplicate global object in other thread"));
       
  1000 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
  1001 
       
  1002 	test.Next(_L("Test duplicate local object in other thead"));
       
  1003 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
  1004 
       
  1005 	test.Next(_L("Closing mutexes"));
       
  1006 	globalObject.Close();
       
  1007 	localObject.Close();
       
  1008 
       
  1009 	test.End();
       
  1010 	}
       
  1011 
       
  1012 
       
  1013 
       
  1014 void TestMessageQueues()
       
  1015 	{
       
  1016 	TInt r;
       
  1017 
       
  1018 	test.Start(_L("Creating message queues"));
       
  1019 	RMsgQueue<TInt> globalObject;
       
  1020 	RMsgQueue<TInt> localObject;
       
  1021 	RMsgQueue<TInt> testObject;
       
  1022 	test((r=globalObject.CreateGlobal(_L("T_SOBJECT-test-global-msgqueue"),1))==KErrNone);
       
  1023 	test((r=localObject.CreateLocal(1))==KErrNone);
       
  1024 
       
  1025 	test.Next(_L("Test open with null name"));
       
  1026 	test((r=testObject.OpenGlobal(KNullDesC))==KErrNotFound);
       
  1027 	testObject.Close();
       
  1028 
       
  1029 	test.Next(_L("Test open global object by name"));
       
  1030 	test((r=testObject.OpenGlobal(globalObject.FullName()))==KErrNone);
       
  1031 	testObject.Close();
       
  1032 
       
  1033 	test.Next(_L("Check can't open local object by name"));
       
  1034 	test((r=testObject.OpenGlobal(localObject.FullName()))==PlatSecFindError);
       
  1035 	testObject.Close();
       
  1036 
       
  1037 	test.Next(_L("Test duplicate global object in other process"));
       
  1038 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
  1039 
       
  1040 	test.Next(_L("Check can't duplicate local object in other process"));
       
  1041 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
  1042 
       
  1043 	test.Next(_L("Test duplicate global object in other thread"));
       
  1044 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
  1045 
       
  1046 	test.Next(_L("Test duplicate local object in other thead"));
       
  1047 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
  1048 
       
  1049 	test.Next(_L("Closing message queues"));
       
  1050 	globalObject.Close();
       
  1051 	localObject.Close();
       
  1052 
       
  1053 	test.End();
       
  1054 	}
       
  1055 
       
  1056 
       
  1057 
       
  1058 void TestConditionVariables()
       
  1059 	{
       
  1060 	TInt r;
       
  1061 
       
  1062 	test.Start(_L("Creating condition variables"));
       
  1063 	RCondVar globalObject;
       
  1064 	RCondVar localObject;
       
  1065 	RCondVar testObject;
       
  1066 	test((r=globalObject.CreateGlobal(_L("T_SOBJECT-test-global-condvar")))==KErrNone);
       
  1067 	test((r=localObject.CreateLocal())==KErrNone);
       
  1068 
       
  1069 	test.Next(_L("Test open with null name"));
       
  1070 	test((r=testObject.OpenGlobal(KNullDesC))==KErrNotFound);
       
  1071 	testObject.Close();
       
  1072 
       
  1073 	test.Next(_L("Test open global object by name"));
       
  1074 	test((r=testObject.OpenGlobal(globalObject.FullName()))==KErrNone);
       
  1075 	testObject.Close();
       
  1076 
       
  1077 	test.Next(_L("Check can't open local object by name"));
       
  1078 	test((r=testObject.OpenGlobal(localObject.FullName()))==PlatSecFindError);
       
  1079 	testObject.Close();
       
  1080 
       
  1081 	test.Next(_L("Test duplicate global object in other process"));
       
  1082 	test((r=DuplicateInOtherProcess(globalObject))==KErrNone);
       
  1083 
       
  1084 	test.Next(_L("Check can't duplicate local object in other process"));
       
  1085 	test((r=DuplicateInOtherProcess(localObject))==PlatSecProcessIsolationError);
       
  1086 
       
  1087 	test.Next(_L("Test duplicate global object in other thread"));
       
  1088 	test((r=DuplicateInOtherThread(globalObject))==KErrNone);
       
  1089 
       
  1090 	test.Next(_L("Test duplicate local object in other thead"));
       
  1091 	test((r=DuplicateInOtherThread(localObject))==KErrNone);
       
  1092 
       
  1093 	test.Next(_L("Closing message queues"));
       
  1094 	globalObject.Close();
       
  1095 	localObject.Close();
       
  1096 
       
  1097 	test.End();
       
  1098 	}
       
  1099 
       
  1100 
       
  1101 
       
  1102 void TestIPCHandles()
       
  1103 	{
       
  1104 	RTestProcess server;
       
  1105 	TRequestStatus rendezvous;
       
  1106 	TInt r;
       
  1107 
       
  1108 	test.Next(_L("Test sending LogicalChannel handles"));
       
  1109 	{
       
  1110 	RLddTest localLdd;
       
  1111 	RLddTest protectedLdd;
       
  1112 	RLddTest returnLdd;
       
  1113 	r=User::LoadLogicalDevice(_L("D_SLDD.LDD"));
       
  1114 	test(r==KErrNone || r==KErrAlreadyExists);
       
  1115 	r=localLdd.OpenLocal();
       
  1116 	test(r==KErrNone);
       
  1117 	r=protectedLdd.OpenProtected();
       
  1118 	test(r==KErrNone);
       
  1119 	TInt lddValue=protectedLdd.Test1();
       
  1120 	test(lddValue==RLddTest::ETest1Value);
       
  1121 	SetName(protectedLdd);
       
  1122 	r = Session.Send(CTestSession::ETestLdd,TIpcArgs(localLdd,protectedLdd,lddValue,&Name));
       
  1123 	r = returnLdd.SetReturnedHandle(r);
       
  1124 	test(r==KErrNone);
       
  1125 	test(CheckName(returnLdd));
       
  1126 	protectedLdd.Close();
       
  1127 	returnLdd.Close();
       
  1128 	localLdd.Close();
       
  1129 	}
       
  1130 
       
  1131 	test.Next(_L("Test sending Chunk handles"));
       
  1132 	{
       
  1133 	RChunk localChunk;
       
  1134 	RChunk globalChunk;
       
  1135 	RChunk protectedChunk;
       
  1136 	RChunk returnChunk;
       
  1137 	test((r=localChunk.CreateLocal(0x1000,0x100000,EOwnerThread))==KErrNone);
       
  1138 	test((r=globalChunk.CreateGlobal(KTestChunkName,0x1000,0x100000,EOwnerProcess))==KErrNone);
       
  1139 	test((r=protectedChunk.CreateGlobal(KNullDesC,0x1000,0x100000,EOwnerProcess))==KErrNone);
       
  1140 	SetName(protectedChunk);
       
  1141 	r = Session.Send(CTestSession::ETestChunk,TIpcArgs(localChunk,globalChunk,protectedChunk,&Name));
       
  1142 	r = returnChunk.SetReturnedHandle(r);
       
  1143 	test(r==KErrNone);
       
  1144 	test(CheckName(returnChunk));
       
  1145 	returnChunk.Close();
       
  1146 	protectedChunk.Close();
       
  1147 	globalChunk.Close();
       
  1148 	localChunk.Close();
       
  1149 	}
       
  1150 
       
  1151 	test.Next(_L("Test sending Semaphore handles"));
       
  1152 	{
       
  1153 	RSemaphore localSemaphore;
       
  1154 	RSemaphore globalSemaphore;
       
  1155 	RSemaphore protectedSemaphore;
       
  1156 	RSemaphore returnSemaphore;
       
  1157 	test((r=localSemaphore.CreateLocal(1,EOwnerThread))==KErrNone);
       
  1158 	test((r=globalSemaphore.CreateGlobal(KTestSemaphoreName,1,EOwnerProcess))==KErrNone);
       
  1159 	test((r=protectedSemaphore.CreateGlobal(KNullDesC,1,EOwnerProcess))==KErrNone);
       
  1160 	SetName(protectedSemaphore);
       
  1161 	r = Session.Send(CTestSession::ETestSemaphore,TIpcArgs(localSemaphore,globalSemaphore,protectedSemaphore,&Name));
       
  1162 	r = returnSemaphore.SetReturnedHandle(r);
       
  1163 	test(r==KErrNone);
       
  1164 	test(CheckName(returnSemaphore));
       
  1165 	returnSemaphore.Close();
       
  1166 	protectedSemaphore.Close();
       
  1167 	globalSemaphore.Close();
       
  1168 	localSemaphore.Close();
       
  1169 	}
       
  1170 
       
  1171 	test.Next(_L("Test sending Mutex handles"));
       
  1172 	{
       
  1173 	RMutex localMutex;
       
  1174 	RMutex globalMutex;
       
  1175 	RMutex protectedMutex;
       
  1176 	RMutex returnMutex;
       
  1177 	test((r=localMutex.CreateLocal(EOwnerThread))==KErrNone);
       
  1178 	test((r=globalMutex.CreateGlobal(KTestMutexName,EOwnerProcess))==KErrNone);
       
  1179 	test((r=protectedMutex.CreateGlobal(KNullDesC,EOwnerProcess))==KErrNone);
       
  1180 	SetName(protectedMutex);
       
  1181 	r = Session.Send(CTestSession::ETestMutex,TIpcArgs(localMutex,globalMutex,protectedMutex,&Name));
       
  1182 	r = returnMutex.SetReturnedHandle(r);
       
  1183 	test(r==KErrNone);
       
  1184 	test(CheckName(returnMutex));
       
  1185 	returnMutex.Close();
       
  1186 	protectedMutex.Close();
       
  1187 	globalMutex.Close();
       
  1188 	localMutex.Close();
       
  1189 	}
       
  1190 
       
  1191 	test.Next(_L("Test sending MsgQueue handles"));
       
  1192 	{
       
  1193 	RMsgQueue<TInt> localMsgQueue;
       
  1194 	RMsgQueue<TInt> globalMsgQueue;
       
  1195 	RMsgQueue<TInt> protectedMsgQueue;
       
  1196 	RMsgQueue<TInt> returnMsgQueue;
       
  1197 	test((r=localMsgQueue.CreateLocal(1,EOwnerThread))==KErrNone);
       
  1198 	test((r=globalMsgQueue.CreateGlobal(KTestMsgQueueName,1,EOwnerProcess))==KErrNone);
       
  1199 	test((r=protectedMsgQueue.CreateGlobal(KNullDesC,1,EOwnerProcess))==KErrNone);
       
  1200 	SetName(protectedMsgQueue);
       
  1201 	r = Session.Send(CTestSession::ETestMsgQueue,TIpcArgs(localMsgQueue,globalMsgQueue,protectedMsgQueue,&Name));
       
  1202 	r = returnMsgQueue.SetReturnedHandle(r);
       
  1203 	test(r==KErrNone);
       
  1204 	test(CheckName(returnMsgQueue));
       
  1205 	returnMsgQueue.Close();
       
  1206 	protectedMsgQueue.Close();
       
  1207 	globalMsgQueue.Close();
       
  1208 	localMsgQueue.Close();
       
  1209 	}
       
  1210 
       
  1211 	test.Next(_L("Test sending CondVar handles"));
       
  1212 	{
       
  1213 	RCondVar localCondVar;
       
  1214 	RCondVar globalCondVar;
       
  1215 	RCondVar protectedCondVar;
       
  1216 	RCondVar returnCondVar;
       
  1217 	test((r=localCondVar.CreateLocal(EOwnerThread))==KErrNone);
       
  1218 	test((r=globalCondVar.CreateGlobal(KTestCondVarName,EOwnerProcess))==KErrNone);
       
  1219 	test((r=protectedCondVar.CreateGlobal(KNullDesC,EOwnerProcess))==KErrNone);
       
  1220 	SetName(protectedCondVar);
       
  1221 	r = Session.Send(CTestSession::ETestCondVar,TIpcArgs(localCondVar,globalCondVar,protectedCondVar,&Name));
       
  1222 	r = returnCondVar.SetReturnedHandle(r);
       
  1223 	test(r==KErrNone);
       
  1224 	test(CheckName(returnCondVar));
       
  1225 	returnCondVar.Close();
       
  1226 	protectedCondVar.Close();
       
  1227 	globalCondVar.Close();
       
  1228 	localCondVar.Close();
       
  1229 	}
       
  1230 
       
  1231 	test.Start(_L("Starting test server 2"));
       
  1232 	server.Create(ETestProcessServer,EGlobalSharableServer);
       
  1233 	server.Rendezvous(rendezvous);
       
  1234 	server.Resume();
       
  1235 	User::WaitForRequest(rendezvous);
       
  1236 	test(rendezvous==KServerRendezvous);
       
  1237 	server.Close();
       
  1238 
       
  1239 	test.Next(_L("Test sending Session handles"));
       
  1240 	{
       
  1241 	RTestSession localSession;
       
  1242 	RTestSession protectedSession;
       
  1243 	RTestSession returnSession;
       
  1244 	RFs fsSession;
       
  1245 	test((r=localSession.Connect(EGlobalSharableServer))==KErrNone);
       
  1246 	test((r=localSession.Send(CTestSession::EPing,TIpcArgs(123)))==123);
       
  1247 	test((r=protectedSession.ConnectProtected(EGlobalSharableServer))==KErrNone);
       
  1248 	test((r=protectedSession.Send(CTestSession::EPing,TIpcArgs(123)))==123);
       
  1249 	test((r=fsSession.Connect())==KErrNone);
       
  1250 	test((r=fsSession.ShareProtected())==KErrNone);
       
  1251 	SetName(protectedSession);
       
  1252 	r = Session.Send(CTestSession::ETestSession,TIpcArgs(localSession,protectedSession,fsSession,&Name));
       
  1253 	r = returnSession.SetReturnedHandle(r);
       
  1254 	test(r==KErrNone);
       
  1255 	test(CheckName(returnSession));
       
  1256 	returnSession.Close();
       
  1257 	protectedSession.Close();
       
  1258 	localSession.Close();
       
  1259 	fsSession.Close();
       
  1260 	}
       
  1261 
       
  1262 	test.Next(_L("Test receiving Session handles"));
       
  1263 	{
       
  1264 	RTestSession returnSession;
       
  1265 	r = Session.Send(CTestSession::ETestSession2);
       
  1266 	r = returnSession.SetReturnedHandle(r);
       
  1267 	test(r==KErrNone);
       
  1268 	test((r=Session.Send(CTestSession::EPing,TIpcArgs(123)))==123);  // So we know server has closed returnedSession
       
  1269 	test((r=returnSession.Send(CTestSession::EPing,TIpcArgs(123)))==123);
       
  1270 	returnSession.Close();
       
  1271 	RFs returnFsSession;
       
  1272 	r = Session.Send(CTestSession::ETestSession3);
       
  1273 	r = returnSession.SetReturnedHandle(r,TSecurityPolicy(TSecureId(0x100039e3))); // f32 sid
       
  1274 	test(r==KErrNone);
       
  1275 	returnFsSession.Close();
       
  1276 	}
       
  1277 
       
  1278 	test.Next(_L("Try global sharing of Sessions without server support"));
       
  1279 	{
       
  1280 	RTestSession protectedSession;
       
  1281 	test((r=protectedSession.ConnectProtected(EMainServer))==KErrPermissionDenied);
       
  1282 	}
       
  1283 
       
  1284 	test.Next(_L("Stopping test server 2"));
       
  1285 	{
       
  1286 	RTestSession session;
       
  1287 	test((r=session.Connect(EGlobalSharableServer))==KErrNone);
       
  1288 	session.Send(CTestSession::EShutdown);
       
  1289 	session.Close();
       
  1290 	}
       
  1291 
       
  1292 	test.End();
       
  1293 	}
       
  1294 
       
  1295 // Test uniqness of object names is enforced by DObjectCon when creating objects
       
  1296 void TestObjectNames()
       
  1297 	{
       
  1298 	_LIT(KNameFormat, "TestObject-%d");
       
  1299 	const TInt KObjectCount = 10;
       
  1300 	RMutex objects[KObjectCount];
       
  1301 
       
  1302 	test.Start(_L("Test uniqueness of object names"));
       
  1303 	
       
  1304 	// Test creating named and unnamed objects is ok
       
  1305 	TInt i;
       
  1306 	for (i = 0 ; i < KObjectCount ; ++i)
       
  1307 		{
       
  1308 		if (i % 2)
       
  1309 			test(objects[i].CreateLocal() == KErrNone);
       
  1310 		else
       
  1311 			{
       
  1312 			TBuf<16> name;
       
  1313 			name.AppendFormat(KNameFormat, i);
       
  1314 			test(objects[i].CreateGlobal(name) == KErrNone);
       
  1315 			}
       
  1316 		}
       
  1317 
       
  1318 	// Test we cannot create objects with duplicate names
       
  1319 	for (i = 0 ; i < KObjectCount ; i+=2)
       
  1320 		{
       
  1321 		TBuf<16> name;
       
  1322 		name.AppendFormat(KNameFormat, i);
       
  1323 		test(objects[i].CreateGlobal(name) == KErrAlreadyExists);
       
  1324 		}	
       
  1325 	
       
  1326 	// Close all objects
       
  1327 	for (i = 0 ; i < KObjectCount ; ++i)
       
  1328 		{
       
  1329 		objects[i].Close();
       
  1330 		}
       
  1331 
       
  1332 	test.End();
       
  1333 	}
       
  1334 
       
  1335 
       
  1336 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
       
  1337 	{
       
  1338 	TInt r;
       
  1339 
       
  1340 	switch(aTestNum)
       
  1341 		{
       
  1342 
       
  1343 	case ETestProcessServer:
       
  1344 		return StartServer((TServerName)aArg1);
       
  1345 
       
  1346 	case ETestProcessDuplicate:
       
  1347 		{
       
  1348 		RHandleBase handle;
       
  1349 		handle.SetHandle(aArg2);
       
  1350 		RThread thread;
       
  1351 		r = thread.Open(TThreadId(aArg1));
       
  1352 		if(r!=KErrNone)
       
  1353 			r = 999;
       
  1354 		else
       
  1355 			r = handle.Duplicate(thread);
       
  1356 		thread.Close();
       
  1357 		}
       
  1358 		return r;
       
  1359 
       
  1360 	case ETestProcessOpenThreadById:
       
  1361 		{
       
  1362 		RThread thread;
       
  1363 		r = thread.Open(TThreadId(aArg1));
       
  1364 		if(r==KErrNone)
       
  1365 			thread.Close();
       
  1366 		}
       
  1367 		return r;
       
  1368 
       
  1369 
       
  1370 	default:
       
  1371 		User::Panic(_L("T_SOBJECT"),1);
       
  1372 		}
       
  1373 
       
  1374 	return KErrNone;
       
  1375 	}
       
  1376 
       
  1377 
       
  1378 GLDEF_C TInt E32Main()
       
  1379     {
       
  1380 	PlatSecProcessIsolationError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)&&PlatSec::ConfigSetting(PlatSec::EPlatSecEnforcement)
       
  1381 		? KErrPermissionDenied : KErrNone;
       
  1382 	PlatSecFindError = PlatSec::ConfigSetting(PlatSec::EPlatSecProcessIsolation)
       
  1383 		? KErrNotFound : KErrNone;
       
  1384 
       
  1385 	TBuf16<512> cmd;
       
  1386 	User::CommandLine(cmd);
       
  1387 	if(cmd.Length() && TChar(cmd[0]).IsDigit())
       
  1388 		{
       
  1389 		TInt function = -1;
       
  1390 		TInt arg1 = -1;
       
  1391 		TInt arg2 = -1;
       
  1392 		TLex lex(cmd);
       
  1393 		lex.Val(function);
       
  1394 		lex.SkipSpace();
       
  1395 		lex.Val(arg1);
       
  1396 		lex.SkipSpace();
       
  1397 		lex.Val(arg2);
       
  1398 		return DoTestProcess(function,arg1,arg2);
       
  1399 		}
       
  1400 
       
  1401 
       
  1402 	test.Title();
       
  1403 	TInt r;
       
  1404 	
       
  1405 	test.Start(_L("Starting test servers"));
       
  1406 	RTestProcess server;
       
  1407 	TRequestStatus rendezvous;
       
  1408 	TRequestStatus svrstat;
       
  1409 	server.Create(ETestProcessServer,EMainServer);
       
  1410 	server.NotifyDestruction(svrstat);
       
  1411 	server.Rendezvous(rendezvous);
       
  1412 	server.Resume();
       
  1413 	User::WaitForRequest(rendezvous);
       
  1414 	test(rendezvous==KServerRendezvous);
       
  1415 	server.Close();
       
  1416 	test((r=StartServerInThread(EAnonymousServer))==KErrNone);
       
  1417 
       
  1418 	test.Next(_L("Openning server session"));
       
  1419 	test((r=Session.Connect())==KErrNone);
       
  1420 
       
  1421 	test.Next(_L("Test Find and Open PhysicalDevices"));
       
  1422 	TestPhysicalDevices();
       
  1423 
       
  1424 	test.Next(_L("Test Find and Open LogicalDevices"));
       
  1425 	TestLogicalDevices();
       
  1426 
       
  1427 	test.Next(_L("Test Find and Open Libraries"));
       
  1428 	TestLibraries();
       
  1429 
       
  1430 	test.Next(_L("Test Find and Open Servers"));
       
  1431 	TestServers();
       
  1432 
       
  1433 	test.Next(_L("Test Find and Open Processes"));
       
  1434 	TestProcesses();
       
  1435 
       
  1436 	test.Next(_L("Test Find and Open Threads"));
       
  1437 	TestThreads();
       
  1438 
       
  1439 	test.Next(_L("Test Find and Open Chunks"));
       
  1440 	TestChunks();
       
  1441 
       
  1442 	test.Next(_L("Test Find and Open Semaphores"));
       
  1443 	TestSemaphores();
       
  1444 
       
  1445 	test.Next(_L("Test Find and Open Mutexes"));
       
  1446 	TestMutexes();
       
  1447 
       
  1448 	test.Next(_L("Test Message Queues"));
       
  1449 	TestMessageQueues();
       
  1450 
       
  1451 	test.Next(_L("Test Condition Variables"));
       
  1452 	TestConditionVariables();
       
  1453 
       
  1454 	test.Next(_L("Test passing handle via IPC"));
       
  1455 	TestIPCHandles();
       
  1456 
       
  1457 	test.Next(_L("Test object names"));
       
  1458 	TestObjectNames();
       
  1459 
       
  1460 	test.Next(_L("Stopping test server"));
       
  1461 	Session.Send(CTestSession::EShutdown);
       
  1462 	Session.Close();
       
  1463 	User::WaitForRequest(svrstat);
       
  1464 	test(svrstat == KErrNone);
       
  1465 
       
  1466 	test.End();
       
  1467 
       
  1468 	return(0);
       
  1469     }
       
  1470