Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\secure\t_smessage.cpp
16 // Test RMessagePtr2 and server message passing
18 // RMessagePtr2, RMessage2
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
31 // Platforms/Drives/Compatibility:
33 // Assumptions/Requirement/Pre-requisites:
34 // Failures and causes:
35 // Base Port information:
41 LOCAL_D RTest test(_L("T_SMESSAGE"));
43 enum TTestProcessFunctions
48 #include "testprocess.h"
52 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
60 case ETestProcessServer:
64 User::Panic(_L("T_SMESSAGE"),1);
72 class RTestThread : public RThread
75 void Create(TThreadFunction aFunction,TAny* aArg=0);
78 void RTestThread::Create(TThreadFunction aFunction,TAny* aArg)
80 TInt r=RThread::Create(_L(""),aFunction,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,aArg);
89 class CTestSession : public CSession2
92 enum {EShutdown,ETestIpc,ETestKill,ETestTerminate,ETestPanic,
93 ETestSetProcessPriority,ETestClient,ETestRMessage,ETestHandle,ETestSession};
96 virtual void ServiceL(const RMessage2& aMessage);
100 CTestSession::CTestSession()
105 const TInt KTestDataMaxLength8 = 20;
106 const TInt KTestDataMaxLength16 = 40;
107 _LIT8(KTestData8,"12345678");
108 _LIT16(KTestData16,"1234567890123456");
109 _LIT(KTestPanicCategory,"TEST PANIC");
111 void CTestSession::ServiceL(const RMessage2& aMessage)
113 RMessagePtr2 m(aMessage);
114 switch (aMessage.Function())
116 case CTestSession::ETestIpc:
120 TBuf8<KTestDataMaxLength8> buf8;
123 l = m.GetDesLength(0);
124 if(l!=KTestData8().Length()) m.Complete(l<0 ? l : KErrGeneral);
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);
133 if(buf8.Compare(KTestData8().Right(l-1))) m.Complete(KErrGeneral);
135 ml = m.GetDesMaxLength(2);
136 if(ml!=KTestDataMaxLength8) m.Complete(ml<0 ? ml : KErrGeneral);
138 r = m.Write(2,KTestData8(),0);
139 if(r!=KErrNone) m.Complete(r);
141 m.WriteL(2,KTestData8(),1);
142 // 16 bit descriptors
143 TBuf16<KTestDataMaxLength16> buf16;
146 l = m.GetDesLength(1);
147 if(l!=KTestData16().Length()) m.Complete(l<0 ? l : KErrGeneral);
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);
156 if(buf16.Compare(KTestData16().Right(l-1))) m.Complete(KErrGeneral);
158 ml = m.GetDesMaxLength(3);
159 if(ml!=KTestDataMaxLength16) m.Complete(ml<0 ? ml : KErrGeneral);
161 r = m.Write(3,KTestData16(),0);
162 if(r!=KErrNone) m.Complete(r);
164 m.WriteL(3,KTestData16(),1);
166 aMessage.Complete(KErrNone);
170 case CTestSession::ETestKill:
174 case CTestSession::ETestTerminate:
178 case CTestSession::ETestPanic:
179 m.Panic(KTestPanicCategory,999);
182 case ETestSetProcessPriority:
183 m.Complete(m.SetProcessPriority((TProcessPriority)aMessage.Int0()));
190 m.Complete(client.Id());
197 RMessage2 message2(m);
198 if (Mem::Compare((TUint8*)&message2,sizeof(message2),(TUint8*)&aMessage,sizeof(aMessage)))
199 m.Complete(KErrGeneral);
201 m.Complete(KErrNone);
206 if (m.Handle()!=aMessage.Handle())
207 m.Complete(KErrGeneral);
209 m.Complete(KErrNone);
212 case CTestSession::ETestSession:
213 if (aMessage.Session()!=this)
214 m.Complete(KErrGeneral);
216 m.Complete(KErrNone);
219 case CTestSession::EShutdown:
220 CActiveScheduler::Stop();
224 m.Complete(KErrNotSupported);
235 class CTestServer : public CServer2
238 CTestServer(TInt aPriority);
239 virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
242 CTestServer::CTestServer(TInt aPriority)
243 : CServer2(aPriority)
247 CSession2* CTestServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const
249 return new (ELeave) CTestSession();
255 // CTestActiveScheduler
258 class CTestActiveScheduler : public CActiveScheduler
261 virtual void Error(TInt anError) const;
264 void CTestActiveScheduler::Error(TInt anError) const
266 User::Panic(_L("TestServer Error"),anError);
275 _LIT(KServerName,"T_SMESSAGE-server");
276 const TInt KServerRendezvous = KRequestPending+1;
280 CTestActiveScheduler* activeScheduler = new (ELeave) CTestActiveScheduler;
281 CActiveScheduler::Install(activeScheduler);
282 CleanupStack::PushL(activeScheduler);
284 CTestServer* server = new (ELeave) CTestServer(0);
285 CleanupStack::PushL(server);
287 User::LeaveIfError(server->Start(KServerName));
289 RProcess::Rendezvous(KServerRendezvous);
291 CActiveScheduler::Start();
293 CleanupStack::PopAndDestroy(2);
298 CTrapCleanup* cleanupStack = CTrapCleanup::New();
301 TRAPD(leaveError,DoStartServer())
312 class RTestSession : public RSessionBase
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); }
329 TInt TestThreadServerKill(TAny* aArg)
331 RTestSession Session;
332 TInt r = Session.Connect();
335 User::SetJustInTime(EFalse);
336 r = Session.Send((TInt)aArg);
342 RTestSession Session;
346 TBuf8<KTestDataMaxLength8> buf8;
347 TBuf16<KTestDataMaxLength16> buf16;
348 TInt r = Session.Send(CTestSession::ETestIpc,TIpcArgs(&KTestData8,&KTestData16,&buf8,&buf16));
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);
363 TRequestStatus logonStatus;
365 test.Start(_L("Test Kill"));
366 thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestKill);
367 thread.Logon(logonStatus);
369 User::WaitForRequest(logonStatus);
370 User::SetJustInTime(ETrue);
371 test(thread.ExitType()==EExitKill);
372 test(logonStatus==999);
373 CLOSE_AND_WAIT(thread);
375 test.Next(_L("Test Terminate"));
376 thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestTerminate);
377 thread.Logon(logonStatus);
379 User::WaitForRequest(logonStatus);
380 User::SetJustInTime(ETrue);
381 test(thread.ExitType()==EExitTerminate);
382 test(logonStatus==999);
383 CLOSE_AND_WAIT(thread);
385 test.Next(_L("Test Panic"));
386 thread.Create(TestThreadServerKill,(TAny*)CTestSession::ETestPanic);
387 thread.Logon(logonStatus);
389 User::WaitForRequest(logonStatus);
390 User::SetJustInTime(ETrue);
391 test(thread.ExitType()==EExitPanic);
392 test(logonStatus==999);
393 CLOSE_AND_WAIT(thread);
400 void TestSetProcessPriority()
403 TProcessPriority priority(process.Priority());
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);
411 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground));
412 test(r==KErrPermissionDenied);
413 test(process.Priority()==priority);
415 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow));
416 test(r==KErrPermissionDenied);
417 test(process.Priority()==priority);
419 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh));
420 test(r==KErrPermissionDenied);
421 test(process.Priority()==priority);
423 test.Next(_L("Test changing priority when Priority Control enabled"));
424 User::SetPriorityControl(ETrue);
426 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityBackground));
428 test(process.Priority()==EPriorityBackground);
430 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityForeground));
432 test(process.Priority()==EPriorityForeground);
434 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityLow));
435 test(r==KErrPermissionDenied);
436 test(process.Priority()==EPriorityForeground);
438 r = Session.Send(CTestSession::ETestSetProcessPriority,TIpcArgs(EPriorityHigh));
439 test(r==KErrPermissionDenied);
440 test(process.Priority()==EPriorityForeground);
442 User::SetPriorityControl(EFalse);
444 process.SetPriority(priority);
451 GLDEF_C TInt E32Main()
454 User::CommandLine(cmd);
455 if(cmd.Length() && TChar(cmd[0]).IsDigit())
466 return DoTestProcess(function,arg1,arg2);
471 test.Start(_L("Starting test server"));
473 TRequestStatus rendezvous;
474 TRequestStatus svrstat;
475 server.Create(ETestProcessServer);
476 server.NotifyDestruction(svrstat);
477 server.Rendezvous(rendezvous);
479 User::WaitForRequest(rendezvous);
480 test(rendezvous==KServerRendezvous);
483 test.Next(_L("Openning server session"));
484 TInt r = Session.Connect();
487 test.Next(_L("Test IPC data transfer"));
490 test.Next(_L("Test RMessagePtr2::Kill, Panic and Teminate"));
493 test.Next(_L("Test RMessagePtr2::SetProcessPriority"));
494 TestSetProcessPriority();
496 test.Next(_L("Test RMessagePtr2::Client"));
497 test(Session.Send(CTestSession::ETestClient)==(TInt)RThread().Id());
499 test.Next(_L("Test RMessagePtr2::RMessage2"));
500 test(Session.Send(CTestSession::ETestRMessage,TIpcArgs(111111,222222,333333,444444))==KErrNone);
502 test.Next(_L("Test RMessagePtr2::Handle"));
503 test(Session.Send(CTestSession::ETestHandle,TIpcArgs())==KErrNone);
505 test.Next(_L("Test RMessagePtr2::Session"));
506 test(Session.Send(CTestSession::ETestSession,TIpcArgs())==KErrNone);
508 test.Next(_L("Stopping test server"));
509 Session.Send(CTestSession::EShutdown);
511 User::WaitForRequest(svrstat);
512 test(svrstat == KErrNone);