kerneltest/e32test/secure/t_smessage.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2001-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_smessage.cpp
       
    15 // Overview:
       
    16 // Test RMessagePtr2 and server message passing
       
    17 // API Information:
       
    18 // RMessagePtr2, RMessage2
       
    19 // Details:
       
    20 // - Start a test server and open a server session. Perform various IPC tests
       
    21 // and verify results are as expected.
       
    22 // - Start a thread and request the server kill, terminate and panic the client
       
    23 // thread. Verify results are as expected.
       
    24 // - Perform various tests of setting the process priority via the 
       
    25 // RMessagePtr2::SetProcessPriority() method. Verify results are as expected.
       
    26 // - Verify the ability to open a handle on the client thread using the 
       
    27 // RMessagePtr2::Client() method.
       
    28 // - Verify message passing via an RMessage2 object. 
       
    29 // - Verify the RMessagePtr2::Handle and RMessagePtr::Session methods work
       
    30 // as expected.
       
    31 // Platforms/Drives/Compatibility:
       
    32 // All.
       
    33 // Assumptions/Requirement/Pre-requisites:
       
    34 // Failures and causes:
       
    35 // Base Port information:
       
    36 // 
       
    37 //
       
    38 
       
    39 #include <e32test.h>
       
    40 
       
    41 LOCAL_D RTest test(_L("T_SMESSAGE"));
       
    42 
       
    43 enum TTestProcessFunctions
       
    44 	{
       
    45 	ETestProcessServer,
       
    46 	};
       
    47 
       
    48 #include "testprocess.h"
       
    49 
       
    50 TInt StartServer();
       
    51 
       
    52 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
       
    53 	{
       
    54 	(void)aArg1;
       
    55 	(void)aArg2;
       
    56 
       
    57 	switch(aTestNum)
       
    58 		{
       
    59 
       
    60 	case ETestProcessServer:
       
    61 		return StartServer();
       
    62 
       
    63 	default:
       
    64 		User::Panic(_L("T_SMESSAGE"),1);
       
    65 		}
       
    66 
       
    67 	return KErrNone;
       
    68 	}
       
    69 
       
    70 
       
    71 
       
    72 class RTestThread : public RThread
       
    73 	{
       
    74 public:
       
    75 	void Create(TThreadFunction aFunction,TAny* aArg=0);
       
    76 	};
       
    77 
       
    78 void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
       
    79 	{
       
    80 	TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
       
    81 	test(r==KErrNone);
       
    82 	}
       
    83 
       
    84 
       
    85 //
       
    86 // CTestSession
       
    87 //
       
    88 
       
    89 class CTestSession : public CSession2
       
    90 	{
       
    91 public:
       
    92 	enum {EShutdown,ETestIpc,ETestKill,ETestTerminate,ETestPanic,
       
    93 		ETestSetProcessPriority,ETestClient,ETestRMessage,ETestHandle,ETestSession};
       
    94 public:
       
    95 	CTestSession();
       
    96 	virtual void ServiceL(const RMessage2& aMessage);
       
    97 public:
       
    98 	};
       
    99 
       
   100 CTestSession::CTestSession()
       
   101 	: CSession2()
       
   102 	{
       
   103 	}
       
   104 
       
   105 const TInt KTestDataMaxLength8 = 20;
       
   106 const TInt KTestDataMaxLength16 = 40;
       
   107 _LIT8(KTestData8,"12345678");
       
   108 _LIT16(KTestData16,"1234567890123456");
       
   109 _LIT(KTestPanicCategory,"TEST PANIC");
       
   110 
       
   111 void CTestSession::ServiceL(const RMessage2& aMessage)
       
   112 	{
       
   113 	RMessagePtr2 m(aMessage);
       
   114 	switch (aMessage.Function())
       
   115 		{
       
   116 		case CTestSession::ETestIpc:
       
   117 			{
       
   118 			TInt r,l,ml;
       
   119 			// 8 bit descriptors
       
   120 			TBuf8<KTestDataMaxLength8> buf8;
       
   121 			buf8.FillZ();
       
   122 			// GetDesLength
       
   123 			l = m.GetDesLength(0);
       
   124 			if(l!=KTestData8().Length()) m.Complete(l<0 ? l : KErrGeneral);
       
   125 			// Read
       
   126 			r = m.Read(0,buf8,1);
       
   127 			if(r!=KErrNone) m.Complete(r);
       
   128 			if(buf8.Compare(KTestData8().Right(l-1))) m.Complete(KErrGeneral);
       
   129 			// ReadL
       
   130 			buf8.Zero();
       
   131 			buf8.FillZ();
       
   132 			m.ReadL(0,buf8,1);
       
   133 			if(buf8.Compare(KTestData8().Right(l-1))) m.Complete(KErrGeneral);
       
   134 			// GetDesMaxLength
       
   135 			ml = m.GetDesMaxLength(2);
       
   136 			if(ml!=KTestDataMaxLength8) m.Complete(ml<0 ? ml : KErrGeneral);
       
   137 			// Write
       
   138 			r = m.Write(2,KTestData8(),0);
       
   139 			if(r!=KErrNone) m.Complete(r);
       
   140 			// WriteL
       
   141 			m.WriteL(2,KTestData8(),1);
       
   142 			// 16 bit descriptors
       
   143 			TBuf16<KTestDataMaxLength16> buf16;
       
   144 			buf16.FillZ();
       
   145 			// GetDesLength
       
   146 			l = m.GetDesLength(1);
       
   147 			if(l!=KTestData16().Length()) m.Complete(l<0 ? l : KErrGeneral);
       
   148 			// Read
       
   149 			r = m.Read(1,buf16,1);
       
   150 			if(r!=KErrNone) m.Complete(r);
       
   151 			if(buf16.Compare(KTestData16().Right(l-1))) m.Complete(KErrGeneral);
       
   152 			// ReadL
       
   153 			buf16.Zero();
       
   154 			buf16.FillZ();
       
   155 			m.ReadL(1,buf16,1);
       
   156 			if(buf16.Compare(KTestData16().Right(l-1))) m.Complete(KErrGeneral);
       
   157 			// GetDesMaxLength
       
   158 			ml = m.GetDesMaxLength(3);
       
   159 			if(ml!=KTestDataMaxLength16) m.Complete(ml<0 ? ml : KErrGeneral);
       
   160 			// Write
       
   161 			r = m.Write(3,KTestData16(),0);
       
   162 			if(r!=KErrNone) m.Complete(r);
       
   163 			// WriteL
       
   164 			m.WriteL(3,KTestData16(),1);
       
   165 			// done
       
   166 			aMessage.Complete(KErrNone);
       
   167 			}
       
   168 			break;
       
   169 
       
   170 		case CTestSession::ETestKill:
       
   171 			m.Kill(999);
       
   172 			break;
       
   173 
       
   174 		case CTestSession::ETestTerminate:
       
   175 			m.Terminate(999);
       
   176 			break;
       
   177 
       
   178 		case CTestSession::ETestPanic:
       
   179 			m.Panic(KTestPanicCategory,999);
       
   180 			break;
       
   181 
       
   182 		case ETestSetProcessPriority:
       
   183 			m.Complete(m.SetProcessPriority((TProcessPriority)aMessage.Int0()));
       
   184 			break;
       
   185 
       
   186 		case ETestClient:
       
   187 			{
       
   188 			RThread client;
       
   189 			m.Client(client);
       
   190 			m.Complete(client.Id());
       
   191 			client.Close();
       
   192 			}
       
   193 			break;
       
   194 
       
   195 		case ETestRMessage:
       
   196 			{
       
   197 			RMessage2 message2(m);
       
   198 			if (Mem::Compare((TUint8*)&message2,sizeof(message2),(TUint8*)&aMessage,sizeof(aMessage)))
       
   199 				m.Complete(KErrGeneral);
       
   200 			else
       
   201 				m.Complete(KErrNone);
       
   202 			}
       
   203 			break;
       
   204 
       
   205 		case ETestHandle:
       
   206 			if (m.Handle()!=aMessage.Handle())
       
   207 				m.Complete(KErrGeneral);
       
   208 			else
       
   209 				m.Complete(KErrNone);
       
   210 			break;
       
   211 
       
   212 		case CTestSession::ETestSession:
       
   213 			if (aMessage.Session()!=this)
       
   214 				m.Complete(KErrGeneral);
       
   215 			else
       
   216 				m.Complete(KErrNone);
       
   217 			break;
       
   218 
       
   219 		case CTestSession::EShutdown:
       
   220 			CActiveScheduler::Stop();
       
   221 			break;
       
   222 
       
   223 		default:
       
   224 			m.Complete(KErrNotSupported);
       
   225 			break;
       
   226 		}
       
   227 	}
       
   228 
       
   229 
       
   230 
       
   231 //
       
   232 // CTestServer
       
   233 //
       
   234 
       
   235 class CTestServer : public CServer2
       
   236 	{
       
   237 public:
       
   238 	CTestServer(TInt aPriority);
       
   239 	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
       
   240 	};
       
   241 
       
   242 CTestServer::CTestServer(TInt aPriority)
       
   243 	: CServer2(aPriority)
       
   244 	{
       
   245 	}
       
   246 
       
   247 CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const
       
   248 	{
       
   249 	return new (ELeave) CTestSession();
       
   250 	}
       
   251 
       
   252 
       
   253 
       
   254 //
       
   255 // CTestActiveScheduler
       
   256 //
       
   257 
       
   258 class CTestActiveScheduler : public CActiveScheduler
       
   259 	{
       
   260 public:
       
   261 	virtual void Error(TInt anError) const;
       
   262 	};
       
   263 
       
   264 void CTestActiveScheduler::Error(TInt anError) const
       
   265 	{
       
   266 	User::Panic(_L("TestServer Error"),anError);
       
   267 	}
       
   268 
       
   269 
       
   270 
       
   271 //
       
   272 // Server thread
       
   273 //
       
   274 
       
   275 _LIT(KServerName,"T_SMESSAGE-server");
       
   276 const TInt KServerRendezvous = KRequestPending+1;
       
   277 
       
   278 void DoStartServer()
       
   279 	{
       
   280 	CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
       
   281 	CActiveScheduler::Install(activeScheduler);
       
   282 	CleanupStack::PushL(activeScheduler);
       
   283 
       
   284 	CTestServer* server = new (ELeave) CTestServer(0);
       
   285 	CleanupStack::PushL(server);
       
   286 
       
   287 	User::LeaveIfError(server->Start(KServerName));
       
   288 
       
   289 	RProcess::Rendezvous(KServerRendezvous);
       
   290 
       
   291 	CActiveScheduler::Start();
       
   292 
       
   293 	CleanupStack::PopAndDestroy(2);
       
   294 	}
       
   295 
       
   296 TInt StartServer()
       
   297 	{
       
   298 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   299 	if(!cleanupStack)
       
   300 		return KErrNoMemory;
       
   301 	TRAPD(leaveError,DoStartServer())
       
   302 	delete cleanupStack;
       
   303 	return leaveError;
       
   304 	}
       
   305 
       
   306 
       
   307 
       
   308 //
       
   309 // RTestSession
       
   310 //
       
   311 
       
   312 class RTestSession : public RSessionBase
       
   313 	{
       
   314 public:
       
   315 	inline TInt Connect()
       
   316 		{ return CreateSession(KServerName,TVersion());}
       
   317 	inline TInt Send(TInt aFunction)
       
   318 		{ return RSessionBase::SendReceive(aFunction); }
       
   319 	inline TInt Send(TInt aFunction,const TIpcArgs& aArgs)
       
   320 		{ return RSessionBase::SendReceive(aFunction,aArgs); }
       
   321 	inline void Send(TInt aFunction,TRequestStatus& aStatus)
       
   322 		{ RSessionBase::SendReceive(aFunction,aStatus); }
       
   323 	inline void Send(TInt aFunction,const TIpcArgs& aArgs,TRequestStatus& aStatus)
       
   324 		{ RSessionBase::SendReceive(aFunction,aArgs,aStatus); }
       
   325 	};
       
   326 
       
   327 
       
   328 
       
   329 TInt TestThreadServerKill(TAny* aArg)
       
   330 	{
       
   331 	RTestSession Session;
       
   332 	TInt r = Session.Connect();
       
   333 	if(r!=KErrNone)
       
   334 		return KErrGeneral;
       
   335 	User::SetJustInTime(EFalse);
       
   336 	r = Session.Send((TInt)aArg);
       
   337 	Session.Close();
       
   338 	return KErrGeneral;
       
   339 	}
       
   340 
       
   341 
       
   342 RTestSession Session;
       
   343 
       
   344 void TestIPC()
       
   345 	{
       
   346 	TBuf8<KTestDataMaxLength8> buf8;
       
   347 	TBuf16<KTestDataMaxLength16> buf16;
       
   348 	TInt r = Session.Send(CTestSession::ETestIpc,TIpcArgs(&KTestData8,&KTestData16,&buf8,&buf16));
       
   349 	test(r==KErrNone);
       
   350 	TInt l = KTestData8().Length();
       
   351 	test(buf8.Length()==l+1);
       
   352 	test(KTestData8().Compare(buf8.Right(l))==0);
       
   353 	l = KTestData16().Length();
       
   354 	test(buf16.Length()==l+1);
       
   355 	test(KTestData16().Compare(buf16.Right(l))==0);
       
   356 	}
       
   357 
       
   358 
       
   359 
       
   360 void TestKill()
       
   361 	{
       
   362 	RTestThread thread;
       
   363 	TRequestStatus logonStatus;
       
   364 
       
   365 	test.Start(_L("Test Kill"));
       
   366 	thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestKill);
       
   367 	thread.Logon(logonStatus);
       
   368 	thread.Resume();
       
   369 	User::WaitForRequest(logonStatus);
       
   370 	User::SetJustInTime(ETrue);
       
   371 	test(thread.ExitType()==EExitKill);
       
   372 	test(logonStatus==999);
       
   373 	CLOSE_AND_WAIT(thread);
       
   374 
       
   375 	test.Next(_L("Test Terminate"));
       
   376 	thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestTerminate);
       
   377 	thread.Logon(logonStatus);
       
   378 	thread.Resume();
       
   379 	User::WaitForRequest(logonStatus);
       
   380 	User::SetJustInTime(ETrue);
       
   381 	test(thread.ExitType()==EExitTerminate);
       
   382 	test(logonStatus==999);
       
   383 	CLOSE_AND_WAIT(thread);
       
   384 
       
   385 	test.Next(_L("Test Panic"));
       
   386 	thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestPanic);
       
   387 	thread.Logon(logonStatus);
       
   388 	thread.Resume();
       
   389 	User::WaitForRequest(logonStatus);
       
   390 	User::SetJustInTime(ETrue);
       
   391 	test(thread.ExitType()==EExitPanic);
       
   392 	test(logonStatus==999);
       
   393 	CLOSE_AND_WAIT(thread);
       
   394 
       
   395 	test.End();
       
   396 	}
       
   397 
       
   398 
       
   399 
       
   400 void TestSetProcessPriority()
       
   401 	{
       
   402 	RProcess process;
       
   403 	TProcessPriority priority(process.Priority());
       
   404 	TInt r;
       
   405 
       
   406 	test.Start(_L("Try changing priority when Priority Control disabled"));
       
   407 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityBackground));
       
   408 	test(r==KErrPermissionDenied);
       
   409 	test(process.Priority()==priority);
       
   410 
       
   411 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground));
       
   412 	test(r==KErrPermissionDenied);
       
   413 	test(process.Priority()==priority);
       
   414 
       
   415 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow));
       
   416 	test(r==KErrPermissionDenied);
       
   417 	test(process.Priority()==priority);
       
   418 
       
   419 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh));
       
   420 	test(r==KErrPermissionDenied);
       
   421 	test(process.Priority()==priority);
       
   422 
       
   423 	test.Next(_L("Test changing priority when Priority Control enabled"));
       
   424 	User::SetPriorityControl(ETrue);
       
   425 
       
   426 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityBackground));
       
   427 	test(r==KErrNone);
       
   428 	test(process.Priority()==EPriorityBackground);
       
   429 
       
   430 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground));
       
   431 	test(r==KErrNone);
       
   432 	test(process.Priority()==EPriorityForeground);
       
   433 
       
   434 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow));
       
   435 	test(r==KErrPermissionDenied);
       
   436 	test(process.Priority()==EPriorityForeground);
       
   437 
       
   438 	r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh));
       
   439 	test(r==KErrPermissionDenied);
       
   440 	test(process.Priority()==EPriorityForeground);
       
   441 
       
   442 	User::SetPriorityControl(EFalse);
       
   443 
       
   444 	process.SetPriority(priority);
       
   445 
       
   446 	test.End();
       
   447 	}
       
   448 
       
   449 
       
   450 
       
   451 GLDEF_C TInt E32Main()
       
   452     {
       
   453 	TBuf16<512> cmd;
       
   454 	User::CommandLine(cmd);
       
   455 	if(cmd.Length() && TChar(cmd[0]).IsDigit())
       
   456 		{
       
   457 		TInt function = -1;
       
   458 		TInt arg1 = -1;
       
   459 		TInt arg2 = -1;
       
   460 		TLex lex(cmd);
       
   461 		lex.Val(function);
       
   462 		lex.SkipSpace();
       
   463 		lex.Val(arg1);
       
   464 		lex.SkipSpace();
       
   465 		lex.Val(arg2);
       
   466 		return DoTestProcess(function,arg1,arg2);
       
   467 		}
       
   468 
       
   469 	test.Title();
       
   470 
       
   471 	test.Start(_L("Starting test server"));
       
   472 	RTestProcess server;
       
   473 	TRequestStatus rendezvous;
       
   474 	TRequestStatus svrstat;
       
   475 	server.Create(ETestProcessServer);
       
   476 	server.NotifyDestruction(svrstat);
       
   477 	server.Rendezvous(rendezvous);
       
   478 	server.Resume();
       
   479 	User::WaitForRequest(rendezvous);
       
   480 	test(rendezvous==KServerRendezvous);
       
   481 	server.Close();
       
   482 
       
   483 	test.Next(_L("Openning server session"));
       
   484 	TInt r = Session.Connect();
       
   485 	test(r==KErrNone);
       
   486 
       
   487 	test.Next(_L("Test IPC data transfer"));
       
   488 	TestIPC();
       
   489 
       
   490 	test.Next(_L("Test RMessagePtr2::Kill, Panic and Teminate"));
       
   491 	TestKill();
       
   492 
       
   493 	test.Next(_L("Test RMessagePtr2::SetProcessPriority"));
       
   494 	TestSetProcessPriority();
       
   495 
       
   496 	test.Next(_L("Test RMessagePtr2::Client"));
       
   497 	test(Session.Send(CTestSession::ETestClient)==(TInt)RThread().Id());
       
   498 
       
   499 	test.Next(_L("Test RMessagePtr2::RMessage2"));
       
   500 	test(Session.Send(CTestSession::ETestRMessage,TIpcArgs(111111,222222,333333,444444))==KErrNone);
       
   501 
       
   502 	test.Next(_L("Test RMessagePtr2::Handle"));
       
   503 	test(Session.Send(CTestSession::ETestHandle,TIpcArgs())==KErrNone);
       
   504 
       
   505 	test.Next(_L("Test RMessagePtr2::Session"));
       
   506 	test(Session.Send(CTestSession::ETestSession,TIpcArgs())==KErrNone);
       
   507 
       
   508 	test.Next(_L("Stopping test server"));
       
   509 	Session.Send(CTestSession::EShutdown);
       
   510 	Session.Close();
       
   511 	User::WaitForRequest(svrstat);
       
   512 	test(svrstat == KErrNone);
       
   513 
       
   514 	test.End();
       
   515 	return(0);
       
   516     }
       
   517